* 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
}
}
}