2023-04-08 20:52:47 +01:00
|
|
|
import chalk from "chalk";
|
2023-06-26 09:53:45 +01:00
|
|
|
import { createWriteStream, mkdirSync, existsSync } from "fs";
|
2023-04-08 20:52:47 +01:00
|
|
|
|
|
|
|
console.clear();
|
|
|
|
|
|
|
|
enum LogType {
|
|
|
|
INFO,
|
|
|
|
WARN,
|
|
|
|
ERROR
|
2023-04-09 04:47:23 +01:00
|
|
|
}
|
2023-04-08 20:52:47 +01:00
|
|
|
|
2023-04-17 02:04:05 +01:00
|
|
|
enum LogTag {
|
|
|
|
INFO,
|
|
|
|
CHAT,
|
|
|
|
WARN,
|
|
|
|
ERROR
|
|
|
|
}
|
|
|
|
|
|
|
|
const LogTags = [
|
|
|
|
chalk.bgGreen(chalk.black(" INFO ")),
|
|
|
|
chalk.bgCyan(chalk.black(" CHAT ")),
|
|
|
|
chalk.bgYellow(chalk.black(" WARN ")),
|
|
|
|
chalk.bgRed(" ERRR ")
|
|
|
|
] as const;
|
|
|
|
|
|
|
|
const TagsForFile = [
|
|
|
|
"[INFO]",
|
|
|
|
"[CHAT]",
|
|
|
|
"[WARN]",
|
|
|
|
"[ERRR]"
|
|
|
|
] as const;
|
2023-04-08 20:52:47 +01:00
|
|
|
|
|
|
|
function correctValue(i:number) : string {
|
|
|
|
if (i <= 9) return `0${i}`;
|
|
|
|
else return i.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
function getTime() : string {
|
|
|
|
const time = new Date();
|
2023-04-17 02:04:05 +01:00
|
|
|
return `[${correctValue(time.getHours())}:${correctValue(time.getMinutes())}:${correctValue(time.getSeconds())}]`;
|
2023-04-08 20:52:47 +01:00
|
|
|
}
|
|
|
|
|
2023-04-17 02:04:05 +01:00
|
|
|
function log(tag:LogTag, log:string, logType:LogType = LogType.INFO) : void {
|
|
|
|
const stringTime = getTime(),
|
|
|
|
fileTag = TagsForFile[tag],
|
|
|
|
consoleTag = LogTags[tag];
|
|
|
|
|
|
|
|
Console.QUEUED_FOR_LOG += `${stringTime} ${fileTag} ${log}\n`;
|
2023-04-08 20:52:47 +01:00
|
|
|
switch (logType) {
|
|
|
|
case LogType.INFO:
|
2023-04-17 02:04:05 +01:00
|
|
|
return console.log(`${chalk.green(stringTime)} ${consoleTag} ${log}`);
|
2023-04-08 20:52:47 +01:00
|
|
|
case LogType.WARN:
|
2023-04-17 02:04:05 +01:00
|
|
|
return console.warn(`${chalk.green(stringTime)} ${consoleTag} ${log}`);
|
2023-04-08 20:52:47 +01:00
|
|
|
case LogType.ERROR:
|
2023-04-17 02:04:05 +01:00
|
|
|
return console.error(`${chalk.green(stringTime)} ${consoleTag} ${log}`);
|
2023-04-08 20:52:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-26 09:53:45 +01:00
|
|
|
// TODO: Keep old logs, rename on startup using file header?
|
|
|
|
if (!existsSync("./logs")) {
|
2023-04-17 02:04:05 +01:00
|
|
|
mkdirSync("./logs/");
|
|
|
|
}
|
2023-04-08 20:52:47 +01:00
|
|
|
|
2023-04-17 02:04:05 +01:00
|
|
|
export class Console {
|
|
|
|
public static QUEUED_FOR_LOG:string = "";
|
|
|
|
private static logFileWriteStream = createWriteStream("./logs/latest.log");
|
|
|
|
private static flushTimer:NodeJS.Timer = setInterval(() => {
|
|
|
|
if (Console.QUEUED_FOR_LOG.length !== 0) {
|
|
|
|
const strRef = Console.QUEUED_FOR_LOG;
|
|
|
|
Console.QUEUED_FOR_LOG = "";
|
|
|
|
Console.logFileWriteStream.write(strRef);
|
|
|
|
}
|
|
|
|
}, 1000 * 10);
|
2023-04-08 20:52:47 +01:00
|
|
|
|
|
|
|
public static printInfo(s:string) : void {
|
2023-04-17 02:04:05 +01:00
|
|
|
log(LogTag.INFO, s);
|
2023-04-08 20:52:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static printChat(s:string) : void {
|
2023-04-17 02:04:05 +01:00
|
|
|
log(LogTag.CHAT, s);
|
2023-04-08 20:52:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static printWarn(s:string) : void {
|
2023-04-17 02:04:05 +01:00
|
|
|
log(LogTag.WARN, s, LogType.WARN);
|
2023-04-08 20:52:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public static printError(s:string) : void {
|
2023-04-17 02:04:05 +01:00
|
|
|
log(LogTag.ERROR, s, LogType.ERROR);
|
2023-04-08 20:52:47 +01:00
|
|
|
}
|
|
|
|
}
|