logの調整

This commit is contained in:
takejohn 2024-03-30 16:07:08 +09:00
parent 422fddafa4
commit 68b7765d58
3 changed files with 19 additions and 6 deletions

View file

@ -115,7 +115,7 @@ export default class 藍 {
} }
@bindThis @bindThis
public static log(msg: string) { private static log(msg: string) {
log(`[${chalk.magenta('AiOS')}]: ${msg}`); log(`[${chalk.magenta('AiOS')}]: ${msg}`);
} }

View file

@ -17,8 +17,13 @@ type Config = {
memoryDir?: string; memoryDir?: string;
}; };
import chalk from 'chalk';
import uncheckedConfig from '../config.json' assert { type: 'json' }; 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<T> { class Type<T> {
public static readonly string = new Type<string>('string'); public static readonly string = new Type<string>('string');
@ -38,7 +43,7 @@ class Type<T> {
function checkProperty<K extends keyof Config>(config: Object, key: K, type: Type<Config[K]>): config is { [J in K]: Config[K] } { function checkProperty<K extends keyof Config>(config: Object, key: K, type: Type<Config[K]>): config is { [J in K]: Config[K] } {
const result = key in config && type.check(config[key as string]); const result = key in config && type.check(config[key as string]);
if (!result) { if (!result) {
log(`config.json: Property ${key}: ${type.name} required`); warnWithPrefix(`config.json: Property '${key}': ${type.name} required`);
} }
return result; return result;
} }
@ -49,7 +54,7 @@ function checkOptionalProperty<K extends keyof Config>(config: Object, key: K, t
} }
const result = type.check(config[key as string]); const result = type.check(config[key as string]);
if (!result) { 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; return result;
} }
@ -60,7 +65,7 @@ function setProperty<K extends keyof Config>(config: Object, key: K, value: Conf
function validate(config: unknown): Config { function validate(config: unknown): Config {
if (!(config instanceof Object)) { if (!(config instanceof Object)) {
log('config.json: Root object required'); warnWithPrefix('config.json: Root object required');
} else if ( } else if (
checkProperty(config, 'host', Type.string) && checkProperty(config, 'host', Type.string) &&
checkOptionalProperty(config, 'serverName', Type.string) && checkOptionalProperty(config, 'serverName', Type.string) &&

View file

@ -1,9 +1,17 @@
import chalk from 'chalk'; import chalk from 'chalk';
export default function(msg: string) { 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 now = new Date();
const date = `${zeroPad(now.getHours())}:${zeroPad(now.getMinutes())}:${zeroPad(now.getSeconds())}`; 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 { function zeroPad(num: number, length: number = 2): string {