1
0
mirror of https://github.com/Ylianst/MeshAgent synced 2025-12-06 00:13:33 +00:00
Files
MeshAgent/modules/util-agentlog.js
2022-10-06 15:43:06 -07:00

223 lines
6.2 KiB
JavaScript

/*
Copyright 2021 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
//
// This is a helper module for parsing and enumerating Mesh Agent log entries
//
//
// This function parses a log entry, and and pushes each parsed log entry into the results array
//
function parseLine(entry)
{
// Use a regex to parse the log entry
var test = entry.match(/^\[.*M\]/);
if (test == null)
{
// Use a regex to find a windows crash entry
test = entry.match(/\[.+ => .+:[0-9]+\]/);
if (test != null)
{
// Windows Crash Entry
var file = test[0].substring(1).match(/(?!.+ ).+(?=:)/);
var line = test[0].match(/(?!:)[0-9]+(?=\]$)/);
var fn = test[0].match(/(?!\[).+(?= =>)/);
if (file != null) { this.results.peek().f = file[0].trim(); }
if (line != null) { this.results.peek().l = line[0]; }
if (fn != null) { this.results.peek().fn = fn[0]; }
}
else
{
// On linux, use a regex to determine if a crash entry contains symbol info
test = entry.match(/^[\.\/].+\(\) \[0x[0-9a-fA-F]+\]$/);
if (test != null)
{
// Linux Crash Stack with no symbols
test = test[0].match(/(?!\[)0x[0-9a-fA-F]+(?=\]$)/);
if (test != null)
{
if (this.results.peek().sx == null) { this.results.peek().sx = []; }
this.results.peek().sx.unshift(test[0]);
}
}
else
{
// use a regex to try to finx the CrashID on linux
test = entry.match(/^\[.+_[0-9a-fA-F]{16}\]$/);
if(test!=null)
{
// Linux Crash ID
test = test[0].match(/(?!_)[0-9a-fA-F]{16}(?=\]$)/);
this.results.peek().h = test[0];
}
}
// Use a regex to find the crash entry
test = entry.match(/(?!^=>)\/+.+:[0-9]+$/);
if(test!=null)
{
// Linux Crash Entry
if (this.results.peek().s == null) { this.results.peek().s = []; }
this.results.peek().s.unshift(test[0]);
}
}
return;
}
test = test[0];
// Parse out the timestamp
var dd = test.substring(1, test.length -1);
var c = dd.split(' ');
var t = c[1].split(':');
if (c[2] == 'PM') { t[0] = parseInt(t[0]) + 12; if (t[0] == 24) { t[0] = 0; } }
// Parse out the message and the agent hash
var d = Date.parse(c[0] + 'T' + t.join(':'));
var msg = entry.substring(test.length).trim();
var hash = msg.match(/^\[[0-9a-fA-F]{16}\]/);
if (hash != null)
{
hash = hash[0].substring(1, hash[0].length - 1);
msg = msg.substring(hash.length + 2).trim();
}
else
{
hash = msg.match(/^\[\]/);
if(hash!=null)
{
msg = msg.substring(2).trim();
hash = null;
}
}
var log = { t: Math.floor(d / 1000), m: msg };
if (hash != null) { log.h = hash; }
// Check for File/Line in generic log entry
test = msg.match(/^.+:[0-9]+ \([0-9]+,[0-9]+\)/);
if (test != null)
{
log.m = log.m.substring(test[0].length).trim();
log.f = test[0].match(/^.+(?=:[0-9]+)/)[0];
log.l = test[0].match(/(?!:)[0-9]+(?= \([0-9]+,[0-9]+\)$)/)[0];
}
this.results.push(log);
}
//
// This function will accumulate a raw data read from the file system, and attempt
// to parse the entries line by line
//
function readLog_data(buffer)
{
var lines = buffer.toString();
if (this.buffered != null) { lines = this.buffered + lines; }
lines = lines.split('\n');
var i;
for (i = 0; i < (lines.length - 1) ; ++i)
{
parseLine.call(this, lines[i]);
}
if (lines.length == 1)
{
parseLine.call(this, lines[0]);
this.buffered = null;
}
else
{
this.buffered = lines[lines.length - 1];
}
}
//
// This function will attempt to read the Mesh Agent log specified by 'path'
// and return an array of log entries
//
function readLogEx(path)
{
var ret = [];
try
{
var s = require('fs').createReadStream(path);
s.buffered = null;
s.results = ret;
s.on('data', readLog_data);
s.resume();
if (s.buffered != null) { readLog_data.call(s, s.buffered); s.buffered = null; }
s.removeAllListeners('data');
s = null;
}
catch(z)
{
}
return (ret);
}
//
// This function will attempt to read the Mesh Agent log specified by 'path'
// and return an array of log entries. 'criteria' can be the number of log entries to fetch, or a timestamp of the oldest entry to fetch
//
function readLog(criteria, path)
{
var objects = readLogEx(path == null ? (process.execPath.split('.exe').join('') + '.log') : path);
var ret = [];
if (typeof (criteria) == 'string')
{
try
{
var dstring = Date.parse(criteria);
criteria = Math.floor(dstring / 1000);
}
catch(z)
{
}
}
if (typeof (criteria) == 'number')
{
if(criteria < 1000)
{
// Return the last xxx entries
ret = objects.slice(objects.length - ((criteria > objects.length) ? objects.length : criteria));
}
else
{
// Return entries that are newer than xxx
var i;
for (i = 0; i < objects.length && objects[i].t <= criteria; ++i) { }
ret = objects.slice(i);
}
}
else
{
ret = objects;
}
return (ret);
}
module.exports = { read: readLog, readEx: readLogEx }