idkanymore

This commit is contained in:
Strix 2022-05-02 19:31:57 +02:00
parent 2ec332dc68
commit de3e95c383
Signed by untrusted user who does not match committer: didier
GPG key ID: 01E71F18AA4398E5
2 changed files with 29 additions and 18 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
node_modules

View file

@ -19,6 +19,7 @@ class Logger {
debug: "\x1b[32m", debug: "\x1b[32m",
info: "\x1b[36m", info: "\x1b[36m",
warn: "\x1b[33m", warn: "\x1b[33m",
"warn;": "\x1b[33m",
error: "\x1b[31m", error: "\x1b[31m",
fatal: "\x1b[35m" fatal: "\x1b[35m"
}; };
@ -28,7 +29,8 @@ class Logger {
this.history = []; this.history = [];
this.historySize = options.historySize || 100; this.historySize = options.historySize || 100;
this.onHistoryFull = options.onHistoryFull || function() { this.onHistoryFull = options.onHistoryFull || function() {
// TODO: implement this.alog("warn;", "History is full, dropping 25 oldest entries.");
this.history = this.history.slice(25);
}; };
} }
@ -39,9 +41,7 @@ class Logger {
* *
*/ */
log (message, ...args) log (message, ...args)
{ {this.alog(this.defaultLevel, message, args);}
this.alog(this.defaultLevel, message, ...args);
}
/** alog /** alog
* *
@ -50,35 +50,45 @@ class Logger {
* @param {any} ...args * @param {any} ...args
* *
*/ */
alog(level, message, ...args) alog(level, message, args = [])
{ {
//replace placeholders with args //replace placeholders with args
let msg = this.template; let msg = this.template;
msg = msg.replace(/\{timestamp\}/g, new Date().toISOString());
msg = msg.replace(/\{level\}/g, level.charAt(0).toUpperCase()); msg = msg.replace(/\{level\}/g, level.charAt(0).toUpperCase());
// [I {timestamp}] {message}
msg = msg.replace(/\{timestamp\}/g, new Date().toISOString());
// [I 1/1/1970 00:00:00] {message}
msg = msg.replace(/\{message\}/g, message); msg = msg.replace(/\{message\}/g, message);
// [I 1/1/1970 00:00:00] spul {} {}
//replace placeholders with args //replace placeholders with args
let argPlaceholder = this.argsPlaceholder; let argPlaceholder = this.argsPlaceholder;
let regex = new RegExp(argPlaceholder, "g"); let matches = (msg.match(new RegExp(argPlaceholder, "g")) || []).length;
let stringArgCount = (regex.exec(msg) || []).length; let argsFilled = 0;
args.forEach((v, i, a) => { args.forEach((v, i, a) => {
if (Buffer.isBuffer(v)) if (Buffer.isBuffer(v))
{
v = conversion.properHex(v).str; v = conversion.properHex(v).str;
}
if (typeof v === 'object') if (typeof v === 'object')
{ v = "\x1b[0m" + JSON.stringify(v, null, 2) + "\x1b[0m";
v = JSON.stringify(v);
}
msg = msg.replace(argPlaceholder, v); msg = msg.replace(argPlaceholder, v);
argsFilled++;
console.log(`${argPlaceholder} -> ${v}`);
}); });
if (stringArgCount < args.length)
msg += ` (${args.length - stringArgCount} args overflow.)`; if (matches !== argsFilled && args.length >= matches)
else if (stringArgCount > args.length) console.log("warn;", "Not all placeholders were replaced.\n" +
msg += ` (${stringArgCount - args.length} args missing.)`; "Placeholders: " + matches + "\n" +
"Args: " + args.length + "\n" +
"Args filled: " + argsFilled + "\n" +
"Message: " + msg);
if (matches < args.length)
msg += ` (${args.length - matches} args overflow.)`;
else if (matches > args.length)
msg += ` (${matches - args.length} args missing.)`;
//replace dictionary keys //replace dictionary keys
if (this.dict) if (this.dict)
@ -96,7 +106,7 @@ class Logger {
message: message, message: message,
args: args args: args
}); });
if (this.history.length >= this.historySize) if (this.history.length >= this.historySize && level !== "warn;")
this.onHistoryFull(); this.onHistoryFull();
} }
} }