From f8f6c4e4095d4435810d0c7eeb0a6d51b03a784b Mon Sep 17 00:00:00 2001 From: Kyle Zsembery Date: Tue, 22 Sep 2020 11:47:49 +1000 Subject: [PATCH 1/7] add bunyan logger --- package-lock.json | 92 +++++++++--- package.json | 1 + src-electron/main-process/modules/backend.js | 137 ++++++++++++++---- src-electron/main-process/modules/logger.js | 24 +++ .../main-process/modules/wallet-rpc.js | 1 - src/components/language_select.vue | 4 +- src/pages/wallet/send.vue | 1 + 7 files changed, 210 insertions(+), 50 deletions(-) create mode 100644 src-electron/main-process/modules/logger.js diff --git a/package-lock.json b/package-lock.json index 10b896d6..1badd9d4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2671,8 +2671,7 @@ "balanced-match": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", - "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=", - "dev": true + "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=" }, "base": { "version": "0.11.2", @@ -3003,7 +3002,6 @@ "version": "1.1.11", "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", - "dev": true, "requires": { "balanced-match": "^1.0.0", "concat-map": "0.0.1" @@ -3248,6 +3246,17 @@ "integrity": "sha1-hZgoeOIbmOHGZCXgPQF0eI9Wnug=", "dev": true }, + "bunyan": { + "version": "1.8.14", + "resolved": "https://registry.npmjs.org/bunyan/-/bunyan-1.8.14.tgz", + "integrity": "sha512-LlahJUxXzZLuw/hetUQJmRgZ1LF6+cr5TPpRj6jf327AsiIq2jhYEH4oqUUkVKTor+9w2BT3oxVwhzE5lw9tcg==", + "requires": { + "dtrace-provider": "~0.8", + "moment": "^2.19.3", + "mv": "~2", + "safe-json-stringify": "~1" + } + }, "bytes": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.1.0.tgz", @@ -4034,8 +4043,7 @@ "concat-map": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", - "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=", - "dev": true + "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=" }, "concat-stream": { "version": "1.6.2", @@ -5230,6 +5238,15 @@ "integrity": "sha512-YXQl1DSa4/PQyRfgrv6aoNjhasp/p4qs9FjJ4q4cQk+8m4r6k4ZSiEyytKG8f8W9gi8WsQtIObNmKd+tMzNTmA==", "dev": true }, + "dtrace-provider": { + "version": "0.8.8", + "resolved": "https://registry.npmjs.org/dtrace-provider/-/dtrace-provider-0.8.8.tgz", + "integrity": "sha512-b7Z7cNtHPhH9EJhNNbbeqTcXB8LGFFZhq1PGgEvpeHlzd36bhbdTWoE/Ba/YguqpBSlAPKnARWhVlhunCMwfxg==", + "optional": true, + "requires": { + "nan": "^2.14.0" + } + }, "duplexer": { "version": "0.1.2", "resolved": "https://registry.npmjs.org/duplexer/-/duplexer-0.1.2.tgz", @@ -8576,7 +8593,6 @@ "version": "1.0.6", "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz", "integrity": "sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=", - "dev": true, "requires": { "once": "^1.3.0", "wrappy": "1" @@ -8585,8 +8601,7 @@ "inherits": { "version": "2.0.4", "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", - "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==", - "dev": true + "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==" }, "ini": { "version": "1.3.5", @@ -10075,7 +10090,6 @@ "version": "3.0.4", "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", - "dev": true, "requires": { "brace-expansion": "^1.1.7" } @@ -10181,8 +10195,7 @@ "moment": { "version": "2.27.0", "resolved": "https://registry.npmjs.org/moment/-/moment-2.27.0.tgz", - "integrity": "sha512-al0MUK7cpIcglMv3YF13qSgdAIqxHTO7brRtaz3DlSULbqfazqkc5kEjNrLDOM7fsjshoFIihnU8snrP7zUvhQ==", - "dev": true + "integrity": "sha512-al0MUK7cpIcglMv3YF13qSgdAIqxHTO7brRtaz3DlSULbqfazqkc5kEjNrLDOM7fsjshoFIihnU8snrP7zUvhQ==" }, "move-concurrently": { "version": "1.0.1", @@ -10236,11 +10249,45 @@ "integrity": "sha512-nnbWWOkoWyUsTjKrhgD0dcz22mdkSnpYqbEjIm2nhwhuxlSkpywJmBo8h0ZqJdkp73mb90SssHkN4rsRaBAfAA==", "dev": true }, + "mv": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/mv/-/mv-2.1.1.tgz", + "integrity": "sha1-rmzg1vbV4KT32JN5jQPB6pVZtqI=", + "optional": true, + "requires": { + "mkdirp": "~0.5.1", + "ncp": "~2.0.0", + "rimraf": "~2.4.0" + }, + "dependencies": { + "glob": { + "version": "6.0.4", + "resolved": "https://registry.npmjs.org/glob/-/glob-6.0.4.tgz", + "integrity": "sha1-DwiGD2oVUSey+t1PnOJLGqtuTSI=", + "optional": true, + "requires": { + "inflight": "^1.0.4", + "inherits": "2", + "minimatch": "2 || 3", + "once": "^1.3.0", + "path-is-absolute": "^1.0.0" + } + }, + "rimraf": { + "version": "2.4.5", + "resolved": "https://registry.npmjs.org/rimraf/-/rimraf-2.4.5.tgz", + "integrity": "sha1-7nEM5dk6j9uFb7Xqj/Di11k0sto=", + "optional": true, + "requires": { + "glob": "^6.0.1" + } + } + } + }, "nan": { "version": "2.14.1", "resolved": "https://registry.npmjs.org/nan/-/nan-2.14.1.tgz", - "integrity": "sha512-isWHgVjnFjh2x2yuJ/tj3JbwoHu3UC2dX5G/88Cm24yB6YopVgxvBObDY7n5xW6ExmFhJpSEQqFPvq9zaXc8Jw==", - "dev": true + "integrity": "sha512-isWHgVjnFjh2x2yuJ/tj3JbwoHu3UC2dX5G/88Cm24yB6YopVgxvBObDY7n5xW6ExmFhJpSEQqFPvq9zaXc8Jw==" }, "nanomatch": { "version": "1.2.13", @@ -10267,6 +10314,12 @@ "integrity": "sha1-Sr6/7tdUHywnrPspvbvRXI1bpPc=", "dev": true }, + "ncp": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/ncp/-/ncp-2.0.0.tgz", + "integrity": "sha1-GVoh1sRuNh0vsSgbo4uR6d9727M=", + "optional": true + }, "negotiator": { "version": "0.6.2", "resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.2.tgz", @@ -10907,7 +10960,6 @@ "version": "1.4.0", "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", "integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=", - "dev": true, "requires": { "wrappy": "1" } @@ -11254,8 +11306,7 @@ "path-is-absolute": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", - "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=", - "dev": true + "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=" }, "path-is-inside": { "version": "1.0.2", @@ -12986,6 +13037,12 @@ "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==" }, + "safe-json-stringify": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/safe-json-stringify/-/safe-json-stringify-1.2.0.tgz", + "integrity": "sha512-gH8eh2nZudPQO6TytOvbxnuhYBOvDBBLW52tz5q6X58lJcd/tkmqFR+5Z9adS8aJtURSXWThWy/xJtJwixErvg==", + "optional": true + }, "safe-regex": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/safe-regex/-/safe-regex-1.1.0.tgz", @@ -16617,8 +16674,7 @@ "wrappy": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", - "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=", - "dev": true + "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=" }, "write": { "version": "1.0.3", diff --git a/package.json b/package.json index f47a2fd2..1221153f 100644 --- a/package.json +++ b/package.json @@ -23,6 +23,7 @@ }, "dependencies": { "axios": "^0.18.1", + "bunyan": "^1.8.14", "electron-is-dev": "^1.0.1", "electron-updater": "^4.2.0", "electron-window-state": "^5.0.3", diff --git a/src-electron/main-process/modules/backend.js b/src-electron/main-process/modules/backend.js index eaa3c279..e326490b 100644 --- a/src-electron/main-process/modules/backend.js +++ b/src-electron/main-process/modules/backend.js @@ -5,6 +5,7 @@ import { dialog } from "electron"; import semver from "semver"; import axios from "axios"; import { version } from "../../../package.json"; +const bunyan = require("bunyan"); const WebSocket = require("ws"); const os = require("os"); @@ -24,6 +25,7 @@ export class Backend { this.config_file = null; this.config_data = {}; this.scee = new SCEE(); + this.log = null; } init(config) { @@ -142,7 +144,10 @@ export class Backend { data }; - let encrypted_data = this.scee.encryptString(JSON.stringify(message), this.token); + let encrypted_data = this.scee.encryptString( + JSON.stringify(message), + this.token + ); this.wss.clients.forEach(function each(client) { if (client.readyState === WebSocket.OPEN) { @@ -185,14 +190,22 @@ export class Backend { case "quick_save_config": // save only partial config settings Object.keys(params).map(key => { - this.config_data[key] = Object.assign(this.config_data[key], params[key]); - }); - fs.writeFile(this.config_file, JSON.stringify(this.config_data, null, 4), "utf8", () => { - this.send("set_app_data", { - config: params, - pending_config: params - }); + this.config_data[key] = Object.assign( + this.config_data[key], + params[key] + ); }); + fs.writeFile( + this.config_file, + JSON.stringify(this.config_data, null, 4), + "utf8", + () => { + this.send("set_app_data", { + config: params, + pending_config: params + }); + } + ); break; case "save_config_init": case "save_config": { @@ -208,12 +221,18 @@ export class Backend { } Object.keys(params).map(key => { - this.config_data[key] = Object.assign(this.config_data[key], params[key]); + this.config_data[key] = Object.assign( + this.config_data[key], + params[key] + ); }); const validated = Object.keys(this.defaults) .filter(k => k in this.config_data) - .map(k => [k, this.validate_values(this.config_data[k], this.defaults[k])]) + .map(k => [ + k, + this.validate_values(this.config_data[k], this.defaults[k]) + ]) .reduce((map, obj) => { map[obj[0]] = obj[1]; return map; @@ -225,19 +244,24 @@ export class Backend { ...validated }; - fs.writeFile(this.config_file, JSON.stringify(this.config_data, null, 4), "utf8", () => { - if (data.method == "save_config_init") { - this.startup(); - } else { - this.send("set_app_data", { - config: this.config_data, - pending_config: this.config_data - }); - if (config_changed) { - this.send("settings_changed_reboot"); + fs.writeFile( + this.config_file, + JSON.stringify(this.config_data, null, 4), + "utf8", + () => { + if (data.method == "save_config_init") { + this.startup(); + } else { + this.send("set_app_data", { + config: this.config_data, + pending_config: this.config_data + }); + if (config_changed) { + this.send("settings_changed_reboot"); + } } } - }); + ); break; } case "init": @@ -255,7 +279,10 @@ export class Backend { } if (path) { - const baseUrl = net_type === "testnet" ? "https://lokitestnet.com" : "https://lokiblocks.com"; + const baseUrl = + net_type === "testnet" + ? "https://lokitestnet.com" + : "https://lokiblocks.com"; const url = `${baseUrl}/${path}/`; require("electron").shell.openExternal(url + params.id); } @@ -279,12 +306,18 @@ export class Backend { if (err) { this.send("show_notification", { type: "negative", - i18n: ["notification.errors.errorSavingItem", { item: params.type }], + i18n: [ + "notification.errors.errorSavingItem", + { item: params.type } + ], timeout: 2000 }); } else { this.send("show_notification", { - i18n: ["notification.positive.itemSaved", { item: params.type, filename }], + i18n: [ + "notification.positive.itemSaved", + { item: params.type, filename } + ], timeout: 2000 }); } @@ -317,6 +350,24 @@ export class Backend { } } + initLogger(logPath) { + console.log("Init Logging at path: " + logPath); + try { + this.log = bunyan.createLogger({ + name: "log", + streams: [ + { + level: "debug", + path: path.join(logPath, "electron.log") + } + ] + }); + } catch (e) { + console.log("Could not create the logger"); + console.log(e); + } + } + startup() { this.send("set_app_data", { remotes: this.remotes, @@ -344,14 +395,20 @@ export class Backend { if (!this.config_data.hasOwnProperty(key)) { this.config_data[key] = {}; } - this.config_data[key] = Object.assign(this.config_data[key], disk_config_data[key]); + this.config_data[key] = Object.assign( + this.config_data[key], + disk_config_data[key] + ); }); // here we may want to check if config data is valid, if not also send code -1 // i.e. check ports are integers and > 1024, check that data dir path exists, etc const validated = Object.keys(this.defaults) .filter(k => k in this.config_data) - .map(k => [k, this.validate_values(this.config_data[k], this.defaults[k])]) + .map(k => [ + k, + this.validate_values(this.config_data[k], this.defaults[k]) + ]) .reduce((map, obj) => { map[obj[0]] = obj[1]; return map; @@ -364,7 +421,12 @@ export class Backend { }; // save config file back to file, so updated options are stored on disk - fs.writeFile(this.config_file, JSON.stringify(this.config_data, null, 4), "utf8", () => {}); + fs.writeFile( + this.config_file, + JSON.stringify(this.config_data, null, 4), + "utf8", + () => {} + ); this.send("set_app_data", { config: this.config_data, @@ -427,6 +489,9 @@ export class Backend { fs.mkdirpSync(log_dir); } + console.log("About to init the logger with log_dir: " + log_dir); + this.initLogger(log_dir); + this.daemon = new Daemon(this); this.walletd = new WalletRPC(this); @@ -605,7 +670,11 @@ export class Backend { // Replace any invalid value with default values validate_values(values, defaults) { - const isDictionary = v => typeof v === "object" && v !== null && !(v instanceof Array) && !(v instanceof Date); + const isDictionary = v => + typeof v === "object" && + v !== null && + !(v instanceof Array) && + !(v instanceof Date); const modified = { ...values }; // Make sure we have valid defaults @@ -616,7 +685,10 @@ export class Backend { if (!(key in defaults)) continue; const defaultValue = defaults[key]; - const invalidDefault = defaultValue === null || defaultValue === undefined || Number.isNaN(defaultValue); + const invalidDefault = + defaultValue === null || + defaultValue === undefined || + Number.isNaN(defaultValue); if (invalidDefault) continue; const value = modified[key]; @@ -626,7 +698,12 @@ export class Backend { modified[key] = this.validate_values(value, defaultValue); } else { // Check if we need to replace the value - const isValidValue = !(value === undefined || value === null || value === "" || Number.isNaN(value)); + const isValidValue = !( + value === undefined || + value === null || + value === "" || + Number.isNaN(value) + ); if (isValidValue) continue; // Otherwise set the default value diff --git a/src-electron/main-process/modules/logger.js b/src-electron/main-process/modules/logger.js new file mode 100644 index 00000000..9f7a6770 --- /dev/null +++ b/src-electron/main-process/modules/logger.js @@ -0,0 +1,24 @@ +// let path = require("upath"); + +// import { wallet_dir, config_dir } from "./lokiPath"; +// let config_file = path.join(config_dir, "gui", "config.json"); + +// console.log("here's the config file: "); +// console.log(config_file); + +// let logFile = path.join(wallet_dir, "electron.log"); + +// // console.log("config dir in logger: " + config_dir); +// // console.log("wallet dir in logger: " + wallet_dir); + +// const log = bunyan.createLogger({ +// name: "log", +// streams: [ +// { +// level: "debug", +// path: logFile +// } +// ] +// }); + +// export { log }; diff --git a/src-electron/main-process/modules/wallet-rpc.js b/src-electron/main-process/modules/wallet-rpc.js index a180d915..07d4b48b 100644 --- a/src-electron/main-process/modules/wallet-rpc.js +++ b/src-electron/main-process/modules/wallet-rpc.js @@ -2581,7 +2581,6 @@ export class WalletRPC { if (timeout > 0) { options.timeout = timeout; } - return this.queue.add(() => { return request(options) .then(response => { diff --git a/src/components/language_select.vue b/src/components/language_select.vue index e962b9bc..0749e347 100644 --- a/src/components/language_select.vue +++ b/src/components/language_select.vue @@ -1,6 +1,8 @@