/**
 * Class used for a delayed logging.
 * I.e. it is logged into a buffer but not directly printed.
 * Then after a certain section is ready one may output the
 * log. This is done by using a 'key'. If key is in the
 * hardcoded 'keyList' then the whole stored logging is printed.
 * Otherwise nothing is printed.
 */ export class DelayedLog { protected static queue = new Array < DelayedLog > ( ) ; protected static keyList = new Set ( ) ; protected static logDisabled = true ; protected loggedLines = new Array < string > ( ) ; protected tabLevel = 1 ; protected key : any ; param handler public static log ( handler ) { if ( this . logDisabled ) return ; const lastIndex = this . queue . length - 1 ; if ( lastIndex < 0 ) return ; const logObj = this . queue [ lastIndex ] ; let line = handler ( ) ; logObj . loggedLines . push ( line ) ; } param handler public static logIf ( key , handler ) { if ( this . logDisabled ) return ; if ( ! this . keyList . has ( key ) ) return ; const lastIndex = this . queue . length - 1 ; if ( lastIndex < 0 ) return ; const logObj = this . queue [ lastIndex ] ; logObj . key = key ; this . log ( handler ) ; } /**
     * Starts logging.
     * A new DelayedLog object is created and all logging goes here.
     * Has to be ended with endLog.
     */ public static startLog ( ) { if ( this . logDisabled ) return ; const logObj = new DelayedLog ( ) ; this . queue . push ( logObj ) ; } /**
     * Shows the current log if  ifFulfilled is true.
     * ifFulfilled key is in the keyList.
     *
     */ public static stopLog ( ) { if ( this . logDisabled ) return ; const logObj = this . queue . pop ( ) ; if ( ! logObj ) return ; if ( ! logObj . key ) return ; console . log ( this . getNumber ( logObj . key ) + ':' ) ; console . log ( logObj . loggedLines . join ( '\n' ) ) ; console . log ( '\n' ) ; } /**
     * Increase tabulator.
     */ public static pushTab ( ) { if ( this . logDisabled ) return ; const lastIndex = this . queue . length - 1 ; if ( lastIndex < 0 ) return ; const logObj = this . queue [ lastIndex ] ; logObj . tabLevel ++ ; } /**
     * Decrease tabulator.
     */ public static popTab ( ) { if ( this . logDisabled ) return ; const lastIndex = this . queue . length - 1 ; if ( lastIndex < 0 ) return ; const logObj = this . queue [ lastIndex ] ; logObj . tabLevel -- ; } param value public static getNumber ( value ) { let hex = value . toString ( 16 ) ; hex = hex . toUpperCase ( ) . padStart ( 4 , '0' ) ; const s = hex + 'h (' + value . toString ( ) + ')' ; return s ; } } ( DelayedLog as any ) . logDisabled = ( ( DelayedLog as any ) . keyList . size == 0 ) ;