forked from DonJayamanne/pythonVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.ts
More file actions
165 lines (156 loc) · 5.53 KB
/
logger.ts
File metadata and controls
165 lines (156 loc) · 5.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// tslint:disable:no-console
import { injectable } from 'inversify';
import { skipIfTest } from './helpers';
import { ILogger, LogLevel } from './types';
const PREFIX = 'Python Extension: ';
@injectable()
export class Logger implements ILogger {
// tslint:disable-next-line:no-any
public static error(title: string = '', message: any) {
new Logger().logError(`${title}, ${message}`);
}
// tslint:disable-next-line:no-any
public static warn(title: string = '', message: any = '') {
new Logger().logWarning(`${title}, ${message}`);
}
// tslint:disable-next-line:no-any
public static verbose(title: string = '') {
new Logger().logInformation(title);
}
@skipIfTest(false)
public logError(message: string, ex?: Error) {
if (ex) {
console.error(`${PREFIX}${message}`, ex);
} else {
console.error(`${PREFIX}${message}`);
}
}
@skipIfTest(false)
public logWarning(message: string, ex?: Error) {
if (ex) {
console.warn(`${PREFIX}${message}`, ex);
} else {
console.warn(`${PREFIX}${message}`);
}
}
@skipIfTest(false)
public logInformation(message: string, ex?: Error) {
if (ex) {
console.info(`${PREFIX}${message}`, ex);
} else {
console.info(`${PREFIX}${message}`);
}
}
}
enum LogOptions {
None = 0,
Arguments = 1,
ReturnValue = 2
}
// tslint:disable-next-line:no-any
function argsToLogString(args: any[]): string {
try {
return (args || []).map((item, index) => {
try {
return `Arg ${index + 1}: ${JSON.stringify(item)}`;
} catch {
return `Arg ${index + 1}: UNABLE TO DETERMINE VALUE`;
}
}).join(', ');
} catch {
return '';
}
}
// tslint:disable-next-line:no-any
function returnValueToLogString(returnValue: any): string {
let returnValueMessage = 'Return Value: ';
if (returnValue) {
try {
returnValueMessage += `${JSON.stringify(returnValue)}`;
} catch {
returnValueMessage += 'UNABLE TO DETERMINE VALUE';
}
}
return returnValueMessage;
}
export function traceVerbose(message: string) {
new Logger().logInformation(message);
}
export function traceError(message: string, ex?: Error) {
new Logger().logError(message, ex);
}
export function traceInfo(message: string) {
new Logger().logInformation(message);
}
export namespace traceDecorators {
export function verbose(message: string) {
return trace(message, LogOptions.Arguments | LogOptions.ReturnValue);
}
export function error(message: string, ex?: Error) {
return trace(message, LogOptions.Arguments | LogOptions.ReturnValue, LogLevel.Error);
}
export function info(message: string) {
return trace(message);
}
export function warn(message: string) {
return trace(message, LogOptions.Arguments | LogOptions.ReturnValue, LogLevel.Warning);
}
}
function trace(message: string, options: LogOptions = LogOptions.None, logLevel?: LogLevel) {
// tslint:disable-next-line:no-function-expression no-any
return function (_: Object, __: string, descriptor: TypedPropertyDescriptor<any>) {
const originalMethod = descriptor.value;
// tslint:disable-next-line:no-function-expression no-any
descriptor.value = function (...args: any[]) {
// tslint:disable-next-line:no-any
function writeSuccess(returnValue?: any) {
if (logLevel === LogLevel.Error) {
return;
}
writeToLog(returnValue);
}
function writeError(ex: Error) {
writeToLog(undefined, ex);
}
// tslint:disable-next-line:no-any
function writeToLog(returnValue?: any, ex?: Error) {
const messagesToLog = [message];
if ((options && LogOptions.Arguments) === LogOptions.Arguments) {
messagesToLog.push(argsToLogString(args));
}
if ((options & LogOptions.ReturnValue) === LogOptions.ReturnValue) {
messagesToLog.push(returnValueToLogString(returnValue));
}
if (ex) {
new Logger().logError(messagesToLog.join(', '), ex);
} else {
new Logger().logInformation(messagesToLog.join(', '));
}
}
try {
// tslint:disable-next-line:no-invalid-this no-use-before-declare no-unsafe-any
const result = originalMethod.apply(this, args);
// If method being wrapped returns a promise then wait for it.
// tslint:disable-next-line:no-unsafe-any
if (result && typeof result.then === 'function' && typeof result.catch === 'function') {
// tslint:disable-next-line:prefer-type-cast
(result as Promise<void>)
.then(data => {
writeSuccess(data);
return data;
})
.catch(ex => {
writeError(ex);
});
} else {
writeSuccess(result);
}
return result;
} catch (ex) {
writeError(ex);
throw ex;
}
};
return descriptor;
};
}