jadefin/JadefinLog.js

89 lines
2.6 KiB
JavaScript

//@ts-check
import JadefinIntegrity from "./JadefinIntegrity.js";
import cyrb53a from "./utils/cyrb53.js";
export default JadefinIntegrity("JadefinLog", import.meta.url, () => class JadefinLog {
tag;
/** @type {{[key: string]: string}} */
style = {
v: `background: black; color: gray; font-weight: 300; font-family: monospace; font-size: 1em; line-height: 2em;`,
i: `background: black; color: white; font-weight: 300; font-family: monospace; font-size: 1em; line-height: 2em;`,
w: `background: black; color: yellow; font-weight: 300; font-family: monospace; font-size: 1em; line-height: 2em;`,
e: `background: #ff0020; color: black; font-weight: 300; font-family: monospace; font-size: 1em; line-height: 2em;`
};
/** @type {(data: string) => void} */
v;
/** @type {(data: string) => void} */
i;
/** @type {(data: string) => void} */
w;
/** @type {(data: string) => void} */
e;
/**
* @param {string} tag
*/
constructor(tag) {
this.tag = tag;
let fg = "black";
const bgInt = Math.floor((Math.abs(Math.sin(cyrb53a(tag)) * 16777215)));
const bgR = (bgInt >> 16) & 0xff;
const bgG = (bgInt >> 8) & 0xff;
const bgB = (bgInt >> 0) & 0xff;
const bgLuma = 0.2126 * bgR + 0.7152 * bgG + 0.0722 * bgB;
if (bgLuma < 40) {
fg = "white";
}
const bg = "#" + bgInt.toString(16);
this.style.tag = `background: ${bg}; color: ${fg}; font-weight: 600; font-family: monospace; font-size: 1.5em; line-height: 1.25em;`;
this.update();
}
update() {
this.v = console.info.bind(console, `%c ${this.tag} %c %s `, this.style.tag, this.style.v);
this.i = console.info.bind(console, `%c ${this.tag} %c %s `, this.style.tag, this.style.i);
this.w = console.info.bind(console, `%c ${this.tag} %c %s `, this.style.tag, this.style.w);
this.e = console.info.bind(console, `%c ${this.tag} %c %s `, this.style.tag, this.style.e);
}
/**
* @param {any[]} args
* @returns {this}
*/
dir(...args) {
if (args.length == 0) {
return this;
}
if (args.length == 1) {
if (args[0] instanceof Error) {
console.error(args[0]);
} else {
console.dir(args[0]);
}
return this;
}
console.groupCollapsed(`${args.length} objects`);
for (let arg of args) {
console.dir(arg);
}
console.groupEnd();
return this;
}
});