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 */
|
|
|
|
|
2021-10-01 18:49:59 +00:00
|
|
|
import { join } from 'path';
|
2021-10-21 22:53:14 +00:00
|
|
|
import split2 from 'split2';
|
|
|
|
import { readdirSync, createReadStream, unlinkSync, writeFileSync } from 'fs';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { BrowserWindow } from 'electron';
|
|
|
|
import { app, ipcMain as ipc } from 'electron';
|
2021-03-10 22:41:38 +00:00
|
|
|
import pino from 'pino';
|
2022-10-03 22:53:41 +00:00
|
|
|
import type { StreamEntry } from 'pino';
|
2021-01-27 21:13:33 +00:00
|
|
|
import * as mkdirp from 'mkdirp';
|
2021-10-21 22:53:14 +00:00
|
|
|
import { filter, flatten, map, pick, sortBy } from 'lodash';
|
2021-01-27 21:13:33 +00:00
|
|
|
import readFirstLine from 'firstline';
|
|
|
|
import { read as readLastLines } from 'read-last-lines';
|
|
|
|
import rimraf from 'rimraf';
|
|
|
|
|
2021-10-01 18:49:59 +00:00
|
|
|
import type { LoggerType } from '../types/Logging';
|
2022-11-22 18:43:43 +00:00
|
|
|
import * as Errors from '../types/errors';
|
2022-05-10 01:12:04 +00:00
|
|
|
import * as durations from '../util/durations';
|
2022-10-03 22:53:41 +00:00
|
|
|
import { createRotatingPinoDest } from '../util/rotatingPinoDest';
|
2021-10-01 18:49:59 +00:00
|
|
|
|
|
|
|
import * as log from './log';
|
2021-04-05 20:44:02 +00:00
|
|
|
import { Environment, getEnvironment } from '../environment';
|
|
|
|
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { FetchLogIpcData, LogEntryType } from './shared';
|
|
|
|
import { LogLevel, cleanArgs, getLogLevelString, isLogEntry } from './shared';
|
2021-01-27 21:13:33 +00:00
|
|
|
|
|
|
|
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-10-21 22:53:14 +00:00
|
|
|
const MAX_LOG_LINES = 1000000;
|
|
|
|
|
2021-10-01 18:49:59 +00:00
|
|
|
let globalLogger: undefined | pino.Logger;
|
2021-05-13 20:54:54 +00:00
|
|
|
let shouldRestart = false;
|
2017-09-25 22:00:19 +00:00
|
|
|
|
2021-04-05 20:44:02 +00:00
|
|
|
const isRunningFromConsole =
|
2021-12-09 08:06:04 +00:00
|
|
|
Boolean(process.stdout.isTTY) || getEnvironment() === Environment.Test;
|
2019-08-19 14:35:09 +00:00
|
|
|
|
2021-07-30 16:43:16 +00:00
|
|
|
export async function initialize(
|
|
|
|
getMainWindow: () => undefined | BrowserWindow
|
2021-10-01 18:49:59 +00:00
|
|
|
): Promise<LoggerType> {
|
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');
|
2021-10-01 18:49:59 +00:00
|
|
|
const logPath = join(basePath, 'logs');
|
2017-09-25 22:00:19 +00:00
|
|
|
mkdirp.sync(logPath);
|
|
|
|
|
2022-05-10 01:12:04 +00:00
|
|
|
let appMetrics = app.getAppMetrics();
|
|
|
|
|
|
|
|
setInterval(() => {
|
|
|
|
// CPU stats are computed since the last call to `getAppMetrics`.
|
|
|
|
appMetrics = app.getAppMetrics();
|
|
|
|
}, 30 * durations.SECOND).unref();
|
|
|
|
|
2019-12-03 20:02:50 +00:00
|
|
|
try {
|
|
|
|
await cleanupLogs(logPath);
|
|
|
|
} catch (error) {
|
2022-11-22 18:43:43 +00:00
|
|
|
const errorString =
|
|
|
|
'Failed to clean logs; deleting all. ' +
|
|
|
|
`Error: ${Errors.toLogFormat(error)}`;
|
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-10-01 18:49:59 +00:00
|
|
|
const logFile = join(logPath, 'main.log');
|
2022-10-03 22:53:41 +00:00
|
|
|
const rotatingStream = createRotatingPinoDest({
|
|
|
|
logFile,
|
2021-03-10 22:41:38 +00:00
|
|
|
});
|
|
|
|
|
2021-05-13 20:54:54 +00:00
|
|
|
const onClose = () => {
|
2021-03-26 16:48:46 +00:00
|
|
|
globalLogger = undefined;
|
|
|
|
|
2021-05-13 20:54:54 +00:00
|
|
|
if (shouldRestart) {
|
2021-07-30 16:43:16 +00:00
|
|
|
initialize(getMainWindow);
|
2021-05-13 20:54:54 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-10-03 22:53:41 +00:00
|
|
|
rotatingStream.on('close', onClose);
|
|
|
|
rotatingStream.on('error', onClose);
|
2021-03-26 16:48:46 +00:00
|
|
|
|
2022-10-03 22:53:41 +00:00
|
|
|
const streams = new Array<StreamEntry>();
|
|
|
|
streams.push({ stream: rotatingStream });
|
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
|
|
|
}
|
|
|
|
|
2022-10-03 22:53:41 +00:00
|
|
|
const logger = pino(
|
|
|
|
{
|
|
|
|
formatters: {
|
|
|
|
// No point in saving pid or hostname
|
|
|
|
bindings: () => ({}),
|
|
|
|
},
|
|
|
|
timestamp: pino.stdTimeFunctions.isoTime,
|
|
|
|
},
|
|
|
|
pino.multistream(streams)
|
|
|
|
);
|
2017-09-25 22:00:19 +00:00
|
|
|
|
2022-05-25 18:04:01 +00:00
|
|
|
ipc.removeHandler('fetch-log');
|
2022-05-10 01:12:04 +00:00
|
|
|
ipc.handle('fetch-log', async () => {
|
2021-07-30 16:43:16 +00:00
|
|
|
const mainWindow = getMainWindow();
|
|
|
|
if (!mainWindow) {
|
|
|
|
logger.info('Logs were requested, but the main window is missing');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let data: FetchLogIpcData;
|
|
|
|
try {
|
|
|
|
const [logEntries, rest] = await Promise.all([
|
|
|
|
fetchLogs(logPath),
|
|
|
|
fetchAdditionalLogData(mainWindow),
|
|
|
|
]);
|
|
|
|
data = {
|
|
|
|
logEntries,
|
2022-05-10 01:12:04 +00:00
|
|
|
appMetrics,
|
2021-07-30 16:43:16 +00:00
|
|
|
...rest,
|
|
|
|
};
|
|
|
|
} catch (error) {
|
2022-11-22 18:43:43 +00:00
|
|
|
logger.error(`Problem loading log data: ${Errors.toLogFormat(error)}`);
|
2021-07-30 16:43:16 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-05-10 01:12:04 +00:00
|
|
|
return data;
|
2019-12-03 20:02:50 +00:00
|
|
|
});
|
2018-03-07 02:19:25 +00:00
|
|
|
|
2022-05-25 18:04:01 +00:00
|
|
|
ipc.removeHandler('delete-all-logs');
|
2022-05-10 01:12:04 +00:00
|
|
|
ipc.handle('delete-all-logs', async () => {
|
2021-05-13 20:54:54 +00:00
|
|
|
// Restart logging when the streams will close
|
|
|
|
shouldRestart = true;
|
|
|
|
|
2019-12-03 20:02:50 +00:00
|
|
|
try {
|
|
|
|
await deleteAllLogs(logPath);
|
|
|
|
} catch (error) {
|
2022-11-22 18:43:43 +00:00
|
|
|
logger.error(`Problem deleting all logs: ${Errors.toLogFormat(error)}`);
|
2019-12-03 20:02:50 +00:00
|
|
|
}
|
2018-03-07 02:19:25 +00:00
|
|
|
});
|
2021-01-27 21:13:33 +00:00
|
|
|
|
|
|
|
globalLogger = logger;
|
|
|
|
|
2021-10-01 18:49:59 +00:00
|
|
|
return log;
|
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);
|
2021-10-01 18:49:59 +00:00
|
|
|
const files = filter(remaining, file => !file.start && file.end);
|
2018-01-08 21:19:25 +00:00
|
|
|
|
|
|
|
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.',
|
2022-11-22 18:43:43 +00:00
|
|
|
Errors.toLogFormat(error)
|
2019-02-21 19:40:05 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// 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) {
|
2022-11-22 18:43:43 +00:00
|
|
|
console.log('error parsing log line', Errors.toLogFormat(e), line);
|
2018-01-08 21:19:25 +00:00
|
|
|
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;
|
|
|
|
}>
|
|
|
|
> {
|
2021-10-01 18:49:59 +00:00
|
|
|
const files = readdirSync(logPath);
|
|
|
|
const paths = files.map(file => join(logPath, file));
|
2018-01-08 21:19:25 +00:00
|
|
|
|
2018-05-02 01:54:43 +00:00
|
|
|
return Promise.all(
|
2021-10-01 18:49:59 +00:00
|
|
|
map(paths, target =>
|
2018-05-02 01:54:43 +00:00
|
|
|
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) {
|
2021-10-01 18:49:59 +00:00
|
|
|
unlinkSync(file.path);
|
2018-05-02 01:54:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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(
|
2021-10-01 18:49:59 +00:00
|
|
|
map(files, file =>
|
2018-05-02 01:54:43 +00:00
|
|
|
fetchLog(file.path).then(lines => {
|
2021-10-01 18:49:59 +00:00
|
|
|
const recent = filter(lines, line => new Date(line.time) >= date);
|
|
|
|
const text = map(recent, line => JSON.stringify(line)).join('\n');
|
2018-05-02 01:54:43 +00:00
|
|
|
|
2021-10-01 18:49:59 +00:00
|
|
|
return writeFileSync(file.path, `${text}\n`);
|
2018-05-02 01:54:43 +00:00
|
|
|
})
|
|
|
|
)
|
|
|
|
);
|
2018-01-08 21:19:25 +00:00
|
|
|
}
|
|
|
|
|
2021-01-27 21:13:33 +00:00
|
|
|
// Exported for testing only.
|
2021-10-21 22:53:14 +00:00
|
|
|
export async function fetchLog(logFile: string): Promise<Array<LogEntryType>> {
|
|
|
|
const results = new Array<LogEntryType>();
|
|
|
|
|
|
|
|
const rawStream = createReadStream(logFile);
|
|
|
|
const jsonStream = rawStream.pipe(
|
|
|
|
split2(line => {
|
|
|
|
try {
|
|
|
|
return JSON.parse(line);
|
|
|
|
} catch (e) {
|
|
|
|
return undefined;
|
2017-10-04 21:40:35 +00:00
|
|
|
}
|
2021-10-21 22:53:14 +00:00
|
|
|
})
|
|
|
|
);
|
2017-10-04 21:40:35 +00:00
|
|
|
|
2021-10-21 22:53:14 +00:00
|
|
|
// Propagate fs errors down to the json stream so that for loop below handles
|
|
|
|
// them.
|
|
|
|
rawStream.on('error', error => jsonStream.emit('error', error));
|
2017-09-25 22:00:19 +00:00
|
|
|
|
2021-10-21 22:53:14 +00:00
|
|
|
for await (const line of jsonStream) {
|
|
|
|
const result = line && pick(line, ['level', 'time', 'msg']);
|
|
|
|
if (!isLogEntry(result)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
results.push(result);
|
|
|
|
if (results.length > MAX_LOG_LINES) {
|
|
|
|
results.shift();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return results;
|
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.
|
2021-07-30 16:43:16 +00:00
|
|
|
export function fetchLogs(logPath: string): Promise<Array<LogEntryType>> {
|
2021-10-01 18:49:59 +00:00
|
|
|
const files = readdirSync(logPath);
|
|
|
|
const paths = files.map(file => 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 => {
|
2021-10-01 18:49:59 +00:00
|
|
|
const data = flatten(results);
|
2017-11-30 19:56:46 +00:00
|
|
|
|
|
|
|
data.push(fileListEntry);
|
|
|
|
|
2021-10-01 18:49:59 +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-07-30 16:43:16 +00:00
|
|
|
export const fetchAdditionalLogData = (
|
|
|
|
mainWindow: BrowserWindow
|
2022-05-10 01:12:04 +00:00
|
|
|
): Promise<Omit<FetchLogIpcData, 'logEntries' | 'appMetrics'>> =>
|
2021-07-30 16:43:16 +00:00
|
|
|
new Promise(resolve => {
|
|
|
|
mainWindow.webContents.send('additional-log-data-request');
|
|
|
|
ipc.once('additional-log-data-response', (_event, data) => {
|
|
|
|
resolve(data);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
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));
|
2021-09-16 14:33:17 +00:00
|
|
|
} else if (isRunningFromConsole && !process.stdout.destroyed) {
|
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) {
|
2021-10-01 18:49:59 +00:00
|
|
|
log.setLogAtLevel(logAtLevel);
|
2021-06-29 17:18:03 +00:00
|
|
|
|
2018-01-08 21:19:25 +00:00
|
|
|
console._log = console.log;
|
2021-10-01 18:49:59 +00:00
|
|
|
console.log = log.info;
|
2018-01-08 21:19:25 +00:00
|
|
|
console._error = console.error;
|
2021-10-01 18:49:59 +00:00
|
|
|
console.error = log.error;
|
2018-01-08 21:19:25 +00:00
|
|
|
console._warn = console.warn;
|
2021-10-01 18:49:59 +00:00
|
|
|
console.warn = log.warn;
|
2018-01-08 21:19:25 +00:00
|
|
|
}
|