Add namespaced logging system, but it isn't actually used yet

This commit is contained in:
Starbeamrainbowlabs 2022-01-09 15:07:33 +00:00
parent 306180d9d8
commit 10cdd5be68
Signed by: sbrl
GPG key ID: 1BE5172E637709C2
3 changed files with 123 additions and 0 deletions

View file

@ -0,0 +1,16 @@
"use strict";
/**
* Hashes a string to a numerical value.
* NOT CRYPTOGRAPHICALLY SECURE!
* @param {string} str The string to hash.
* @return {number} The resulting hashed value as a number.
*/
export default function(str) {
let hash = 0;
for (let i = 0; i < str.length; i++) {
hash = ((hash << 5) - hash) + str.charCodeAt(i);
hash |= 0; // Convert to 32bit integer
}
return hash;
}

71
src/lib/io/Log.mjs Normal file
View file

@ -0,0 +1,71 @@
"use strict";
import a from './Ansi.mjs';
const LOG_LEVELS = {
DEBUG: 0,
INFO: 1,
LOG: 2,
WARN: 4,
ERROR: 8,
NONE: 2048
};
class Log {
constructor() {
this.start = new Date();
this.level = LOG_LEVELS.DEBUG;
}
debug(...message) {
if(this.level > LOG_LEVELS.DEBUG) return;
this.__do_log("debug", ...message);
}
info(...message) {
if(this.level > LOG_LEVELS.INFO) return;
this.__do_log("info", ...message);
}
log(...message) {
if(this.level > LOG_LEVELS.LOG) return;
this.__do_log("log", ...message);
}
warn(...message) {
if(this.level > LOG_LEVELS.WARN) return;
this.__do_log("warn", ...message);
}
error(...message) {
if(this.level > LOG_LEVELS.ERROR) return;
this.__do_log("error", ...message);
}
__do_log(level, ...message) {
message.unshift(`${a.locol}[ ${((new Date() - this.start) / 1000).toFixed(3)}]${a.reset}`);
let part = `[ ${level} ]`;
switch(level) {
case "debug":
part = a.locol + part;
break;
case "warn":
part = a.fyellow + part;
break;
case "error":
part = a.fred + part;
break;
}
message.unshift(part)
console.error(...message);
}
}
// You won't normally need these
export { LOG_LEVELS };
export default new Log();

View file

@ -0,0 +1,36 @@
"use strict";
import a from './Ansi.mjs';
import l from './Log.mjs';
import hash_numeric from '../crypto/hash_numeric.mjs';
class NamespacedLog {
constructor(namespace) {
this.namespace = namespace;
// From https://github.com/debug-js/debug/blob/master/src/node.js#L35
let colours = [
20, 21, 26, 27, 32, 33, 38, 39, 40, 41, 42, 43, 44, 45, 56, 57, 62,
63, 68, 69, 74, 75, 76, 77, 78, 79, 80, 81, 92, 93, 98, 99, 112,
113, 128, 129, 134, 135, 148, 149, 160, 161, 162, 163, 164, 165,
166, 167, 168, 169, 170, 171, 172, 173, 178, 179, 184, 185, 196,
197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209,
214, 215, 220, 221
];
this.nscolour = colours[Math.abs(hash_numeric(this.namespace)) % colours.length];
let colouransi = `\u001b[3${(this.nscolour < 8 ? this.nscolour : '8;5;' + this.nscolour)}m`;
if(!a.enabled) colouransi = "";
this.ns = `${colouransi}${this.namespace}${a.reset}`;
}
debug(...msg) { l.debug(this.ns, ...msg); }
info(...msg) { l.info(this.ns, ...msg); }
log(...msg) { l.log(this.ns, ...msg); }
warn(...msg) { l.warn(this.ns, ...msg); }
error(...msg) { l.error(this.ns, ...msg); }
}
export default function(namespace) {
return new NamespacedLog(namespace);
};