diff --git a/README.md b/README.md index 42c3d19b97..595ece48da 100644 --- a/README.md +++ b/README.md @@ -135,6 +135,7 @@ Debug messages are disabled by default, enable them with the `--log-debug` flag. | `--log-debug` | Log debug messages | | `--log-to-console` | Output the log to stout / chrome dev console | | `--machine-readable-stacktrace` | Enable JSON stacktrace | +| `--no-color` | Disable colors in the output of main process | #### Log locations diff --git a/package-lock.json b/package-lock.json index 6b2ac45271..1a567bdc50 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "deltachat-desktop", - "version": "0.200.0", + "version": "0.201.0", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -2777,10 +2777,9 @@ } }, "colors": { - "version": "1.3.3", - "resolved": "https://registry.npmjs.org/colors/-/colors-1.3.3.tgz", - "integrity": "sha512-mmGt/1pZqYRjMxB1axhTo16/snVZ5krrKkcmMeVKxzECMMXoCgnvTPp10QgHfcbQZw8Dq2jMNG6je4JlWU0gWg==", - "dev": true + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/colors/-/colors-1.4.0.tgz", + "integrity": "sha512-a+UqTh4kgZg/SlGvfbzDHpgRu7AAQOmmqRHJnxhRZICKFUT91brVhNNt58CMWU9PsBbv3PDCZUHbVxuDiH2mtA==" }, "combined-stream": { "version": "1.0.8", diff --git a/package.json b/package.json index 4faf7ba4df..5532dca6ad 100644 --- a/package.json +++ b/package.json @@ -82,7 +82,8 @@ "simple-markdown": "^0.4.4", "styled-components": "^4.3.2", "tempy": "^0.3.0", - "use-debounce": "^3.0.1" + "use-debounce": "^3.0.1", + "colors": "^1.4.0" }, "devDependencies": { "@babel/core": "^7.5.5", @@ -91,7 +92,6 @@ "@types/mapbox-gl": "^0.54.2", "babel-loader": "^8.0.6", "chai": "^4.2.0", - "colors": "^1.3.3", "cp-file": "^7.0.0", "depcheck": "^0.8.3", "electron": "^4.2.9", diff --git a/src/logger.js b/src/logger.js index 9cc4a2f489..2a0d6d7ca1 100644 --- a/src/logger.js +++ b/src/logger.js @@ -1,5 +1,7 @@ const esp = require('error-stack-parser') const { app, remote } = require('electron') +const colors = require('colors/safe') +const startTime = Date.now() const rc = remote ? remote.app.rc : app ? app.rc : {} @@ -17,7 +19,7 @@ function setLogHandler (LogHandler) { handler = LogHandler } -function log (channel, level, ...args) { +function log ({ channel, isMainProcess }, level, stacktrace, args) { const variant = LoggerVariants[level] if (!handler) { /* ignore-console-log */ @@ -26,9 +28,30 @@ function log (channel, level, ...args) { console.log(`Log Message: ${channel} ${level} ${args.join(' ')}`) throw Error('Failed to log message - Handler not initilized yet') } - handler(channel, variant.level, ...args) + handler(channel, variant.level, stacktrace, ...args) if (rc['log-to-console']) { - variant.log(channel, variant.level, ...args) + if (isMainProcess) { + const begining = `${Math.round((Date.now() - startTime) / 100) / 10}s ${[ + '[D]', + colors.blue('[i]'), + colors.yellow('[w]'), + colors.red('[E]'), + colors.red('[C]') + ][level]}${colors.grey(channel)}:` + if (!stacktrace) { + /* ignore-console-log */ + console.log(begining, ...args) + } else { + /* ignore-console-log */ + console.log(begining, ...args, colors.red(stacktrace)) + } + } else { + if (stacktrace) { + variant.log(channel, variant.level, stacktrace, ...args) + } else { + variant.log(channel, variant.level, ...args) + } + } } } @@ -39,34 +62,35 @@ function getStackTrace () { } class Logger { - constructor (channel) { + constructor (channel, isMainProcess) { this.channel = channel + this.isMainProcess = isMainProcess } debug (...args) { if (!rc['log-debug']) return - log(this.channel, 0, [], ...args) + log(this, 0, undefined, args) } info (...args) { - log(this.channel, 1, [], ...args) + log(this, 1, undefined, args) } warn (...args) { - log(this.channel, 2, getStackTrace(), ...args) + log(this, 2, getStackTrace(), args) } error (...args) { - log(this.channel, 3, getStackTrace(), ...args) + log(this, 3, getStackTrace(), args) } critical (...args) { - log(this.channel, 4, getStackTrace(), ...args) + log(this, 4, getStackTrace(), args) } } -function getLogger (channel) { - return new Logger(channel) +function getLogger (channel, isMainProcess = false) { + return new Logger(channel, isMainProcess) } module.exports = { setLogHandler, Logger, getLogger } diff --git a/src/main/deltachat/backup.js b/src/main/deltachat/backup.js index d78e910fcb..d339a47bca 100644 --- a/src/main/deltachat/backup.js +++ b/src/main/deltachat/backup.js @@ -6,7 +6,7 @@ const fs = require('fs-extra') const { ipcMain } = require('electron') const path = require('path') const EventEmitter = require('events').EventEmitter -const log = require('../../logger').getLogger('main/deltachat/backup') +const log = require('../../logger').getLogger('main/deltachat/backup', true) function backupExport (dir) { this._dc.importExport(C.DC_IMEX_EXPORT_BACKUP, dir) diff --git a/src/main/deltachat/chatlist.js b/src/main/deltachat/chatlist.js index 4efba2bbc7..ec0415e0e8 100644 --- a/src/main/deltachat/chatlist.js +++ b/src/main/deltachat/chatlist.js @@ -1,5 +1,5 @@ const C = require('deltachat-node/constants') -const log = require('../../logger').getLogger('main/deltachat/chatlist') +const log = require('../../logger').getLogger('main/deltachat/chatlist', true) const { app } = require('electron') function selectChat (chatId) { diff --git a/src/main/deltachat/chatmethods.js b/src/main/deltachat/chatmethods.js index 1db46f9bcb..ae917f1234 100644 --- a/src/main/deltachat/chatmethods.js +++ b/src/main/deltachat/chatmethods.js @@ -1,6 +1,6 @@ const DeltaChat = require('deltachat-node') const C = require('deltachat-node/constants') -const log = require('../../logger').getLogger('main/deltachat/chatmethods') +const log = require('../../logger').getLogger('main/deltachat/chatmethods', true) function getInfo () { if (this.ready === true) { diff --git a/src/main/deltachat/index.js b/src/main/deltachat/index.js index 236fa524bf..3d007503f9 100644 --- a/src/main/deltachat/index.js +++ b/src/main/deltachat/index.js @@ -1,7 +1,8 @@ const C = require('deltachat-node/constants') const eventStrings = require('deltachat-node/events') const EventEmitter = require('events').EventEmitter -const log = require('../../logger').getLogger('main/deltachat') +const log = require('../../logger').getLogger('main/deltachat', true) +const logCoreEv = require('../../logger').getLogger('core/event', true) const windows = require('../windows') const { app } = require('electron') @@ -38,7 +39,7 @@ class DeltaChatController extends EventEmitter { if (data1 === 0) data1 = '' - log.debug('Core Event', event, data1, data2) + logCoreEv.debug(event, data1, data2) } callMethod (evt, methodName, args) { diff --git a/src/main/deltachat/login.js b/src/main/deltachat/login.js index 4b5add6385..53b5a6e43f 100644 --- a/src/main/deltachat/login.js +++ b/src/main/deltachat/login.js @@ -1,6 +1,6 @@ const DeltaChat = require('deltachat-node') const C = require('deltachat-node/constants') -const log = require('../../logger').getLogger('main/deltachat/login') +const log = require('../../logger').getLogger('main/deltachat/login', true) const path = require('path') /** diff --git a/src/main/deltachat/settings.js b/src/main/deltachat/settings.js index 9cea264981..a97e38689b 100644 --- a/src/main/deltachat/settings.js +++ b/src/main/deltachat/settings.js @@ -1,5 +1,5 @@ const C = require('deltachat-node/constants') -const log = require('../../logger').getLogger('main/deltachat/settings') +const log = require('../../logger').getLogger('main/deltachat/settings', true) function setConfig (key, value) { log.info(`Setting config ${key}:${value}`) diff --git a/src/main/index.js b/src/main/index.js index ac32bb758c..d11896a520 100644 --- a/src/main/index.js +++ b/src/main/index.js @@ -19,7 +19,7 @@ mkdirp.sync(getLogsPath()) // Setup Logger const logHandler = require('./log-handler')() const logger = require('../logger') -const log = logger.getLogger('main/index') +const log = logger.getLogger('main/index', true) logger.setLogHandler(logHandler.log) process.on('exit', logHandler.end) diff --git a/src/main/ipc.js b/src/main/ipc.js index 9aaabb50fb..e0ddb555e4 100644 --- a/src/main/ipc.js +++ b/src/main/ipc.js @@ -11,7 +11,7 @@ const { getConfigPath } = require('../application-constants') const localize = require('../localize') const menu = require('./menu') const windows = require('./windows') -const log = require('../logger').getLogger('main/ipc') +const log = require('../logger').getLogger('main/ipc', true) const DeltaChat = (() => { try { return require('./deltachat/index.js') diff --git a/src/main/log-handler.js b/src/main/log-handler.js index 2281983304..98652aca2a 100644 --- a/src/main/log-handler.js +++ b/src/main/log-handler.js @@ -15,7 +15,7 @@ function logName () { `${pad(d.getHours())}-`, `${pad(d.getMinutes())}-`, `${pad(d.getSeconds())}`, - '.log' + '.log.tsv' ].join('') return path.join(dir, fileName) } diff --git a/src/main/menu.js b/src/main/menu.js index b77a442f40..f2f7104b86 100644 --- a/src/main/menu.js +++ b/src/main/menu.js @@ -1,7 +1,7 @@ module.exports = { init } const { app, Menu, shell } = require('electron') -const log = require('../logger').getLogger('main/menu') +const log = require('../logger').getLogger('main/menu', true) const windows = require('./windows') const { homePageUrl, diff --git a/src/main/state.js b/src/main/state.js index 4399231eab..fbafd2aef2 100644 --- a/src/main/state.js +++ b/src/main/state.js @@ -1,6 +1,6 @@ const appConfig = require('../application-config') const { EventEmitter } = require('events') -const log = require('../logger').getLogger('main/state') +const log = require('../logger').getLogger('main/state', true) const SAVE_DEBOUNCE_INTERVAL = 1000 diff --git a/src/main/windows/main.js b/src/main/windows/main.js index c3b193e293..da205bd0ba 100644 --- a/src/main/windows/main.js +++ b/src/main/windows/main.js @@ -22,7 +22,7 @@ const { windowDefaults } = require('../../application-constants') -const log = require('../../logger').getLogger('main/mainWindow') +const log = require('../../logger').getLogger('main/mainWindow', true) function init (app, options) { if (main.win) {