2023-01-03 19:55:46 +00:00
|
|
|
// Copyright 2017 Signal Messenger, LLC
|
2021-01-27 21:13:33 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
/* eslint-env node */
|
|
|
|
|
|
|
|
/* eslint-disable no-console */
|
|
|
|
|
|
|
|
import { ipcRenderer as ipc } from 'electron';
|
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';
|
2021-01-27 21:13:33 +00:00
|
|
|
|
2021-05-14 01:18:43 +00:00
|
|
|
import {
|
|
|
|
initLogger,
|
|
|
|
LogLevel as SignalClientLogLevel,
|
2022-03-24 21:47:21 +00:00
|
|
|
} from '@signalapp/libsignal-client';
|
2021-04-16 23:13:13 +00:00
|
|
|
|
2021-01-27 21:13:33 +00:00
|
|
|
import {
|
|
|
|
LogLevel,
|
|
|
|
cleanArgs,
|
|
|
|
getLogLevelString,
|
2021-09-17 18:27:53 +00:00
|
|
|
levelMaxLength,
|
2021-01-27 21:13:33 +00:00
|
|
|
} from './shared';
|
2021-02-04 19:54:03 +00:00
|
|
|
import * as log from './log';
|
2021-08-18 20:08:14 +00:00
|
|
|
import { Environment, getEnvironment } from '../environment';
|
2022-11-22 18:43:43 +00:00
|
|
|
import * as Errors from '../types/errors';
|
2022-10-03 22:53:41 +00:00
|
|
|
import { createRotatingPinoDest } from '../util/rotatingPinoDest';
|
2021-01-27 21:13:33 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-10-03 22:53:41 +00:00
|
|
|
const stream = createRotatingPinoDest({
|
|
|
|
logFile,
|
|
|
|
});
|
|
|
|
|
2021-05-13 20:54:54 +00:00
|
|
|
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(
|
|
|
|
{
|
2022-10-03 22:53:41 +00:00
|
|
|
formatters: {
|
|
|
|
// No point in saving pid or hostname
|
|
|
|
bindings: () => ({}),
|
|
|
|
},
|
2021-03-10 22:41:38 +00:00
|
|
|
timestamp: pino.stdTimeFunctions.isoTime,
|
|
|
|
},
|
|
|
|
stream
|
|
|
|
);
|
2021-03-04 21:44:57 +00:00
|
|
|
}
|
|
|
|
|
2021-01-27 21:13:33 +00:00
|
|
|
// A modern logging interface for the browser
|
|
|
|
|
|
|
|
function logAtLevel(level: LogLevel, ...args: ReadonlyArray<unknown>): void {
|
2024-09-04 18:12:45 +00:00
|
|
|
if (getEnvironment() !== Environment.PackagedApp) {
|
2021-01-27 21:13:33 +00:00
|
|
|
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');
|
|
|
|
}
|
|
|
|
|
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-10-07 23:28:47 +00:00
|
|
|
window.SignalContext = window.SignalContext || {};
|
|
|
|
window.SignalContext.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
|
|
|
};
|
|
|
|
|
2023-03-17 00:03:38 +00:00
|
|
|
function toLocation(
|
|
|
|
event: string | Event,
|
|
|
|
sourceArg?: string,
|
|
|
|
lineArg?: number,
|
|
|
|
columnArg?: number
|
|
|
|
) {
|
|
|
|
let source = sourceArg;
|
|
|
|
let line = lineArg;
|
|
|
|
let column = columnArg;
|
|
|
|
|
|
|
|
if (event instanceof ErrorEvent) {
|
|
|
|
source ??= event.filename;
|
|
|
|
line ??= event.lineno;
|
|
|
|
column ??= event.colno;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (source == null) {
|
|
|
|
return '(@ unknown)';
|
|
|
|
}
|
|
|
|
if (line != null && column != null) {
|
|
|
|
return `(@ ${source}:${line}:${column})`;
|
|
|
|
}
|
|
|
|
if (line != null) {
|
|
|
|
return `(@ ${source}:${line})`;
|
|
|
|
}
|
|
|
|
return `(@ ${source})`;
|
|
|
|
}
|
|
|
|
|
|
|
|
window.onerror = (event, source, line, column, error) => {
|
2022-11-22 18:43:43 +00:00
|
|
|
const errorInfo = Errors.toLogFormat(error);
|
2023-03-17 00:03:38 +00:00
|
|
|
log.error(
|
|
|
|
`Top-level unhandled error: ${errorInfo}`,
|
|
|
|
toLocation(event, source, line, column)
|
|
|
|
);
|
2021-01-27 21:13:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
window.addEventListener('unhandledrejection', rejectionEvent => {
|
|
|
|
const error = rejectionEvent.reason;
|
2022-11-22 18:43:43 +00:00
|
|
|
const errorString = Errors.toLogFormat(error);
|
2021-09-17 18:27:53 +00:00
|
|
|
log.error(`Top-level unhandled promise rejection: ${errorString}`);
|
2021-01-27 21:13:33 +00:00
|
|
|
});
|
2021-04-16 23:13:13 +00:00
|
|
|
|
|
|
|
initLogger(
|
2021-09-20 18:51:30 +00:00
|
|
|
SignalClientLogLevel.Info,
|
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}`;
|
|
|
|
}
|
2022-03-24 21:47:21 +00:00
|
|
|
const logString = `@signalapp/libsignal-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})`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|