2Copyright 2021 Intel Corporation
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
18function parseLine(entry)
20 var test = entry.match(/^\[.*M\]/);
23 test = entry.match(/\[.+ => .+:[0-9]+\]/);
26 // Windows Crash Entry
27 var file = test[0].substring(1).match(/(?!.+ ).+(?=:)/);
28 var line = test[0].match(/(?!:)[0-9]+(?=\]$)/);
29 var fn = test[0].match(/(?!\[).+(?= =>)/);
31 if (file != null) { this.results.peek().f = file[0].trim(); }
32 if (line != null) { this.results.peek().l = line[0]; }
33 if (fn != null) { this.results.peek().fn = fn[0]; }
37 test = entry.match(/^[\.\/].+\(\) \[0x[0-9a-fA-F]+\]$/);
40 // Linux Crash Stack with no symbols
41 test = test[0].match(/(?!\[)0x[0-9a-fA-F]+(?=\]$)/);
44 if (this.results.peek().sx == null) { this.results.peek().sx = []; }
45 this.results.peek().sx.unshift(test[0]);
50 test = entry.match(/^\[.+_[0-9a-fA-F]{16}\]$/);
54 test = test[0].match(/(?!_)[0-9a-fA-F]{16}(?=\]$)/);
55 this.results.peek().h = test[0];
59 test = entry.match(/(?!^=>)\/+.+:[0-9]+$/);
63 if (this.results.peek().s == null) { this.results.peek().s = []; }
64 this.results.peek().s.unshift(test[0]);
72 var dd = test.substring(1, test.length -1);
73 var c = dd.split(' ');
74 var t = c[1].split(':');
75 if (c[2] == 'PM') { t[0] = parseInt(t[0]) + 12; if (t[0] == 24) { t[0] = 0; } }
77 var d = Date.parse(c[0] + 'T' + t.join(':'));
78 var msg = entry.substring(test.length).trim();
79 var hash = msg.match(/^\[[0-9a-fA-F]{16}\]/);
82 hash = hash[0].substring(1, hash[0].length - 1);
83 msg = msg.substring(hash.length + 2).trim();
87 hash = msg.match(/^\[\]/);
90 msg = msg.substring(2).trim();
95 var log = { t: Math.floor(d / 1000), m: msg };
96 if (hash != null) { log.h = hash; }
98 // Check for File/Line in generic log entry
99 test = msg.match(/^.+:[0-9]+ \([0-9]+,[0-9]+\)/);
102 log.m = log.m.substring(test[0].length).trim();
103 log.f = test[0].match(/^.+(?=:[0-9]+)/)[0];
104 log.l = test[0].match(/(?!:)[0-9]+(?= \([0-9]+,[0-9]+\)$)/)[0];
107 this.results.push(log);
110function readLog_data(buffer)
112 var lines = buffer.toString();
113 if (this.buffered != null) { lines = this.buffered + lines; }
114 lines = lines.split('\n');
117 for (i = 0; i < (lines.length - 1) ; ++i)
119 parseLine.call(this, lines[i]);
122 if (lines.length == 1)
124 parseLine.call(this, lines[0]);
125 this.buffered = null;
129 this.buffered = lines[lines.length - 1];
133function readLogEx(path)
138 var s = require('fs').createReadStream(path);
141 s.on('data', readLog_data);
143 if (s.buffered != null) { readLog_data.call(s, s.buffered); s.buffered = null; }
144 s.removeAllListeners('data');
154function readLog(criteria, path)
156 var objects = readLogEx(path == null ? (process.execPath.split('.exe').join('') + '.log') : path);
159 if (typeof (criteria) == 'string')
163 var dstring = Date.parse(criteria);
164 criteria = Math.floor(dstring / 1000);
171 if (typeof (criteria) == 'number')
175 // Return the last xxx entries
176 ret = objects.slice(objects.length - ((criteria > objects.length) ? objects.length : criteria));
180 // Return entries that are newer than xxx
182 for (i = 0; i < objects.length && objects[i].t <= criteria; ++i) { }
183 ret = objects.slice(i);
194module.exports = { read: readLog, readEx: readLogEx }