2021-01-27 21:13:33 +00:00
|
|
|
// Copyright 2017-2021 Signal Messenger, LLC
|
2020-10-30 20:34:04 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
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 */
|
2021-01-27 21:13:33 +00:00
|
|
|
/* eslint-disable no-console */
|
|
|
|
|
|
|
|
import * as path from 'path';
|
|
|
|
import * as fs from 'fs';
|
|
|
|
import { app, ipcMain as ipc } from 'electron';
|
2021-03-10 22:41:38 +00:00
|
|
|
import pinoms from 'pino-multi-stream';
|
|
|
|
import pino from 'pino';
|
2021-01-27 21:13:33 +00:00
|
|
|
import * as mkdirp from 'mkdirp';
|
|
|
|
import * as _ from 'lodash';
|
|
|
|
import readFirstLine from 'firstline';
|
|
|
|
import { read as readLastLines } from 'read-last-lines';
|
|
|
|
import rimraf from 'rimraf';
|
2021-03-10 22:41:38 +00:00
|
|
|
import { createStream } from 'rotating-file-stream';
|
2021-01-27 21:13:33 +00:00
|
|
|
|
|
|
|
import {
|
|
|
|
LogEntryType,
|
|
|
|
LogLevel,
|
|
|
|
cleanArgs,
|
|
|
|
getLogLevelString,
|
|
|
|
isLogEntry,
|
|
|
|
} from './shared';
|
|
|
|
|
|
|
|
declare global {
|
|
|
|
// We want to extend `Console`, so we need an interface.
|
|
|
|
// eslint-disable-next-line no-restricted-syntax
|
|
|
|
interface Console {
|
|
|
|
_log: typeof console.log;
|
|
|
|
_warn: typeof console.warn;
|
|
|
|
_error: typeof console.error;
|
|
|
|
}
|
|
|
|
}
|
2018-02-22 18:21:53 +00:00
|
|
|
|
2021-03-10 22:41:38 +00:00
|
|
|
let globalLogger: undefined | pinoms.Logger;
|
2017-09-25 22:00:19 +00:00
|
|
|
|
2019-08-19 14:35:09 +00:00
|
|
|
const isRunningFromConsole = Boolean(process.stdout.isTTY);
|
|
|
|
|
2021-03-10 22:41:38 +00:00
|
|
|
export async function initialize(): Promise<pinoms.Logger> {
|
2021-01-27 21:13:33 +00:00
|
|
|
if (globalLogger) {
|
2017-09-25 22:00:19 +00:00
|
|
|
throw new Error('Already called initialize!');
|
|
|
|
}
|
|
|
|
|
|
|
|
const basePath = app.getPath('userData');
|
|
|
|
const logPath = path.join(basePath, 'logs');
|
|
|
|
mkdirp.sync(logPath);
|
|
|
|
|
2019-12-03 20:02:50 +00:00
|
|
|
try {
|
|
|
|
await cleanupLogs(logPath);
|
|
|
|
} catch (error) {
|
2020-01-08 17:44:54 +00:00
|
|
|
const errorString = `Failed to clean logs; deleting all. Error: ${error.stack}`;
|
2019-12-03 20:02:50 +00:00
|
|
|
console.error(errorString);
|
|
|
|
await deleteAllLogs(logPath);
|
|
|
|
mkdirp.sync(logPath);
|
|
|
|
|
|
|
|
// If we want this log entry to persist on disk, we need to wait until we've
|
|
|
|
// set up our logging infrastructure.
|
|
|
|
setTimeout(() => {
|
|
|
|
console.error(errorString);
|
|
|
|
}, 500);
|
|
|
|
}
|
2019-08-19 14:35:09 +00:00
|
|
|
|
2021-03-04 21:44:57 +00:00
|
|
|
const logFile = path.join(logPath, 'main.log');
|
2021-03-10 22:41:38 +00:00
|
|
|
const stream = createStream(logFile, {
|
|
|
|
interval: '1d',
|
2021-03-12 00:40:59 +00:00
|
|
|
rotate: 3,
|
2021-03-10 22:41:38 +00:00
|
|
|
});
|
|
|
|
|
2021-03-26 16:48:46 +00:00
|
|
|
stream.on('close', () => {
|
|
|
|
globalLogger = undefined;
|
|
|
|
});
|
|
|
|
|
|
|
|
stream.on('error', () => {
|
|
|
|
globalLogger = undefined;
|
|
|
|
});
|
|
|
|
|
2021-03-10 22:41:38 +00:00
|
|
|
const streams: pinoms.Streams = [];
|
|
|
|
streams.push({ stream });
|
2017-09-25 22:00:19 +00:00
|
|
|
|
2019-12-03 20:02:50 +00:00
|
|
|
if (isRunningFromConsole) {
|
2021-03-10 22:41:38 +00:00
|
|
|
streams.push({
|
|
|
|
level: 'debug' as const,
|
2019-12-03 20:02:50 +00:00
|
|
|
stream: process.stdout,
|
2017-09-25 22:00:19 +00:00
|
|
|
});
|
2019-12-03 20:02:50 +00:00
|
|
|
}
|
|
|
|
|
2021-03-10 22:41:38 +00:00
|
|
|
const logger = pinoms({
|
|
|
|
streams,
|
|
|
|
timestamp: pino.stdTimeFunctions.isoTime,
|
|
|
|
});
|
2017-09-25 22:00:19 +00:00
|
|
|
|
2019-12-03 20:02:50 +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}`);
|
2018-03-07 02:19:25 +00:00
|
|
|
}
|
2019-12-03 20:02:50 +00:00
|
|
|
);
|
|
|
|
});
|
2018-03-07 02:19:25 +00:00
|
|
|
|
2019-12-03 20:02:50 +00:00
|
|
|
ipc.on('delete-all-logs', async event => {
|
|
|
|
try {
|
|
|
|
await deleteAllLogs(logPath);
|
|
|
|
} catch (error) {
|
|
|
|
logger.error(`Problem deleting all logs: ${error.stack}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
event.sender.send('delete-all-logs-complete');
|
2018-03-07 02:19:25 +00:00
|
|
|
});
|
2021-01-27 21:13:33 +00:00
|
|
|
|
|
|
|
globalLogger = logger;
|
|
|
|
|
|
|
|
return logger;
|
2018-03-07 02:19:25 +00:00
|
|
|
}
|
|
|
|
|
2021-01-27 21:13:33 +00:00
|
|
|
async function deleteAllLogs(logPath: string): Promise<void> {
|
2018-03-07 02:19:25 +00:00
|
|
|
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
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-01-27 21:13:33 +00:00
|
|
|
async function cleanupLogs(logPath: string) {
|
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
|
|
|
}
|
|
|
|
|
2021-01-27 21:13:33 +00:00
|
|
|
// Exported for testing only.
|
|
|
|
export function isLineAfterDate(line: string, date: Readonly<Date>): boolean {
|
2018-01-08 21:19:25 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-27 21:13:33 +00:00
|
|
|
// Exported for testing only.
|
|
|
|
export function eliminateOutOfDateFiles(
|
|
|
|
logPath: string,
|
|
|
|
date: Readonly<Date>
|
|
|
|
): Promise<
|
|
|
|
Array<{
|
|
|
|
path: string;
|
|
|
|
start: boolean;
|
|
|
|
end: boolean;
|
|
|
|
}>
|
|
|
|
> {
|
2018-01-08 21:19:25 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-01-27 21:13:33 +00:00
|
|
|
// Exported for testing only.
|
|
|
|
export async function eliminateOldEntries(
|
|
|
|
files: ReadonlyArray<{ path: string }>,
|
|
|
|
date: Readonly<Date>
|
|
|
|
): Promise<void> {
|
|
|
|
await Promise.all(
|
2018-05-02 01:54:43 +00:00
|
|
|
_.map(files, file =>
|
|
|
|
fetchLog(file.path).then(lines => {
|
2021-01-27 21:13:33 +00:00
|
|
|
const recent = _.filter(lines, line => new Date(line.time) >= date);
|
2018-05-02 01:54:43 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-01-27 21:13:33 +00:00
|
|
|
// Exported for testing only.
|
|
|
|
export function fetchLog(logFile: string): Promise<Array<LogEntryType>> {
|
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 {
|
2021-01-27 21:13:33 +00:00
|
|
|
const result = _.pick(JSON.parse(line), ['level', 'time', 'msg']);
|
|
|
|
return isLogEntry(result) ? result : null;
|
2018-05-02 01:54:43 +00:00
|
|
|
} 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
|
|
|
|
2021-01-27 21:13:33 +00:00
|
|
|
// Exported for testing only.
|
|
|
|
export function fetch(logPath: string): Promise<Array<LogEntryType>> {
|
2017-10-04 21:40:35 +00:00
|
|
|
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
|
2021-01-27 21:13:33 +00:00
|
|
|
const fileListEntry: LogEntryType = {
|
|
|
|
level: LogLevel.Info,
|
|
|
|
time: new Date().toISOString(),
|
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);
|
|
|
|
|
2021-01-27 21:13:33 +00:00
|
|
|
return _.sortBy(data, logEntry => logEntry.time);
|
2017-10-04 21:40:35 +00:00
|
|
|
});
|
2017-09-25 22:00:19 +00:00
|
|
|
}
|
|
|
|
|
2021-01-27 21:13:33 +00:00
|
|
|
function logAtLevel(level: LogLevel, ...args: ReadonlyArray<unknown>) {
|
|
|
|
if (globalLogger) {
|
|
|
|
const levelString = getLogLevelString(level);
|
|
|
|
globalLogger[levelString](cleanArgs(args));
|
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;
|
2021-01-27 21:13:33 +00:00
|
|
|
console.log = _.partial(logAtLevel, LogLevel.Info);
|
2018-01-08 21:19:25 +00:00
|
|
|
console._error = console.error;
|
2021-01-27 21:13:33 +00:00
|
|
|
console.error = _.partial(logAtLevel, LogLevel.Error);
|
2018-01-08 21:19:25 +00:00
|
|
|
console._warn = console.warn;
|
2021-01-27 21:13:33 +00:00
|
|
|
console.warn = _.partial(logAtLevel, LogLevel.Warn);
|
2018-01-08 21:19:25 +00:00
|
|
|
}
|