From f96d9ae1ffa71ffaac4050e8cedb1d1aec5b09f3 Mon Sep 17 00:00:00 2001 From: NoahAndrews <10224994+NoahAndrews@users.noreply.github.com.> Date: Tue, 4 Jun 2024 16:20:44 -0500 Subject: [PATCH] Write log messages from the daemon to a file --- src/debug.ts | 58 ++++++++++++++++++++++++++++++++---------- src/service/daemon.mts | 2 ++ 2 files changed, 46 insertions(+), 14 deletions(-) diff --git a/src/debug.ts b/src/debug.ts index e748457..e14728a 100644 --- a/src/debug.ts +++ b/src/debug.ts @@ -1,29 +1,59 @@ import { format } from 'util' +import { createWriteStream } from 'node:fs' +import { mkdirSync } from 'fs' + +type LogWriter = (logEntry: string) => void +let isClient = true +let logWriter: LogWriter = console.error + +const useColor = () => process.stderr.isTTY && isClient const yellow = (s: string) => - process.stderr.isTTY ? `\x1b[43;30m${s}\x1b[m` : s -const blue = (s: string) => - process.stderr.isTTY ? `\x1b[44;37m${s}\x1b[m` : s + useColor() ? `\x1b[43;30m${s}\x1b[m` : s +const blue = (s: string) => (useColor() ? `\x1b[44;37m${s}\x1b[m` : s) const red = (s: string) => - process.stderr.isTTY ? `\x1b[41;30;1m${s}\x1b[m` : s + useColor() ? `\x1b[41;30;1m${s}\x1b[m` : s const magenta = (s: string) => - process.stderr.isTTY ? `\x1b[45;30m${s}\x1b[m` : s + useColor() ? `\x1b[45;30m${s}\x1b[m` : s const green = (s: string) => - process.stderr.isTTY ? `\x1b[42;30;2m${s}\x1b[m` : s + useColor() ? `\x1b[42;30;2m${s}\x1b[m` : s const level = parseInt(process.env.TSIMP_DEBUG || '0', 10) +type Logger = (...args: any[]) => undefined + const getLogger = - (name: string, color: (s: string) => string) => + (name: string, color: (s: string) => string): Logger => (...args: any[]) => { const prefix = `TSIMP ${color(name)} ${process.pid}: ` const msg = format(...args).trim() - console.error(prefix + msg.split('\n').join(`\n${prefix}`)) + logWriter(prefix + msg.split('\n').join(`\n${prefix}`)) + } + +export let error: Logger +export let warn: Logger +export let debug: Logger +export let info: Logger +export let trace: Logger + +const initializeLoggers = () => { + error = getLogger('error', red) + warn = level > 0 ? getLogger('warn', yellow) : () => {} + debug = level > 1 ? getLogger('debug', magenta) : () => {} + info = level > 2 ? getLogger('info', blue) : () => {} + trace = getLogger('trace', green) +} + +export const markProcessAsDaemon = () => { + if (isClient) { + isClient = false + mkdirSync('.tsimp/daemon', { recursive: true }) + const writeStream = createWriteStream('.tsimp/daemon/log', { + flags: 'a', + }) + logWriter = text => writeStream.write(`${text}\n`) + initializeLoggers() } +} -export const error = getLogger('error', red) -export const warn = level > 0 ? getLogger('warn', yellow) : () => {} -export const debug = - level > 1 ? getLogger('debug', magenta) : () => {} -export const info = level > 2 ? getLogger('info', blue) : () => {} -export const trace = getLogger('trace', green) +initializeLoggers() diff --git a/src/service/daemon.mts b/src/service/daemon.mts index fa9e8a2..28f3a4d 100644 --- a/src/service/daemon.mts +++ b/src/service/daemon.mts @@ -1,4 +1,6 @@ import { DaemonServer } from './service.js' +import { markProcessAsDaemon } from '../debug.js' +markProcessAsDaemon() const cwd = process.cwd() /* c8 ignore start */ const home = (process.env.HOME ?? '').replace(/[\\\/]$/, '')