From: Chris Duncan Date: Sun, 12 Jul 2026 07:50:45 +0000 (-0700) Subject: Refactor logger to completely replace methods when debug mode is toggled. X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=2c469a37c4b5d004297010a5fee544a3b39b9c91;p=nano-pow.git Refactor logger to completely replace methods when debug mode is toggled. --- diff --git a/src/utils/logger.ts b/src/utils/logger.ts index 0e89283..632ae29 100644 --- a/src/utils/logger.ts +++ b/src/utils/logger.ts @@ -6,41 +6,56 @@ * to only output when debug mode is enabled. */ export class Logger { - isEnabled: boolean = false - groups: Record = {} + #groups: Record = {} + #isEnabled: boolean = false + #noop (...args: any[]): void { } - groupStart (name: string): void { - if (this.isEnabled) { - console.groupCollapsed(name) - this.groups[name] = true - } + #groupStart (name: string): void { + console.groupCollapsed(name) + this.#groups[name] = true } - groupEnd (name: string): void { - if (this.groups[name]) { + #groupEnd (name: string): void { + if (this.#groups[name]) { console.groupEnd() - delete this.groups[name] + delete this.#groups[name] } } - log (...args: any[]): void { - if (this.isEnabled) { - const datetime = new Date(Date.now()).toLocaleString(Intl.DateTimeFormat().resolvedOptions().locale ?? 'en-US', { hour12: false, dateStyle: 'medium', timeStyle: 'medium' }) - for (let i = 0; i < args.length; i++) { - if (typeof args[i] === 'string') { - args[i] = args[i].replace(datetime, '').trimStart() - } - if (args[i] instanceof Error) { - if ('stack' in args[i]) { - args.splice(i + 1, 0, args[i].stack) - } else if ('message' in args[i]) { - args.splice(i + 1, 0, args[i].message) - } + #log (...args: any[]): void { + const datetime = new Date(Date.now()).toLocaleString(Intl.DateTimeFormat().resolvedOptions().locale ?? 'en-US', { hour12: false, dateStyle: 'medium', timeStyle: 'medium' }) + for (let i = 0; i < args.length; i++) { + if (typeof args[i] === 'string') { + args[i] = args[i].replace(datetime, '').trimStart() + } + if (args[i] instanceof Error) { + if ('stack' in args[i]) { + args.splice(i + 1, 0, args[i].stack) + } else if ('message' in args[i]) { + args.splice(i + 1, 0, args[i].message) } } - const entry = `${datetime} ${globalThis.process?.title ?? 'NanoPow'}[${globalThis.process?.pid ?? 'browser'}]:` - console.log(entry, ...args) - globalThis.process?.send?.({ type: 'console', data: `${entry} ${args}` }) + } + const entry = `${datetime} ${globalThis.process?.title ?? 'NanoPow'}[${globalThis.process?.pid ?? 'browser'}]:` + console.log(entry, ...args) + globalThis.process?.send?.({ type: 'console', data: `${entry} ${args}` }) + } + + groupStart = this.#noop + groupEnd = this.#noop + log = this.#noop + + set isEnabled (v: boolean) { + if (this.#isEnabled === v) return + this.#isEnabled = v + if (v) { + this.groupStart = this.#groupStart + this.groupEnd = this.#groupEnd + this.log = this.#log + } else { + this.groupStart = this.#noop + this.groupEnd = this.#noop + this.log = this.#noop } } }