@@ -2,7 +2,21 @@ import logger from '../logger';
2
2
import util from 'util' ;
3
3
4
4
let LoggifiedClasses : { [ key : string ] : boolean } = { } ;
5
- export function LoggifyClass < T extends { new ( ...args : any [ ] ) : { } } > (
5
+
6
+ /**
7
+ * Wraps each method on a class with a function that logs out the
8
+ * class name, method name, and arguments passed to the method
9
+ * @export
10
+ * @param {T } aClass
11
+ * @returns a new class where each method is wrapped
12
+
13
+ * ## Example
14
+ * ```
15
+ * @LoggifyClass
16
+ * export class P2pService extends EventEmitter {
17
+ * ```
18
+ */
19
+ export function LoggifyClass < T extends { new ( ...args : any [ ] ) : { } } > (
6
20
aClass : T
7
21
) {
8
22
return class extends aClass {
@@ -19,23 +33,38 @@ export function LoggifyClass<T extends { new (...args: any[]): {} }>(
19
33
}
20
34
} ;
21
35
}
22
-
36
+ /**
37
+ * Wraps a method on a class with a function that logs out the
38
+ * method name, and arguments passed to the method
39
+ *
40
+ * @export
41
+ * @param {string } className
42
+ * @returns
43
+ */
23
44
export function LoggifyMethod ( className : string ) {
24
- return function (
45
+ return function (
25
46
descriptor : TypedPropertyDescriptor < Function >
26
47
) {
27
48
if ( descriptor . value != undefined ) {
28
49
descriptor . value = LoggifyFunction ( descriptor . value , className ) ;
29
50
}
30
51
} ;
31
52
}
32
-
53
+ /**
54
+ * Wraps a function and logs out a message and the function params
55
+ *
56
+ * @export
57
+ * @param {Function } fn the function to wrap
58
+ * @param {string } [logPrefix] a message to be prefixed to the log
59
+ * @param {* } [bind] what the function's 'this' should bound to
60
+ * @returns a new wrapped function
61
+ */
33
62
export function LoggifyFunction ( fn : Function , logPrefix ?: string , bind ?: any ) {
34
63
let copy = fn ;
35
64
if ( bind ) {
36
65
copy = copy . bind ( bind ) ;
37
66
}
38
- return function ( ...methodargs : any [ ] ) {
67
+ return function ( ...methodargs : any [ ] ) {
39
68
logger . debug ( `${ logPrefix } ::args::${ util . inspect ( methodargs ) } ` ) ;
40
69
let returnVal = copy ( ...methodargs ) ;
41
70
if ( returnVal && < Promise < any > > returnVal . then ) {
@@ -54,7 +83,16 @@ export function LoggifyFunction(fn: Function, logPrefix?: string, bind?: any) {
54
83
return returnVal ;
55
84
} ;
56
85
}
57
-
86
+ /**
87
+ Wraps each method on an object with a function that logs out the
88
+ * method name, and arguments passed to the method
89
+ *
90
+ * @export
91
+ * @param {* } obj The object to wrap
92
+ * @param {string } [logPrefix=''] a message to be prefixed to the logs
93
+ * @param {* } [bind] what the function's 'this' should bound to
94
+ * @returns an object where each method is wrapped
95
+ */
58
96
export function LoggifyObject ( obj : any , logPrefix : string = '' , bind ?: any ) {
59
97
for ( let prop of Object . getOwnPropertyNames ( obj ) ) {
60
98
if ( typeof obj [ prop ] === 'function' ) {
0 commit comments