Skip to content

Commit

Permalink
refactor(@embark/transaction-logger): change log storage and read
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jrainville committed Jan 21, 2020
1 parent e0ac539 commit 8892813
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
2 changes: 1 addition & 1 deletion packages/cockpit/ui/src/actions/index.js
Expand Up @@ -173,7 +173,7 @@ export const processLogs = {
export const CONTRACT_LOGS = createRequestTypes('CONTRACT_LOGS');
export const contractLogs = {
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'})
};

Expand Down
27 changes: 13 additions & 14 deletions packages/plugins/transaction-logger/src/index.js
Expand Up @@ -63,7 +63,9 @@ export default class TransactionLogger {
this.writeLogFile = async.cargo((tasks, callback) => {
let appendThis = '';
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) => {
if (err) {
Expand Down Expand Up @@ -250,8 +252,7 @@ export default class TransactionLogger {
apiRoute,
(ws, _req) => {
this.events.on('contracts:log', (log) => {
ws.send(JSON.stringify(log), () => {
});
ws.send(JSON.stringify(log), () => {});
});
}
);
Expand All @@ -260,39 +261,37 @@ export default class TransactionLogger {
'get',
apiRoute,
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) {
this.writeLogFile.push(log);
}

async _readLogs() {
async _readLogs(asString = false) {
try {
await this.fs.ensureFile(this.logFile);
let data = await this.fs.readFile(this.logFile);

data = data.toString();

if (!data) {
return {};
return asString ? '[]' : [];
}

// remove last comma and add braces around
data = `{${data.substring(0, data.length - 2)}}`;
// remove last comma and add brackets around to make it an array of object logs
data = `[${data.substring(0, data.length - 2)}]`;
if (asString) {
return data;
}

return JSON.parse(data);
} catch (error) {
this.logger.error('Error reading contract log file', error.message);
this.logger.trace(error.trace);
return {};
return asString ? '[]' : [];
}
}
}

0 comments on commit 8892813

Please sign in to comment.