import { LogLevel , Logger } from "./logger" ; export class ConsoleLogger implements Logger { protected _logName : string ; protected _logEmitter : string ; constructor ( logName , emitter ? ) { this . _logName = logName ; let n ; if ( emitter instanceof Function ) { n = emitter . name ; } else if ( typeof emitter === "string" ) { n = emitter ; } else if ( emitter && emitter . constructor instanceof Function ) { n = emitter . constructor . name ; } else { n = "" + ( emitter || "<script>" ) ; } this . _logEmitter = n ; } public todo ( message , ... args ) { if ( LogLevel . bellow ( LogLevel . ALL ) ) return ; message = this . format ( "TODO" , message ) ; console . log ( message , ... args ) ; } public fatal ( message , ... args ) { let err = message ; if ( LogLevel . bellow ( LogLevel . FATAL ) ) return err ; message = this . format ( "FATAL" , message ) ; console . error ( message , ... args ) ; return err ; } public error ( message , ... args ) { let err = message ; if ( LogLevel . bellow ( LogLevel . ERROR ) ) return err ; message = this . format ( "ERROR" , message ) ; console . error ( message , ... args ) ; return err ; } public info ( message , ... args ) { if ( LogLevel . bellow ( LogLevel . INFO ) ) return ; message = this . format ( "INFO" , message ) ; console . info ( message , ... args ) ; } public warn ( message , ... args ) { if ( LogLevel . bellow ( LogLevel . WARN ) ) return ; message = this . format ( "WARN" , message ) ; console . warn ( message , ... args ) ; } public debug ( message , ... args ) { if ( LogLevel . bellow ( LogLevel . DEBUG ) ) return ; message = this . format ( "DEBUG" , message ) ; console . log ( message , ... args ) ; } public trace ( message , ... args ) { if ( LogLevel . bellow ( LogLevel . TRACE ) ) return ; message = this . format ( "TRACE" , message ) ; console . trace ( message , ... args ) ; } public time ( ) { return process . hrtime ( ) ; } public timeEnd ( start , message , ... args ) { let lapse = process . hrtime ( start ) ; let ms = lapse [ 0 ] * 1000 + Math . floor ( lapse [ 1 ] / 1000000 ) ; let ns = Math . floor ( ( lapse [ 1 ] % 1000000 ) / 1000 ) ; message = this . format ( "TIME" , message ) + ": " + ms + "." + ns + "ms" ; console . log ( message , ... args ) ; } private format ( type , message ) { return `${ type } ${ this . prefix } : ${ message }` ; } private get prefix ( ) : string { return `[${ this . _logName }:${ this . _logEmitter }]` ; } }