Show additional data in debug log header

This commit is contained in:
Evan Hahn 2021-07-30 11:43:16 -05:00 committed by GitHub
parent 03874a788f
commit 689542a9b4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 278 additions and 89 deletions

View file

@ -7,7 +7,7 @@
import * as path from 'path';
import * as fs from 'fs';
import { app, ipcMain as ipc } from 'electron';
import { BrowserWindow, app, ipcMain as ipc } from 'electron';
import pinoms from 'pino-multi-stream';
import pino from 'pino';
import * as mkdirp from 'mkdirp';
@ -21,6 +21,7 @@ import { setLogAtLevel } from './log';
import { Environment, getEnvironment } from '../environment';
import {
FetchLogIpcData,
LogEntryType,
LogLevel,
cleanArgs,
@ -46,7 +47,9 @@ const isRunningFromConsole =
getEnvironment() === Environment.Test ||
getEnvironment() === Environment.TestLib;
export async function initialize(): Promise<pinoms.Logger> {
export async function initialize(
getMainWindow: () => undefined | BrowserWindow
): Promise<pinoms.Logger> {
if (globalLogger) {
throw new Error('Already called initialize!');
}
@ -80,7 +83,7 @@ export async function initialize(): Promise<pinoms.Logger> {
globalLogger = undefined;
if (shouldRestart) {
initialize();
initialize(getMainWindow);
}
};
@ -102,34 +105,44 @@ export async function initialize(): Promise<pinoms.Logger> {
timestamp: pino.stdTimeFunctions.isoTime,
});
ipc.on('fetch-log', event => {
fetch(logPath).then(
data => {
try {
event.sender.send('fetched-log', data);
} catch (err: unknown) {
// NOTE(evanhahn): We don't want to send a message to a window that's closed.
// I wanted to use `event.sender.isDestroyed()` but that seems to fail.
// Instead, we attempt the send and catch the failure as best we can.
const hasUserClosedWindow = isProbablyObjectHasBeenDestroyedError(
err
);
if (hasUserClosedWindow) {
logger.info(
'Logs were requested, but it seems the window was closed'
);
} else {
logger.error(
'Problem replying with fetched logs',
err instanceof Error && err.stack ? err.stack : err
);
}
}
},
error => {
logger.error(`Problem loading log from disk: ${error.stack}`);
ipc.on('fetch-log', async event => {
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,
...rest,
};
} catch (error) {
logger.error(`Problem loading log data: ${error.stack}`);
return;
}
try {
event.sender.send('fetched-log', data);
} catch (err: unknown) {
// NOTE(evanhahn): We don't want to send a message to a window that's closed.
// I wanted to use `event.sender.isDestroyed()` but that seems to fail.
// Instead, we attempt the send and catch the failure as best we can.
const hasUserClosedWindow = isProbablyObjectHasBeenDestroyedError(err);
if (hasUserClosedWindow) {
logger.info('Logs were requested, but it seems the window was closed');
} else {
logger.error(
'Problem replying with fetched logs',
err instanceof Error && err.stack ? err.stack : err
);
}
);
}
});
ipc.on('delete-all-logs', async event => {
@ -293,7 +306,7 @@ export function fetchLog(logFile: string): Promise<Array<LogEntryType>> {
}
// Exported for testing only.
export function fetch(logPath: string): Promise<Array<LogEntryType>> {
export function fetchLogs(logPath: string): Promise<Array<LogEntryType>> {
const files = fs.readdirSync(logPath);
const paths = files.map(file => path.join(logPath, file));
@ -313,6 +326,16 @@ export function fetch(logPath: string): Promise<Array<LogEntryType>> {
});
}
export const fetchAdditionalLogData = (
mainWindow: BrowserWindow
): Promise<Omit<FetchLogIpcData, 'logEntries'>> =>
new Promise(resolve => {
mainWindow.webContents.send('additional-log-data-request');
ipc.once('additional-log-data-response', (_event, data) => {
resolve(data);
});
});
function logAtLevel(level: LogLevel, ...args: ReadonlyArray<unknown>) {
if (globalLogger) {
const levelString = getLogLevelString(level);