11// tslint:disable:no-console
22
33import { injectable } from 'inversify' ;
4+ import { skipIfTest } from './helpers' ;
45import { ILogger } from './types' ;
56
67const PREFIX = 'Python Extension: ' ;
78
89@injectable ( )
910export class Logger implements ILogger {
11+ @skipIfTest ( false )
1012 public logError ( message : string , ex ?: Error ) {
1113 if ( ex ) {
1214 console . error ( `${ PREFIX } ${ message } ` , ex ) ;
1315 } else {
1416 console . error ( `${ PREFIX } ${ message } ` ) ;
1517 }
1618 }
19+ @skipIfTest ( false )
1720 public logWarning ( message : string , ex ?: Error ) {
1821 if ( ex ) {
1922 console . warn ( `${ PREFIX } ${ message } ` , ex ) ;
2023 } else {
2124 console . warn ( `${ PREFIX } ${ message } ` ) ;
2225 }
2326 }
27+ @skipIfTest ( false )
2428 public logInformation ( message : string , ex ?: Error ) {
2529 if ( ex ) {
2630 console . info ( `${ PREFIX } ${ message } ` , ex ) ;
@@ -37,3 +41,93 @@ export function error(title: string = '', message: any) {
3741export function warn ( title : string = '' , message : any ) {
3842 new Logger ( ) . logWarning ( `${ title } , ${ message } ` ) ;
3943}
44+
45+ export enum LogOptions {
46+ Arguments , ReturnValue
47+ }
48+
49+ // tslint:disable-next-line:no-any
50+ function argsToLogString ( args : any [ ] ) : string {
51+ try {
52+ return ( args || [ ] ) . map ( ( item , index ) => {
53+ try {
54+ return `Arg ${ index + 1 } : ${ JSON . stringify ( item ) } ` ;
55+ } catch {
56+ return `Arg ${ index + 1 } : UNABLE TO DETERMINE VALUE` ;
57+ }
58+ } ) . join ( ', ' ) ;
59+ } catch {
60+ return '' ;
61+ }
62+ }
63+
64+ // tslint:disable-next-line:no-any
65+ function returnValueToLogString ( returnValue : any ) : string {
66+ let returnValueMessage = 'Return Value: ' ;
67+ if ( returnValue ) {
68+ try {
69+ returnValueMessage += `${ JSON . stringify ( returnValue ) } ` ;
70+ } catch {
71+ returnValueMessage += 'UNABLE TO DETERMINE VALUE' ;
72+ }
73+ }
74+ return returnValueMessage ;
75+ }
76+
77+ export function log ( message : string , options : LogOptions = LogOptions . Arguments | LogOptions . ReturnValue ) {
78+ // tslint:disable-next-line:no-function-expression no-any
79+ return function ( _ : Object , __ : string , descriptor : TypedPropertyDescriptor < any > ) {
80+ const originalMethod = descriptor . value ;
81+ // tslint:disable-next-line:no-function-expression no-any
82+ descriptor . value = function ( ...args : any [ ] ) {
83+ // tslint:disable-next-line:no-any
84+ function writeSuccess ( returnValue ?: any ) {
85+ writeToLog ( returnValue ) ;
86+ }
87+ function writeError ( ex : Error ) {
88+ writeToLog ( undefined , ex ) ;
89+ }
90+ // tslint:disable-next-line:no-any
91+ function writeToLog ( returnValue ?: any , ex ?: Error ) {
92+ const messagesToLog = [ message ] ;
93+ if ( ( options && LogOptions . Arguments ) === LogOptions . Arguments ) {
94+ messagesToLog . push ( argsToLogString ( args ) ) ;
95+ }
96+ if ( ( options & LogOptions . ReturnValue ) === LogOptions . ReturnValue ) {
97+ messagesToLog . push ( returnValueToLogString ( returnValue ) ) ;
98+ }
99+ if ( ex ) {
100+ new Logger ( ) . logError ( messagesToLog . join ( ', ' ) , ex ) ;
101+ } else {
102+ new Logger ( ) . logInformation ( messagesToLog . join ( ', ' ) ) ;
103+ }
104+ }
105+ try {
106+ // tslint:disable-next-line:no-invalid-this no-use-before-declare no-unsafe-any
107+ const result = originalMethod . apply ( this , args ) ;
108+ // If method being wrapped returns a promise then wait for it.
109+ // tslint:disable-next-line:no-unsafe-any
110+ if ( result && typeof result . then === 'function' && typeof result . catch === 'function' ) {
111+ // tslint:disable-next-line:prefer-type-cast
112+ ( result as Promise < void > )
113+ . then ( data => {
114+ writeSuccess ( data ) ;
115+ return data ;
116+ } )
117+ . catch ( ex => {
118+ writeError ( ex ) ;
119+ return Promise . reject ( ex ) ;
120+ } ) ;
121+ } else {
122+ writeSuccess ( result ) ;
123+ }
124+ return result ;
125+ } catch ( ex ) {
126+ writeError ( ex ) ;
127+ throw ex ;
128+ }
129+ } ;
130+
131+ return descriptor ;
132+ } ;
133+ }
0 commit comments