Skip to content

Commit 77dec97

Browse files
author
Akos Kitta
committed
fix: forward log from electron to backend process
Otherwise log messages from the electron-main process will not be in the log files when the app is running in production.
1 parent 73ddbef commit 77dec97

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

Diff for: arduino-ide-extension/src/electron-main/theia/electron-main-application.ts

+30
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
} from '@theia/core/electron-shared/electron';
99
import { fork } from 'node:child_process';
1010
import { AddressInfo } from 'node:net';
11+
import { format as logFormat } from 'node:util';
1112
import { join, isAbsolute, resolve } from 'node:path';
1213
import { promises as fs, rm, rmSync } from 'node:fs';
1314
import type { MaybePromise, Mutable } from '@theia/core/lib/common/types';
@@ -39,6 +40,7 @@ import {
3940
CHANNEL_SHOW_PLOTTER_WINDOW,
4041
isShowPlotterWindowParams,
4142
} from '../../electron-common/electron-arduino';
43+
import { ChildProcess } from 'child_process';
4244

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

@@ -493,6 +495,11 @@ export class ElectronMainApplication extends TheiaElectronMainApplication {
493495
args,
494496
await this.getForkOptions()
495497
);
498+
// In production, the electron main's `console` must be rebind, and delegate all log messages to the Theia backend via IPC.
499+
// Otherwise, any error that happens in this process won't be in the log files.
500+
if (!environment.electron.isDevMode()) {
501+
forwardConsoleTo(backendProcess);
502+
}
496503
console.log(`Starting backend process. PID: ${backendProcess.pid}`);
497504
return new Promise((resolve, reject) => {
498505
// The backend server main file is also supposed to send the resolved http(s) server port via IPC.
@@ -777,3 +784,26 @@ function updateAppInfo(
777784
});
778785
return toUpdate;
779786
}
787+
788+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
789+
type ConsoleLogFunction = (message?: any, ...optionalParams: any[]) => void;
790+
const consoleLogFunctionNames = [
791+
'log',
792+
'trace',
793+
'debug',
794+
'info',
795+
'warn',
796+
'error',
797+
] as const;
798+
function forwardConsoleTo(child: ChildProcess) {
799+
for (const name of consoleLogFunctionNames) {
800+
const original = <ConsoleLogFunction>console[name as keyof Console];
801+
console[name] = function () {
802+
// eslint-disable-next-line prefer-rest-params
803+
const messages = Object.values(arguments);
804+
const message = logFormat(...messages);
805+
child.send({ severity: name, message });
806+
original(message);
807+
};
808+
}
809+
}

Diff for: electron-app/arduino-ide-backend-main.js

+18
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,24 @@ function enableFileLogger() {
3333
log(message);
3434
};
3535
}
36+
37+
function forward(message) {
38+
if (
39+
Boolean(message) &&
40+
typeof message === 'object' &&
41+
'severity' in message &&
42+
'message' in message
43+
) {
44+
const severity = message.severity;
45+
const logMessage = message.message;
46+
const logFunction = console[severity];
47+
if (typeof logFunction === 'function' && typeof logMessage === 'string') {
48+
logFunction(logMessage);
49+
}
50+
}
51+
}
52+
53+
process.on('message', forward);
3654
}
3755

3856
if (process.env.IDE2_FILE_LOGGER === 'true') {

0 commit comments

Comments
 (0)