2021-01-27 21:13:33 +00:00
|
|
|
// Copyright 2017-2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
/* eslint-env node */
|
|
|
|
|
|
|
|
/* eslint-disable no-console */
|
|
|
|
|
|
|
|
import { ipcRenderer as ipc } from 'electron';
|
|
|
|
import _ from 'lodash';
|
2021-03-04 21:44:57 +00:00
|
|
|
import * as path from 'path';
|
2021-03-10 22:41:38 +00:00
|
|
|
import pino from 'pino';
|
|
|
|
import { createStream } from 'rotating-file-stream';
|
2021-01-27 21:13:33 +00:00
|
|
|
|
2021-05-14 01:18:43 +00:00
|
|
|
import {
|
|
|
|
initLogger,
|
|
|
|
LogLevel as SignalClientLogLevel,
|
|
|
|
} from '@signalapp/signal-client';
|
2021-04-16 23:13:13 +00:00
|
|
|
|
2021-01-27 21:13:33 +00:00
|
|
|
import { uploadDebugLogs } from './debuglogs';
|
2021-06-01 18:15:23 +00:00
|
|
|
import { redactAll } from '../util/privacy';
|
2021-01-27 21:13:33 +00:00
|
|
|
import {
|
2021-07-30 16:43:16 +00:00
|
|
|
FetchLogIpcData,
|
2021-01-27 21:13:33 +00:00
|
|
|
LogEntryType,
|
|
|
|
LogLevel,
|
|
|
|
cleanArgs,
|
|
|
|
getLogLevelString,
|
2021-07-30 16:43:16 +00:00
|
|
|
isFetchLogIpcData,
|
2021-01-27 21:13:33 +00:00
|
|
|
isLogEntry,
|
|
|
|
} from './shared';
|
2021-02-04 19:54:03 +00:00
|
|
|
import * as log from './log';
|
2021-01-27 21:13:33 +00:00
|
|
|
import { reallyJsonStringify } from '../util/reallyJsonStringify';
|
|
|
|
|
|
|
|
// To make it easier to visually scan logs, we make all levels the same length
|
2021-03-10 22:41:38 +00:00
|
|
|
const levelFromName = pino().levels.values;
|
|
|
|
const levelMaxLength: number = Object.keys(levelFromName).reduce(
|
2021-01-27 21:13:33 +00:00
|
|
|
(maxLength, level) => Math.max(maxLength, level.length),
|
|
|
|
0
|
|
|
|
);
|
|
|
|
|
|
|
|
// Backwards-compatible logging, simple strings and no level (defaulted to INFO)
|
|
|
|
function now() {
|
|
|
|
const date = new Date();
|
|
|
|
return date.toJSON();
|
|
|
|
}
|
|
|
|
|
2021-02-04 19:54:03 +00:00
|
|
|
function consoleLog(...args: ReadonlyArray<unknown>) {
|
2021-01-27 21:13:33 +00:00
|
|
|
logAtLevel(LogLevel.Info, ...args);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (window.console) {
|
|
|
|
console._log = console.log;
|
2021-02-04 19:54:03 +00:00
|
|
|
console.log = consoleLog;
|
2021-01-27 21:13:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// The mechanics of preparing a log for publish
|
|
|
|
|
2021-07-30 16:43:16 +00:00
|
|
|
const headerSectionTitle = (title: string) => `========= ${title} =========`;
|
|
|
|
|
|
|
|
const headerSection = (
|
|
|
|
title: string,
|
|
|
|
data: Readonly<Record<string, unknown>>
|
|
|
|
): string => {
|
|
|
|
const sortedEntries = _.sortBy(Object.entries(data), ([key]) => key);
|
|
|
|
return [
|
|
|
|
headerSectionTitle(title),
|
|
|
|
...sortedEntries.map(
|
|
|
|
([key, value]) => `${key}: ${redactAll(String(value))}`
|
|
|
|
),
|
|
|
|
'',
|
|
|
|
].join('\n');
|
|
|
|
};
|
|
|
|
|
|
|
|
const getHeader = ({
|
|
|
|
capabilities,
|
|
|
|
remoteConfig,
|
|
|
|
statistics,
|
|
|
|
user,
|
|
|
|
}: Omit<FetchLogIpcData, 'logEntries'>): string =>
|
|
|
|
[
|
|
|
|
headerSection('System info', {
|
|
|
|
Time: Date.now(),
|
|
|
|
'User agent': window.navigator.userAgent,
|
|
|
|
'Node version': window.getNodeVersion(),
|
|
|
|
Environment: window.getEnvironment(),
|
|
|
|
'App version': window.getVersion(),
|
|
|
|
}),
|
|
|
|
headerSection('User info', user),
|
|
|
|
headerSection('Capabilities', capabilities),
|
|
|
|
headerSection('Remote config', remoteConfig),
|
|
|
|
headerSection('Statistics', statistics),
|
|
|
|
headerSectionTitle('Logs'),
|
|
|
|
].join('\n');
|
2021-01-27 21:13:33 +00:00
|
|
|
|
|
|
|
const getLevel = _.memoize((level: LogLevel): string => {
|
|
|
|
const text = getLogLevelString(level);
|
|
|
|
return text.toUpperCase().padEnd(levelMaxLength, ' ');
|
|
|
|
});
|
|
|
|
|
2021-07-30 16:43:16 +00:00
|
|
|
function formatLine(mightBeEntry: unknown): string {
|
2021-01-27 21:13:33 +00:00
|
|
|
const entry: LogEntryType = isLogEntry(mightBeEntry)
|
|
|
|
? mightBeEntry
|
|
|
|
: {
|
|
|
|
level: LogLevel.Error,
|
|
|
|
msg: `Invalid IPC data when fetching logs. Here's what we could recover: ${reallyJsonStringify(
|
|
|
|
mightBeEntry
|
|
|
|
)}`,
|
|
|
|
time: new Date().toISOString(),
|
|
|
|
};
|
|
|
|
|
|
|
|
return `${getLevel(entry.level)} ${entry.time} ${entry.msg}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
function fetch(): Promise<string> {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
ipc.send('fetch-log');
|
|
|
|
|
2021-07-30 16:43:16 +00:00
|
|
|
ipc.on('fetched-log', (_event, data: unknown) => {
|
|
|
|
let header: string;
|
2021-01-27 21:13:33 +00:00
|
|
|
let body: string;
|
2021-07-30 16:43:16 +00:00
|
|
|
if (isFetchLogIpcData(data)) {
|
|
|
|
const { logEntries } = data;
|
|
|
|
header = getHeader(data);
|
2021-01-27 21:13:33 +00:00
|
|
|
body = logEntries.map(formatLine).join('\n');
|
|
|
|
} else {
|
2021-07-30 16:43:16 +00:00
|
|
|
header = headerSectionTitle('Partial logs');
|
2021-01-27 21:13:33 +00:00
|
|
|
const entry: LogEntryType = {
|
|
|
|
level: LogLevel.Error,
|
|
|
|
msg: 'Invalid IPC data when fetching logs; dropping all logs',
|
|
|
|
time: new Date().toISOString(),
|
|
|
|
};
|
|
|
|
body = formatLine(entry);
|
|
|
|
}
|
|
|
|
|
2021-07-30 16:43:16 +00:00
|
|
|
const result = `${header}\n${body}`;
|
2021-01-27 21:13:33 +00:00
|
|
|
resolve(result);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-03-10 22:41:38 +00:00
|
|
|
let globalLogger: undefined | pino.Logger;
|
2021-05-13 20:54:54 +00:00
|
|
|
let shouldRestart = false;
|
|
|
|
|
|
|
|
export function beforeRestart(): void {
|
|
|
|
shouldRestart = true;
|
|
|
|
}
|
2021-03-04 21:44:57 +00:00
|
|
|
|
|
|
|
export function initialize(): void {
|
|
|
|
if (globalLogger) {
|
|
|
|
throw new Error('Already called initialize!');
|
|
|
|
}
|
|
|
|
|
|
|
|
const basePath = ipc.sendSync('get-user-data-path');
|
|
|
|
const logFile = path.join(basePath, 'logs', 'app.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-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) {
|
|
|
|
initialize();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
stream.on('close', onClose);
|
|
|
|
stream.on('error', onClose);
|
2021-03-26 16:48:46 +00:00
|
|
|
|
2021-03-10 22:41:38 +00:00
|
|
|
globalLogger = pino(
|
|
|
|
{
|
|
|
|
timestamp: pino.stdTimeFunctions.isoTime,
|
|
|
|
},
|
|
|
|
stream
|
|
|
|
);
|
2021-03-04 21:44:57 +00:00
|
|
|
}
|
|
|
|
|
2021-01-27 21:13:33 +00:00
|
|
|
const publish = uploadDebugLogs;
|
|
|
|
|
|
|
|
// A modern logging interface for the browser
|
|
|
|
|
|
|
|
const env = window.getEnvironment();
|
|
|
|
const IS_PRODUCTION = env === 'production';
|
|
|
|
|
|
|
|
function logAtLevel(level: LogLevel, ...args: ReadonlyArray<unknown>): void {
|
|
|
|
if (!IS_PRODUCTION) {
|
|
|
|
const prefix = getLogLevelString(level)
|
|
|
|
.toUpperCase()
|
|
|
|
.padEnd(levelMaxLength, ' ');
|
|
|
|
console._log(prefix, now(), ...args);
|
|
|
|
}
|
|
|
|
|
2021-03-04 21:44:57 +00:00
|
|
|
const levelString = getLogLevelString(level);
|
|
|
|
const msg = cleanArgs(args);
|
|
|
|
|
|
|
|
if (!globalLogger) {
|
|
|
|
throw new Error('Logger has not been initialized yet');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-03-10 22:41:38 +00:00
|
|
|
globalLogger[levelString](msg);
|
2021-01-27 21:13:33 +00:00
|
|
|
}
|
|
|
|
|
2021-02-04 19:54:03 +00:00
|
|
|
log.setLogAtLevel(logAtLevel);
|
|
|
|
|
2021-01-27 21:13:33 +00:00
|
|
|
window.log = {
|
2021-02-04 19:54:03 +00:00
|
|
|
fatal: log.fatal,
|
|
|
|
error: log.error,
|
|
|
|
warn: log.warn,
|
|
|
|
info: log.info,
|
|
|
|
debug: log.debug,
|
|
|
|
trace: log.trace,
|
2021-01-27 21:13:33 +00:00
|
|
|
fetch,
|
|
|
|
publish,
|
|
|
|
};
|
|
|
|
|
|
|
|
window.onerror = (_message, _script, _line, _col, error) => {
|
|
|
|
const errorInfo = error && error.stack ? error.stack : JSON.stringify(error);
|
|
|
|
window.log.error(`Top-level unhandled error: ${errorInfo}`);
|
|
|
|
};
|
|
|
|
|
|
|
|
window.addEventListener('unhandledrejection', rejectionEvent => {
|
|
|
|
const error = rejectionEvent.reason;
|
|
|
|
const errorString =
|
|
|
|
error && error.stack ? error.stack : JSON.stringify(error);
|
|
|
|
window.log.error(`Top-level unhandled promise rejection: ${errorString}`);
|
|
|
|
});
|
2021-04-16 23:13:13 +00:00
|
|
|
|
|
|
|
initLogger(
|
2021-06-01 19:40:09 +00:00
|
|
|
SignalClientLogLevel.Warn,
|
2021-04-16 23:13:13 +00:00
|
|
|
(
|
|
|
|
level: unknown,
|
|
|
|
target: string,
|
|
|
|
file: string | null,
|
|
|
|
line: number | null,
|
|
|
|
message: string
|
|
|
|
) => {
|
|
|
|
let fileString = '';
|
|
|
|
if (file && line) {
|
|
|
|
fileString = ` ${file}:${line}`;
|
|
|
|
} else if (file) {
|
|
|
|
fileString = ` ${file}`;
|
|
|
|
}
|
2021-05-14 01:18:43 +00:00
|
|
|
const logString = `@signalapp/signal-client ${message} ${target}${fileString}`;
|
2021-04-16 23:13:13 +00:00
|
|
|
|
|
|
|
if (level === SignalClientLogLevel.Trace) {
|
|
|
|
log.trace(logString);
|
|
|
|
} else if (level === SignalClientLogLevel.Debug) {
|
|
|
|
log.debug(logString);
|
|
|
|
} else if (level === SignalClientLogLevel.Info) {
|
|
|
|
log.info(logString);
|
|
|
|
} else if (level === SignalClientLogLevel.Warn) {
|
|
|
|
log.warn(logString);
|
|
|
|
} else if (level === SignalClientLogLevel.Error) {
|
|
|
|
log.error(logString);
|
|
|
|
} else {
|
|
|
|
log.error(`${logString} (unknown log level ${level})`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|