2017-03-01 18:32:21 -06:00
|
|
|
'use strict';
|
|
|
|
|
2017-08-24 02:51:02 -05:00
|
|
|
const getCmdLineArg = require('./utils/getCmdLineArg.js');
|
2017-08-24 06:22:32 -05:00
|
|
|
const logLevels = require('./enums/logLevels.js');
|
2017-05-31 23:39:08 -05:00
|
|
|
|
|
|
|
const MAX_LOG_QUEUE_LENGTH = 100;
|
|
|
|
|
2017-08-24 06:46:12 -05:00
|
|
|
let electronLog;
|
|
|
|
|
2017-05-31 23:39:08 -05:00
|
|
|
class Logger {
|
2017-08-24 05:48:32 -05:00
|
|
|
|
2017-05-31 23:39:08 -05:00
|
|
|
constructor() {
|
|
|
|
// browser window that has registered a logger
|
|
|
|
this.logWindow = null;
|
|
|
|
|
|
|
|
// holds log messages received before logger has been registered.
|
|
|
|
this.logQueue = [];
|
2017-08-24 06:22:32 -05:00
|
|
|
|
|
|
|
// Initializes the local logger
|
2018-04-06 03:12:41 -05:00
|
|
|
if (!process.env.ELECTRON_QA) {
|
2017-08-24 06:46:12 -05:00
|
|
|
initializeLocalLogger();
|
|
|
|
}
|
2017-05-31 23:39:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send log messages from main process to logger hosted by
|
|
|
|
* renderer process. Allows main process to use logger
|
|
|
|
* provided by JS.
|
|
|
|
* @param {enum} level enum from ./enums/LogLevel.js
|
|
|
|
* @param {string} details msg to be logged
|
|
|
|
*/
|
|
|
|
send(level, details) {
|
|
|
|
if (!level || !details) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-06 03:12:41 -05:00
|
|
|
if (!process.env.ELECTRON_QA) {
|
2017-08-24 06:22:32 -05:00
|
|
|
logLocally(level, details);
|
|
|
|
}
|
|
|
|
|
2017-05-31 23:39:08 -05:00
|
|
|
let logMsg = {
|
2017-04-07 15:13:10 -05:00
|
|
|
level: level,
|
2017-05-31 23:39:08 -05:00
|
|
|
details: details,
|
|
|
|
startTime: Date.now()
|
|
|
|
};
|
|
|
|
|
|
|
|
if (this.logWindow) {
|
|
|
|
this.logWindow.send('log', {
|
|
|
|
msgs: [ logMsg ]
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// store log msgs for later when (if) we get logger registered
|
|
|
|
this.logQueue.push(logMsg);
|
|
|
|
// don't store more than 100 msgs. keep most recent log msgs.
|
|
|
|
if (this.logQueue.length > MAX_LOG_QUEUE_LENGTH) {
|
|
|
|
this.logQueue.shift();
|
|
|
|
}
|
|
|
|
}
|
2017-03-01 18:32:21 -06:00
|
|
|
}
|
|
|
|
|
2017-08-24 05:48:32 -05:00
|
|
|
/**
|
|
|
|
* Sets a window instance for the remote object
|
|
|
|
* @param win
|
|
|
|
*/
|
2017-05-31 23:39:08 -05:00
|
|
|
setLogWindow(win) {
|
|
|
|
this.logWindow = win;
|
|
|
|
|
|
|
|
if (this.logWindow) {
|
2017-08-24 02:51:02 -05:00
|
|
|
let logMsg = {};
|
2017-05-31 23:39:08 -05:00
|
|
|
|
|
|
|
if (Array.isArray(this.logQueue)) {
|
|
|
|
logMsg.msgs = this.logQueue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// configure desired log level and send pending log msgs
|
2017-08-24 02:51:02 -05:00
|
|
|
let logLevel = getCmdLineArg(process.argv, '--logLevel=', false);
|
2017-05-31 23:39:08 -05:00
|
|
|
if (logLevel) {
|
|
|
|
let level = logLevel.split('=')[1];
|
|
|
|
if (level) {
|
|
|
|
logMsg.logLevel = level;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-24 02:51:02 -05:00
|
|
|
if (getCmdLineArg(process.argv, '--enableConsoleLogging', false)) {
|
2017-05-31 23:39:08 -05:00
|
|
|
logMsg.showInConsole = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Object.keys(logMsg).length) {
|
|
|
|
this.logWindow.send('log', logMsg);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.logQueue = [];
|
|
|
|
}
|
|
|
|
}
|
2017-03-01 18:32:21 -06:00
|
|
|
}
|
|
|
|
|
2017-08-24 02:51:02 -05:00
|
|
|
let loggerInstance = new Logger();
|
2017-05-31 23:39:08 -05:00
|
|
|
|
2017-08-24 06:22:32 -05:00
|
|
|
/**
|
|
|
|
* Initializes the electron logger for local logging
|
|
|
|
*/
|
|
|
|
function initializeLocalLogger() {
|
2017-08-24 06:46:12 -05:00
|
|
|
// eslint-disable-next-line global-require
|
|
|
|
electronLog = require('electron-log');
|
2017-08-24 06:22:32 -05:00
|
|
|
electronLog.transports.file.level = 'debug';
|
|
|
|
electronLog.transports.file.format = '{h}:{i}:{s}:{ms} {text}';
|
|
|
|
electronLog.transports.file.maxSize = 10 * 1024 * 1024;
|
|
|
|
electronLog.transports.file.appName = 'Symphony';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Logs locally using the electron-logger
|
|
|
|
* @param level
|
|
|
|
* @param message
|
|
|
|
*/
|
|
|
|
function logLocally(level, message) {
|
|
|
|
switch (level) {
|
|
|
|
case logLevels.ERROR: electronLog.error(message); break;
|
|
|
|
case logLevels.CONFLICT: electronLog.error(message); break;
|
|
|
|
case logLevels.WARN: electronLog.warn(message); break;
|
|
|
|
case logLevels.ACTION: electronLog.warn(message); break;
|
|
|
|
case logLevels.INFO: electronLog.info(message); break;
|
|
|
|
case logLevels.DEBUG: electronLog.debug(message); break;
|
|
|
|
default: electronLog.debug(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-31 23:39:08 -05:00
|
|
|
// Logger class is only exposed for testing purposes.
|
2017-03-01 18:32:21 -06:00
|
|
|
module.exports = {
|
2017-05-31 23:39:08 -05:00
|
|
|
Logger: Logger,
|
|
|
|
send: loggerInstance.send.bind(loggerInstance),
|
|
|
|
setLogWindow: loggerInstance.setLogWindow.bind(loggerInstance)
|
2017-08-24 02:51:02 -05:00
|
|
|
};
|