1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.logger = logger;
|
7 | exports.loggerFormat = loggerFormat;
|
8 |
|
9 | var _formatDate = require("./format/formatDate");
|
10 |
|
11 | var _bn = require("./is/bn");
|
12 |
|
13 | var _buffer = require("./is/buffer");
|
14 |
|
15 | var _function = require("./is/function");
|
16 |
|
17 | var _object = require("./is/object");
|
18 |
|
19 | var _u8a = require("./is/u8a");
|
20 |
|
21 | var _toHex = require("./u8a/toHex");
|
22 |
|
23 | var _toU8a = require("./u8a/toU8a");
|
24 |
|
25 | var _has = require("./has");
|
26 |
|
27 |
|
28 |
|
29 | const logTo = {
|
30 | debug: 'log',
|
31 | error: 'error',
|
32 | log: 'log',
|
33 | warn: 'warn'
|
34 | };
|
35 |
|
36 | function formatOther(value) {
|
37 | if (value && (0, _object.isObject)(value) && value.constructor === Object) {
|
38 | const result = {};
|
39 |
|
40 | for (const k of Object.keys(value)) {
|
41 | result[k] = loggerFormat(value[k]);
|
42 | }
|
43 |
|
44 | return result;
|
45 | }
|
46 |
|
47 | return value;
|
48 | }
|
49 |
|
50 | function loggerFormat(value) {
|
51 | if (Array.isArray(value)) {
|
52 | return value.map(loggerFormat);
|
53 | } else if ((0, _bn.isBn)(value)) {
|
54 | return value.toString();
|
55 | } else if ((0, _u8a.isU8a)(value) || (0, _buffer.isBuffer)(value)) {
|
56 | return (0, _toHex.u8aToHex)((0, _toU8a.u8aToU8a)(value));
|
57 | }
|
58 |
|
59 | return formatOther(value);
|
60 | }
|
61 |
|
62 | function formatWithLength(maxLength) {
|
63 | return v => {
|
64 | if (maxLength <= 0) {
|
65 | return v;
|
66 | }
|
67 |
|
68 | const r = `${v}`;
|
69 | return r.length < maxLength ? v : `${r.substring(0, maxLength)} ...`;
|
70 | };
|
71 | }
|
72 |
|
73 | function apply(log, type, values) {
|
74 | let maxSize = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : -1;
|
75 |
|
76 | if (values.length === 1 && (0, _function.isFunction)(values[0])) {
|
77 | const fnResult = values[0]();
|
78 | return apply(log, type, Array.isArray(fnResult) ? fnResult : [fnResult], maxSize);
|
79 | }
|
80 |
|
81 | console[logTo[log]]((0, _formatDate.formatDate)(new Date()), type, ...values.map(loggerFormat).map(formatWithLength(maxSize)));
|
82 | }
|
83 |
|
84 | function noop() {
|
85 | }
|
86 |
|
87 | function isDebugOn(e, type) {
|
88 | return !!e && (e === '*' || type === e || e.endsWith('*') && type.startsWith(e.slice(0, -1)));
|
89 | }
|
90 |
|
91 | function isDebugOff(e, type) {
|
92 | return !!e && e.startsWith('-') && (type === e.slice(1) || e.endsWith('*') && type.startsWith(e.slice(1, -1)));
|
93 | }
|
94 |
|
95 | function getDebugFlag(env, type) {
|
96 | let flag = false;
|
97 |
|
98 | for (const e of env) {
|
99 | if (isDebugOn(e, type)) {
|
100 | flag = true;
|
101 | } else if (isDebugOff(e, type)) {
|
102 | flag = false;
|
103 | }
|
104 | }
|
105 |
|
106 | return flag;
|
107 | }
|
108 |
|
109 | function parseEnv(type) {
|
110 | const env = (_has.hasProcess ? process : {}).env || {};
|
111 | const maxSize = parseInt(env.DEBUG_MAX || '-1', 10);
|
112 | return [getDebugFlag((env.DEBUG || '').toLowerCase().split(','), type), isNaN(maxSize) ? -1 : maxSize];
|
113 | }
|
114 |
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 | function logger(_type) {
|
131 | const type = `${_type.toUpperCase()}:`.padStart(16);
|
132 | const [isDebug, maxSize] = parseEnv(_type.toLowerCase());
|
133 | return {
|
134 | debug: isDebug ? function () {
|
135 | for (var _len = arguments.length, values = new Array(_len), _key = 0; _key < _len; _key++) {
|
136 | values[_key] = arguments[_key];
|
137 | }
|
138 |
|
139 | return apply('debug', type, values, maxSize);
|
140 | } : noop,
|
141 | error: function () {
|
142 | for (var _len2 = arguments.length, values = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
|
143 | values[_key2] = arguments[_key2];
|
144 | }
|
145 |
|
146 | return apply('error', type, values);
|
147 | },
|
148 | log: function () {
|
149 | for (var _len3 = arguments.length, values = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
|
150 | values[_key3] = arguments[_key3];
|
151 | }
|
152 |
|
153 | return apply('log', type, values);
|
154 | },
|
155 | noop,
|
156 | warn: function () {
|
157 | for (var _len4 = arguments.length, values = new Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
|
158 | values[_key4] = arguments[_key4];
|
159 | }
|
160 |
|
161 | return apply('warn', type, values);
|
162 | }
|
163 | };
|
164 | } |
\ | No newline at end of file |