Skip to content

Commit ef12a9f

Browse files
author
Micah Riggan
committed
Doing some doc work on Loggify
1 parent 7728bfc commit ef12a9f

File tree

1 file changed

+44
-6
lines changed

1 file changed

+44
-6
lines changed

packages/bitcore-node/src/decorators/Loggify.ts

+44-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,21 @@ import logger from '../logger';
22
import util from 'util';
33

44
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[]): {} }>(
620
aClass: T
721
) {
822
return class extends aClass {
@@ -19,23 +33,38 @@ export function LoggifyClass<T extends { new (...args: any[]): {} }>(
1933
}
2034
};
2135
}
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+
*/
2344
export function LoggifyMethod(className: string) {
24-
return function(
45+
return function (
2546
descriptor: TypedPropertyDescriptor<Function>
2647
) {
2748
if (descriptor.value != undefined) {
2849
descriptor.value = LoggifyFunction(descriptor.value, className);
2950
}
3051
};
3152
}
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+
*/
3362
export function LoggifyFunction(fn: Function, logPrefix?: string, bind?: any) {
3463
let copy = fn;
3564
if (bind) {
3665
copy = copy.bind(bind);
3766
}
38-
return function(...methodargs: any[]) {
67+
return function (...methodargs: any[]) {
3968
logger.debug(`${logPrefix}::args::${util.inspect(methodargs)}`);
4069
let returnVal = copy(...methodargs);
4170
if (returnVal && <Promise<any>>returnVal.then) {
@@ -54,7 +83,16 @@ export function LoggifyFunction(fn: Function, logPrefix?: string, bind?: any) {
5483
return returnVal;
5584
};
5685
}
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+
*/
5896
export function LoggifyObject(obj: any, logPrefix: string = '', bind?: any) {
5997
for (let prop of Object.getOwnPropertyNames(obj)) {
6098
if (typeof obj[prop] === 'function') {

0 commit comments

Comments
 (0)