import * as debug from './debug' ; export enum LogLevel { Error = 'error' , Warn = 'warn' , Info = 'info' , Log = 'log' } export interface ConsoleInterface { error : ( message : string ) => void ; warn : ( message : string ) => void ; info : ( message : string ) => void ; log : ( message : string ) => void ; } export class NullConsole implements ConsoleInterface { logCount = 0 ; infoCount = 0 ; warnCount = 0 ; errorCount = 0 ; log ( message ) { this . logCount ++ ; } info ( message ) { this . infoCount ++ ; } warn ( message ) { this . warnCount ++ ; } error ( message ) { this . errorCount ++ ; } } export class StandardConsole implements ConsoleInterface { log ( message ) { console . info ( message ) ; } info ( message ) { console . info ( message ) ; } warn ( message ) { console . warn ( message ) ; } error ( message ) { console . error ( message ) ; } } export class StandardConsoleWithLevel extends StandardConsole { constructor ( private _maxLevel = LogLevel . Log ) { super ( ) ; } get level ( ) : LogLevel { return this . _maxLevel ; } } export class StderrConsole implements ConsoleInterface { log ( message ) { console . error ( message ) ; } info ( message ) { console . error ( message ) ; } warn ( message ) { console . error ( message ) ; } error ( message ) { console . error ( message ) ; } } export class StderrConsoleWithLevel extends StderrConsole { constructor ( private _maxLevel = LogLevel . Log ) { super ( ) ; } get level ( ) : LogLevel { return this . _maxLevel ; } } export class ConsoleWithLogLevel implements ConsoleInterface { private _levelMap : Map < string , number > = new Map ( [ [ LogLevel . Error , 0 ] , [ LogLevel . Warn , 1 ] , [ LogLevel . Info , 2 ] , [ LogLevel . Log , 3 ] , ] ) ; private _maxLevel = 2 ; constructor ( private _console ) { } get level ( ) : LogLevel { switch ( this . _maxLevel ) { case 0 : return LogLevel . Error ; case 1 : return LogLevel . Warn ; case 2 : return LogLevel . Info ; } return LogLevel . Log ; } set level ( value ) { let maxLevel = this . _levelMap . get ( value ) ; if ( maxLevel === undefined ) { maxLevel = this . _levelMap . get ( LogLevel . Info ) ! ; } this . _maxLevel = maxLevel ; } error ( message ) { this . _log ( LogLevel . Error , message ) ; } warn ( message ) { this . _log ( LogLevel . Warn , message ) ; } info ( message ) { this . _log ( LogLevel . Info , message ) ; } log ( message ) { this . _log ( LogLevel . Log , message ) ; } private _log ( level , message ) { if ( this . _getNumericalLevel ( level ) > this . _maxLevel ) { return ; } log ( this . _console , level , message ) ; } private _getNumericalLevel ( level ) { const numericLevel = this . _levelMap . get ( level ) ; debug . assert ( numericLevel !== undefined , 'Logger: unknown log level.' ) ; return numericLevel !== undefined ? numericLevel : 2 ; } } export function log ( console , logType , msg ) { switch ( logType ) { case LogLevel . Log : console . log ( msg ) ; break ; case LogLevel . Info : console . info ( msg ) ; break ; case LogLevel . Warn : console . warn ( msg ) ; break ; case LogLevel . Error : console . error ( msg ) ; break ; default : debug . fail ( `${ logType } is not expected` ) ; } }