Main.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. MWF.xApplication.LogViewer.Main = new Class({
  2. Extends: MWF.xApplication.Common.Main,
  3. Implements: [Options, Events],
  4. options: {
  5. "style": "default",
  6. "name": "LogViewer",
  7. "icon": "icon.png",
  8. "width": "800",
  9. "height": "600",
  10. "title": MWF.xApplication.LogViewer.LP.title
  11. },
  12. onQueryLoad: function(){
  13. this.lp = MWF.xApplication.LogViewer.LP;
  14. },
  15. loadApplication: function(callback){
  16. if (!this.options.isRefresh){
  17. this.maxSize(function(){
  18. this.doLog();
  19. }.bind(this));
  20. }else{
  21. this.doLog();
  22. }
  23. if (callback) callback();
  24. },
  25. doLog: function(){
  26. this.status = "prompt";
  27. this.actions = MWF.Actions.get("x_program_center");
  28. this.node = new Element("div", {"styles": this.css.contentNode}).inject(this.content);
  29. this.toolbarNode = new Element("div", {"styles": this.css.toolbarNode}).inject(this.node);
  30. this.screenNode = new Element("div", {"styles": this.css.screenNode}).inject(this.node);
  31. this.bottomNode = new Element("div", {"styles": this.css.bottomNode}).inject(this.node);
  32. this.loadToolbar();
  33. this.loadScreen();
  34. this.initLog();
  35. this.loadLog();
  36. },
  37. initLog: function(){
  38. this.screenInforAreaNode.empty();
  39. this.date = this.dateSelect.options[this.dateSelect.selectedIndex];
  40. this.method = "listPromptErrorLog";
  41. this.count = 20;
  42. this.currentId = "(0)";
  43. },
  44. loadToolbar: function(){
  45. this.promptErrorButton = this.createTopButton("PromptError", "prompt.png", "prompt");
  46. this.unexpectedErrorButton = this.createTopButton("UnexpectedError", "unexpected.png", "unexpected");
  47. this.warnErrorButton = this.createTopButton("Warn", "warn.png", "warn");
  48. this.dateSelect = new Element("select", {"styles": this.css.toolbarDateSelect}).inject(this.toolbarNode);
  49. new Element("option", {
  50. "value": "all",
  51. "selected": true,
  52. "text": this.lp.current
  53. }).inject(this.dateSelect);
  54. var d = new Date();
  55. var t = d.format("%Y-%m-%d");
  56. new Element("option", { "value": t, "text": t }).inject(this.dateSelect);
  57. for (var i=0; i<=7; i++){
  58. d = d.decrement("day", 1);
  59. t = d.format("%Y-%m-%d");
  60. new Element("option", { "value": t, "text": t }).inject(this.dateSelect);
  61. }
  62. this.dateSelect.addEvent("change", function(){
  63. this.initLog();
  64. this.loadLog();
  65. }.bind(this));
  66. // this.promptErrorButton.addEvent("click", function(){
  67. // this.showTypeLog("prompt");
  68. // }.bind(this));
  69. // this.unexpectedErrorButton.addEvent("click", function(){
  70. // this.showTypeLog("unexpected");
  71. // }.bind(this));
  72. // this.warnErrorButton.addEvent("click", function(){
  73. // this.showTypeLog("warn");
  74. // }.bind(this));
  75. },
  76. showTypeLog: function(status){
  77. if (this.status!==status){
  78. switch (this.status){
  79. case "prompt":
  80. this.promptErrorButton.setStyles(this.css.toolbarButton);
  81. break;
  82. case "unexpected":
  83. this.unexpectedErrorButton.setStyles(this.css.toolbarButton);
  84. break;
  85. case "warn":
  86. this.warnErrorButton.setStyles(this.css.toolbarButton);
  87. break;
  88. }
  89. switch (status){
  90. case "prompt":
  91. this.promptErrorButton.setStyles(this.css.toolbarButton_down);
  92. this.status="prompt";
  93. break;
  94. case "unexpected":
  95. this.unexpectedErrorButton.setStyles(this.css.toolbarButton_down);
  96. this.status="unexpected";
  97. break;
  98. case "warn":
  99. this.warnErrorButton.setStyles(this.css.toolbarButton_down);
  100. this.status="warn";
  101. break;
  102. default:
  103. this.promptErrorButton.setStyles(this.css.toolbarButton_down);
  104. this.status="prompt";
  105. }
  106. this.initLog();
  107. this.loadLog();
  108. }
  109. },
  110. createTopButton: function(text, img, status){
  111. var node = new Element("div", {"styles": this.css.toolbarButton}).inject(this.toolbarNode);
  112. var iconNode = new Element("div", {"styles": this.css.toolbarIconButton}).inject(node);
  113. var textNode = new Element("div", {"styles": this.css.toolbarTextButton, "text": text}).inject(node);
  114. iconNode.setStyle("background-image", "url("+"/x_component_LogViewer/$Main/default/"+img+")");
  115. if (status==this.status) node.setStyles(this.css.toolbarButton_down);
  116. var _self = this;
  117. node.addEvents({
  118. "mouseover": function(){if (_self.status != status) this.setStyles(_self.css.toolbarButton_over);},
  119. "mousedown": function(){if (_self.status != status) this.setStyles(_self.css.toolbarButton_down);},
  120. "mouseup": function(){if (_self.status != status) this.setStyles(_self.css.toolbarButton_over);},
  121. "mouseout": function(){if (_self.status != status) this.setStyles(_self.css.toolbarButton);},
  122. "click": function(){_self.showTypeLog(status);}.bind(this)
  123. });
  124. return node;
  125. },
  126. loadLog: function(){
  127. this.date = this.dateSelect.options[this.dateSelect.selectedIndex].value;
  128. switch (this.status){
  129. case "prompt":
  130. this.method = (this.date==="all") ? "listPromptErrorLog" : "listPromptErrorLogWithDate";
  131. break;
  132. case "unexpected":
  133. this.method = (this.date==="all") ? "listUnexpectedErrorLog" : "listUnexpectedErrorLogWithDate";
  134. break;
  135. case "warn":
  136. this.method = (this.date==="all") ? "listWarnLog" : "listWarnLogWithDate";
  137. break;
  138. default:
  139. this.method = (this.date==="all") ? "listPromptErrorLog" : "listPromptErrorLogWithDate";
  140. }
  141. if (this.date==="all"){
  142. this.actions[this.method](this.currentId, this.count, function(json){
  143. this.showLog(json.data);
  144. }.bind(this));
  145. }else{
  146. var d = new Date().parse(this.date).format("%Y-%m-%d");
  147. this.actions[this.method](this.currentId, this.count, d, function(json){
  148. this.showLog(json.data);
  149. }.bind(this));
  150. }
  151. },
  152. showLog: function(data){
  153. if (data.length){
  154. var last = data[data.length-1];
  155. this.currentId = last.id;
  156. data.each(function(log){
  157. new MWF.xApplication.LogViewer.Log(log, this);
  158. }.bind(this));
  159. // switch (this.status){
  160. // case "prompt":
  161. // this.showPromptLog(data);
  162. // break;
  163. // case "unexpected":
  164. // this.showUnexpectedLog(data);
  165. // break;
  166. // case "warn":
  167. // this.showWarnLog(data);
  168. // break;
  169. // default:
  170. // this.showPromptLog(data);
  171. // }
  172. }else{
  173. this.logFinish = true;
  174. }
  175. this.checkLoadNext();
  176. },
  177. checkLoadNext: function(){
  178. if (!this.logFinish){
  179. var s = this.screenInforAreaNode.getScroll();
  180. var ssize = this.screenInforAreaNode.getScrollSize();
  181. var size = this.screenInforAreaNode.getSize();
  182. if (ssize.y-s.y-size.y<200){
  183. this.loadLog();
  184. }
  185. }
  186. },
  187. // showPromptLog: function(data){
  188. // data.each(function(log){
  189. // new MWF.xApplication.LogViewer.Log(log, this);
  190. // }.bind(this));
  191. // },
  192. begin: function(){
  193. this.beginButton.setStyle("background-image", "url("+"/x_component_Console/$Main/default/play_gray.png)");
  194. this.stopButton.setStyle("background-image", "url("+"/x_component_Console/$Main/default/stop.png)");
  195. this.status = "begin";
  196. },
  197. stop: function(){
  198. this.beginButton.setStyle("background-image", "url("+"/x_component_Console/$Main/default/play.png)");
  199. this.stopButton.setStyle("background-image", "url("+"/x_component_Console/$Main/default/stop_gray.png)");
  200. this.status = "stop";
  201. },
  202. loadScreen: function(){
  203. this.screenInforAreaNode = new Element("div", {"styles": this.css.screenInforAreaNode}).inject(this.screenNode);
  204. this.screenInforAreaNode.addEvent("scroll", function(){
  205. this.checkLoadNext();
  206. }.bind(this));
  207. //this.screenInforAreaNode = new Element("div", {"styles": this.css.screenInforAreaNode}).inject(this.screenScrollNode);
  208. // MWF.require("MWF.widget.ScrollBar", function(){
  209. // new MWF.widget.ScrollBar(this.screenScrollNode, {
  210. // "style":"xApp_console", "where": "before", "indent": false, "distance": 50, "friction": 6, "axis": {"x": false, "y": true},
  211. // "onScroll": function(y, x){
  212. // // var scrollSize = _self.listScrollAreaNode.getScrollSize();
  213. // // var clientSize = _self.listScrollAreaNode.getSize();
  214. // // var scrollHeight = scrollSize.y-clientSize.y;
  215. // // if (y+200>scrollHeight) {
  216. // // if (!_self.isElementLoaded) _self.listItemNext();
  217. // // }
  218. // }
  219. // });
  220. // }.bind(this));
  221. this.setScreenHeight();
  222. this.addEvent("resize", this.setScreenHeight.bind(this));
  223. },
  224. setScreenHeight: function(){
  225. var size = this.node.getSize();
  226. var toolbarSize = this.toolbarNode.getSize();
  227. var bottomSize = this.bottomNode.getSize();
  228. var y = size.y-toolbarSize.y-bottomSize.y;
  229. this.screenNode.setStyle("height", ""+y+"px");
  230. }
  231. });
  232. MWF.xApplication.LogViewer.Log = new Class({
  233. initialize: function(log, app){
  234. this.log = log;
  235. this.app = app;
  236. this.css = this.app.css;
  237. this.lp = this.app.lp;
  238. this.load();
  239. },
  240. load: function(){
  241. this.node = new Element("div", {"styles": this.css.logItemNode}).inject(this.app.screenInforAreaNode);
  242. var m = this.log.message.substr(0, this.log.message.indexOf("\n"));
  243. var message = m + ((this.log.person) ? "&nbsp;("+this.log.person+")": "");
  244. var html = "<pre><span class='MWFLogCheckbox' style='cursor: pointer;float: left; height: 20px; width: 30px; background: url(/x_component_LogViewer/$Main/default/check.png) no-repeat center center'></span>" +
  245. "<span style='float: left;font-size: 14px; font-weight: bold; width: 160px; text-align: right'>"+this.log.occurTime+"</span>" +
  246. "<span style='float:left'>\t</span>" +
  247. "<span style='font-size: 14px; font-weight: bold;'>"+o2.common.encodeHtml(message)+"</span><br/>";
  248. if (this.log.exceptionClass){
  249. html += "<span style='float: left; width: 190px; text-align: right; color: #6BC5FC;'>ExceptionClass: </span>" +
  250. "<span style='float:left'>\t</span>" +
  251. "<span>"+o2.common.encodeHtml(this.log.exceptionClass)+"</span><br/>";
  252. }
  253. if (this.log.loggerName){
  254. html += "<span style='float: left; width: 190px; text-align: right; color: #6BC5FC;'>LoggerName: </span>" +
  255. "<span style='float:left'>\t</span>" +
  256. "<span>"+o2.common.encodeHtml(this.log.loggerName)+"</span><br/>";
  257. }
  258. if (this.log.stackTrace){
  259. var traces = this.log.stackTrace.split(/[\r\n\t]/g);
  260. html += "<span class='MWFLogStackTrace'><span style='float: left; width: 190px; text-align: right; color: #6BC5FC;'>StackTrace: </span>" +
  261. "<span style='float:left'>\t</span>";
  262. if (traces.length>1) {
  263. html += "<span class='MWFLogStackTraceAction' style='float: left; cursor: pointer; height: 20px; width: 16px; background: url(/x_component_LogViewer/$Main/default/right.png) no-repeat left center'></span>";
  264. }
  265. html += "<span>"+o2.common.encodeHtml(traces[0])+"</span></span><br/>";
  266. // traces.each(function(trace, i){
  267. // if (i!==0){
  268. // html += "<span style='float: left; width: 190px; text-align: right; color: #6BC5FC;'>&nbsp;</span>" +
  269. // "<span>&nbsp;&nbsp;</span>" +
  270. // "<span>\t"+trace+"</span><br/>";
  271. // }
  272. // }.bind(this));
  273. }
  274. if (this.log.requestUrl){
  275. var request = ((this.log.requestMethod) ? this.log.requestMethod+"&nbsp;": "")+
  276. this.log.requestUrl+
  277. ((this.log.requestRemoteAddr) ? "&nbsp; From &nbsp;"+this.log.requestRemoteAddr : "");
  278. html += "<span class='MWFLogRequest'><span style='float: left; width: 190px; text-align: right; color: #6BC5FC;'>RequestInfor: </span>" +
  279. "<span style='float:left'>\t</span>";
  280. html += "<span class='MWFLogRequestAction' style='float: left;cursor: pointer; height: 20px; width: 16px; background: url(/x_component_LogViewer/$Main/default/right.png) no-repeat left center'></span>";
  281. html += "<span>"+o2.common.encodeHtml(request)+"</span></span><br/>";
  282. }
  283. html += "</pre>";
  284. this.node.set("html", html);
  285. this.checkbox = this.node.getElement(".MWFLogCheckbox");
  286. this.traceNode = this.node.getElement(".MWFLogStackTrace");
  287. this.requestNode = this.node.getElement(".MWFLogRequest");
  288. this.traceActionNode = this.node.getElement(".MWFLogStackTraceAction");
  289. this.requestActionNode = this.node.getElement(".MWFLogRequestAction");
  290. this.setEvent();
  291. },
  292. setEvent: function(){
  293. this.node.addEvents({
  294. "mouseover": function(){ this.node.setStyles(this.css.logItemNode_over); }.bind(this),
  295. "mouseout": function(){ this.node.setStyles(this.css.logItemNode); }.bind(this)
  296. });
  297. if (this.checkbox){
  298. this.checkbox.addEvent("click", function(e){
  299. this.checkSelected();
  300. e.stopPropagation();
  301. }.bind(this))
  302. }
  303. if (this.traceActionNode){
  304. this.traceActionNode.addEvent("click", function(e){
  305. this.expandOrCollapseTrace();
  306. e.stopPropagation();
  307. }.bind(this))
  308. }
  309. if (this.requestActionNode){
  310. this.requestActionNode.addEvent("click", function(e){
  311. this.expandOrCollapseRequest();
  312. e.stopPropagation();
  313. }.bind(this))
  314. }
  315. },
  316. checkSelected: function(){
  317. var range = document.createRange();
  318. range.selectNode(this.node);
  319. var s = window.getSelection();
  320. s.selectAllChildren(this.node);
  321. },
  322. expandOrCollapseTrace: function(){
  323. if (this.log.stackTrace){
  324. if (!this.isTraceExpanded){
  325. this.expandedTrace();
  326. }else{
  327. this.collapseTrace();
  328. }
  329. }
  330. },
  331. expandedTrace: function(){
  332. if (!this.traceAllNode) this.createTraceAllNode();
  333. this.traceAllNode.setStyle("display", "inline");
  334. this.traceActionNode.setStyle("background-image", "url(/x_component_LogViewer/$Main/default/down.png)");
  335. this.isTraceExpanded = true;
  336. },
  337. collapseTrace: function(){
  338. if (this.traceAllNode){
  339. this.traceAllNode.destroy();
  340. this.traceAllNode = null;
  341. }
  342. this.traceActionNode.setStyle("background-image", "url(/x_component_LogViewer/$Main/default/right.png)");
  343. this.isTraceExpanded = false;
  344. },
  345. createTraceAllNode: function(){
  346. var brNode = this.traceNode.getNext();
  347. this.traceAllNode = new Element("span").inject(brNode, "after");
  348. var traces = this.log.stackTrace.split(/[\r\n\t]/g);
  349. var html = "";
  350. traces.each(function(t, i){
  351. if (i!==0){
  352. html += "<span style='float: left; width: 190px;'>\t</span>" +
  353. "<span>\t</span>" +
  354. "<span>\t"+t+"</span><br/>";
  355. }
  356. }.bind(this));
  357. this.traceAllNode.set("html", html);
  358. },
  359. expandOrCollapseRequest: function(){
  360. if (this.log.requestUrl){
  361. if (!this.isRequestExpanded){
  362. this.expandedRequest();
  363. }else{
  364. this.collapseRequest();
  365. }
  366. }
  367. },
  368. expandedRequest: function(){
  369. if (!this.requestAllNode) this.createRequestAllNode();
  370. this.requestAllNode.setStyle("display", "inline");
  371. this.requestActionNode.setStyle("background-image", "url(/x_component_LogViewer/$Main/default/down.png)");
  372. this.isRequestExpanded = true;
  373. },
  374. collapseRequest: function(){
  375. if (this.requestAllNode){
  376. this.requestAllNode.destroy();
  377. this.requestAllNode = null;
  378. }
  379. this.requestActionNode.setStyle("background-image", "url(/x_component_LogViewer/$Main/default/right.png)");
  380. this.isRequestExpanded = false;
  381. },
  382. createRequestAllNode: function(){
  383. var brNode = this.requestNode.getNext();
  384. this.requestAllNode = new Element("span").inject(brNode, "after");
  385. var html = "";
  386. html += "<span style='float: left; width: 190px;'>\t</span>" +
  387. "<span style='float: left;'>\t</span>" +
  388. "<span style='color: #6BC5FC; width: 108px;'>requestRemoteAddr: </span><span>"+(this.log.requestRemoteAddr || "&nbsp;")+"</span>&nbsp;&nbsp;" +
  389. "<span style='color: #6BC5FC; width: 108px;'>requestRemoteHost: </span><span>"+(this.log.requestRemoteHost || "&nbsp;")+"</span>&nbsp;&nbsp;" +
  390. "<span style='color: #6BC5FC; width: 108px;'>requestBodyLength: </span><span>"+(this.log.requestBodyLength || "&nbsp;")+"</span>" +
  391. "<br/>";
  392. // if (this.log.requestHead) html += "<span style='float: left; width: 190px;'>\t</span>" +
  393. // "<span style='float: left;'>\t</span>" +
  394. // "<span style='float: left; color: #6BC5FC; width: 108px;'>requestHead: </span><span style='word-break:break-all;'>"+this.log.requestHead+"</span><br/>";
  395. var headers = this.log.requestHead.split(/\n/g);
  396. headers.each(function(head, i){
  397. if (i===0){
  398. html += "<span style='float: left; width: 190px;'>\t</span>" +
  399. "<span style='float: left;'>\t</span>" +
  400. "<span style='float: left; color: #6BC5FC; width: 108px;'>requestHead: </span><span>"+head+"</span><br/>";
  401. }else{
  402. html += "<span style='float: left; width: 190px;'>\t</span>" +
  403. "<span style='float: left;'>\t</span>" +
  404. "<span style='float: left; color: #6BC5FC; width: 108px;'>\t</span><span>"+head+"</span><br/>";
  405. }
  406. }.bind(this));
  407. if (this.log.requestBody){
  408. var bodys = this.log.requestBody.split(/\n/g);
  409. bodys.each(function(body, i){
  410. if (i===0){
  411. html += "<span style='float: left; width: 190px;'>\t</span>" +
  412. "<span style='float: left;'>\t</span>" +
  413. "<span style='float: left; color: #6BC5FC; width: 108px;'>requestBody: </span><span>"+body+"</span><br/>";
  414. }else{
  415. html += "<span style='float: left; width: 190px;'>\t</span>" +
  416. "<span style='float: left;'>\t</span>" +
  417. "<span style='float: left; color: #6BC5FC; width: 108px;'>\t</span><span>"+body+"</span><br/>";
  418. }
  419. }.bind(this));
  420. }
  421. // requestHead
  422. // requestRemoteAddr
  423. // requestRemoteHost
  424. // requestBodyLength
  425. // requestBody
  426. this.requestAllNode.set("html", html);
  427. }
  428. });