debug.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. (function () {
  2. 'use strict';
  3. angular.module('ariaNg').controller('AriaNgDebugController', ['$rootScope', '$scope', '$location', '$interval', '$timeout', '$filter', 'ariaNgConstants', 'ariaNgCommonService', 'ariaNgLocalizationService', 'ariaNgLogService', 'ariaNgKeyboardService', 'ariaNgSettingService', 'aria2RpcService', 'ariaNgNativeElectronService', function ($rootScope, $scope, $location, $interval, $timeout, $filter, ariaNgConstants, ariaNgCommonService, ariaNgLocalizationService, ariaNgLogService, ariaNgKeyboardService, ariaNgSettingService, aria2RpcService, ariaNgNativeElectronService) {
  4. var tabStatusItems = [
  5. {
  6. name: 'logs',
  7. show: true
  8. },
  9. {
  10. name: 'startup-command',
  11. show: true
  12. },
  13. {
  14. name: 'rpc',
  15. show: true
  16. }
  17. ];
  18. var debugLogRefreshPromise = null;
  19. var getVisibleTabOrders = function () {
  20. var items = [];
  21. for (var i = 0; i < tabStatusItems.length; i++) {
  22. if (tabStatusItems[i].show) {
  23. items.push(tabStatusItems[i].name);
  24. }
  25. }
  26. return items;
  27. };
  28. var loadAria2RPCMethods = function () {
  29. if ($scope.context.availableRpcMethods && $scope.context.availableRpcMethods.length) {
  30. return;
  31. }
  32. return aria2RpcService.listMethods({
  33. silent: false,
  34. callback: function (response) {
  35. if (response.success) {
  36. $scope.context.availableRpcMethods = response.data;
  37. }
  38. }
  39. });
  40. };
  41. var loadStartupCommandOutput = function () {
  42. return ariaNgNativeElectronService.getStartupCommandOutputAsync()
  43. .then(function (outputs) {
  44. $scope.$apply(function () {
  45. $scope.context.startupCommandOutput = outputs;
  46. });
  47. });
  48. };
  49. $scope.context = {
  50. currentTab: 'logs',
  51. logMaxCount: ariaNgConstants.cachedDebugLogsLimit,
  52. logAutoRefreshAvailableInterval: ariaNgCommonService.getTimeOptions([100, 200, 500, 1000, 2000], true),
  53. logAutoRefreshInterval: 1000,
  54. logListDisplayOrder: 'time:desc',
  55. logLevelFilter: 'DEBUG',
  56. logs: [],
  57. currentLog: null,
  58. startupCommandOutput: null,
  59. availableRpcMethods: [],
  60. rpcRequestMethod: '',
  61. rpcRequestParameters: '{}',
  62. rpcResponse: null
  63. };
  64. $scope.enableDebugMode = function () {
  65. return ariaNgSettingService.isEnableDebugMode();
  66. };
  67. $scope.changeTab = function (tabName) {
  68. if (tabName === 'logs') {
  69. $scope.context.logs = ariaNgLogService.getDebugLogs().slice();
  70. } else if (tabName === 'startup-command') {
  71. $rootScope.loadPromise = loadStartupCommandOutput();
  72. } else if (tabName === 'rpc') {
  73. $rootScope.loadPromise = loadAria2RPCMethods();
  74. }
  75. $scope.context.currentTab = tabName;
  76. };
  77. $scope.changeLogListDisplayOrder = function (type) {
  78. var oldType = ariaNgCommonService.parseOrderType($scope.context.logListDisplayOrder);
  79. var newType = ariaNgCommonService.parseOrderType(type);
  80. if (newType.type === oldType.type) {
  81. newType.reverse = !oldType.reverse;
  82. }
  83. $scope.context.logListDisplayOrder = newType.getValue();
  84. };
  85. $scope.isLogListSetDisplayOrder = function (type) {
  86. var orderType = ariaNgCommonService.parseOrderType($scope.context.logListDisplayOrder);
  87. var targetType = ariaNgCommonService.parseOrderType(type);
  88. return orderType.equals(targetType);
  89. };
  90. $scope.getLogListOrderType = function () {
  91. return $scope.context.logListDisplayOrder;
  92. };
  93. $scope.filterLog = function (log) {
  94. if (!log || !angular.isString(log.level)) {
  95. return false;
  96. }
  97. if (!$scope.context.logLevelFilter) {
  98. return true;
  99. }
  100. return ariaNgLogService.compareLogLevel(log.level, $scope.context.logLevelFilter) >= 0;
  101. };
  102. $scope.setLogLevelFilter = function (filter) {
  103. $scope.context.logLevelFilter = filter;
  104. };
  105. $scope.isSetLogLevelFilter = function (filter) {
  106. return $scope.context.logLevelFilter === filter;
  107. };
  108. $scope.getLogLevelFilter = function () {
  109. return $scope.context.logLevelFilter;
  110. };
  111. $scope.setAutoRefreshInterval = function (interval) {
  112. $scope.context.logAutoRefreshInterval = interval;
  113. if (debugLogRefreshPromise) {
  114. $interval.cancel(debugLogRefreshPromise);
  115. }
  116. if (interval > 0) {
  117. $scope.reloadLogs();
  118. debugLogRefreshPromise = $interval(function () {
  119. $scope.reloadLogs();
  120. }, $scope.context.logAutoRefreshInterval);
  121. }
  122. };
  123. $scope.reloadLogs = function () {
  124. if ($scope.context.currentTab === 'logs') {
  125. $scope.context.logs = ariaNgLogService.getDebugLogs().slice();
  126. } else if ($scope.context.currentTab === 'startup-command') {
  127. return loadStartupCommandOutput();
  128. }
  129. };
  130. $scope.clearDebugLogs = function () {
  131. ariaNgCommonService.confirm('Confirm Clear', 'Are you sure you want to clear debug logs?', 'warning', function () {
  132. ariaNgLogService.clearDebugLogs();
  133. $scope.reloadLogs();
  134. }, false);
  135. };
  136. $scope.showDevTools = function () {
  137. ariaNgNativeElectronService.showDevTools();
  138. };
  139. $scope.showLogDetail = function (log) {
  140. $scope.context.currentLog = log;
  141. angular.element('#log-detail-modal').modal();
  142. };
  143. $('#log-detail-modal').on('show.bs.modal', function (e) {
  144. ariaNgNativeElectronService.updateTitleBarBackgroundColorWithModalOverlay();
  145. });
  146. $('#log-detail-modal').on('hide.bs.modal', function (e) {
  147. $scope.context.currentLog = null;
  148. ariaNgNativeElectronService.updateTitleBarBackgroundColor();
  149. });
  150. $scope.executeAria2Method = function () {
  151. if (!$scope.context.rpcRequestMethod || $scope.context.rpcRequestMethod.indexOf('.') < 0) {
  152. ariaNgCommonService.showError('RPC method is illegal!');
  153. return;
  154. }
  155. var methodNameParts = $scope.context.rpcRequestMethod.split('.');
  156. if (methodNameParts.length !== 2) {
  157. ariaNgCommonService.showError('RPC method is illegal!');
  158. return;
  159. }
  160. var methodName = methodNameParts[1];
  161. if (!angular.isFunction(aria2RpcService[methodName])) {
  162. ariaNgCommonService.showError('AriaNg does not support this RPC method!');
  163. return;
  164. }
  165. var context = {
  166. silent: false,
  167. callback: function (response) {
  168. if (response) {
  169. $scope.context.rpcResponse = $filter('json')(response.data);
  170. } else {
  171. $scope.context.rpcResponse = $filter('json')(response);
  172. }
  173. }
  174. };
  175. var parameters = {};
  176. try {
  177. parameters = angular.fromJson($scope.context.rpcRequestParameters);
  178. } catch (ex) {
  179. ariaNgLogService.error('[AriaNgDebugController.executeAria2Method] failed to parse request parameters: ' + $scope.context.rpcRequestParameters, ex);
  180. ariaNgCommonService.showError('RPC request parameters are invalid!');
  181. return;
  182. }
  183. for (var key in parameters) {
  184. if (!parameters.hasOwnProperty(key) || key === 'silent' || key === 'callback') {
  185. continue;
  186. }
  187. context[key] = parameters[key];
  188. }
  189. return aria2RpcService[methodName](context);
  190. };
  191. $scope.requestParametersTextboxKeyDown = function (event) {
  192. if (!ariaNgSettingService.getKeyboardShortcuts()) {
  193. return;
  194. }
  195. if (ariaNgKeyboardService.isCtrlEnterPressed(event) && $scope.executeMethodForm.$valid) {
  196. if (event.preventDefault) {
  197. event.preventDefault();
  198. }
  199. $scope.executeAria2Method();
  200. return false;
  201. }
  202. };
  203. $scope.$on('$destroy', function () {
  204. if (debugLogRefreshPromise) {
  205. $interval.cancel(debugLogRefreshPromise);
  206. }
  207. });
  208. $rootScope.swipeActions.extendLeftSwipe = function () {
  209. var tabItems = getVisibleTabOrders();
  210. var tabIndex = tabItems.indexOf($scope.context.currentTab);
  211. if (tabIndex < tabItems.length - 1) {
  212. $scope.changeTab(tabItems[tabIndex + 1]);
  213. return true;
  214. } else {
  215. return false;
  216. }
  217. };
  218. $rootScope.swipeActions.extendRightSwipe = function () {
  219. var tabItems = getVisibleTabOrders();
  220. var tabIndex = tabItems.indexOf($scope.context.currentTab);
  221. if (tabIndex > 0) {
  222. $scope.changeTab(tabItems[tabIndex - 1]);
  223. return true;
  224. } else {
  225. return false;
  226. }
  227. };
  228. $rootScope.loadPromise = $timeout(function () {
  229. if (!ariaNgSettingService.isEnableDebugMode()) {
  230. ariaNgCommonService.showError('Access Denied!', function () {
  231. if (!ariaNgSettingService.isEnableDebugMode()) {
  232. $location.path('/settings/ariang');
  233. }
  234. });
  235. return;
  236. }
  237. $scope.setAutoRefreshInterval($scope.context.logAutoRefreshInterval);
  238. }, 100);
  239. }]);
  240. }());