From b5fe10e36c5555767ece47336fdc7ab520af7502 Mon Sep 17 00:00:00 2001 From: nekosu Date: Thu, 2 Nov 2023 01:18:42 +0800 Subject: [PATCH] feat: allow inspect object in debug console\n\nTODO: add a flag to disable it when not using vscode --- packages/main/utils/log/index.ts | 13 ++++++++++++- packages/main/utils/log/types.ts | 1 + packages/main/utils/logger.ts | 2 +- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/packages/main/utils/log/index.ts b/packages/main/utils/log/index.ts index cc43178c..1a366de2 100644 --- a/packages/main/utils/log/index.ts +++ b/packages/main/utils/log/index.ts @@ -69,6 +69,7 @@ export function createLogger(name: string, output: (env: TLoggerEnv) => void) { '', ...args ).slice(1), + objs: args, }, } output(env) @@ -83,7 +84,9 @@ export function createLogger(name: string, output: (env: TLoggerEnv) => void) { return [logger, ctrl] as const } -export function createPresetFormatter(output: (out: { pretty: string; mono: string }) => void) { +export function createPresetFormatter( + output: (out: { pretty: string; mono: string; cons: [prefix: string, objs: any[]] }) => void +) { return (env: TLoggerEnv) => { const time = `${env.date.year.toString().padStart(4, '0')}-${env.date.month .toString() @@ -106,6 +109,14 @@ export function createPresetFormatter(output: (out: { pretty: string; mono: stri `[${env.name} ${env.source.file} ${env.source.func}]`, env.content.mono, ].join('\t'), + cons: [ + [ + time, + chalk.bold(env.level), + `[${chalk.bold(env.name)} ${env.source.file} ${env.source.func}]`, + ].join('\t'), + env.content.objs, + ], }) } } diff --git a/packages/main/utils/log/types.ts b/packages/main/utils/log/types.ts index 681db060..121a4cbd 100644 --- a/packages/main/utils/log/types.ts +++ b/packages/main/utils/log/types.ts @@ -37,6 +37,7 @@ export interface TLoggerEnv { content: { pretty: string mono: string + objs: any[] } } diff --git a/packages/main/utils/logger.ts b/packages/main/utils/logger.ts index b5e79f3a..7910f636 100644 --- a/packages/main/utils/logger.ts +++ b/packages/main/utils/logger.ts @@ -18,7 +18,7 @@ class Logger { this.log_file_ = createWriteStream(this.log_file_path_, { flags: 'a' }) const formatter = createPresetFormatter(out => { - console.log(out.pretty) + console.log(out.cons[0], ...out.cons[1]) this.log_file_.write(out.mono + '\n') })