Files
gulp-haxetool/src/common/log.ts
T
2026-10-20 00:54:16 +03:00

64 lines
1.4 KiB
TypeScript

import colors from "ansi-colors";
const Level = {
VERBOSE: -1,
DEBUG: 0,
INFO: 1,
WARN: 2,
ERROR: 3,
};
const level = process.argv.indexOf("--verbose") > -1 ? Level.VERBOSE : Level.INFO;
const formatTime = (date: Date): string => date.toTimeString().split(" ")[0];
export class Log {
static level = level;
static colorPatterns: [string, (m: string) => string][] = [
["_", colors.cyan],
["\\*", colors.magenta],
["!", colors.red],
];
static colorize(pattern: string) {
for (const item of this.colorPatterns) {
const r = new RegExp(item[0] + "(.*?)" + item[0], "g");
pattern = pattern.replace(r, item[1]("$1"));
}
return pattern;
}
static write(level: number, tag: string, pattern: string, args: any[]) {
if (level >= this.level) {
const message = `[${colors.gray(formatTime(new Date()))}] ${colors.green(tag)} ${this.colorize(pattern)}`;
console.log(message, ...args);
}
}
public tag: string;
constructor(tag: string) {
this.tag = tag;
}
v(message: string, ...args: any) {
Log.write(-1, this.tag, message, args);
}
d(message: string, ...args: any) {
Log.write(0, this.tag, message, args);
}
i(message: string, ...args: any) {
Log.write(1, this.tag, message, args);
}
w(message: string, ...args: any) {
Log.write(2, this.tag, message, args);
}
e(message: string, ...args: any) {
Log.write(3, this.tag, message, args);
}
}