import fs = require ( 'fs' ) ; import os = require ( 'os' ) ; import path = require ( 'path' ) ; import utils = require ( './utils' ) ; import jsonrpc = require ( 'vscode-jsonrpc' ) ; import { NotificationManager } from "atom" ; export enum LogLevel { Verbose , Normal , Warning , Error } export class Logger { private logFilePath : string ; public logBasePath : string ; public logSessionPath : string ; public MinimumLogLevel : LogLevel = LogLevel . Normal ; constructor ( ) { this . logBasePath = path . resolve ( __dirname , "../logs" ) ; utils . ensurePathExists ( this . logBasePath ) ; } public getLogFilePath ( baseName ) { return path . resolve ( this . logSessionPath , `${ baseName }.log` ) ; } public writeAtLevel ( logLevel , message , ... additionalMessages ) { if ( logLevel >= this . MinimumLogLevel ) { this . writeLine ( message , logLevel ) ; additionalMessages . forEach ( ( line ) => { this . writeLine ( line , logLevel ) ; } ) ; } } public write ( message , ... additionalMessages ) { this . writeAtLevel ( LogLevel . Normal , message , ... additionalMessages ) ; } public writeVerbose ( message , ... additionalMessages ) { this . writeAtLevel ( LogLevel . Verbose , message , ... additionalMessages ) ; } public writeWarning ( message , ... additionalMessages ) { this . writeAtLevel ( LogLevel . Warning , message , ... additionalMessages ) ; } public writeAndShowWarning ( message , ... additionalMessages ) { this . writeWarning ( message , ... additionalMessages ) ; atom . notifications . addWarning ( message ) ; } public writeError ( message , ... additionalMessages ) { this . writeAtLevel ( LogLevel . Error , message , ... additionalMessages ) ; } public writeAndShowError ( message , ... additionalMessages ) { this . writeError ( message , ... additionalMessages ) ; atom . notifications . addError ( message ) ; } public startNewLog ( minimumLogLevel = "Normal" ) { this . MinimumLogLevel = this . logLevelNameToValue ( minimumLogLevel . trim ( ) ) ; this . logSessionPath = path . resolve ( this . logBasePath , `${ Math . floor ( Date . now ( ) / 1000 ) }-${ process . pid }` ) ; this . logFilePath = this . getLogFilePath ( "ide-powershell" ) ; utils . ensurePathExists ( this . logSessionPath ) ; } private logLevelNameToValue ( logLevelName ) { switch ( logLevelName . toLowerCase ( ) ) { case "normal" : return LogLevel . Normal ; case "verbose" : return LogLevel . Verbose ; case "warning" : return LogLevel . Warning ; case "error" : return LogLevel . Error ; default : return LogLevel . Normal ; } } public dispose ( ) { } private writeLine ( message , level = LogLevel . Normal ) { let now = new Date ( ) ; let timestampedMessage = `${ now . toLocaleDateString ( ) } ${ now . toLocaleTimeString ( ) } [${ LogLevel [ level ] . toUpperCase ( ) }] - ${ message }` ; console . log ( timestampedMessage ) ; if ( this . logFilePath ) { fs . appendFile ( this . logFilePath , timestampedMessage + os . EOL , err => { if ( err ) { console . log ( `Error writing to vscode-powershell log file: ${ err }` ) ; } } ) ; } } }