Skip to content

Commit

Permalink
Upgraded logger utility
Browse files Browse the repository at this point in the history
  • Loading branch information
DaelonSuzuka committed Oct 28, 2023
1 parent 8b16dd4 commit e987d37
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 37 deletions.
93 changes: 58 additions & 35 deletions src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,62 +44,85 @@ export class Logger {
}
}

export class Logger2 {
protected tag: string = "";
protected level: string = "";
protected time: boolean = false;
export enum LOG_LEVEL {
SILENT,
ERROR,
WARNING,
INFO,
DEBUG,
}

constructor(tag: string) {
this.tag = tag;
const LOG_LEVEL_NAMES = [
"SILENT",
"ERROR",
"WARN ",
"INFO ",
"DEBUG",
]

const RESET = "\u001b[0m"

const LOG_COLORS = [
RESET, // SILENT, normal
"\u001b[1;31m", // ERROR, red
"\u001b[1;33m", // WARNING, yellow
"\u001b[1;36m", // INFO, cyan
"\u001b[1;32m", // DEBUG, green
]

export class Logger2 {
private time: boolean;
private label: boolean;

constructor(
private tag: string,
private level: LOG_LEVEL = LOG_LEVEL.DEBUG,
{ time = false, label = false }: { time?: boolean, label?: boolean } = {},
) {
this.time = time;
this.label = label;
}

log(...messages) {
let line = "[godotTools]";
private log(level: LOG_LEVEL, ...messages) {
let prefix = "";
if (this.label) {
prefix += "[godotTools]";
}
if (this.time) {
line += `[${new Date().toISOString()}]`;
prefix += `[${new Date().toISOString()}]`;
}
if (this.level) {
line += `[${this.level}]`;
this.level = "";
if (level) {
prefix += "[" + LOG_COLORS[level] + LOG_LEVEL_NAMES[level] + RESET + "]";
}
if (this.tag) {
line += `[${this.tag}]`;
}
if (line) {
line += " ";
}

for (let index = 0; index < messages.length; index++) {
line += messages[index];
if (index < messages.length) {
line += " ";
} else {
line += "\n";
}
prefix += `[${this.tag}]`;
}

console.log(line);
console.log(prefix, ...messages);
}

info(...messages) {
this.level = "INFO";
this.log(messages);
if (LOG_LEVEL.INFO <= this.level) {
this.log(LOG_LEVEL.INFO, ...messages);
}
}
debug(...messages) {
this.level = "DEBUG";
this.log(messages);
if (LOG_LEVEL.DEBUG <= this.level) {
this.log(LOG_LEVEL.DEBUG, ...messages);
}
}
warn(...messages) {
this.level = "WARNING";
this.log(messages);
if (LOG_LEVEL.WARNING <= this.level) {
this.log(LOG_LEVEL.WARNING, ...messages);
}
}
error(...messages) {
this.level = "ERROR";
this.log(messages);
if (LOG_LEVEL.ERROR <= this.level) {
this.log(LOG_LEVEL.ERROR, ...messages);
}
}
}


export function createLogger(tag) {
return new Logger2(tag);
}
Expand Down
4 changes: 2 additions & 2 deletions src/lsp/GDScriptLanguageClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export default class GDScriptLanguageClient extends LanguageClient {
}

private on_send_message(message: RequestMessage) {
log.debug("tx: " + JSON.stringify(message));
log.debug("tx:", message);

this.sentMessages.set(message.id, message.method);

Expand All @@ -131,7 +131,7 @@ export default class GDScriptLanguageClient extends LanguageClient {

private on_message(message: ResponseMessage) {
const msgString = JSON.stringify(message);
log.debug("rx: " + msgString);
log.debug("rx:", message);

// This is a dirty hack to fix the language server sending us
// invalid file URIs
Expand Down

0 comments on commit e987d37

Please sign in to comment.