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

48
lib/conversion.js Normal file
View file

@ -0,0 +1,48 @@
/** properHex
*
* @param {string} val original value
*
* @returns {{hex: Array<number>, str: string}}
*/
let properHex = (val) => {
let retval = {
hex: [],
str: ""
};
if (Buffer.isBuffer(val))
{
retval.str = val.toString('hex').toUpperCase().replace(/([0-9A-F]{2})/g, '$1 ');
retval.str = "[ " + retval.str.substring(0, retval.str.length - 1) + " ]";
retval.hex = val;
return retval;
}
if (typeof val === 'string')
{
let b = Buffer.from(val);
return properHex(b);
}
}
/** dictionaryConversion
*
* @param {string} origin
* @param {Map} dict
*
* @returns {string}
*
*/
let dictionaryConversion = (origin, dict) => {
let retval = "";
dict.forEach((v, k, m) => {
// if k is found in origin replace it with v
if (origin.indexOf(k) > -1) {
retval = origin.replace(k, v);
}
});
return retval;
}
module.exports = {
properHex,
dictionaryConversion
}

104
lib/logging.js Normal file
View file

@ -0,0 +1,104 @@
const conversion = require("./conversion");
class Logger {
/** Logger
*
* @param {{
* output: NodeJS.WritableStream,
* template: string,
* argsPlaceholder: string,
* historySize: number,
* defaultLevel: string
* }} options
*/
constructor(options = {})
{
this.output = options.output || process.stdout;
this.template = options.template || "[{level} {timestamp}] {message}";
this.levelsColors = { // no same colors
debug: "\x1b[32m",
info: "\x1b[36m",
warn: "\x1b[33m",
error: "\x1b[31m",
fatal: "\x1b[35m"
};
this.dict = options.dict || null;
this.argsPlaceholder = options.argsPlaceholder || "{}";
this.defaultLevel = options.defaultLevel || "info";
this.history = [];
this.historySize = options.historySize || 100;
this.onHistoryFull = options.onHistoryFull || function() {
// TODO: implement
};
}
/** log
*
* @param {string} message
* @param {any} ...args
*
*/
log (message, ...args)
{
this.alog(this.defaultLevel, message, ...args);
}
/** alog
*
* @param {string} level
* @param {string} message
* @param {any} ...args
*
*/
alog(level, message, ...args)
{
//replace placeholders with args
let msg = this.template;
msg = msg.replace(/\{timestamp\}/g, new Date().toISOString());
msg = msg.replace(/\{level\}/g, level.charAt(0).toUpperCase());
msg = msg.replace(/\{message\}/g, message);
//replace placeholders with args
let argPlaceholder = this.argsPlaceholder;
let regex = new RegExp(argPlaceholder, "g");
let stringArgCount = (regex.exec(msg) || []).length;
args.forEach((v, i, a) => {
if (Buffer.isBuffer(v))
{
v = conversion.properHex(v).str;
}
if (typeof v === 'object')
{
v = JSON.stringify(v);
}
msg = msg.replace(argPlaceholder, v);
});
if (stringArgCount < args.length)
msg += ` (${args.length - stringArgCount} args overflow.)`;
else if (stringArgCount > args.length)
msg += ` (${stringArgCount - args.length} args missing.)`;
//replace dictionary keys
if (this.dict)
msg = dictionaryConversion(msg, this.dict);
// add color
msg = this.levelsColors[level] + msg + "\x1b[0m";
//log
this.output.write(msg + "\n");
//save to history
this.history.push({
level: level,
message: message,
args: args
});
if (this.history.length >= this.historySize)
this.onHistoryFull();
}
}
module.exports = Logger;