Skip to content

Commit

Permalink
fix: forward log from electron to backend process
Browse files Browse the repository at this point in the history
Otherwise log messages from the electron-main process will not be in
the log files when the app is running in production.
  • Loading branch information
Akos Kitta committed Sep 22, 2023
1 parent 73ddbef commit 77dec97
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from '@theia/core/electron-shared/electron';
import { fork } from 'node:child_process';
import { AddressInfo } from 'node:net';
import { format as logFormat } from 'node:util';
import { join, isAbsolute, resolve } from 'node:path';
import { promises as fs, rm, rmSync } from 'node:fs';
import type { MaybePromise, Mutable } from '@theia/core/lib/common/types';
Expand Down Expand Up @@ -39,6 +40,7 @@ import {
CHANNEL_SHOW_PLOTTER_WINDOW,
isShowPlotterWindowParams,
} from '../../electron-common/electron-arduino';
import { ChildProcess } from 'child_process';

app.commandLine.appendSwitch('disable-http-cache');

Expand Down Expand Up @@ -493,6 +495,11 @@ export class ElectronMainApplication extends TheiaElectronMainApplication {
args,
await this.getForkOptions()
);
// In production, the electron main's `console` must be rebind, and delegate all log messages to the Theia backend via IPC.
// Otherwise, any error that happens in this process won't be in the log files.
if (!environment.electron.isDevMode()) {
forwardConsoleTo(backendProcess);
}
console.log(`Starting backend process. PID: ${backendProcess.pid}`);
return new Promise((resolve, reject) => {
// The backend server main file is also supposed to send the resolved http(s) server port via IPC.
Expand Down Expand Up @@ -777,3 +784,26 @@ function updateAppInfo(
});
return toUpdate;
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
type ConsoleLogFunction = (message?: any, ...optionalParams: any[]) => void;
const consoleLogFunctionNames = [
'log',
'trace',
'debug',
'info',
'warn',
'error',
] as const;
function forwardConsoleTo(child: ChildProcess) {
for (const name of consoleLogFunctionNames) {
const original = <ConsoleLogFunction>console[name as keyof Console];
console[name] = function () {
// eslint-disable-next-line prefer-rest-params
const messages = Object.values(arguments);
const message = logFormat(...messages);
child.send({ severity: name, message });
original(message);
};
}
}
18 changes: 18 additions & 0 deletions electron-app/arduino-ide-backend-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,24 @@ function enableFileLogger() {
log(message);
};
}

function forward(message) {
if (
Boolean(message) &&
typeof message === 'object' &&
'severity' in message &&
'message' in message
) {
const severity = message.severity;
const logMessage = message.message;
const logFunction = console[severity];
if (typeof logFunction === 'function' && typeof logMessage === 'string') {
logFunction(logMessage);
}
}
}

process.on('message', forward);
}

if (process.env.IDE2_FILE_LOGGER === 'true') {
Expand Down

0 comments on commit 77dec97

Please sign in to comment.