// Copyright 2025 Signal Messenger, LLC // SPDX-License-Identifier: AGPL-3.0-only import { parentPort } from 'node:worker_threads'; import { format } from 'node:util'; import type { LoggerType } from '../types/Logging.js'; import type { WrappedWorkerLogEntry, WrappedWorkerResponse } from './main.js'; import { consoleLogger } from '../util/consoleLogger.js'; import { strictAssert } from '../util/assert.js'; class SQLLogger { #msgPrefix: string; constructor(msgPrefix: string) { this.#msgPrefix = msgPrefix; } fatal(...args: Array) { this.#log('fatal', args); } error(...args: Array) { this.#log('error', args); } warn(...args: Array) { this.#log('warn', args); } info(...args: Array) { this.#log('info', args); } debug(...args: Array) { this.#log('debug', args); } trace(...args: Array) { this.#log('trace', args); } child(subsystem: string) { return new SQLLogger(`${this.#msgPrefix}[${subsystem}] `); } #log(level: WrappedWorkerLogEntry['level'], args: Array): void { if (parentPort) { const [fmt, ...rest] = args; const wrappedResponse: WrappedWorkerResponse = { type: 'log', level, args: ([this.#msgPrefix + fmt] as Array).concat(rest), }; parentPort.postMessage(wrappedResponse); } else { strictAssert(process.env.NODE_ENV === 'test', 'must be test environment'); consoleLogger[level](this.#msgPrefix + format(...args)); } } } export const sqlLogger: LoggerType = new SQLLogger('');