@onekeyfe/hd-shared 0.1.14 → 0.1.18

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.
@@ -22,6 +22,8 @@ export declare const HardwareErrorCode: {
22
22
  readonly DeviceInitializeFailed: 106;
23
23
  readonly DeviceInterruptedFromOutside: 107;
24
24
  readonly DeviceUnexpectedBootloaderMode: 108;
25
+ readonly DeviceInterruptedFromUser: 109;
26
+ readonly DeviceCheckDeviceIdError: 110;
25
27
  readonly NotInitialized: 200;
26
28
  readonly IFrameNotInitialized: 300;
27
29
  readonly IFrameAleradyInitialized: 301;
@@ -55,6 +57,7 @@ export declare const HardwareErrorCode: {
55
57
  readonly ResponseUnexpectTypeError: 805;
56
58
  readonly BridgeNetworkError: 806;
57
59
  readonly BridgeTimeoutError: 807;
60
+ readonly BridgeNotInstalled: 808;
58
61
  };
59
62
  export declare const HardwareErrorCodeMessage: HardwareErrorCodeMessageMapping;
60
63
  export declare const TypedError: (hardwareError: ErrorCodeUnion | string, message?: string | undefined) => HardwareError;
@@ -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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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"}
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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA2LpB,CAAC;AAEX,eAAO,MAAM,wBAAwB,EAAE,+BA6E7B,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
@@ -31,6 +31,8 @@ declare const HardwareErrorCode: {
31
31
  readonly DeviceInitializeFailed: 106;
32
32
  readonly DeviceInterruptedFromOutside: 107;
33
33
  readonly DeviceUnexpectedBootloaderMode: 108;
34
+ readonly DeviceInterruptedFromUser: 109;
35
+ readonly DeviceCheckDeviceIdError: 110;
34
36
  readonly NotInitialized: 200;
35
37
  readonly IFrameNotInitialized: 300;
36
38
  readonly IFrameAleradyInitialized: 301;
@@ -64,6 +66,7 @@ declare const HardwareErrorCode: {
64
66
  readonly ResponseUnexpectTypeError: 805;
65
67
  readonly BridgeNetworkError: 806;
66
68
  readonly BridgeTimeoutError: 807;
69
+ readonly BridgeNotInstalled: 808;
67
70
  };
68
71
  declare const HardwareErrorCodeMessage: HardwareErrorCodeMessageMapping;
69
72
  declare const TypedError: (hardwareError: ErrorCodeUnion | string, message?: string | undefined) => HardwareError;
@@ -90,36 +93,4 @@ declare namespace HardwareError$1 {
90
93
  };
91
94
  }
92
95
 
93
- declare type LogMessage = {
94
- level: string;
95
- prefix: string;
96
- message: any[];
97
- timestamp: number;
98
- };
99
- declare type LoggerFn = (...data: any[]) => void;
100
- declare type LoggerMoreParams = (message?: any, ...optionalParams: any[]) => void;
101
- declare type Logger = {
102
- debug: LoggerFn | LoggerMoreParams;
103
- info: LoggerFn | LoggerMoreParams;
104
- warn: LoggerFn | LoggerMoreParams;
105
- error: LoggerFn | LoggerMoreParams;
106
- };
107
- declare class Log {
108
- prefix: string;
109
- enabled: boolean;
110
- messages: LogMessage[];
111
- logger?: Logger;
112
- constructor(prefix: string, enabled: boolean, logger?: Logger);
113
- addMessage(level: string, prefix: string, ...args: any[]): void;
114
- log(...args: any[]): void;
115
- error(...args: any[]): void;
116
- warn(...args: any[]): void;
117
- debug(...args: any[]): void;
118
- }
119
- declare const initLog: (prefix: string, enabled?: boolean | undefined, logger?: Logger | undefined) => Log;
120
- declare const enableLog: (enabled?: boolean | undefined) => void;
121
- declare const setOutsideLogger: (logger: Logger) => void;
122
- declare const enableLogByPrefix: (prefix: string, enabled: boolean) => void;
123
- declare const getLog: () => LogMessage[];
124
-
125
- export { CreateErrorByMessage, Deferred, HardwareError$1 as ERRORS, HardwareError, HardwareErrorCode, HardwareErrorCodeMessage, IHardwareError, TypedError, createDeferred, enableLog, enableLogByPrefix, getLog, initLog, serializeError, setOutsideLogger };
96
+ 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 {
@@ -89,6 +91,8 @@ const HardwareErrorCode = {
89
91
  DeviceInitializeFailed: 106,
90
92
  DeviceInterruptedFromOutside: 107,
91
93
  DeviceUnexpectedBootloaderMode: 108,
94
+ DeviceInterruptedFromUser: 109,
95
+ DeviceCheckDeviceIdError: 110,
92
96
  NotInitialized: 200,
93
97
  IFrameNotInitialized: 300,
94
98
  IFrameAleradyInitialized: 301,
@@ -122,6 +126,7 @@ const HardwareErrorCode = {
122
126
  ResponseUnexpectTypeError: 805,
123
127
  BridgeNetworkError: 806,
124
128
  BridgeTimeoutError: 807,
129
+ BridgeNotInstalled: 808,
125
130
  };
126
131
  const HardwareErrorCodeMessage = {
127
132
  [HardwareErrorCode.UnknownError]: 'Unknown error occurred. Check message property.',
@@ -132,7 +137,9 @@ const HardwareErrorCodeMessage = {
132
137
  [HardwareErrorCode.DeviceNotFound]: 'Device not found',
133
138
  [HardwareErrorCode.DeviceInitializeFailed]: 'Device initialization failed',
134
139
  [HardwareErrorCode.DeviceInterruptedFromOutside]: 'Device interrupted',
140
+ [HardwareErrorCode.DeviceInterruptedFromUser]: 'Device interrupted',
135
141
  [HardwareErrorCode.DeviceUnexpectedBootloaderMode]: 'Device should be in bootloader mode',
142
+ [HardwareErrorCode.DeviceCheckDeviceIdError]: 'Device Id in the features is not same.',
136
143
  [HardwareErrorCode.NotInitialized]: 'Not initialized',
137
144
  [HardwareErrorCode.IFrameNotInitialized]: 'IFrame not initialized',
138
145
  [HardwareErrorCode.IFrameAleradyInitialized]: 'IFrame alerady initialized',
@@ -166,6 +173,7 @@ const HardwareErrorCodeMessage = {
166
173
  [HardwareErrorCode.ResponseUnexpectTypeError]: 'Response type is not expected',
167
174
  [HardwareErrorCode.BridgeNetworkError]: 'Bridge network error',
168
175
  [HardwareErrorCode.BridgeTimeoutError]: 'Bridge network timeout',
176
+ [HardwareErrorCode.BridgeNotInstalled]: 'Bridge not installed',
169
177
  };
170
178
  const TypedError = (hardwareError, message) => {
171
179
  if (typeof hardwareError === 'string') {
@@ -201,106 +209,6 @@ var HardwareError$1 = /*#__PURE__*/Object.freeze({
201
209
  CreateErrorByMessage: CreateErrorByMessage
202
210
  });
203
211
 
204
- const MAX_ENTRIES = 100;
205
- class Log {
206
- constructor(prefix, enabled, logger) {
207
- this.prefix = prefix;
208
- this.enabled = enabled;
209
- this.messages = [];
210
- if (logger) {
211
- this.logger = logger;
212
- }
213
- }
214
- addMessage(level, prefix, ...args) {
215
- this.messages.push({
216
- level,
217
- prefix,
218
- message: args,
219
- timestamp: new Date().getTime(),
220
- });
221
- if (this.messages.length > MAX_ENTRIES) {
222
- this.messages.shift();
223
- }
224
- }
225
- log(...args) {
226
- this.addMessage('log', this.prefix, ...args);
227
- if (!this.enabled) {
228
- return;
229
- }
230
- if (this.logger) {
231
- this.logger.info(this.prefix, ...args);
232
- }
233
- else {
234
- console.log(this.prefix, ...args);
235
- }
236
- }
237
- error(...args) {
238
- this.addMessage('error', this.prefix, ...args);
239
- if (!this.enabled) {
240
- return;
241
- }
242
- if (this.logger) {
243
- this.logger.error(this.prefix, ...args);
244
- }
245
- else {
246
- console.error(this.prefix, ...args);
247
- }
248
- }
249
- warn(...args) {
250
- this.addMessage('warn', this.prefix, ...args);
251
- if (!this.enabled) {
252
- return;
253
- }
254
- if (this.logger) {
255
- this.logger.warn(this.prefix, ...args);
256
- }
257
- else {
258
- console.warn(this.prefix, ...args);
259
- }
260
- }
261
- debug(...args) {
262
- this.addMessage('debug', this.prefix, ...args);
263
- if (!this.enabled) {
264
- return;
265
- }
266
- if (this.logger) {
267
- this.logger.debug(this.prefix, ...args);
268
- }
269
- else {
270
- console.log(this.prefix, ...args);
271
- }
272
- }
273
- }
274
- const _logs = {};
275
- const initLog = (prefix, enabled, logger) => {
276
- const instance = new Log(prefix, !!enabled, logger);
277
- _logs[prefix] = instance;
278
- return instance;
279
- };
280
- const enableLog = (enabled) => {
281
- Object.keys(_logs).forEach(key => {
282
- _logs[key].enabled = !!enabled;
283
- });
284
- };
285
- const setOutsideLogger = (logger) => {
286
- Object.keys(_logs).forEach(key => {
287
- _logs[key].logger = logger;
288
- });
289
- };
290
- const enableLogByPrefix = (prefix, enabled) => {
291
- if (_logs[prefix]) {
292
- _logs[prefix].enabled = enabled;
293
- }
294
- };
295
- const getLog = () => {
296
- let logs = [];
297
- Object.keys(_logs).forEach(key => {
298
- logs = logs.concat(_logs[key].messages);
299
- });
300
- logs.sort((a, b) => a.timestamp - b.timestamp);
301
- return logs;
302
- };
303
-
304
212
  exports.CreateErrorByMessage = CreateErrorByMessage;
305
213
  exports.ERRORS = HardwareError$1;
306
214
  exports.HardwareError = HardwareError;
@@ -308,9 +216,4 @@ exports.HardwareErrorCode = HardwareErrorCode;
308
216
  exports.HardwareErrorCodeMessage = HardwareErrorCodeMessage;
309
217
  exports.TypedError = TypedError;
310
218
  exports.createDeferred = createDeferred;
311
- exports.enableLog = enableLog;
312
- exports.enableLogByPrefix = enableLogByPrefix;
313
- exports.getLog = getLog;
314
- exports.initLog = initLog;
315
219
  exports.serializeError = serializeError;
316
- exports.setOutsideLogger = setOutsideLogger;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onekeyfe/hd-shared",
3
- "version": "0.1.14",
3
+ "version": "0.1.18",
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": "216fee1795067013da0d68f4fc25f583509bb509"
28
+ "gitHead": "eebfd1dd46b146c64a8bfda691dc4d8402fa105b"
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
 
@@ -85,6 +86,13 @@ export const HardwareErrorCode = {
85
86
  */
86
87
  DeviceUnexpectedBootloaderMode: 108,
87
88
 
89
+ /**
90
+ * Device interrupted from user
91
+ */
92
+ DeviceInterruptedFromUser: 109,
93
+
94
+ DeviceCheckDeviceIdError: 110,
95
+
88
96
  /**
89
97
  * Not initialized
90
98
  */
@@ -213,6 +221,11 @@ export const HardwareErrorCode = {
213
221
  * Bridge network timeout
214
222
  */
215
223
  BridgeTimeoutError: 807,
224
+
225
+ /**
226
+ * Bridge not installed
227
+ */
228
+ BridgeNotInstalled: 808,
216
229
  } as const;
217
230
 
218
231
  export const HardwareErrorCodeMessage: HardwareErrorCodeMessageMapping = {
@@ -228,7 +241,9 @@ export const HardwareErrorCodeMessage: HardwareErrorCodeMessageMapping = {
228
241
  [HardwareErrorCode.DeviceNotFound]: 'Device not found',
229
242
  [HardwareErrorCode.DeviceInitializeFailed]: 'Device initialization failed',
230
243
  [HardwareErrorCode.DeviceInterruptedFromOutside]: 'Device interrupted',
244
+ [HardwareErrorCode.DeviceInterruptedFromUser]: 'Device interrupted',
231
245
  [HardwareErrorCode.DeviceUnexpectedBootloaderMode]: 'Device should be in bootloader mode',
246
+ [HardwareErrorCode.DeviceCheckDeviceIdError]: 'Device Id in the features is not same.',
232
247
 
233
248
  /**
234
249
  * Node Errors
@@ -289,6 +304,7 @@ export const HardwareErrorCodeMessage: HardwareErrorCodeMessageMapping = {
289
304
  [HardwareErrorCode.ResponseUnexpectTypeError]: 'Response type is not expected',
290
305
  [HardwareErrorCode.BridgeNetworkError]: 'Bridge network error',
291
306
  [HardwareErrorCode.BridgeTimeoutError]: 'Bridge network timeout',
307
+ [HardwareErrorCode.BridgeNotInstalled]: 'Bridge not installed',
292
308
  } as const;
293
309
 
294
310
  export const TypedError = (hardwareError: ErrorCodeUnion | string, message?: string) => {
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
- };