blaze-2d
Version:
A fast and simple WebGL 2 2D game engine written in TypeScript
70 lines • 2.11 kB
JavaScript
export default class Logger {
/**
* Logs a message.
*
* @param id The identifier of who is logging
* @param args The args to log
*/
static log(id, ...args) {
const str = this.str(id, ...args);
console.log(str);
this.callListeners("log", str, id, args);
return str;
}
static error(id, ...args) {
const str = this.str(id, ...args);
console.log("%c" + str, `color: ${this.errorColor}`);
this.callListeners("error", str, id, args);
return str;
}
static warn(id, ...args) {
const str = this.str(id, ...args);
console.log("%c" + str, `color: ${this.warnColor}`);
this.callListeners("warning", str, id, args);
return str;
}
/**
* Constructs the string to be logged.
*
* @param id The identifier of who is logging
* @param args The args to log
*/
static str(id, ...args) {
const str = args.join(" ");
const date = new Date();
const time = `${date.getHours().toString().padStart(2, "0")}:${date.getMinutes().toString().padStart(2, "0")}:${date
.getSeconds()
.toString()
.padStart(2, "0")}`;
return `[${time} ${id}]: ${str}`;
}
static callListeners(type, str, id, args) {
for (const cb of this.listeners) {
cb(type, str, id, args);
}
}
/**
* Adds a callback which will be called whenever a message is logged.
*
* @param callback The listener's callback
*/
static addListener(callback) {
this.listeners.push(callback);
}
/**
* Removes a listener from the logger.
*
* @param callback The listener to remove
*/
static removeListener(callback) {
const index = this.listeners.findIndex((cb) => cb === callback);
if (index === -1)
return;
this.listeners.splice(index, 1);
}
}
Logger.errorColor = "#dc3545";
Logger.successColor = "#28a745";
Logger.warnColor = "#ffc107";
Logger.listeners = [];
//# sourceMappingURL=logger.js.map