bitburner/scripts/lib/logger.js

64 lines
1.3 KiB
JavaScript
Raw Normal View History

2022-03-13 21:49:22 -04:00
export class Logger {
2022-03-13 21:54:30 -04:00
historyLen = 19;
2022-03-13 21:49:22 -04:00
history = [];
2022-03-15 22:05:34 -04:00
/**
* Create a nicer log with a banner.
2022-04-20 11:32:10 -04:00
*
* @param {NS} ns - BitBurner API
* @param {Function[]} lineFns - Functions to generate a line (Seperated by a linebreak)
2022-03-15 22:05:34 -04:00
*/
2022-03-13 21:54:30 -04:00
constructor(ns, lineFns = []) {
2022-03-13 21:49:22 -04:00
this.ns = ns;
2022-03-13 21:54:30 -04:00
this.fns = lineFns;
2022-03-15 22:05:34 -04:00
this.historyLen -= lineFns.length * 2;
2022-03-13 21:49:22 -04:00
this.history = Array(this.historyLen).fill('');
2022-03-24 00:34:09 -04:00
this.log();
2022-03-13 21:49:22 -04:00
}
2022-03-24 00:34:09 -04:00
/**
2022-04-20 11:32:10 -04:00
* Add red error message to logs.
*
* @param {string} message - Text that will be added
2022-03-24 00:34:09 -04:00
*/
error(message) { this.log(`ERROR: ${message}`); }
2022-03-15 22:05:34 -04:00
/**
* Add a linebreak
*/
2022-03-13 21:49:22 -04:00
lineBreak() {
this.ns.print('===================================================');
}
2022-03-15 22:05:34 -04:00
/**
2022-04-20 11:32:10 -04:00
* Print the header using the provided functions.
2022-03-15 22:05:34 -04:00
*/
2022-03-13 21:49:22 -04:00
header() {
this.lineBreak();
2022-03-13 21:54:30 -04:00
this.fns.forEach(fn => {
this.ns.print(fn());
2022-03-13 21:49:22 -04:00
this.lineBreak();
});
}
2022-03-15 22:05:34 -04:00
/**
2022-04-20 11:32:10 -04:00
* Add message to the logs.
*
* @param {string} message - Text that will be added
2022-03-15 22:05:34 -04:00
*/
2022-03-24 00:34:09 -04:00
log(message = '') {
2022-03-13 21:49:22 -04:00
this.ns.clearLog();
this.header();
2022-03-24 00:34:09 -04:00
if(message) this.history.push(message);
2022-03-13 21:49:22 -04:00
this.history.splice(0, this.history.length - this.historyLen);
this.history.forEach(m => this.ns.print(m));
}
2022-03-24 00:34:09 -04:00
/**
2022-04-20 11:32:10 -04:00
* Add orange warning to the logs.
*
* @param {string} message - Text that will be added
2022-03-24 00:34:09 -04:00
*/
warn(message) { this.log(`WARN: ${message}`); }
2022-03-13 21:49:22 -04:00
}