UNPKG

4.03 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.logger = exports.loggerFormat = void 0;
4const x_global_1 = require("@polkadot/x-global");
5const formatDate_js_1 = require("./format/formatDate.js");
6const bn_js_1 = require("./is/bn.js");
7const buffer_js_1 = require("./is/buffer.js");
8const function_js_1 = require("./is/function.js");
9const object_js_1 = require("./is/object.js");
10const u8a_js_1 = require("./is/u8a.js");
11const toHex_js_1 = require("./u8a/toHex.js");
12const toU8a_js_1 = require("./u8a/toU8a.js");
13const has_js_1 = require("./has.js");
14const noop_js_1 = require("./noop.js");
15const logTo = {
16 debug: 'log',
17 error: 'error',
18 log: 'log',
19 warn: 'warn'
20};
21function formatOther(value) {
22 if (value && (0, object_js_1.isObject)(value) && value.constructor === Object) {
23 const result = {};
24 for (const [k, v] of Object.entries(value)) {
25 result[k] = loggerFormat(v);
26 }
27 return result;
28 }
29 return value;
30}
31function loggerFormat(value) {
32 if (Array.isArray(value)) {
33 return value.map(loggerFormat);
34 }
35 else if ((0, bn_js_1.isBn)(value)) {
36 return value.toString();
37 }
38 else if ((0, u8a_js_1.isU8a)(value) || (0, buffer_js_1.isBuffer)(value)) {
39 return (0, toHex_js_1.u8aToHex)((0, toU8a_js_1.u8aToU8a)(value));
40 }
41 return formatOther(value);
42}
43exports.loggerFormat = loggerFormat;
44function formatWithLength(maxLength) {
45 return (v) => {
46 if (maxLength <= 0) {
47 return v;
48 }
49 const r = `${v}`;
50 return r.length < maxLength
51 ? v
52 : `${r.substring(0, maxLength)} ...`;
53 };
54}
55function apply(log, type, values, maxSize = -1) {
56 if (values.length === 1 && (0, function_js_1.isFunction)(values[0])) {
57 const fnResult = values[0]();
58 return apply(log, type, Array.isArray(fnResult) ? fnResult : [fnResult], maxSize);
59 }
60 console[logTo[log]]((0, formatDate_js_1.formatDate)(new Date()), type, ...values
61 .map(loggerFormat)
62 .map(formatWithLength(maxSize)));
63}
64function isDebugOn(e, type) {
65 return !!e && (e === '*' ||
66 type === e ||
67 (e.endsWith('*') &&
68 type.startsWith(e.slice(0, -1))));
69}
70function isDebugOff(e, type) {
71 return !!e && (e.startsWith('-') &&
72 (type === e.slice(1) ||
73 (e.endsWith('*') &&
74 type.startsWith(e.slice(1, -1)))));
75}
76function getDebugFlag(env, type) {
77 let flag = false;
78 for (const e of env) {
79 if (isDebugOn(e, type)) {
80 flag = true;
81 }
82 else if (isDebugOff(e, type)) {
83 flag = false;
84 }
85 }
86 return flag;
87}
88function parseEnv(type) {
89 const env = (has_js_1.hasProcess ? x_global_1.xglobal.process : {}).env || {};
90 const maxSize = parseInt(env['DEBUG_MAX'] || '-1', 10);
91 return [
92 getDebugFlag((env['DEBUG'] || '').toLowerCase().split(','), type),
93 isNaN(maxSize)
94 ? -1
95 : maxSize
96 ];
97}
98/**
99 * @name Logger
100 * @summary Creates a consistent log interface for messages
101 * @description
102 * Returns a `Logger` that has `.log`, `.error`, `.warn` and `.debug` (controlled with environment `DEBUG=typeA,typeB`) methods. Logging is done with a consistent prefix (type of logger, date) followed by the actual message using the underlying console.
103 * @example
104 * <BR>
105 *
106 * ```javascript
107 * import { logger } from '@polkadot/util';
108 *
109 * const l = logger('test');
110 * ```
111 */
112function logger(origin) {
113 const type = `${origin.toUpperCase()}:`.padStart(16);
114 const [isDebug, maxSize] = parseEnv(origin.toLowerCase());
115 return {
116 debug: isDebug
117 ? (...values) => apply('debug', type, values, maxSize)
118 : noop_js_1.noop,
119 error: (...values) => apply('error', type, values),
120 log: (...values) => apply('log', type, values),
121 noop: noop_js_1.noop,
122 warn: (...values) => apply('warn', type, values)
123 };
124}
125exports.logger = logger;