util-agentlog.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. Copyright 2021 Intel Corporation
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. function parseLine(entry)
  14. {
  15. var test = entry.match(/^\[.*M\]/);
  16. if (test == null)
  17. {
  18. test = entry.match(/\[.+ => .+:[0-9]+\]/);
  19. if (test != null)
  20. {
  21. // Windows Crash Entry
  22. var file = test[0].substring(1).match(/(?!.+ ).+(?=:)/);
  23. var line = test[0].match(/(?!:)[0-9]+(?=\]$)/);
  24. var fn = test[0].match(/(?!\[).+(?= =>)/);
  25. if (file != null) { this.results.peek().f = file[0].trim(); }
  26. if (line != null) { this.results.peek().l = line[0]; }
  27. if (fn != null) { this.results.peek().fn = fn[0]; }
  28. }
  29. else
  30. {
  31. test = entry.match(/^[\.\/].+\(\) \[0x[0-9a-fA-F]+\]$/);
  32. if (test != null)
  33. {
  34. // Linux Crash Stack with no symbols
  35. test = test[0].match(/(?!\[)0x[0-9a-fA-F]+(?=\]$)/);
  36. if (test != null)
  37. {
  38. if (this.results.peek().sx == null) { this.results.peek().sx = []; }
  39. this.results.peek().sx.unshift(test[0]);
  40. }
  41. }
  42. else
  43. {
  44. test = entry.match(/^\[.+_[0-9a-fA-F]{16}\]$/);
  45. if(test!=null)
  46. {
  47. // Linux Crash ID
  48. test = test[0].match(/(?!_)[0-9a-fA-F]{16}(?=\]$)/);
  49. this.results.peek().h = test[0];
  50. }
  51. }
  52. test = entry.match(/(?!^=>)\/+.+:[0-9]+$/);
  53. if(test!=null)
  54. {
  55. // Linux Crash Entry
  56. if (this.results.peek().s == null) { this.results.peek().s = []; }
  57. this.results.peek().s.unshift(test[0]);
  58. }
  59. }
  60. return;
  61. }
  62. test = test[0];
  63. var dd = test.substring(1, test.length -1);
  64. var c = dd.split(' ');
  65. var t = c[1].split(':');
  66. if (c[2] == 'PM') { t[0] = parseInt(t[0]) + 12; if (t[0] == 24) { t[0] = 0; } }
  67. var d = Date.parse(c[0] + 'T' + t.join(':'));
  68. var msg = entry.substring(test.length).trim();
  69. var hash = msg.match(/^\[[0-9a-fA-F]{16}\]/);
  70. if (hash != null)
  71. {
  72. hash = hash[0].substring(1, hash[0].length - 1);
  73. msg = msg.substring(hash.length + 2).trim();
  74. }
  75. else
  76. {
  77. hash = msg.match(/^\[\]/);
  78. if(hash!=null)
  79. {
  80. msg = msg.substring(2).trim();
  81. hash = null;
  82. }
  83. }
  84. var log = { t: Math.floor(d / 1000), m: msg };
  85. if (hash != null) { log.h = hash; }
  86. // Check for File/Line in generic log entry
  87. test = msg.match(/^.+:[0-9]+ \([0-9]+,[0-9]+\)/);
  88. if (test != null)
  89. {
  90. log.m = log.m.substring(test[0].length).trim();
  91. log.f = test[0].match(/^.+(?=:[0-9]+)/)[0];
  92. log.l = test[0].match(/(?!:)[0-9]+(?= \([0-9]+,[0-9]+\)$)/)[0];
  93. }
  94. this.results.push(log);
  95. }
  96. function readLog_data(buffer)
  97. {
  98. var lines = buffer.toString();
  99. if (this.buffered != null) { lines = this.buffered + lines; }
  100. lines = lines.split('\n');
  101. var i;
  102. for (i = 0; i < (lines.length - 1) ; ++i)
  103. {
  104. parseLine.call(this, lines[i]);
  105. }
  106. if (lines.length == 1)
  107. {
  108. parseLine.call(this, lines[0]);
  109. this.buffered = null;
  110. }
  111. else
  112. {
  113. this.buffered = lines[lines.length - 1];
  114. }
  115. }
  116. function readLogEx(path)
  117. {
  118. var ret = [];
  119. try
  120. {
  121. var s = require('fs').createReadStream(path);
  122. s.buffered = null;
  123. s.results = ret;
  124. s.on('data', readLog_data);
  125. s.resume();
  126. if (s.buffered != null) { readLog_data.call(s, s.buffered); s.buffered = null; }
  127. s.removeAllListeners('data');
  128. s = null;
  129. }
  130. catch(z)
  131. {
  132. }
  133. return (ret);
  134. }
  135. function readLog(criteria, path)
  136. {
  137. var objects = readLogEx(path == null ? (process.execPath.split('.exe').join('') + '.log') : path);
  138. var ret = [];
  139. if (typeof (criteria) == 'string')
  140. {
  141. try
  142. {
  143. var dstring = Date.parse(criteria);
  144. criteria = Math.floor(dstring / 1000);
  145. }
  146. catch(z)
  147. {
  148. }
  149. }
  150. if (typeof (criteria) == 'number')
  151. {
  152. if(criteria < 1000)
  153. {
  154. // Return the last xxx entries
  155. ret = objects.slice(objects.length - ((criteria > objects.length) ? objects.length : criteria));
  156. }
  157. else
  158. {
  159. // Return entries that are newer than xxx
  160. var i;
  161. for (i = 0; i < objects.length && objects[i].t <= criteria; ++i) { }
  162. ret = objects.slice(i);
  163. }
  164. }
  165. else
  166. {
  167. ret = objects;
  168. }
  169. return (ret);
  170. }
  171. module.exports = { read: readLog, readEx: readLogEx }