refactor(@embark/transaction-logger): change log storage and read

Changes the way the logs are stored to straight up be logged as an
array and then reads it as such. It also removes the reverse from
the read and puts it in the UI instead since it's the UI that needs
it reversed.
This commit is contained in:
Jonathan Rainville 2020-01-20 15:10:16 -05:00 committed by Iuri Matias
parent d0d584a934
commit df2aaabfc3
2 changed files with 14 additions and 15 deletions

View File

@ -173,7 +173,7 @@ export const processLogs = {
export const CONTRACT_LOGS = createRequestTypes('CONTRACT_LOGS'); export const CONTRACT_LOGS = createRequestTypes('CONTRACT_LOGS');
export const contractLogs = { export const contractLogs = {
request: () => action(CONTRACT_LOGS[REQUEST]), request: () => action(CONTRACT_LOGS[REQUEST]),
success: (contractLogs) => action(CONTRACT_LOGS[SUCCESS], {contractLogs}), success: (contractLogs) => action(CONTRACT_LOGS[SUCCESS], {contractLogs: contractLogs ? contractLogs.reverse() : []}),
failure: (error) => action(CONTRACT_LOGS[FAILURE], {error, name: 'contractLogs'}) failure: (error) => action(CONTRACT_LOGS[FAILURE], {error, name: 'contractLogs'})
}; };

View File

@ -63,7 +63,9 @@ export default class TransactionLogger {
this.writeLogFile = async.cargo((tasks, callback) => { this.writeLogFile = async.cargo((tasks, callback) => {
let appendThis = ''; let appendThis = '';
tasks.forEach(task => { tasks.forEach(task => {
appendThis += `"${new Date().getTime()}":${JSON.stringify(task, getCircularReplacer())},\n`; // Write each line to a JSON string. The replacer is to avoid circular dependencies
// Add a comma at the end to be able to make an array off of it when reading
appendThis += `${JSON.stringify(task, getCircularReplacer())},\n`;
}); });
this.fs.appendFile(this.logFile, appendThis, (err) => { this.fs.appendFile(this.logFile, appendThis, (err) => {
if (err) { if (err) {
@ -250,8 +252,7 @@ export default class TransactionLogger {
apiRoute, apiRoute,
(ws, _req) => { (ws, _req) => {
this.events.on('contracts:log', (log) => { this.events.on('contracts:log', (log) => {
ws.send(JSON.stringify(log), () => { ws.send(JSON.stringify(log), () => {});
});
}); });
} }
); );
@ -260,21 +261,16 @@ export default class TransactionLogger {
'get', 'get',
apiRoute, apiRoute,
async (req, res) => { async (req, res) => {
res.send(JSON.stringify(await this._getLogs())); res.send(await this._readLogs(true));
} }
); );
} }
async _getLogs() {
const data = await this._readLogs();
return Object.values(data).reverse();
}
_saveLog(log) { _saveLog(log) {
this.writeLogFile.push(log); this.writeLogFile.push(log);
} }
async _readLogs() { async _readLogs(asString = false) {
try { try {
await this.fs.ensureFile(this.logFile); await this.fs.ensureFile(this.logFile);
let data = await this.fs.readFile(this.logFile); let data = await this.fs.readFile(this.logFile);
@ -282,17 +278,20 @@ export default class TransactionLogger {
data = data.toString(); data = data.toString();
if (!data) { if (!data) {
return {}; return asString ? '[]' : [];
} }
// remove last comma and add braces around // remove last comma and add brackets around to make it an array of object logs
data = `{${data.substring(0, data.length - 2)}}`; data = `[${data.substring(0, data.length - 2)}]`;
if (asString) {
return data;
}
return JSON.parse(data); return JSON.parse(data);
} catch (error) { } catch (error) {
this.logger.error('Error reading contract log file', error.message); this.logger.error('Error reading contract log file', error.message);
this.logger.trace(error.trace); this.logger.trace(error.trace);
return {}; return asString ? '[]' : [];
} }
} }
} }