1 | import { xglobal } from '@polkadot/x-global';
|
2 | import { formatDate } from './format/formatDate.js';
|
3 | import { isBn } from './is/bn.js';
|
4 | import { isBuffer } from './is/buffer.js';
|
5 | import { isFunction } from './is/function.js';
|
6 | import { isObject } from './is/object.js';
|
7 | import { isU8a } from './is/u8a.js';
|
8 | import { u8aToHex } from './u8a/toHex.js';
|
9 | import { u8aToU8a } from './u8a/toU8a.js';
|
10 | import { noop } from './noop.js';
|
11 | const logTo = {
|
12 | debug: 'log',
|
13 | error: 'error',
|
14 | log: 'log',
|
15 | warn: 'warn'
|
16 | };
|
17 | function formatOther(value) {
|
18 | if (value && isObject(value) && value.constructor === Object) {
|
19 | const result = {};
|
20 | for (const [k, v] of Object.entries(value)) {
|
21 | result[k] = loggerFormat(v);
|
22 | }
|
23 | return result;
|
24 | }
|
25 | return value;
|
26 | }
|
27 | export function loggerFormat(value) {
|
28 | if (Array.isArray(value)) {
|
29 | return value.map(loggerFormat);
|
30 | }
|
31 | else if (isBn(value)) {
|
32 | return value.toString();
|
33 | }
|
34 | else if (isU8a(value) || isBuffer(value)) {
|
35 | return u8aToHex(u8aToU8a(value));
|
36 | }
|
37 | return formatOther(value);
|
38 | }
|
39 | function formatWithLength(maxLength) {
|
40 | return (v) => {
|
41 | if (maxLength <= 0) {
|
42 | return v;
|
43 | }
|
44 | const r = `${v}`;
|
45 | return r.length < maxLength
|
46 | ? v
|
47 | : `${r.substring(0, maxLength)} ...`;
|
48 | };
|
49 | }
|
50 | function apply(log, type, values, maxSize = -1) {
|
51 | if (values.length === 1 && isFunction(values[0])) {
|
52 | const fnResult = values[0]();
|
53 | return apply(log, type, Array.isArray(fnResult) ? fnResult : [fnResult], maxSize);
|
54 | }
|
55 | console[logTo[log]](formatDate(new Date()), type, ...values
|
56 | .map(loggerFormat)
|
57 | .map(formatWithLength(maxSize)));
|
58 | }
|
59 | function isDebugOn(e, type) {
|
60 | return !!e && (e === '*' ||
|
61 | type === e ||
|
62 | (e.endsWith('*') &&
|
63 | type.startsWith(e.slice(0, -1))));
|
64 | }
|
65 | function isDebugOff(e, type) {
|
66 | return !!e && (e.startsWith('-') &&
|
67 | (type === e.slice(1) ||
|
68 | (e.endsWith('*') &&
|
69 | type.startsWith(e.slice(1, -1)))));
|
70 | }
|
71 | function getDebugFlag(env, type) {
|
72 | let flag = false;
|
73 | for (const e of env) {
|
74 | if (isDebugOn(e, type)) {
|
75 | flag = true;
|
76 | }
|
77 | else if (isDebugOff(e, type)) {
|
78 | flag = false;
|
79 | }
|
80 | }
|
81 | return flag;
|
82 | }
|
83 | function parseEnv(type) {
|
84 | const maxSize = parseInt(xglobal.process?.env?.['DEBUG_MAX'] || '-1', 10);
|
85 | return [
|
86 | getDebugFlag((xglobal.process?.env?.['DEBUG'] || '').toLowerCase().split(','), type),
|
87 | isNaN(maxSize)
|
88 | ? -1
|
89 | : maxSize
|
90 | ];
|
91 | }
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 | export function logger(origin) {
|
107 | const type = `${origin.toUpperCase()}:`.padStart(16);
|
108 | const [isDebug, maxSize] = parseEnv(origin.toLowerCase());
|
109 | return {
|
110 | debug: isDebug
|
111 | ? (...values) => apply('debug', type, values, maxSize)
|
112 | : noop,
|
113 | error: (...values) => apply('error', type, values),
|
114 | log: (...values) => apply('log', type, values),
|
115 | noop,
|
116 | warn: (...values) => apply('warn', type, values)
|
117 | };
|
118 | }
|