-
Notifications
You must be signed in to change notification settings - Fork 38
/
debug.ts
34 lines (30 loc) · 1.3 KB
/
debug.ts
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
import * as Debug from 'debug';
import { performance } from 'perf_hooks';
import * as util from 'util';
type MethodDecorator = (target: any, propertyKey: string, descriptor: PropertyDescriptor) => any;
export function debug(key: string): MethodDecorator {
const logger = Debug(key);
return (target: any, propertyKey: string, descriptor: PropertyDescriptor) => {
const originalMethod = descriptor.value;
if (util.types.isAsyncFunction(originalMethod)) {
descriptor.value = async function(...args: unknown[]): Promise<any> {
logger(`Entering ${propertyKey} with args: ${JSON.stringify(args)}`);
const start = performance.now();
const result = await originalMethod.apply(this, args);
const end = performance.now();
logger(`Exiting ${propertyKey} after ${(end - start).toFixed(2)} milliseconds.`);
return result;
};
} else {
descriptor.value = function(...args: unknown[]) {
logger(`Entering ${propertyKey} with args: ${JSON.stringify(args)}`);
const start = performance.now();
const result = originalMethod.apply(this, args);
const end = performance.now();
logger(`Exiting ${propertyKey} after ${(end - start).toFixed(2)} milliseconds.`);
return result;
};
}
return descriptor;
};
}