2021-01-27 21:13:33 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
import pino from 'pino';
|
2022-05-10 01:12:04 +00:00
|
|
|
import type { ProcessMetric } from 'electron';
|
2021-07-30 16:43:16 +00:00
|
|
|
import { isRecord } from '../util/isRecord';
|
2021-06-01 18:15:23 +00:00
|
|
|
import { redactAll } from '../util/privacy';
|
2021-01-27 21:13:33 +00:00
|
|
|
import { missingCaseError } from '../util/missingCaseError';
|
|
|
|
import { reallyJsonStringify } from '../util/reallyJsonStringify';
|
2021-09-21 19:17:17 +00:00
|
|
|
import { LogLevel } from '../types/Logging';
|
|
|
|
|
|
|
|
export { LogLevel };
|
2021-01-27 21:13:33 +00:00
|
|
|
|
2021-07-30 16:43:16 +00:00
|
|
|
export type FetchLogIpcData = {
|
|
|
|
capabilities: Record<string, unknown>;
|
|
|
|
remoteConfig: Record<string, unknown>;
|
|
|
|
statistics: Record<string, unknown>;
|
|
|
|
user: Record<string, unknown>;
|
2022-05-10 01:12:04 +00:00
|
|
|
appMetrics: ReadonlyArray<ProcessMetric>;
|
2021-07-30 16:43:16 +00:00
|
|
|
|
|
|
|
// We expect `logEntries` to be `Array<LogEntryType>`, but we don't validate that
|
|
|
|
// upfront—we only validate it when we go to log each line. This improves the
|
|
|
|
// performance, because we don't have to iterate over every single log entry twice. It
|
|
|
|
// also means we can log entries if only some of them are invalid.
|
|
|
|
logEntries: Array<unknown>;
|
|
|
|
};
|
|
|
|
|
|
|
|
// We don't use Zod here because it'd be slow parsing all of the log entries.
|
|
|
|
// Unfortunately, Zod is a bit slow even with `z.array(z.unknown())`.
|
|
|
|
export const isFetchLogIpcData = (data: unknown): data is FetchLogIpcData =>
|
|
|
|
isRecord(data) &&
|
|
|
|
isRecord(data.capabilities) &&
|
|
|
|
isRecord(data.remoteConfig) &&
|
|
|
|
isRecord(data.statistics) &&
|
|
|
|
isRecord(data.user) &&
|
|
|
|
Array.isArray(data.logEntries);
|
|
|
|
|
2021-03-10 22:41:38 +00:00
|
|
|
// These match [Pino's core fields][1].
|
|
|
|
// [1]: https://getpino.io/#/?id=usage
|
2021-06-02 20:13:33 +00:00
|
|
|
export type LogEntryType = Readonly<{
|
|
|
|
level: LogLevel;
|
|
|
|
msg: string;
|
|
|
|
time: string;
|
|
|
|
}>;
|
|
|
|
|
|
|
|
// The code below is performance sensitive since it runs for > 100k log entries
|
|
|
|
// whenever we want to send the debug log. We can't use `zod` because it clones
|
|
|
|
// the data on successful parse and ruins the performance.
|
|
|
|
export const isLogEntry = (data: unknown): data is LogEntryType => {
|
2021-07-30 16:43:16 +00:00
|
|
|
if (!isRecord(data)) {
|
2021-06-02 20:13:33 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { level, msg, time } = data as Partial<LogEntryType>;
|
|
|
|
|
|
|
|
if (typeof level !== 'number') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!LogLevel[level]) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof msg !== 'string') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof time !== 'string') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return !Number.isNaN(new Date(time).getTime());
|
|
|
|
};
|
2021-01-27 21:13:33 +00:00
|
|
|
|
2021-03-10 22:41:38 +00:00
|
|
|
export function getLogLevelString(value: LogLevel): pino.Level {
|
2021-01-27 21:13:33 +00:00
|
|
|
switch (value) {
|
|
|
|
case LogLevel.Fatal:
|
|
|
|
return 'fatal';
|
|
|
|
case LogLevel.Error:
|
|
|
|
return 'error';
|
|
|
|
case LogLevel.Warn:
|
|
|
|
return 'warn';
|
|
|
|
case LogLevel.Info:
|
|
|
|
return 'info';
|
|
|
|
case LogLevel.Debug:
|
|
|
|
return 'debug';
|
|
|
|
case LogLevel.Trace:
|
|
|
|
return 'trace';
|
|
|
|
default:
|
|
|
|
throw missingCaseError(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function cleanArgs(args: ReadonlyArray<unknown>): string {
|
|
|
|
return redactAll(
|
|
|
|
args
|
|
|
|
.map(item =>
|
|
|
|
typeof item === 'string' ? item : reallyJsonStringify(item)
|
|
|
|
)
|
|
|
|
.join(' ')
|
|
|
|
);
|
|
|
|
}
|
2021-09-17 18:27:53 +00:00
|
|
|
|
|
|
|
// To make it easier to visually scan logs, we make all levels the same length
|
|
|
|
const levelFromName = pino().levels.values;
|
|
|
|
export const levelMaxLength: number = Object.keys(levelFromName).reduce(
|
|
|
|
(maxLength, level) => Math.max(maxLength, level.length),
|
|
|
|
0
|
|
|
|
);
|