From 22f1f72897efc4285748cd150a9e244ac6c622d9 Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Tue, 7 Jan 2020 12:08:21 -0500 Subject: [PATCH] refactor(transaction-logger): replace log file write by append ## Problem Doing read, then write each a trasaction or call exectues could get heavy, especially with regular txs on This was especially true in the tests, which led to deactivate the tx logger in the tests ## Solution Instead of reading the whole file, adding the JSON line a writing, we instead just append some pseudo JSON to it that later gets read and parsed correctly back to JSON --- .../plugins/transaction-logger/src/index.js | 28 ++++++++++++------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/packages/plugins/transaction-logger/src/index.js b/packages/plugins/transaction-logger/src/index.js index 42764d0bf..2c1a67567 100644 --- a/packages/plugins/transaction-logger/src/index.js +++ b/packages/plugins/transaction-logger/src/index.js @@ -28,7 +28,7 @@ export default class TransactionLogger { this.contractsConfig = embark.config.contractsConfig; this.contractsDeployed = false; this.outputDone = false; - this.logFile = dappPath(".embark", "contractLogs.json"); + this.logFile = dappPath(".embark", "contractLogs.json.txt"); this.transactions = {}; this._listenForLogRequests(); @@ -48,16 +48,14 @@ export default class TransactionLogger { }); this.writeLogFile = async.cargo((tasks, callback) => { - // TODO change this to only read once then use memory, because it slows things down a lot to read on each TX - const data = this._readLogs(); - + let appendThis = ''; tasks.forEach(task => { - data[new Date().getTime()] = task; + appendThis += `"${new Date().getTime()}":${JSON.stringify(task)},\n`; }); - - this.fs.writeJson(this.logFile, data, err => { + this.fs.appendFile(this.logFile, appendThis, (err) => { if (err) { - console.error(err); + this.logger.error('Error writing to the log file', err.message); + this.logger.trace(err); } callback(); }); @@ -266,8 +264,18 @@ export default class TransactionLogger { _readLogs() { this.fs.ensureFileSync(this.logFile); try { - return JSON.parse(this.fs.readFileSync(this.logFile)); - } catch (_error) { + let data = this.fs.readFileSync(this.logFile).toString(); + + if (!data) { + return {}; + } + + // remove last comma and add braces around + data = `{${data.substring(0, data.length - 2)}}`; + return JSON.parse(data); + } catch (error) { + this.logger.error('Error reading contract log file', error.message); + this.logger.trace(error); return {}; } }