import { OutputChannel } from 'vscode' ; import { LogLevel } from './enums' ; import { isEnum } from './utils' ; export type log = { ( message , ... optionalParams ) : void ; ( level , message ? , ... optionalParams ) : void ; } ; export class Logger { private readonly _channel ? : OutputChannel ; constructor ( outputChannel ? , thisArgs ? ) { this . _channel = outputChannel ; this . log = this . log . bind ( thisArgs || this ) ; } public log ( message , ... optionalParams ) ; public log ( levelOrMessage , message ? , ... optionalParams ) { const captains = console ; let level ; if ( isEnum ( levelOrMessage , LogLevel ) ) { level = levelOrMessage ; } else { level = LogLevel . Information ; message = levelOrMessage ; } const getTime = ( ) : { hours : string ; minutes : string ; seconds : string ; } => { const date = new Date ( ) ; const hours = date . getHours ( ) ; const minutes = date . getMinutes ( ) ; const seconds = date . getSeconds ( ) ; const prefix = ( value ) : string => { return value < 10 ? `0${ value }` : `${ value }` ; } ; return { hours : prefix ( hours ) , minutes : prefix ( minutes ) , seconds : prefix ( seconds ) } ; } ; const { hours , minutes , seconds } = getTime ( ) ; const log = `[${ hours }:${ minutes }:${ seconds }] ${ message }` ; captains [ level ] ( log , ... optionalParams ) ; if ( this . _channel && level !== LogLevel . Debug ) { this . _channel . appendLine ( log ) ; } } }