bitburner/scripts/lib/logger.js

47 lines
1007 B
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 = [];
/**
* Create a nicer log with a banner.
* @param ns {NS} - BitBurner API
2022-03-13 21:54:30 -04:00
* @param lineFns {Function[]} - Functions to generate a line (Seperated by a linebreak)
2022-03-13 21:49:22 -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;
this.historyLen -= fns.length * 2;
2022-03-13 21:49:22 -04:00
this.history = Array(this.historyLen).fill('');
2022-03-15 20:41:23 -04:00
this.log();
2022-03-13 21:49:22 -04:00
}
/**
* Add a linebreak
*/
lineBreak() {
this.ns.print('===================================================');
}
/**
* Print the header using the provided functions
*/
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();
});
}
/**
* Add message to logs & output
*/
log(message) {
this.ns.clearLog();
this.header();
if(message != null) this.history.push(message);
this.history.splice(0, this.history.length - this.historyLen);
this.history.forEach(m => this.ns.print(m));
}
}