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