2018-02-22 18:21:53 +00:00
|
|
|
// NOTE: Temporarily allow `then` until we convert the entire file to `async` / `await`:
|
|
|
|
/* eslint-disable more/no-then */
|
|
|
|
|
2017-09-25 22:00:19 +00:00
|
|
|
const path = require('path');
|
|
|
|
const fs = require('fs');
|
|
|
|
|
2018-01-08 21:19:25 +00:00
|
|
|
const electron = require('electron');
|
2017-09-25 22:00:19 +00:00
|
|
|
const bunyan = require('bunyan');
|
|
|
|
const mkdirp = require('mkdirp');
|
|
|
|
const _ = require('lodash');
|
2018-01-08 21:19:25 +00:00
|
|
|
const readFirstLine = require('firstline');
|
|
|
|
const readLastLines = require('read-last-lines').read;
|
2018-03-07 02:19:25 +00:00
|
|
|
const rimraf = require('rimraf');
|
2017-09-25 22:00:19 +00:00
|
|
|
|
2019-02-19 23:15:43 +00:00
|
|
|
const { redactAll } = require('../js/modules/privacy');
|
|
|
|
|
2018-05-02 01:54:43 +00:00
|
|
|
const { app, ipcMain: ipc } = electron;
|
2017-09-25 22:00:19 +00:00
|
|
|
const LEVELS = ['fatal', 'error', 'warn', 'info', 'debug', 'trace'];
|
|
|
|
let logger;
|
|
|
|
|
2019-08-19 14:35:09 +00:00
|
|
|
const isRunningFromConsole = Boolean(process.stdout.isTTY);
|
|
|
|
|
2018-01-08 21:19:25 +00:00
|
|
|
module.exports = {
|
|
|
|
initialize,
|
|
|
|
getLogger,
|
|
|
|
// for tests only:
|
|
|
|
isLineAfterDate,
|
|
|
|
eliminateOutOfDateFiles,
|
|
|
|
eliminateOldEntries,
|
|
|
|
fetchLog,
|
|
|
|
fetch,
|
|
|
|
};
|
2017-09-25 22:00:19 +00:00
|
|
|
|
|
|
|
function initialize() {
|
|
|
|
if (logger) {
|
|
|
|
throw new Error('Already called initialize!');
|
|
|
|
}
|
|
|
|
|
|
|
|
const basePath = app.getPath('userData');
|
|
|
|
const logPath = path.join(basePath, 'logs');
|
|
|
|
mkdirp.sync(logPath);
|
|
|
|
|
2018-01-08 21:19:25 +00:00
|
|
|
return cleanupLogs(logPath).then(() => {
|
|
|
|
const logFile = path.join(logPath, 'log.log');
|
2019-08-19 14:35:09 +00:00
|
|
|
const loggerOptions = {
|
2018-01-08 21:19:25 +00:00
|
|
|
name: 'log',
|
2018-05-02 01:54:43 +00:00
|
|
|
streams: [
|
|
|
|
{
|
|
|
|
type: 'rotating-file',
|
|
|
|
path: logFile,
|
|
|
|
period: '1d',
|
|
|
|
count: 3,
|
|
|
|
},
|
|
|
|
],
|
2019-08-19 14:35:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if (isRunningFromConsole) {
|
|
|
|
loggerOptions.streams.push({
|
|
|
|
level: 'debug',
|
|
|
|
stream: process.stdout,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
logger = bunyan.createLogger(loggerOptions);
|
2017-09-25 22:00:19 +00:00
|
|
|
|
2018-05-02 01:54:43 +00:00
|
|
|
LEVELS.forEach(level => {
|
2018-01-08 21:19:25 +00:00
|
|
|
ipc.on(`log-${level}`, (first, ...rest) => {
|
|
|
|
logger[level](...rest);
|
|
|
|
});
|
2017-09-25 22:00:19 +00:00
|
|
|
});
|
|
|
|
|
2019-09-26 19:56:31 +00:00
|
|
|
ipc.on('batch-log', (first, batch) => {
|
|
|
|
batch.forEach(item => {
|
|
|
|
logger[item.level](
|
|
|
|
{
|
|
|
|
time: new Date(item.timestamp),
|
|
|
|
},
|
|
|
|
item.logText
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-05-02 01:54:43 +00:00
|
|
|
ipc.on('fetch-log', event => {
|
|
|
|
fetch(logPath).then(
|
|
|
|
data => {
|
|
|
|
event.sender.send('fetched-log', data);
|
|
|
|
},
|
|
|
|
error => {
|
|
|
|
logger.error(`Problem loading log from disk: ${error.stack}`);
|
|
|
|
}
|
|
|
|
);
|
2017-10-04 21:40:35 +00:00
|
|
|
});
|
2018-03-07 02:19:25 +00:00
|
|
|
|
2018-05-02 01:54:43 +00:00
|
|
|
ipc.on('delete-all-logs', async event => {
|
2018-03-07 02:19:25 +00:00
|
|
|
try {
|
|
|
|
await deleteAllLogs(logPath);
|
2018-03-07 22:09:56 +00:00
|
|
|
} catch (error) {
|
|
|
|
logger.error(`Problem deleting all logs: ${error.stack}`);
|
2018-03-07 02:19:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
event.sender.send('delete-all-logs-complete');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function deleteAllLogs(logPath) {
|
|
|
|
return new Promise((resolve, reject) => {
|
2018-05-02 01:54:43 +00:00
|
|
|
rimraf(
|
|
|
|
logPath,
|
|
|
|
{
|
|
|
|
disableGlob: true,
|
|
|
|
},
|
|
|
|
error => {
|
|
|
|
if (error) {
|
|
|
|
return reject(error);
|
|
|
|
}
|
2018-03-07 02:19:25 +00:00
|
|
|
|
2018-05-02 01:54:43 +00:00
|
|
|
return resolve();
|
|
|
|
}
|
|
|
|
);
|
2017-09-25 22:00:19 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-02-21 19:40:05 +00:00
|
|
|
async function cleanupLogs(logPath) {
|
2018-01-08 21:19:25 +00:00
|
|
|
const now = new Date();
|
2018-05-02 01:54:43 +00:00
|
|
|
const earliestDate = new Date(
|
|
|
|
Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate() - 3)
|
|
|
|
);
|
2018-01-08 21:19:25 +00:00
|
|
|
|
2019-02-21 19:40:05 +00:00
|
|
|
try {
|
|
|
|
const remaining = await eliminateOutOfDateFiles(logPath, earliestDate);
|
2018-01-08 21:19:25 +00:00
|
|
|
const files = _.filter(remaining, file => !file.start && file.end);
|
|
|
|
|
|
|
|
if (!files.length) {
|
2019-02-21 19:40:05 +00:00
|
|
|
return;
|
2018-01-08 21:19:25 +00:00
|
|
|
}
|
|
|
|
|
2019-02-21 19:40:05 +00:00
|
|
|
await eliminateOldEntries(files, earliestDate);
|
|
|
|
} catch (error) {
|
|
|
|
console.error(
|
|
|
|
'Error cleaning logs; deleting and starting over from scratch.',
|
|
|
|
error.stack
|
|
|
|
);
|
|
|
|
|
|
|
|
// delete and re-create the log directory
|
|
|
|
await deleteAllLogs(logPath);
|
|
|
|
mkdirp.sync(logPath);
|
|
|
|
}
|
2018-01-08 21:19:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function isLineAfterDate(line, date) {
|
|
|
|
if (!line) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const data = JSON.parse(line);
|
2018-05-02 01:54:43 +00:00
|
|
|
return new Date(data.time).getTime() > date.getTime();
|
2018-01-08 21:19:25 +00:00
|
|
|
} catch (e) {
|
|
|
|
console.log('error parsing log line', e.stack, line);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function eliminateOutOfDateFiles(logPath, date) {
|
|
|
|
const files = fs.readdirSync(logPath);
|
|
|
|
const paths = files.map(file => path.join(logPath, file));
|
|
|
|
|
2018-05-02 01:54:43 +00:00
|
|
|
return Promise.all(
|
|
|
|
_.map(paths, target =>
|
|
|
|
Promise.all([readFirstLine(target), readLastLines(target, 2)]).then(
|
|
|
|
results => {
|
|
|
|
const start = results[0];
|
|
|
|
const end = results[1].split('\n');
|
|
|
|
|
|
|
|
const file = {
|
|
|
|
path: target,
|
|
|
|
start: isLineAfterDate(start, date),
|
|
|
|
end:
|
|
|
|
isLineAfterDate(end[end.length - 1], date) ||
|
|
|
|
isLineAfterDate(end[end.length - 2], date),
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!file.start && !file.end) {
|
|
|
|
fs.unlinkSync(file.path);
|
|
|
|
}
|
|
|
|
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2018-01-08 21:19:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function eliminateOldEntries(files, date) {
|
|
|
|
const earliest = date.getTime();
|
|
|
|
|
2018-05-02 01:54:43 +00:00
|
|
|
return Promise.all(
|
|
|
|
_.map(files, file =>
|
|
|
|
fetchLog(file.path).then(lines => {
|
|
|
|
const recent = _.filter(
|
|
|
|
lines,
|
|
|
|
line => new Date(line.time).getTime() >= earliest
|
|
|
|
);
|
|
|
|
const text = _.map(recent, line => JSON.stringify(line)).join('\n');
|
|
|
|
|
|
|
|
return fs.writeFileSync(file.path, `${text}\n`);
|
|
|
|
})
|
|
|
|
)
|
|
|
|
);
|
2018-01-08 21:19:25 +00:00
|
|
|
}
|
|
|
|
|
2017-09-25 22:00:19 +00:00
|
|
|
function getLogger() {
|
|
|
|
if (!logger) {
|
2018-05-02 01:54:43 +00:00
|
|
|
throw new Error("Logger hasn't been initialized yet!");
|
2017-09-25 22:00:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return logger;
|
|
|
|
}
|
|
|
|
|
2017-10-04 21:40:35 +00:00
|
|
|
function fetchLog(logFile) {
|
2018-01-08 21:19:25 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
fs.readFile(logFile, { encoding: 'utf8' }, (err, text) => {
|
2017-10-04 21:40:35 +00:00
|
|
|
if (err) {
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
const lines = _.compact(text.split('\n'));
|
2018-05-02 01:54:43 +00:00
|
|
|
const data = _.compact(
|
|
|
|
lines.map(line => {
|
|
|
|
try {
|
|
|
|
return _.pick(JSON.parse(line), ['level', 'time', 'msg']);
|
|
|
|
} catch (e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
);
|
2017-09-25 22:00:19 +00:00
|
|
|
|
2017-10-04 21:40:35 +00:00
|
|
|
return resolve(data);
|
|
|
|
});
|
2017-09-25 22:00:19 +00:00
|
|
|
});
|
2017-10-04 21:40:35 +00:00
|
|
|
}
|
2017-09-25 22:00:19 +00:00
|
|
|
|
2017-10-04 21:40:35 +00:00
|
|
|
function fetch(logPath) {
|
|
|
|
const files = fs.readdirSync(logPath);
|
2018-01-08 21:19:25 +00:00
|
|
|
const paths = files.map(file => path.join(logPath, file));
|
2017-09-25 22:00:19 +00:00
|
|
|
|
2017-11-30 19:56:46 +00:00
|
|
|
// creating a manual log entry for the final log result
|
2018-01-08 21:19:25 +00:00
|
|
|
const now = new Date();
|
2017-11-30 19:56:46 +00:00
|
|
|
const fileListEntry = {
|
|
|
|
level: 30, // INFO
|
|
|
|
time: now.toJSON(),
|
2018-01-08 21:19:25 +00:00
|
|
|
msg: `Loaded this list of log files from logPath: ${files.join(', ')}`,
|
2017-11-30 19:56:46 +00:00
|
|
|
};
|
|
|
|
|
2018-05-02 01:54:43 +00:00
|
|
|
return Promise.all(paths.map(fetchLog)).then(results => {
|
2017-10-04 21:40:35 +00:00
|
|
|
const data = _.flatten(results);
|
2017-11-30 19:56:46 +00:00
|
|
|
|
|
|
|
data.push(fileListEntry);
|
|
|
|
|
2017-10-04 21:40:35 +00:00
|
|
|
return _.sortBy(data, 'time');
|
|
|
|
});
|
2017-09-25 22:00:19 +00:00
|
|
|
}
|
|
|
|
|
2018-01-08 21:19:25 +00:00
|
|
|
function logAtLevel(level, ...args) {
|
2017-10-04 21:03:59 +00:00
|
|
|
if (logger) {
|
|
|
|
// To avoid [Object object] in our log since console.log handles non-strings smoothly
|
2018-05-02 01:54:43 +00:00
|
|
|
const str = args.map(item => {
|
2017-10-04 21:03:59 +00:00
|
|
|
if (typeof item !== 'string') {
|
|
|
|
try {
|
|
|
|
return JSON.stringify(item);
|
2018-01-08 21:19:25 +00:00
|
|
|
} catch (e) {
|
2017-10-04 21:03:59 +00:00
|
|
|
return item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return item;
|
|
|
|
});
|
2019-02-19 23:15:43 +00:00
|
|
|
logger[level](redactAll(str.join(' ')));
|
2019-08-19 14:35:09 +00:00
|
|
|
} else if (isRunningFromConsole) {
|
2018-01-08 21:19:25 +00:00
|
|
|
console._log(...args);
|
2017-10-04 21:03:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-08 21:19:25 +00:00
|
|
|
// This blows up using mocha --watch, so we ensure it is run just once
|
|
|
|
if (!console._log) {
|
|
|
|
console._log = console.log;
|
|
|
|
console.log = _.partial(logAtLevel, 'info');
|
|
|
|
console._error = console.error;
|
|
|
|
console.error = _.partial(logAtLevel, 'error');
|
|
|
|
console._warn = console.warn;
|
|
|
|
console.warn = _.partial(logAtLevel, 'warn');
|
|
|
|
}
|