UNPKG

@naturalcycles/js-lib

Version:

Standard library for universal (browser + Node.js) javascript

89 lines (88 loc) 3.56 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports._LogMethod = _LogMethod; const __1 = require(".."); const time_util_1 = require("../time/time.util"); const decorator_util_1 = require("./decorator.util"); /** * Console-logs when method had started, when it finished, time taken and if error happened. * Supports both sync and async methods. * Awaits if method returns a Promise. * * @example output: * * >> syncMethodSuccess() * << syncMethodSuccess() took 124 ms * * >> asyncMethod() * << asyncMethodThrow() took 10 ms ERROR: MyError */ // eslint-disable-next-line @typescript-eslint/naming-convention function _LogMethod(opt = {}) { return (target, key, descriptor) => { (0, __1._assert)(typeof descriptor.value === 'function', '@_LogMethod can be applied only to methods'); const originalFn = descriptor.value; const keyStr = String(key); const { avg, logArgs = true, logStart, logResult, logResultLength = true, logger = console, } = opt; let { logResultFn } = opt; if (!logResultFn) { if (logResult) { logResultFn = r => ['result:', (0, __1._stringify)(r)]; } else if (logResultLength) { logResultFn = r => (Array.isArray(r) ? [`result: ${r.length} items`] : []); } } const sma = avg ? new __1.SimpleMovingAverage(avg) : undefined; let count = 0; descriptor.value = function (...args) { const started = Date.now(); const ctx = this; // e.g `NameOfYourClass.methodName` // or `NameOfYourClass(instanceId).methodName` const methodSignature = (0, decorator_util_1._getMethodSignature)(ctx, keyStr); const argsStr = (0, decorator_util_1._getArgsSignature)(args, logArgs); const callSignature = `${methodSignature}(${argsStr}) #${++count}`; if (logStart) logger.log(`>> ${callSignature}`); try { const res = originalFn.apply(ctx, args); if (res && typeof res.then === 'function') { // Result is a Promise, will wait for resolution or rejection return res .then((r) => { logFinished(logger, callSignature, started, sma, logResultFn, r); return r; }) .catch((err) => { logFinished(logger, callSignature, started, sma, logResultFn, undefined, err); throw err; }); } // not a Promise logFinished(logger, callSignature, started, sma, logResultFn, res); return res; } catch (err) { logFinished(logger, callSignature, started, sma, logResultFn, undefined, err); throw err; // rethrow } }; return descriptor; }; } // eslint-disable-next-line max-params function logFinished(logger, callSignature, started, sma, logResultFn, res, err) { const millis = Date.now() - started; const t = ['<<', callSignature, 'took', (0, time_util_1._ms)(millis)]; if (sma) { t.push(`(avg ${(0, time_util_1._ms)(sma.pushGetAvg(millis))})`); } if (err !== undefined) { t.push('ERROR:', err); } else if (logResultFn) { t.push(...logResultFn(res)); } logger.log(...t.filter(Boolean)); }