From 35773f4a18293dca1dee14b8dff3a2c26957a2e8 Mon Sep 17 00:00:00 2001 From: Sebastian Kupke Date: Fri, 24 May 2019 10:29:10 +0200 Subject: [PATCH] Further developed logging for electron. --- dist/iwmlib.js | 35 +++++++++++++++++++++++++++++++---- dist/iwmlib.pixi.js | 35 +++++++++++++++++++++++++++++++---- lib/capabilities.js | 20 ++++++++++++++++++-- lib/logging.html | 22 ++++++++++++++++++++++ lib/logging.js | 15 +++++++++++++-- 5 files changed, 115 insertions(+), 12 deletions(-) create mode 100644 lib/logging.html diff --git a/dist/iwmlib.js b/dist/iwmlib.js index e5de4dd..ed4d4ff 100644 --- a/dist/iwmlib.js +++ b/dist/iwmlib.js @@ -1545,6 +1545,12 @@ } + const ipc = null; + + try { + ipc = require('electron').ipcRenderer; + } catch (e) {} + /** Basic class for app specific logging requirements. * Can be used to implement persistent logging in electron apps. */ @@ -1553,8 +1559,13 @@ /** Static log function. * @param {*} message */ - log(message) { - console.log(message); + static log(message) { + + if (ipc) { + ipc.send('log', message); + } else { + console.log(message); + } } } @@ -2683,10 +2694,26 @@ /** * Distincts if the app is running inside electron or not. * - * source: https://discuss.atom.io/t/detect-electron-or-web-page-running/33180/3 + * source: https://github.com/cheton/is-electron */ static get isElectron() { - return typeof process != 'undefined' && process.versions && process.versions.electron !== undefined + + // Renderer process + if (typeof window !== 'undefined' && typeof window.process === 'object' && window.process.type === 'renderer') { + return true + } + + // Main process + if (typeof process !== 'undefined' && typeof process.versions === 'object' && !!process.versions.electron) { + return true + } + + // Detect the user agent when the `nodeIntegration` option is set to true + if (typeof navigator === 'object' && typeof navigator.userAgent === 'string' && navigator.userAgent.indexOf('Electron') >= 0) { + return true + } + + return false } /** Returns the display resolution. Necessary for retina displays. diff --git a/dist/iwmlib.pixi.js b/dist/iwmlib.pixi.js index b157618..a8c76f0 100644 --- a/dist/iwmlib.pixi.js +++ b/dist/iwmlib.pixi.js @@ -4709,6 +4709,12 @@ // } } + const ipc = null; + + try { + ipc = require('electron').ipcRenderer; + } catch (e) {} + /** Basic class for app specific logging requirements. * Can be used to implement persistent logging in electron apps. */ @@ -4717,8 +4723,13 @@ /** Static log function. * @param {*} message */ - log(message) { - console.log(message); + static log(message) { + + if (ipc) { + ipc.send('log', message); + } else { + console.log(message); + } } } @@ -5847,10 +5858,26 @@ /** * Distincts if the app is running inside electron or not. * - * source: https://discuss.atom.io/t/detect-electron-or-web-page-running/33180/3 + * source: https://github.com/cheton/is-electron */ static get isElectron() { - return typeof process != 'undefined' && process.versions && process.versions.electron !== undefined + + // Renderer process + if (typeof window !== 'undefined' && typeof window.process === 'object' && window.process.type === 'renderer') { + return true + } + + // Main process + if (typeof process !== 'undefined' && typeof process.versions === 'object' && !!process.versions.electron) { + return true + } + + // Detect the user agent when the `nodeIntegration` option is set to true + if (typeof navigator === 'object' && typeof navigator.userAgent === 'string' && navigator.userAgent.indexOf('Electron') >= 0) { + return true + } + + return false } /** Returns the display resolution. Necessary for retina displays. diff --git a/lib/capabilities.js b/lib/capabilities.js index 08cf516..16c1ea5 100644 --- a/lib/capabilities.js +++ b/lib/capabilities.js @@ -37,10 +37,26 @@ export class Capabilities { /** * Distincts if the app is running inside electron or not. * - * source: https://discuss.atom.io/t/detect-electron-or-web-page-running/33180/3 + * source: https://github.com/cheton/is-electron */ static get isElectron() { - return typeof process != 'undefined' && process.versions && process.versions.electron !== undefined + + // Renderer process + if (typeof window !== 'undefined' && typeof window.process === 'object' && window.process.type === 'renderer') { + return true + } + + // Main process + if (typeof process !== 'undefined' && typeof process.versions === 'object' && !!process.versions.electron) { + return true + } + + // Detect the user agent when the `nodeIntegration` option is set to true + if (typeof navigator === 'object' && typeof navigator.userAgent === 'string' && navigator.userAgent.indexOf('Electron') >= 0) { + return true + } + + return false } /** Returns the display resolution. Necessary for retina displays. diff --git a/lib/logging.html b/lib/logging.html new file mode 100644 index 0000000..955418a --- /dev/null +++ b/lib/logging.html @@ -0,0 +1,22 @@ + + + + + Logging Doctest + + + + + + + + + +

+ Logging +

+

Store informations of your app permanently.

+ + \ No newline at end of file diff --git a/lib/logging.js b/lib/logging.js index c73b177..97df4df 100644 --- a/lib/logging.js +++ b/lib/logging.js @@ -1,3 +1,9 @@ +const ipc = null + +try { + ipc = require('electron').ipcRenderer +} catch (e) {} + /** Basic class for app specific logging requirements. * Can be used to implement persistent logging in electron apps. */ @@ -6,7 +12,12 @@ export default class Logging { /** Static log function. * @param {*} message */ - log(message) { - console.log(message) + static log(message) { + + if (ipc) { + ipc.send('log', message) + } else { + console.log(message) + } } }