blob: 9bc7e5c23b8ae28b4bc45b287696ca21908d9f7f [file] [log] [blame]
// Copyright 2015 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
'use strict';
/**
* @fileoverview Class to hold methods for processing syslog messages.
*/
var syslogSummary = {};
/**
* RegExp for kernel info logging lines.
* @type {RegExp}
*/
syslogSummary.KERNEL_INFO = / kernel: \[/;
/**
* Array of Objects holding regexs and handlers for syslog subsections
* @type {Object[]}
* @return {string} handler functions return log line tag label
*/
syslogSummary.taggedLines = {
'supplicant_terminated': {
re: / Received crash notification for wpa_supplicant/,
handler: function(syslogProcessingState) {
console.log('supplicant crashed!!!!');
var manager = syslogSummary.getManager(syslogProcessingState);
console.log('manager check ', manager);
logHelper.addNote(manager,
syslogProcessingState.time,
syslogProcessingState.result[0]);
return 'crash';
}
},
'shill_terminated': {
re: / Received crash notification for shill/,
handler: function(syslogProcessingState) {
console.log('shill crashed!!!!');
var manager = syslogSummary.getManager(syslogProcessingState);
logHelper.addNote(manager,
syslogProcessingState.time,
syslogProcessingState.result[0]);
// Shill crashing means we will have a new manager. Update the manager
// end time.
manager.endTime = Date.parse(syslogProcessingState.time[0]);
return 'crash';
}
},
'shill_segfault': {
re: / shill\[.*\]: segfault at /,
handler: function(syslogProcessingState) {
console.log('shill segfault!!!');
var manager = syslogSummary.getManager(syslogProcessingState);
logHelper.addNote(manager,
syslogProcessingState.time,
syslogProcessingState.result[0]);
return 'segfault';
}
},
'mwifiex_card_reset': {
re: / mwifiex_sdio: Resetting card.../,
handler: function(syslogProcessingState) {
console.log('mwifiex card reset!!!');
var manager = syslogSummary.getManager(syslogProcessingState);
console.log('adding note with time: ' + syslogProcessingState.time);
logHelper.addNote(manager,
syslogProcessingState.time,
syslogProcessingState.result[0]);
return 'mwifiex';
}
},
'kernel_msg': {
re: / kernel: \[/,
handler: function(syslogProcessingState) {
console.log('found a kernel message');
return null;
}
}
};
/**
* This method processes each log line in the supplied String array. Each log
* line is checked to see if matches one of the regular expressions from the
* logHelper. Each flagged log line is then processed.
*
* @param {String[]} logLines String array for lines in a log.
* @param {LogSummary} logSummary Object to hold processed log state.
* @return {Object[]} Returns an Object[] of text and anchor labels if the log
* was processed, null if the log is not a recognized format.
*/
syslogSummary.processLogLines = function(logLines, logSummary) {
/**
* Object used for passing state between main processing loop and
* regeular expression handler functions.
* @constructor
* @type {Object}
* @param {LogSummary} logSummary object holding log state after processing
* @param {String} time timestamp of current log line
* @param {String[]} results results from regex test
*/
function SyslogProcessingState(logSummary, time, results) {
this.logSummary = logSummary;
this.time = time;
this.results = results;
}
var logText = [];
var time;
var processingState = new SyslogProcessingState(logSummary, null, null);
var type;
var tag;
for (var i = 0; i < logLines.length; i++) {
time = null;
time = logLines[i].match(logHelper.TIME_FORMAT);
if (time == null) {
continue;
}
if (logSummary.logStartTime == -1) {
logSummary.logStartTime = Date.parse(time[0]);
console.log('set the log summary start time: ' + logSummary.logStartTime);
}
type = null;
tag = null;
for (var taggedLine in syslogSummary.taggedLines) {
var result = logLines[i].match(syslogSummary.taggedLines[taggedLine].re);
if (result !== null) {
processingState.time = time[0];
processingState.result = result;
type = syslogSummary.taggedLines[taggedLine].handler(processingState);
if (type === null) {
tag = null;
} else {
tag = time[0];
}
break;
}
}
var timeCheck = Date.parse(time[0]);
var tempLogText = {text: logLines[i],
tag: tag,
type: type,
file: 'syslog',
ts: timeCheck};
logText.push(tempLogText);
}
return logText;
};
/**
* This method processes each log line in the supplied String array to determine
* if the log is a syslog.
*
* @param {String[]} logLines String array for lines in a log.
* @return {boolean} Returns true is the log is a syslog, false otherwise.
*/
syslogSummary.syslogTypeCheck = function(logLines) {
for (var i = 0; i < logLines.length; i++) {
if (syslogSummary.KERNEL_INFO.test(logLines[i])) {
return true;
}
}
return false;
};
/**
* Check entries for applicable network manager given a log timestamp.
*
* @param {Object} syslogProcessingState holding the current processed log state
* for processed logs.
* @return {Manager} Appropriate manager for a given timestamp.
*/
syslogSummary.getManager = function(syslogProcessingState) {
var logSummary = syslogProcessingState.logSummary;
var logTimeMS = logHelper.getOffsetMS(syslogProcessingState.time);
var manager;
var lastManagerEndTime;
if (syslogProcessingState.logSummary.managers.length == 0) {
console.log('no managers in logSummary');
var newManager = new Manager(1, logSummary.logStartTime, logTimeMS);
logSummary.managers.push(newManager);
console.log('added a manager ', logSummary.managers);
return newManager;
}
for (var i = 0; i < logSummary.managers.length; i++) {
manager = logSummary.managers[i];
lastManagerEndTime = manager.endTime;
console.log('checking manager: ', manager);
if (manager.timeOffset <= logTimeMS) {
console.log('manager ' + manager.id +
' started before this time... potential match');
if (manager.endTime == -1 ||
(manager.endTime - logSummary.logStartTime) > logTimeMS) {
console.log('endTime was -1 or after this time... a match!! ' +
manager.endTime);
return manager;
} else if (manager.endTime > syslogProcessingState.time[0]) {
console.log('this is the correct manager!');
return manager;
}
console.log('not the right manager... try again');
} else {
console.log('next manager.. this one started too late ' +
manager.timeOffset + ' vs ' + logTimeMS);
}
}
// no manager found... add a new manager
var newManager = new Manager(1, lastManagerEndTime, logTimeMS);
logSummary.managers.push(newManager);
return newManager;
};