]> git.codecow.com Git - nano-pow.git/commitdiff
Refactor logger to completely replace methods when debug mode is toggled.
authorChris Duncan <chris@codecow.com>
Sun, 12 Jul 2026 07:50:45 +0000 (00:50 -0700)
committerChris Duncan <chris@codecow.com>
Sun, 12 Jul 2026 07:50:45 +0000 (00:50 -0700)
src/utils/logger.ts

index 0e89283170373b6e3d75304f19c68c9273fa6ba2..632ae294f982edc857e775e46aacada75619d206 100644 (file)
@@ -6,41 +6,56 @@
  * to only output when debug mode is enabled.
  */
 export class Logger {
-       isEnabled: boolean = false
-       groups: Record<string, boolean> = {}
+       #groups: Record<string, boolean> = {}
+       #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
                }
        }
 }