Skip to content

Commit ec8b887

Browse files
committed
Avoid an inifnite loop in the logger
1 parent cac535b commit ec8b887

1 file changed

Lines changed: 53 additions & 16 deletions

File tree

packages/core/src/logger/nuclear-logger.ts

Lines changed: 53 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -293,45 +293,82 @@ if (isMain) {
293293
const rendererLogger = new NuclearLogger({ name: 'renderer' });
294294

295295
if (ipcMain.listenerCount(logChannel) === 0) {
296-
ipcMain.on(logChannel, (_event: IpcMainEvent, data: LogArgs) => {
297-
// Forward to all renderer windows
298-
BrowserWindow.getAllWindows().forEach(win => {
299-
win.webContents.send(logChannel, data);
300-
});
296+
ipcMain.on(logChannel, (event: IpcMainEvent, data: LogArgs) => {
297+
// Forward to all renderer windows except sender
298+
BrowserWindow.getAllWindows()
299+
.filter(win => win.webContents.id !== event.sender.id)
300+
.forEach(win => {
301+
win.webContents.send(logChannel, data);
302+
});
301303
rendererLogger.log(...data);
302304
});
303305
}
304306

305307
if (ipcMain.listenerCount(warnChannel) === 0) {
306-
ipcMain.on(warnChannel, (_event: IpcMainEvent, data: LogArgs) => {
307-
BrowserWindow.getAllWindows().forEach(win => {
308-
win.webContents.send(warnChannel, data);
309-
});
308+
ipcMain.on(warnChannel, (event: IpcMainEvent, data: LogArgs) => {
309+
BrowserWindow.getAllWindows()
310+
.filter(win => win.webContents.id !== event.sender.id)
311+
.forEach(win => {
312+
win.webContents.send(warnChannel, data);
313+
});
310314
rendererLogger.warn(...data);
311315
});
312316
}
313317

314318
if (ipcMain.listenerCount(errorChannel) === 0) {
315-
ipcMain.on(errorChannel, (_event: IpcMainEvent, data: LogArgs) => {
316-
BrowserWindow.getAllWindows().forEach(win => {
317-
win.webContents.send(errorChannel, data);
318-
});
319+
ipcMain.on(errorChannel, (event: IpcMainEvent, data: LogArgs) => {
320+
BrowserWindow.getAllWindows()
321+
.filter(win => win.webContents.id !== event.sender.id)
322+
.forEach(win => {
323+
win.webContents.send(errorChannel, data);
324+
});
319325
rendererLogger.error(...data);
320326
});
321327
}
322328
} else if (isRenderer) {
329+
// Add flag to prevent re-logging messages that originated from this renderer
330+
let isLoggingInProgress = false;
331+
323332
ipcRenderer.on(logChannel, (_event: IpcRendererEvent, data: LogArgs) => {
324-
logger.log(...data);
333+
if (!isLoggingInProgress) {
334+
logger.log(...data);
335+
}
325336
});
326337

327338
ipcRenderer.on(warnChannel, (_event: IpcRendererEvent, data: LogArgs) => {
328-
logger.warn(...data);
339+
if (!isLoggingInProgress) {
340+
logger.warn(...data);
341+
}
329342
});
330343

331344
ipcRenderer.on(errorChannel, (_event: IpcRendererEvent, data: LogArgs) => {
332-
logger.error(...data);
345+
if (!isLoggingInProgress) {
346+
logger.error(...data);
347+
}
333348
});
334349

350+
const originalLog = logger.log.bind(logger);
351+
const originalWarn = logger.warn.bind(logger);
352+
const originalError = logger.error.bind(logger);
353+
354+
logger.log = (...args: LogArgs) => {
355+
isLoggingInProgress = true;
356+
originalLog(...args);
357+
isLoggingInProgress = false;
358+
};
359+
360+
logger.warn = (...args: LogArgs) => {
361+
isLoggingInProgress = true;
362+
originalWarn(...args);
363+
isLoggingInProgress = false;
364+
};
365+
366+
logger.error = (...args: LogArgs) => {
367+
isLoggingInProgress = true;
368+
originalError(...args);
369+
isLoggingInProgress = false;
370+
};
371+
335372
ipcRenderer.on(updateChannel, (_event: IpcRendererEvent, flag: boolean) => {
336373
if (flag) {
337374
logger.hookConsole();

0 commit comments

Comments
 (0)