import { configuration } from "./configuration" ; import { OutputChannel , ExtensionContext , commands , window } from "vscode" ; export enum LogColor { black = "\\u001b[30m" , red = "\\u001b[31" , green = "\\u001b[32m" , yellow = "\\u001b[33m" , blue = "\\u001b[34m" , magenta = "\\u001b[35" , cyan = "\\u001b[36m" , white = "\\u001b[37m" } const logValueWhiteSpace = 40 ; let writeToConsole = false ; let writeToConsoleLevel = 2 ; let logOutputChannel ; export function initLog ( settingGrpName , dispName , context ? , showLog ? ) { function showLogOutput ( show ) { if ( logOutputChannel && show ) { logOutputChannel . show ( ) ; } } logOutputChannel = window . createOutputChannel ( dispName ) ; if ( context ) { context . subscriptions . push ( logOutputChannel ) ; context . subscriptions . push ( commands . registerCommand ( settingGrpName + ".showOutput" , showLogOutput ) ) ; } showLogOutput ( showLog || false ) ; } export function isLoggingEnabled ( ) { return configuration . get ( "debug" ) === true ; } export function methodStart ( msg , level ? , logPad = "" , doLogBlank ? , params ? , color ? ) { if ( msg === null || msg === undefined ) { return ; } if ( isLoggingEnabled ( ) ) { const lLevel = level || 1 ; if ( doLogBlank === true ) { blank ( lLevel ) ; } write ( logPad + "*start* " + msg , lLevel , color ) ; if ( params ) { for ( const [ n , v ] of params ) { value ( logPad + "   " + n , v , lLevel + 1 ) ; } } } } export function methodDone ( msg , level ? , logPad = "" , doLogBlank ? , params ? ) { if ( msg === null || msg === undefined ) { return ; } if ( isLoggingEnabled ( ) ) { const lLevel = level || 1 ; if ( doLogBlank === true ) { blank ( lLevel ) ; } if ( params ) { for ( const [ n , v ] of params ) { value ( logPad + "   " + n , v , lLevel + 1 ) ; } } write ( "*done* " + msg , lLevel , logPad , LogColor . cyan ) ; } } export function write ( msg , level ? , logPad = "" , color ? ) { if ( msg === null || msg === undefined ) { return ; } if ( isLoggingEnabled ( ) ) { const tsMsg = new Date ( ) . toISOString ( ) . replace ( /[TZ]/g , " " ) + logPad + msg ; if ( logOutputChannel && ( ! level || level <= ( configuration . get < number > ( "debugLevel" ) || - 1 ) ) ) { logOutputChannel . appendLine ( tsMsg ) ; } if ( writeToConsole ) { if ( ! level || level <= writeToConsoleLevel ) { console . log ( tsMsg ) ; } } } } export function blank ( level ? ) { write ( "" , level ) ; } export function error ( msg ) { if ( msg === null || msg === undefined ) { return ; } if ( isLoggingEnabled ( ) ) { write ( "***" ) ; if ( typeof msg === "string" ) { write ( "*** " + msg ) ; } else { for ( const m of msg ) { write ( "*** " + m ) ; } } write ( "***" ) ; } } export function value ( msg , value , level ? , logPad = "" ) { if ( isLoggingEnabled ( ) ) { let logMsg = msg ; const spaces = msg && msg . length ? msg . length : ( value === undefined ? 9 : 4 ) ; for ( let i = spaces ; i < logValueWhiteSpace ; i ++ ) { logMsg += " " ; } if ( value || value === 0 || value === "" || value === false ) { logMsg += ": " ; logMsg += value . toString ( ) ; } else if ( value === undefined ) { logMsg += ": undefined" ; } else if ( value === null ) { logMsg += ": null" ; } write ( logMsg , level , logPad ) ; } } export function values ( level , logPad , params , doLogBlank ? ) { if ( params === null || params === undefined || params . length === 0 ) { return ; } if ( isLoggingEnabled ( ) ) { if ( doLogBlank === true ) { blank ( level ) ; } for ( const [ n , v ] of params ) { value ( n , v , level , logPad ) ; } } } export function setWriteToConsole ( set , level = 2 ) { writeToConsole = set ; writeToConsoleLevel = level ; }