How to use the electron-util.is.main function in electron-util

To help you get started, we’ve selected a few electron-util examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github sindresorhus / electron-timber / index.js View on Github external
const logChannel = '__ELECTRON_TIMBER_LOG__';
const warnChannel = '__ELECTRON_TIMBER_WARN__';
const errorChannel = '__ELECTRON_TIMBER_ERROR__';
const updateChannel = '__ELECTRON_TIMBER_UPDATE__';
const defaultsNameSpace = '__ELECTRON_TIMBER_DEFAULTS__';

const filteredLoggers = process.env.TIMBER_LOGGERS && new Set(process.env.TIMBER_LOGGERS.split(','));
const preloadScript = path.resolve(__dirname, 'preload.js');

const logLevels = {
	info: 0,
	warn: 1,
	error: 2
};

if (is.main) {
	global[defaultsNameSpace] = {
		ignore: null,
		shouldHookConsole: false,
		logLevel: is.development ? logLevels.info : logLevels.warn
	};
}

// Flag to indicate whether the console has been hooked or not
let isConsoleHooked = false;
const _console = {};
const hookableMethods = ['log', 'warn', 'error', 'time', 'timeEnd'];

let longestNameLength = 0;

class Timber {
	constructor(options = {}) {
github sindresorhus / electron-timber / index.js View on Github external
if (is.renderer) {
			throw new Error('setDefaults can only be called from the main process');
		}

		// We don't want the `name` property being set as a default
		delete newDefaults.name;
		if (Reflect.has(newDefaults, 'logLevel')) {
			newDefaults.logLevel = logLevels[newDefaults.logLevel];
		}

		Object.assign(global[defaultsNameSpace], newDefaults);
	}
}

const logger = new Timber({
	name: is.main ? 'main' : null
});

const unhookConsoleFn = (hookThisConsole, shouldHookRenderers) => () => {
	if (isConsoleHooked) {
		if (hookThisConsole) {
			isConsoleHooked = false;
			for (const key of hookableMethods) {
				console[key] = _console[key];
				_console[key] = null;
			}
		}

		if (shouldHookRenderers) {
			hookRenderers(false);
		}
	}
github sindresorhus / electron-timber / index.js View on Github external
logger.hookConsole = ({main, renderer} = {main: is.main, renderer: is.renderer}) => {
	if (main && is.renderer) {
		throw new Error('You cannot hook the console in the main process from a renderer process.');
	}

	const hookThisConsole = (is.main && main) || (is.renderer && renderer);
	const shouldHookRenderers = is.main && renderer;

	if (hookThisConsole) {
		if (isConsoleHooked) {
			return unhookConsoleFn(hookThisConsole, shouldHookRenderers);
		}

		isConsoleHooked = true;

		for (const key of hookableMethods) {
			_console[key] = console[key];
			console[key] = logger[key];
		}
	}

	if (shouldHookRenderers) {
		hookRenderers(true);
github sindresorhus / electron-timber / index.js View on Github external
function hookRenderers(flag) {
	if (is.main) {
		global[defaultsNameSpace].shouldHookConsole = flag;
		for (const win of electron.BrowserWindow.getAllWindows()) {
			win.webContents.send(updateChannel, flag);
		}
	}
}