Main.js 22 KB

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