signal-desktop/ts/logging/set_up_renderer_logging.ts

112 lines
2.8 KiB
TypeScript
Raw Normal View History

2023-01-03 11:55:46 -08:00
// Copyright 2017 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
/* eslint-env node */
import { ipcRenderer as ipc } from 'electron';
2021-03-04 16:44:57 -05:00
import * as path from 'path';
import {
initLogger,
LogLevel as SignalClientLogLevel,
} from '@signalapp/libsignal-client';
2025-06-16 11:59:31 -07:00
import { setPinoDestination, log } from './log';
import * as Errors from '../types/errors';
2022-10-03 15:53:41 -07:00
import { createRotatingPinoDest } from '../util/rotatingPinoDest';
2025-06-16 09:47:18 -07:00
import { redactAll } from '../util/privacy';
2025-06-16 09:47:18 -07:00
let isInitialized = false;
let shouldRestart = false;
export function beforeRestart(): void {
shouldRestart = true;
}
2021-03-04 16:44:57 -05:00
export function initialize(): void {
2025-06-16 09:47:18 -07:00
if (isInitialized) {
throw new Error('Already initialized');
2021-03-04 16:44:57 -05:00
}
2025-06-16 09:47:18 -07:00
isInitialized = true;
2021-03-04 16:44:57 -05:00
const basePath = ipc.sendSync('get-user-data-path');
const logFile = path.join(basePath, 'logs', 'app.log');
2021-03-10 17:41:38 -05:00
const onClose = () => {
if (shouldRestart) {
initialize();
}
};
2022-10-03 15:53:41 -07:00
const stream = createRotatingPinoDest({
logFile,
});
stream.on('close', onClose);
stream.on('error', onClose);
2025-06-16 11:59:31 -07:00
setPinoDestination(stream, redactAll);
2021-03-04 16:44:57 -05:00
}
function toLocation(source?: string, line?: number, column?: number) {
if (source == null) {
return '(@ unknown)';
}
if (line != null && column != null) {
return `(@ ${source}:${line}:${column})`;
}
if (line != null) {
return `(@ ${source}:${line})`;
}
return `(@ ${source})`;
}
window.onerror = (message, source, line, column, error) => {
const errorInfo = Errors.toLogFormat(error);
log.error(
`Top-level unhandled error: ${message}, ${errorInfo}`,
toLocation(source, line, column)
);
};
window.addEventListener('unhandledrejection', rejectionEvent => {
const error = rejectionEvent.reason;
const errorString = Errors.toLogFormat(error);
log.error(`Top-level unhandled promise rejection: ${errorString}`);
});
2025-06-16 11:59:31 -07:00
const libSignalLog = log.child('@signalapp/libsignal-client');
initLogger(
SignalClientLogLevel.Info,
(
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}`;
}
2025-06-16 11:59:31 -07:00
const logString = `${message} ${target}${fileString}`;
if (level === SignalClientLogLevel.Trace) {
2025-06-16 11:59:31 -07:00
libSignalLog.trace(logString);
} else if (level === SignalClientLogLevel.Debug) {
2025-06-16 11:59:31 -07:00
libSignalLog.debug(logString);
} else if (level === SignalClientLogLevel.Info) {
2025-06-16 11:59:31 -07:00
libSignalLog.info(logString);
} else if (level === SignalClientLogLevel.Warn) {
2025-06-16 11:59:31 -07:00
libSignalLog.warn(logString);
} else if (level === SignalClientLogLevel.Error) {
2025-06-16 11:59:31 -07:00
libSignalLog.error(logString);
} else {
2025-06-16 11:59:31 -07:00
libSignalLog.error(`${logString} (unknown log level ${level})`);
}
}
);