UNPKG

plaxtony

Version:

Static code analysis of SC2 Galaxy Script

120 lines 4.39 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.logIt = exports.logger = void 0; const winston_1 = require("winston"); const util = require("util"); exports.logger = winston_1.createLogger({ level: process.env['PLAXTONY_LOG_LEVEL'] || 'warn', format: winston_1.format.combine(winston_1.format.timestamp({ alias: 'time', format: 'hh:mm:ss.SSS', }), winston_1.format.ms(), winston_1.format.prettyPrint({ colorize: false, depth: 2 }), winston_1.format.printf(info => { const out = [ `${info.time} ${info.level.substr(0, 3).toUpperCase()} ${info.message}` ]; if (info.durationMs) { out[out.length - 1] += ` ${info.ms}`; } const splat = info[Symbol.for('splat')]; if (Array.isArray(splat)) { const dump = splat.length === 1 ? splat.pop() : splat; out.push(util.inspect(dump, { colors: false, depth: 3, compact: true, maxArrayLength: 500, breakLength: 140, })); } return out.join('\n'); })), transports: [ new winston_1.transports.Console(), ], }); function isPromise(val) { return !!val && typeof val === 'object' && typeof val.then === 'function' && typeof val.catch === 'function'; } function formatMemoryUsage(mem) { function toMegabytes(n) { return (Math.round(n / 1024 / 1024 * 100) / 100).toFixed(1).padEnd(5); } return `${toMegabytes(mem.rss)}M ${toMegabytes(mem.heapTotal)}M ${toMegabytes(mem.heapUsed)}M`; } function logIt(lgOpts = {}) { lgOpts = Object.assign({ level: 'info', profiling: true, profTime: false, profMemory: false, }, lgOpts); return function (target, propertyKey, descriptor) { if (!exports.logger.isLevelEnabled(lgOpts.level)) { return; } const fn = descriptor.value; let msgPrefix; function markDone(fnResult, pTimeSnapshot) { const diff = process.hrtime(pTimeSnapshot); const diffMs = (diff[0] * 1000) + (diff[1] / 1000000); const out = [`-${msgPrefix.padEnd(35)} = ${diffMs.toFixed(0).padStart(4)}ms`]; if (lgOpts.profiling || lgOpts.profMemory) { out.push(` | ${formatMemoryUsage(process.memoryUsage())}`); } let metaArgs = []; if (lgOpts.resDump === true) { metaArgs = [fnResult]; } else if (typeof lgOpts.resDump === 'function') { metaArgs = [lgOpts.resDump(fnResult)]; } else { if (!lgOpts.profiling) { return; } } exports.logger.log(lgOpts.level, out.join(''), ...metaArgs); } const proxyFn = function (...args) { if (!msgPrefix) { msgPrefix = `${this.constructor.name}:${propertyKey}`; } const pTimeSnapshot = process.hrtime(); const out = [` ${msgPrefix.padEnd(35 + 9)}`]; if (lgOpts.message) { out.push(lgOpts.message); } else if (lgOpts.profiling || lgOpts.profMemory) { out.push(` | ${formatMemoryUsage(process.memoryUsage())}`); } let metaArgs = []; if (lgOpts.argsDump === true) { metaArgs = args; } else if (typeof lgOpts.argsDump === 'function') { metaArgs = [lgOpts.argsDump(...args)]; } exports.logger.log(lgOpts.level, out.join(''), ...metaArgs); let fnResult = fn.apply(this, args); if (isPromise(fnResult)) { fnResult = fnResult .then(res => { markDone(res, pTimeSnapshot); return res; }) .catch((err) => { markDone(err, pTimeSnapshot); throw err; }); return fnResult; } else { markDone(fnResult, pTimeSnapshot); return fnResult; } }; descriptor.value = proxyFn; }; } exports.logIt = logIt; //# sourceMappingURL=common.js.map