1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.logger = exports.loggerFormat = void 0;
|
4 | const x_global_1 = require("@polkadot/x-global");
|
5 | const formatDate_js_1 = require("./format/formatDate.js");
|
6 | const bn_js_1 = require("./is/bn.js");
|
7 | const buffer_js_1 = require("./is/buffer.js");
|
8 | const function_js_1 = require("./is/function.js");
|
9 | const object_js_1 = require("./is/object.js");
|
10 | const u8a_js_1 = require("./is/u8a.js");
|
11 | const toHex_js_1 = require("./u8a/toHex.js");
|
12 | const toU8a_js_1 = require("./u8a/toU8a.js");
|
13 | const has_js_1 = require("./has.js");
|
14 | const noop_js_1 = require("./noop.js");
|
15 | const logTo = {
|
16 | debug: 'log',
|
17 | error: 'error',
|
18 | log: 'log',
|
19 | warn: 'warn'
|
20 | };
|
21 | function 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 | }
|
31 | function 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 | }
|
43 | exports.loggerFormat = loggerFormat;
|
44 | function 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 | }
|
55 | function 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 | }
|
64 | function isDebugOn(e, type) {
|
65 | return !!e && (e === '*' ||
|
66 | type === e ||
|
67 | (e.endsWith('*') &&
|
68 | type.startsWith(e.slice(0, -1))));
|
69 | }
|
70 | function isDebugOff(e, type) {
|
71 | return !!e && (e.startsWith('-') &&
|
72 | (type === e.slice(1) ||
|
73 | (e.endsWith('*') &&
|
74 | type.startsWith(e.slice(1, -1)))));
|
75 | }
|
76 | function 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 | }
|
88 | function 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 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 | function 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 | }
|
125 | exports.logger = logger;
|