@onekeyfe/hd-shared 0.1.13 → 0.1.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -54,9 +54,11 @@ export declare const HardwareErrorCode: {
54
54
  readonly FirmwareError: 804;
55
55
  readonly ResponseUnexpectTypeError: 805;
56
56
  readonly BridgeNetworkError: 806;
57
+ readonly BridgeTimeoutError: 807;
57
58
  };
58
59
  export declare const HardwareErrorCodeMessage: HardwareErrorCodeMessageMapping;
59
60
  export declare const TypedError: (hardwareError: ErrorCodeUnion | string, message?: string | undefined) => HardwareError;
60
61
  export declare const serializeError: (payload: any) => any;
62
+ export declare const CreateErrorByMessage: (message: string) => HardwareError;
61
63
  export {};
62
64
  //# sourceMappingURL=HardwareError.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"HardwareError.d.ts","sourceRoot":"","sources":["../src/HardwareError.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,cAAc;IAC7B,SAAS,EAAE,OAAO,CAAC,OAAO,iBAAiB,CAAC,CAAC;IAC7C,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED,aAAK,OAAO,CAAC,CAAC,SAAS,MAAM,IAAI,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC;AAE5C,aAAK,+BAA+B,GAAG;KAAG,CAAC,IAAI,OAAO,CAAC,OAAO,iBAAiB,CAAC,GAAG,MAAM;CAAE,CAAC;AAE5F,aAAK,cAAc,GAAG,OAAO,CAAC,OAAO,iBAAiB,CAAC,CAAC;AAMxD,qBAAa,aAAc,SAAQ,KAAK;IACtC,SAAS,EAAE,cAAc,CAAkC;IAE3D,OAAO,SAAM;gBAED,aAAa,EAAE,cAAc,GAAG,MAAM;CAkBnD;AAED,eAAO,MAAM,iBAAiB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA0KpB,CAAC;AAEX,eAAO,MAAM,wBAAwB,EAAE,+BAyE7B,CAAC;AAEX,eAAO,MAAM,UAAU,kBAAmB,cAAc,GAAG,MAAM,gDAKhE,CAAC;AAEF,eAAO,MAAM,cAAc,YAAa,GAAG,QAQ1C,CAAC"}
1
+ {"version":3,"file":"HardwareError.d.ts","sourceRoot":"","sources":["../src/HardwareError.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,cAAc;IAC7B,SAAS,EAAE,OAAO,CAAC,OAAO,iBAAiB,CAAC,CAAC;IAC7C,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED,aAAK,OAAO,CAAC,CAAC,SAAS,MAAM,IAAI,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC;AAE5C,aAAK,+BAA+B,GAAG;KAAG,CAAC,IAAI,OAAO,CAAC,OAAO,iBAAiB,CAAC,GAAG,MAAM;CAAE,CAAC;AAE5F,aAAK,cAAc,GAAG,OAAO,CAAC,OAAO,iBAAiB,CAAC,CAAC;AAOxD,qBAAa,aAAc,SAAQ,KAAK;IACtC,SAAS,EAAE,cAAc,CAAkC;IAE3D,OAAO,SAAM;gBAED,aAAa,EAAE,cAAc,GAAG,MAAM;CAkBnD;AAED,eAAO,MAAM,iBAAiB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA+KpB,CAAC;AAEX,eAAO,MAAM,wBAAwB,EAAE,+BA0E7B,CAAC;AAEX,eAAO,MAAM,UAAU,kBAAmB,cAAc,GAAG,MAAM,gDAKhE,CAAC;AAEF,eAAO,MAAM,cAAc,YAAa,GAAG,QAQ1C,CAAC;AAEF,eAAO,MAAM,oBAAoB,YAAa,MAAM,KAAG,aAOtD,CAAC"}
package/dist/index.d.ts CHANGED
@@ -63,10 +63,12 @@ declare const HardwareErrorCode: {
63
63
  readonly FirmwareError: 804;
64
64
  readonly ResponseUnexpectTypeError: 805;
65
65
  readonly BridgeNetworkError: 806;
66
+ readonly BridgeTimeoutError: 807;
66
67
  };
67
68
  declare const HardwareErrorCodeMessage: HardwareErrorCodeMessageMapping;
68
69
  declare const TypedError: (hardwareError: ErrorCodeUnion | string, message?: string | undefined) => HardwareError;
69
70
  declare const serializeError: (payload: any) => any;
71
+ declare const CreateErrorByMessage: (message: string) => HardwareError;
70
72
 
71
73
  type HardwareError$1_IHardwareError = IHardwareError;
72
74
  type HardwareError$1_HardwareError = HardwareError;
@@ -75,6 +77,7 @@ declare const HardwareError$1_HardwareErrorCode: typeof HardwareErrorCode;
75
77
  declare const HardwareError$1_HardwareErrorCodeMessage: typeof HardwareErrorCodeMessage;
76
78
  declare const HardwareError$1_TypedError: typeof TypedError;
77
79
  declare const HardwareError$1_serializeError: typeof serializeError;
80
+ declare const HardwareError$1_CreateErrorByMessage: typeof CreateErrorByMessage;
78
81
  declare namespace HardwareError$1 {
79
82
  export {
80
83
  HardwareError$1_IHardwareError as IHardwareError,
@@ -83,39 +86,8 @@ declare namespace HardwareError$1 {
83
86
  HardwareError$1_HardwareErrorCodeMessage as HardwareErrorCodeMessage,
84
87
  HardwareError$1_TypedError as TypedError,
85
88
  HardwareError$1_serializeError as serializeError,
89
+ HardwareError$1_CreateErrorByMessage as CreateErrorByMessage,
86
90
  };
87
91
  }
88
92
 
89
- declare type LogMessage = {
90
- level: string;
91
- prefix: string;
92
- message: any[];
93
- timestamp: number;
94
- };
95
- declare type LoggerFn = (...data: any[]) => void;
96
- declare type LoggerMoreParams = (message?: any, ...optionalParams: any[]) => void;
97
- declare type Logger = {
98
- debug: LoggerFn | LoggerMoreParams;
99
- info: LoggerFn | LoggerMoreParams;
100
- warn: LoggerFn | LoggerMoreParams;
101
- error: LoggerFn | LoggerMoreParams;
102
- };
103
- declare class Log {
104
- prefix: string;
105
- enabled: boolean;
106
- messages: LogMessage[];
107
- logger?: Logger;
108
- constructor(prefix: string, enabled: boolean, logger?: Logger);
109
- addMessage(level: string, prefix: string, ...args: any[]): void;
110
- log(...args: any[]): void;
111
- error(...args: any[]): void;
112
- warn(...args: any[]): void;
113
- debug(...args: any[]): void;
114
- }
115
- declare const initLog: (prefix: string, enabled?: boolean | undefined, logger?: Logger | undefined) => Log;
116
- declare const enableLog: (enabled?: boolean | undefined) => void;
117
- declare const setOutsideLogger: (logger: Logger) => void;
118
- declare const enableLogByPrefix: (prefix: string, enabled: boolean) => void;
119
- declare const getLog: () => LogMessage[];
120
-
121
- export { Deferred, HardwareError$1 as ERRORS, HardwareError, HardwareErrorCode, HardwareErrorCodeMessage, IHardwareError, TypedError, createDeferred, enableLog, enableLogByPrefix, getLog, initLog, serializeError, setOutsideLogger };
93
+ export { CreateErrorByMessage, Deferred, HardwareError$1 as ERRORS, HardwareError, HardwareErrorCode, HardwareErrorCodeMessage, IHardwareError, TypedError, createDeferred, serializeError };
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,iBAAiB,CAAC;AAChC,cAAc,UAAU,CAAC;AACzB,OAAO,KAAK,MAAM,MAAM,iBAAiB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,YAAY,CAAC;AAC3B,cAAc,iBAAiB,CAAC;AAChC,OAAO,KAAK,MAAM,MAAM,iBAAiB,CAAC"}
package/dist/index.js CHANGED
@@ -55,6 +55,8 @@ function createDeferred(arg, data) {
55
55
  }
56
56
 
57
57
  function fillStringWithArguments(value, object) {
58
+ if (typeof value !== 'string')
59
+ return value;
58
60
  return value.replace(/\{([^}]+)\}/g, (_, arg) => object[arg] || '?');
59
61
  }
60
62
  class HardwareError extends Error {
@@ -121,6 +123,7 @@ const HardwareErrorCode = {
121
123
  FirmwareError: 804,
122
124
  ResponseUnexpectTypeError: 805,
123
125
  BridgeNetworkError: 806,
126
+ BridgeTimeoutError: 807,
124
127
  };
125
128
  const HardwareErrorCodeMessage = {
126
129
  [HardwareErrorCode.UnknownError]: 'Unknown error occurred. Check message property.',
@@ -164,6 +167,7 @@ const HardwareErrorCodeMessage = {
164
167
  [HardwareErrorCode.FirmwareError]: 'Firmware installation failed',
165
168
  [HardwareErrorCode.ResponseUnexpectTypeError]: 'Response type is not expected',
166
169
  [HardwareErrorCode.BridgeNetworkError]: 'Bridge network error',
170
+ [HardwareErrorCode.BridgeTimeoutError]: 'Bridge network timeout',
167
171
  };
168
172
  const TypedError = (hardwareError, message) => {
169
173
  if (typeof hardwareError === 'string') {
@@ -180,6 +184,14 @@ const serializeError = (payload) => {
180
184
  }
181
185
  return payload;
182
186
  };
187
+ const CreateErrorByMessage = (message) => {
188
+ for (const code of Object.values(HardwareErrorCode)) {
189
+ if (HardwareErrorCodeMessage[code] === message) {
190
+ return TypedError(code);
191
+ }
192
+ }
193
+ return new HardwareError(message);
194
+ };
183
195
 
184
196
  var HardwareError$1 = /*#__PURE__*/Object.freeze({
185
197
  __proto__: null,
@@ -187,118 +199,15 @@ var HardwareError$1 = /*#__PURE__*/Object.freeze({
187
199
  HardwareErrorCode: HardwareErrorCode,
188
200
  HardwareErrorCodeMessage: HardwareErrorCodeMessage,
189
201
  TypedError: TypedError,
190
- serializeError: serializeError
202
+ serializeError: serializeError,
203
+ CreateErrorByMessage: CreateErrorByMessage
191
204
  });
192
205
 
193
- const MAX_ENTRIES = 100;
194
- class Log {
195
- constructor(prefix, enabled, logger) {
196
- this.prefix = prefix;
197
- this.enabled = enabled;
198
- this.messages = [];
199
- if (logger) {
200
- this.logger = logger;
201
- }
202
- }
203
- addMessage(level, prefix, ...args) {
204
- this.messages.push({
205
- level,
206
- prefix,
207
- message: args,
208
- timestamp: new Date().getTime(),
209
- });
210
- if (this.messages.length > MAX_ENTRIES) {
211
- this.messages.shift();
212
- }
213
- }
214
- log(...args) {
215
- this.addMessage('log', this.prefix, ...args);
216
- if (!this.enabled) {
217
- return;
218
- }
219
- if (this.logger) {
220
- this.logger.info(this.prefix, ...args);
221
- }
222
- else {
223
- console.log(this.prefix, ...args);
224
- }
225
- }
226
- error(...args) {
227
- this.addMessage('error', this.prefix, ...args);
228
- if (!this.enabled) {
229
- return;
230
- }
231
- if (this.logger) {
232
- this.logger.error(this.prefix, ...args);
233
- }
234
- else {
235
- console.error(this.prefix, ...args);
236
- }
237
- }
238
- warn(...args) {
239
- this.addMessage('warn', this.prefix, ...args);
240
- if (!this.enabled) {
241
- return;
242
- }
243
- if (this.logger) {
244
- this.logger.warn(this.prefix, ...args);
245
- }
246
- else {
247
- console.warn(this.prefix, ...args);
248
- }
249
- }
250
- debug(...args) {
251
- this.addMessage('debug', this.prefix, ...args);
252
- if (!this.enabled) {
253
- return;
254
- }
255
- if (this.logger) {
256
- this.logger.debug(this.prefix, ...args);
257
- }
258
- else {
259
- console.log(this.prefix, ...args);
260
- }
261
- }
262
- }
263
- const _logs = {};
264
- const initLog = (prefix, enabled, logger) => {
265
- const instance = new Log(prefix, !!enabled, logger);
266
- _logs[prefix] = instance;
267
- return instance;
268
- };
269
- const enableLog = (enabled) => {
270
- Object.keys(_logs).forEach(key => {
271
- _logs[key].enabled = !!enabled;
272
- });
273
- };
274
- const setOutsideLogger = (logger) => {
275
- Object.keys(_logs).forEach(key => {
276
- _logs[key].logger = logger;
277
- });
278
- };
279
- const enableLogByPrefix = (prefix, enabled) => {
280
- if (_logs[prefix]) {
281
- _logs[prefix].enabled = enabled;
282
- }
283
- };
284
- const getLog = () => {
285
- let logs = [];
286
- Object.keys(_logs).forEach(key => {
287
- logs = logs.concat(_logs[key].messages);
288
- });
289
- logs.sort((a, b) => a.timestamp - b.timestamp);
290
- return logs;
291
- };
292
-
206
+ exports.CreateErrorByMessage = CreateErrorByMessage;
293
207
  exports.ERRORS = HardwareError$1;
294
208
  exports.HardwareError = HardwareError;
295
209
  exports.HardwareErrorCode = HardwareErrorCode;
296
210
  exports.HardwareErrorCodeMessage = HardwareErrorCodeMessage;
297
211
  exports.TypedError = TypedError;
298
212
  exports.createDeferred = createDeferred;
299
- exports.enableLog = enableLog;
300
- exports.enableLogByPrefix = enableLogByPrefix;
301
- exports.getLog = getLog;
302
- exports.initLog = initLog;
303
213
  exports.serializeError = serializeError;
304
- exports.setOutsideLogger = setOutsideLogger;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onekeyfe/hd-shared",
3
- "version": "0.1.13",
3
+ "version": "0.1.17",
4
4
  "description": "Hardware SDK's shared tool library",
5
5
  "keywords": [
6
6
  "Hardware-SDK",
@@ -25,5 +25,5 @@
25
25
  "lint": "eslint .",
26
26
  "lint:fix": "eslint . --fix"
27
27
  },
28
- "gitHead": "f69fd0696e38efbc838848fb361cb900043bb2ec"
28
+ "gitHead": "65350a66c7f2b1e80cb1d413ee56da4aef507fa3"
29
29
  }
@@ -10,6 +10,7 @@ type HardwareErrorCodeMessageMapping = { [P in ValueOf<typeof HardwareErrorCode>
10
10
  type ErrorCodeUnion = ValueOf<typeof HardwareErrorCode>;
11
11
 
12
12
  function fillStringWithArguments(value: string, object: object) {
13
+ if (typeof value !== 'string') return value;
13
14
  return value.replace(/\{([^}]+)\}/g, (_, arg: string) => (object as unknown as any)[arg] || '?');
14
15
  }
15
16
 
@@ -208,6 +209,11 @@ export const HardwareErrorCode = {
208
209
  * bridge network error
209
210
  */
210
211
  BridgeNetworkError: 806,
212
+
213
+ /**
214
+ * Bridge network timeout
215
+ */
216
+ BridgeTimeoutError: 807,
211
217
  } as const;
212
218
 
213
219
  export const HardwareErrorCodeMessage: HardwareErrorCodeMessageMapping = {
@@ -283,6 +289,7 @@ export const HardwareErrorCodeMessage: HardwareErrorCodeMessageMapping = {
283
289
  [HardwareErrorCode.FirmwareError]: 'Firmware installation failed',
284
290
  [HardwareErrorCode.ResponseUnexpectTypeError]: 'Response type is not expected',
285
291
  [HardwareErrorCode.BridgeNetworkError]: 'Bridge network error',
292
+ [HardwareErrorCode.BridgeTimeoutError]: 'Bridge network timeout',
286
293
  } as const;
287
294
 
288
295
  export const TypedError = (hardwareError: ErrorCodeUnion | string, message?: string) => {
@@ -301,3 +308,12 @@ export const serializeError = (payload: any) => {
301
308
  }
302
309
  return payload;
303
310
  };
311
+
312
+ export const CreateErrorByMessage = (message: string): HardwareError => {
313
+ for (const code of Object.values(HardwareErrorCode)) {
314
+ if (HardwareErrorCodeMessage[code] === message) {
315
+ return TypedError(code);
316
+ }
317
+ }
318
+ return new HardwareError(message);
319
+ };
package/src/index.ts CHANGED
@@ -1,4 +1,3 @@
1
1
  export * from './deferred';
2
2
  export * from './HardwareError';
3
- export * from './logger';
4
3
  export * as ERRORS from './HardwareError';
package/dist/logger.d.ts DELETED
@@ -1,33 +0,0 @@
1
- declare type LogMessage = {
2
- level: string;
3
- prefix: string;
4
- message: any[];
5
- timestamp: number;
6
- };
7
- declare type LoggerFn = (...data: any[]) => void;
8
- declare type LoggerMoreParams = (message?: any, ...optionalParams: any[]) => void;
9
- declare type Logger = {
10
- debug: LoggerFn | LoggerMoreParams;
11
- info: LoggerFn | LoggerMoreParams;
12
- warn: LoggerFn | LoggerMoreParams;
13
- error: LoggerFn | LoggerMoreParams;
14
- };
15
- declare class Log {
16
- prefix: string;
17
- enabled: boolean;
18
- messages: LogMessage[];
19
- logger?: Logger;
20
- constructor(prefix: string, enabled: boolean, logger?: Logger);
21
- addMessage(level: string, prefix: string, ...args: any[]): void;
22
- log(...args: any[]): void;
23
- error(...args: any[]): void;
24
- warn(...args: any[]): void;
25
- debug(...args: any[]): void;
26
- }
27
- export declare const initLog: (prefix: string, enabled?: boolean | undefined, logger?: Logger | undefined) => Log;
28
- export declare const enableLog: (enabled?: boolean | undefined) => void;
29
- export declare const setOutsideLogger: (logger: Logger) => void;
30
- export declare const enableLogByPrefix: (prefix: string, enabled: boolean) => void;
31
- export declare const getLog: () => LogMessage[];
32
- export {};
33
- //# sourceMappingURL=logger.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../src/logger.ts"],"names":[],"mappings":"AAAA,aAAK,UAAU,GAAG;IAChB,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,GAAG,EAAE,CAAC;IACf,SAAS,EAAE,MAAM,CAAC;CACnB,CAAC;AAEF,aAAK,QAAQ,GAAG,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,IAAI,CAAC;AACzC,aAAK,gBAAgB,GAAG,CAAC,OAAO,CAAC,EAAE,GAAG,EAAE,GAAG,cAAc,EAAE,GAAG,EAAE,KAAK,IAAI,CAAC;AAE1E,aAAK,MAAM,GAAG;IACZ,KAAK,EAAE,QAAQ,GAAG,gBAAgB,CAAC;IACnC,IAAI,EAAE,QAAQ,GAAG,gBAAgB,CAAC;IAClC,IAAI,EAAE,QAAQ,GAAG,gBAAgB,CAAC;IAClC,KAAK,EAAE,QAAQ,GAAG,gBAAgB,CAAC;CACpC,CAAC;AAIF,cAAM,GAAG;IACP,MAAM,EAAE,MAAM,CAAC;IAEf,OAAO,EAAE,OAAO,CAAC;IAEjB,QAAQ,EAAE,UAAU,EAAE,CAAC;IAEvB,MAAM,CAAC,EAAE,MAAM,CAAC;gBAEJ,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,CAAC,EAAE,MAAM;IAS7D,UAAU,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE;IAYxD,GAAG,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE;IAYlB,KAAK,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE;IAYpB,IAAI,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE;IAYnB,KAAK,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE;CAWrB;AAID,eAAO,MAAM,OAAO,WAAY,MAAM,oEAIrC,CAAC;AAEF,eAAO,MAAM,SAAS,yCAIrB,CAAC;AAEF,eAAO,MAAM,gBAAgB,WAAY,MAAM,SAI9C,CAAC;AAEF,eAAO,MAAM,iBAAiB,WAAY,MAAM,WAAW,OAAO,SAIjE,CAAC;AAEF,eAAO,MAAM,MAAM,oBAOlB,CAAC"}
package/src/logger.ts DELETED
@@ -1,132 +0,0 @@
1
- type LogMessage = {
2
- level: string;
3
- prefix: string;
4
- message: any[];
5
- timestamp: number;
6
- };
7
-
8
- type LoggerFn = (...data: any[]) => void;
9
- type LoggerMoreParams = (message?: any, ...optionalParams: any[]) => void;
10
-
11
- type Logger = {
12
- debug: LoggerFn | LoggerMoreParams;
13
- info: LoggerFn | LoggerMoreParams;
14
- warn: LoggerFn | LoggerMoreParams;
15
- error: LoggerFn | LoggerMoreParams;
16
- };
17
-
18
- const MAX_ENTRIES = 100;
19
-
20
- class Log {
21
- prefix: string;
22
-
23
- enabled: boolean;
24
-
25
- messages: LogMessage[];
26
-
27
- logger?: Logger;
28
-
29
- constructor(prefix: string, enabled: boolean, logger?: Logger) {
30
- this.prefix = prefix;
31
- this.enabled = enabled;
32
- this.messages = [];
33
- if (logger) {
34
- this.logger = logger;
35
- }
36
- }
37
-
38
- addMessage(level: string, prefix: string, ...args: any[]) {
39
- this.messages.push({
40
- level,
41
- prefix,
42
- message: args,
43
- timestamp: new Date().getTime(),
44
- });
45
- if (this.messages.length > MAX_ENTRIES) {
46
- this.messages.shift();
47
- }
48
- }
49
-
50
- log(...args: any[]) {
51
- this.addMessage('log', this.prefix, ...args);
52
- if (!this.enabled) {
53
- return;
54
- }
55
- if (this.logger) {
56
- this.logger.info(this.prefix, ...args);
57
- } else {
58
- console.log(this.prefix, ...args);
59
- }
60
- }
61
-
62
- error(...args: any[]) {
63
- this.addMessage('error', this.prefix, ...args);
64
- if (!this.enabled) {
65
- return;
66
- }
67
- if (this.logger) {
68
- this.logger.error(this.prefix, ...args);
69
- } else {
70
- console.error(this.prefix, ...args);
71
- }
72
- }
73
-
74
- warn(...args: any[]) {
75
- this.addMessage('warn', this.prefix, ...args);
76
- if (!this.enabled) {
77
- return;
78
- }
79
- if (this.logger) {
80
- this.logger.warn(this.prefix, ...args);
81
- } else {
82
- console.warn(this.prefix, ...args);
83
- }
84
- }
85
-
86
- debug(...args: any[]) {
87
- this.addMessage('debug', this.prefix, ...args);
88
- if (!this.enabled) {
89
- return;
90
- }
91
- if (this.logger) {
92
- this.logger.debug(this.prefix, ...args);
93
- } else {
94
- console.log(this.prefix, ...args);
95
- }
96
- }
97
- }
98
-
99
- const _logs: { [k: string]: Log } = {};
100
-
101
- export const initLog = (prefix: string, enabled?: boolean, logger?: Logger) => {
102
- const instance = new Log(prefix, !!enabled, logger);
103
- _logs[prefix] = instance;
104
- return instance;
105
- };
106
-
107
- export const enableLog = (enabled?: boolean) => {
108
- Object.keys(_logs).forEach(key => {
109
- _logs[key].enabled = !!enabled;
110
- });
111
- };
112
-
113
- export const setOutsideLogger = (logger: Logger) => {
114
- Object.keys(_logs).forEach(key => {
115
- _logs[key].logger = logger;
116
- });
117
- };
118
-
119
- export const enableLogByPrefix = (prefix: string, enabled: boolean) => {
120
- if (_logs[prefix]) {
121
- _logs[prefix].enabled = enabled;
122
- }
123
- };
124
-
125
- export const getLog = () => {
126
- let logs: LogMessage[] = [];
127
- Object.keys(_logs).forEach(key => {
128
- logs = logs.concat(_logs[key].messages);
129
- });
130
- logs.sort((a, b) => a.timestamp - b.timestamp);
131
- return logs;
132
- };