UNPKG

@550w-tools/core

Version:

550w-tools 核心库,暴露若干API供应用层调用

44 lines 1.87 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.APILogger = exports.withTimeLog = void 0; const tslib_1 = require("tslib"); const chalk_1 = tslib_1.__importDefault(require("chalk")); async function withTimeLog(func, name) { var _a; const logName = (_a = name !== null && name !== void 0 ? name : func.name) !== null && _a !== void 0 ? _a : 'Anonymous'; const startTime = Date.now(); console.log(`${logName} start`, startTime); const res = func(); const endTime = Date.now(); console.log(`${logName} end`, endTime); console.log(`${logName} takes`, endTime - startTime, 'ms'); return res; } exports.withTimeLog = withTimeLog; const LOG_PREFIX = '【API CORE】'; class APILogger { constructor(logName, manualStart = false) { this.start = () => { this.startDate = new Date(); this.log('------------------------------------------------------------------------'); this.log('start at', this.startDate.toLocaleString(), this.startDate.valueOf()); }; this.end = () => { this.endDate = new Date(); this.log('end at', this.endDate.toLocaleString(), this.endDate.valueOf()); this.log('takes', this.endDate.valueOf() - this.startDate.valueOf(), 'ms'); this.log('------------------------------------------------------------------------'); }; this.log = (...msg) => { console.log(chalk_1.default.cyan(`${LOG_PREFIX}${this.logName}」`), ...msg); }; this.error = (...msg) => { console.log(chalk_1.default.red(`${LOG_PREFIX}${this.logName}」`, ...msg)); }; this.logName = logName; !manualStart && this.start(); } } exports.APILogger = APILogger; exports.default = APILogger; //# sourceMappingURL=APILogger.js.map