From 188fcc8e4caf2b03b506e4705459bc1fa87af227 Mon Sep 17 00:00:00 2001 From: shmyga Date: Mon, 26 Oct 2026 22:22:16 +0300 Subject: [PATCH] refactor(log): refactor log and output --- src/common/debug.ts | 42 ------------------ src/common/exec.ts | 4 +- src/common/log.ts | 91 ++++++++++++++++++++++++-------------- src/common/output.ts | 55 +++++++++++++++++++++++ src/core/sdk.ts | 6 +-- src/project/core/runner.ts | 4 +- 6 files changed, 119 insertions(+), 83 deletions(-) delete mode 100755 src/common/debug.ts create mode 100644 src/common/output.ts diff --git a/src/common/debug.ts b/src/common/debug.ts deleted file mode 100755 index bfcb8ef..0000000 --- a/src/common/debug.ts +++ /dev/null @@ -1,42 +0,0 @@ -import colors from "ansi-colors"; - -const _colors = { - "[DEBUG]": colors.white, - "[INFO]": colors.cyan, - "[ERROR]": colors.red, - "Called from ": colors.red, - "[WARNING]": colors.yellow, -}; - -export type Color = (v: string) => string; - -export const getColor = (line: string): Color | null => { - for (const [tag, color] of Object.entries(_colors)) { - if (line.indexOf(tag) > -1) { - return color; - } - } - return null; // colors.reset; -}; - -export class Debug { - public color: Color; - - constructor() { - this.color = colors.white; - this.log = this.log.bind(this); - } - - log(line: string) { - const newColor = getColor(line); - if (newColor) this.color = newColor; - if (line[0] === "\t" || line.startsWith("Called from ")) { - console.log(this.color(line)); - } else { - const result = line.split(" "); - console.log(colors.gray(result.slice(0, 4).join(" ")) + " " + this.color(result.slice(4).join(" "))); - } - } -} - -export const DEBUG = new Debug(); diff --git a/src/common/exec.ts b/src/common/exec.ts index ecdb0b4..cee9bc8 100755 --- a/src/common/exec.ts +++ b/src/common/exec.ts @@ -1,8 +1,8 @@ import async from "async"; import child_process from "child_process"; -import { Log } from "./log"; +import { Logger } from "./log"; -const log = new Log("exec"); +const log = new Logger("exec"); const queue = async.queue((task: any, done: any) => { log.d("*%s*", task.command); diff --git a/src/common/log.ts b/src/common/log.ts index 0118d83..db94704 100644 --- a/src/common/log.ts +++ b/src/common/log.ts @@ -1,63 +1,86 @@ import colors from "ansi-colors"; -const Level = { - VERBOSE: -1, - DEBUG: 0, - INFO: 1, - WARN: 2, - ERROR: 3, -}; +export enum Level { + VERBOSE = -1, + DEBUG = 0, + INFO = 1, + WARNING = 2, + ERROR = 3, +} -const level = process.argv.indexOf("--verbose") > -1 ? Level.VERBOSE : Level.INFO; +export const DEFAULT_LEVEL: Level = process.argv.indexOf("--verbose") > -1 ? Level.VERBOSE : Level.INFO; -const formatTime = (date: Date): string => date.toTimeString().split(" ")[0]; +export type FormatPattern = [string, colors.StyleFunction]; -export class Log { - static level = level; - static colorPatterns: [string, (m: string) => string][] = [ - ["_", colors.cyan], - ["\\*", colors.magenta], - ["!", colors.red], - ]; +export const DEFAULT_PATTERNS: FormatPattern[] = [ + ["_", colors.cyan], + ["\\*", colors.magenta], + ["!", colors.red], +]; - static colorize(pattern: string) { - for (const item of this.colorPatterns) { +export class Formatter { + constructor( + private patterns: FormatPattern[] = DEFAULT_PATTERNS, + private time: boolean = true, + ) {} + + colorize(text: string): string { + for (const item of this.patterns) { const r = new RegExp(item[0] + "(.*?)" + item[0], "g"); - pattern = pattern.replace(r, item[1]("$1")); + text = text.replace(r, item[1]("$1")); } - return pattern; + return text; } - static write(level: number, tag: string, pattern: string, args: any[]) { + buildTime(): string { + return new Date().toTimeString().split(" ")[0]; + } + + format(message: string, tag: string, level: Level): string { + const values = []; + if (this.time) { + values.push(`[${colors.gray(this.buildTime())}]`); + } + if (tag) { + values.push(colors.green(tag)); + } + values.push(this.colorize(message)); + return values.join(" "); + } +} + +export const DEFAULT_FORMATTER = new Formatter(); + +export class Logger { + constructor( + private tag: string, + private level: Level = DEFAULT_LEVEL, + private formatter: Formatter = DEFAULT_FORMATTER, + ) {} + + write(level: Level, message: string, args: any[]) { if (level >= this.level) { - const message = `[${colors.gray(formatTime(new Date()))}] ${colors.green(tag)} ${this.colorize(pattern)}`; - console.log(message, ...args); + console.log(this.formatter.format(message, this.tag, level), ...args); } } - public tag: string; - - constructor(tag: string) { - this.tag = tag; - } - v(message: string, ...args: any) { - Log.write(-1, this.tag, message, args); + this.write(Level.VERBOSE, message, args); } d(message: string, ...args: any) { - Log.write(0, this.tag, message, args); + this.write(Level.DEBUG, message, args); } i(message: string, ...args: any) { - Log.write(1, this.tag, message, args); + this.write(Level.INFO, message, args); } w(message: string, ...args: any) { - Log.write(2, this.tag, message, args); + this.write(Level.WARNING, message, args); } e(message: string, ...args: any) { - Log.write(3, this.tag, message, args); + this.write(Level.ERROR, message, args); } } diff --git a/src/common/output.ts b/src/common/output.ts new file mode 100644 index 0000000..096df06 --- /dev/null +++ b/src/common/output.ts @@ -0,0 +1,55 @@ +import colors from "ansi-colors"; +import { Formatter, Level, Logger } from "./log"; + +export class OutputFormatter extends Formatter { + private colorMap: Record = { + [Level.VERBOSE]: colors.white, + [Level.DEBUG]: colors.white, + [Level.INFO]: colors.cyan, + [Level.ERROR]: colors.red, + [Level.WARNING]: colors.yellow, + }; + + format(message: string, tag: string, level: Level): string { + const color = this.colorMap[level]; + if (message[0] === "\t" || message.startsWith("Called from ")) { + return color(message); + } else { + const result = message.split(" "); + return colors.gray(result.slice(0, 4).join(" ")) + " " + color(result.slice(4).join(" ")); + } + } +} + +export const DEFAULT_OUTPUT_FORMATTER = new OutputFormatter(); + +export class OutputLogger extends Logger { + private levelMap: Record = { + "[DEBUG]": Level.DEBUG, + "[INFO]": Level.INFO, + "[ERROR]": Level.ERROR, + "Called from ": Level.ERROR, + "[WARNING]": Level.WARNING, + }; + + constructor() { + super("", Level.VERBOSE, DEFAULT_OUTPUT_FORMATTER); + } + + detectLevel(line: string): number { + for (const [tag, color] of Object.entries(this.levelMap)) { + if (line.indexOf(tag) > -1) { + return color; + } + } + return Level.DEBUG; + } + + log(line: string) { + this.write(this.detectLevel(line), line, []); + } +} + +export const OUTPUT_LOGGER = new OutputLogger(); + +export default (line: string) => OUTPUT_LOGGER.log(line); diff --git a/src/core/sdk.ts b/src/core/sdk.ts index f222a24..47951f6 100755 --- a/src/core/sdk.ts +++ b/src/core/sdk.ts @@ -1,7 +1,7 @@ import os from "os"; import path from "path"; import { Downloader } from "../common/download"; -import { Log } from "../common/log"; +import { Logger } from "../common/log"; export class Sdk { static _dir: string; @@ -20,12 +20,12 @@ export class Sdk { public name: string; public version: string; - public log: Log; + public log: Logger; public path: string; constructor(name: string, version: string) { this.name = name; - this.log = new Log(name); + this.log = new Logger(name); this.version = version; this.path = Sdk.path(name, version); } diff --git a/src/project/core/runner.ts b/src/project/core/runner.ts index b5c7293..9c615cc 100644 --- a/src/project/core/runner.ts +++ b/src/project/core/runner.ts @@ -1,6 +1,6 @@ import gulp from "gulp"; import rename from "gulp-rename"; -import { DEBUG } from "../../common/debug"; +import output from "../../common/output"; import { tail } from "../../common/tail"; import { Config } from "../../core/config"; import { Target } from "./target"; @@ -18,7 +18,7 @@ export class Runner extends Target { } log(stream: any) { - return stream.pipe(tail(DEBUG.log)).pipe(rename("out.log")).pipe(gulp.dest(this.targetPath)); + return stream.pipe(tail(output)).pipe(rename("out.log")).pipe(gulp.dest(this.targetPath)); } static register(platform: string, runner: RunnerFactory) {