From 68b7765d58877cc13d49616e50b595275a1fdc55 Mon Sep 17 00:00:00 2001 From: takejohn <105504345+takejohn@users.noreply.github.com> Date: Sat, 30 Mar 2024 16:07:08 +0900 Subject: [PATCH] =?UTF-8?q?log=E3=81=AE=E8=AA=BF=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ai.ts | 2 +- src/config.ts | 13 +++++++++---- src/utils/log.ts | 10 +++++++++- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/ai.ts b/src/ai.ts index ad38f6a..5d7476f 100644 --- a/src/ai.ts +++ b/src/ai.ts @@ -115,7 +115,7 @@ export default class 藍 { } @bindThis - public static log(msg: string) { + private static log(msg: string) { log(`[${chalk.magenta('AiOS')}]: ${msg}`); } diff --git a/src/config.ts b/src/config.ts index c307ef2..1f17f5c 100644 --- a/src/config.ts +++ b/src/config.ts @@ -17,8 +17,13 @@ type Config = { memoryDir?: string; }; +import chalk from 'chalk'; import uncheckedConfig from '../config.json' assert { type: 'json' }; -import log from '@/utils/log.js'; +import { warn } from '@/utils/log.js'; + +function warnWithPrefix(msg: string): void { + warn(`[Config]: ${chalk.red(msg)}`); +} class Type { public static readonly string = new Type('string'); @@ -38,7 +43,7 @@ class Type { function checkProperty(config: Object, key: K, type: Type): config is { [J in K]: Config[K] } { const result = key in config && type.check(config[key as string]); if (!result) { - log(`config.json: Property ${key}: ${type.name} required`); + warnWithPrefix(`config.json: Property '${key}': ${type.name} required`); } return result; } @@ -49,7 +54,7 @@ function checkOptionalProperty(config: Object, key: K, t } const result = type.check(config[key as string]); if (!result) { - log(`config.json: The type of property ${key} must be ${type.name}`); + warnWithPrefix(`config.json: The type of property '${key}' must be ${type.name}`); } return result; } @@ -60,7 +65,7 @@ function setProperty(config: Object, key: K, value: Conf function validate(config: unknown): Config { if (!(config instanceof Object)) { - log('config.json: Root object required'); + warnWithPrefix('config.json: Root object required'); } else if ( checkProperty(config, 'host', Type.string) && checkOptionalProperty(config, 'serverName', Type.string) && diff --git a/src/utils/log.ts b/src/utils/log.ts index 8b9eba8..fac5a61 100644 --- a/src/utils/log.ts +++ b/src/utils/log.ts @@ -1,9 +1,17 @@ import chalk from 'chalk'; export default function(msg: string) { + console.log(createMessage(msg)); +} + +export function warn(msg: string) { + console.warn(createMessage(msg)); +} + +function createMessage(msg: string) { const now = new Date(); const date = `${zeroPad(now.getHours())}:${zeroPad(now.getMinutes())}:${zeroPad(now.getSeconds())}`; - console.log(`${chalk.gray(date)} ${msg}`); + return `${chalk.gray(date)} ${msg}`; } function zeroPad(num: number, length: number = 2): string {