johnhaley81 / electron-background-task-app

Example of running a background task in electron
MIT License
83 stars 13 forks source link

Error In background Process #1

Open akshaygarg834 opened 7 years ago

akshaygarg834 commented 7 years ago

Console.log in background process not working. You can't see a single output out of it. How to debug it? Is there a way to change stdout of this bakground process to main process stdout so that all console.log() of background process start show in main process console.

matortheeternal commented 7 years ago

You can use IPC to send a message to the main process to log messages to it, e.g. via:

Background window

const electron = require('electron');
const ipcRenderer = electron.ipcRenderer;

let log = function(message) {
    ipcRenderer.send('worker-message', message);
};

log('Hello world');

Main process

ipcMain.on('worker-message', (e, message) => console.log(message));

You could also pipe it to the developer tools console of the main window via:

Main process

ipcMain.on('worker-message', function(e, message) {
    mainWindow.webContents.send('worker-message', message);
});

Main window

const electron = require('electron');
const ipcRenderer = electron.ipcRenderer;
ipcRenderer.on('worker-message', (event, message) => console.log(message));

You can also see this stack overflow topic for ways to write to stdout in the main process.