UNPKG

@ledgerhq/logs

Version:
150 lines 4.35 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.listen = exports.LocalTracer = exports.trace = exports.log = void 0; let id = 0; const subscribers = []; /** * Logs something * * @param type a namespaced identifier of the log (it is not a level like "debug", "error" but more like "apdu-in", "apdu-out", etc...) * @param message a clear message of the log associated to the type */ const log = (type, message, data) => { const obj = { type, id: String(++id), date: new Date(), }; if (message) obj.message = message; if (data) obj.data = data; dispatch(obj); }; exports.log = log; /** * A simple tracer function, only expanding the existing log function * * Its goal is to capture more context than a log function. * This is simple for now, but can be improved later. * * @param context Anything representing the context where the log occurred */ const trace = ({ type, message, data, context, }) => { const obj = { type, id: String(++id), date: new Date(), }; if (message) obj.message = message; if (data) obj.data = data; if (context) obj.context = context; dispatch(obj); }; exports.trace = trace; /** * A simple tracer class, that can be used to avoid repetition when using the `trace` function * * Its goal is to capture more context than a log function. * This is simple for now, but can be improved later. * * @param type A given type (not level) for the current local tracer ("hw", "withDevice", etc.) * @param context Anything representing the context where the log occurred */ class LocalTracer { type; context; constructor(type, context) { this.type = type; this.context = context; } trace(message, data) { (0, exports.trace)({ type: this.type, message, data, context: this.context, }); } getContext() { return this.context; } setContext(context) { this.context = context; } updateContext(contextToAdd) { this.context = { ...this.context, ...contextToAdd }; } getType() { return this.type; } setType(type) { this.type = type; } /** * Create a new instance of the LocalTracer with an updated `type` * * It does not mutate the calling instance, but returns a new LocalTracer, * following a simple builder pattern. */ withType(type) { return new LocalTracer(type, this.context); } /** * Create a new instance of the LocalTracer with a new `context` * * It does not mutate the calling instance, but returns a new LocalTracer, * following a simple builder pattern. * * @param context A TraceContext, that can undefined to reset the context */ withContext(context) { return new LocalTracer(this.type, context); } /** * Create a new instance of the LocalTracer with an updated `context`, * on which an additional context is merged with the existing one. * * It does not mutate the calling instance, but returns a new LocalTracer, * following a simple builder pattern. */ withUpdatedContext(contextToAdd) { return new LocalTracer(this.type, { ...this.context, ...contextToAdd }); } } exports.LocalTracer = LocalTracer; /** * Adds a subscribers to the emitted logs. * * @param cb that is called for each future log() with the Log object * @return a function that can be called to unsubscribe the listener */ const listen = (cb) => { subscribers.push(cb); return () => { const i = subscribers.indexOf(cb); if (i !== -1) { // equivalent of subscribers.splice(i, 1) // https://twitter.com/Rich_Harris/status/1125850391155965952 subscribers[i] = subscribers[subscribers.length - 1]; subscribers.pop(); } }; }; exports.listen = listen; function dispatch(log) { for (let i = 0; i < subscribers.length; i++) { try { subscribers[i](log); } catch (e) { console.error(e); } } } if (typeof window !== "undefined") { window.__ledgerLogsListen = exports.listen; } //# sourceMappingURL=index.js.map