@@ -63,7 +63,9 @@ export default class TransactionLogger {
6363 this . writeLogFile = async . cargo ( ( tasks , callback ) => {
6464 let appendThis = '' ;
6565 tasks . forEach ( task => {
66- appendThis += `"${ new Date ( ) . getTime ( ) } ":${ JSON . stringify ( task , getCircularReplacer ( ) ) } ,\n` ;
66+ // Write each line to a JSON string. The replacer is to avoid circular dependencies
67+ // Add a comma at the end to be able to make an array off of it when reading
68+ appendThis += `${ JSON . stringify ( task , getCircularReplacer ( ) ) } ,\n` ;
6769 } ) ;
6870 this . fs . appendFile ( this . logFile , appendThis , ( err ) => {
6971 if ( err ) {
@@ -250,8 +252,7 @@ export default class TransactionLogger {
250252 apiRoute ,
251253 ( ws , _req ) => {
252254 this . events . on ( 'contracts:log' , ( log ) => {
253- ws . send ( JSON . stringify ( log ) , ( ) => {
254- } ) ;
255+ ws . send ( JSON . stringify ( log ) , ( ) => { } ) ;
255256 } ) ;
256257 }
257258 ) ;
@@ -260,39 +261,37 @@ export default class TransactionLogger {
260261 'get' ,
261262 apiRoute ,
262263 async ( req , res ) => {
263- res . send ( JSON . stringify ( await this . _getLogs ( ) ) ) ;
264+ res . send ( await this . _readLogs ( true ) ) ;
264265 }
265266 ) ;
266267 }
267268
268- async _getLogs ( ) {
269- const data = await this . _readLogs ( ) ;
270- return Object . values ( data ) . reverse ( ) ;
271- }
272-
273269 _saveLog ( log ) {
274270 this . writeLogFile . push ( log ) ;
275271 }
276272
277- async _readLogs ( ) {
273+ async _readLogs ( asString = false ) {
278274 try {
279275 await this . fs . ensureFile ( this . logFile ) ;
280276 let data = await this . fs . readFile ( this . logFile ) ;
281277
282278 data = data . toString ( ) ;
283279
284280 if ( ! data ) {
285- return { } ;
281+ return asString ? '[]' : [ ] ;
286282 }
287283
288- // remove last comma and add braces around
289- data = `{${ data . substring ( 0 , data . length - 2 ) } }` ;
284+ // remove last comma and add brackets around to make it an array of object logs
285+ data = `[${ data . substring ( 0 , data . length - 2 ) } ]` ;
286+ if ( asString ) {
287+ return data ;
288+ }
290289
291290 return JSON . parse ( data ) ;
292291 } catch ( error ) {
293292 this . logger . error ( 'Error reading contract log file' , error . message ) ;
294293 this . logger . trace ( error . trace ) ;
295- return { } ;
294+ return asString ? '[]' : [ ] ;
296295 }
297296 }
298297}
0 commit comments