bitburner/scripts/lib/logger.js

46 lines
955 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 = [];
2022-03-15 22:05:34 -04:00
/**
* Create a nicer log with a banner.
* @param ns {NS} - BitBurner API
* @param lineFns {Function[]} - Functions to generate a line (Seperated by a linebreak)
*/
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-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
/**
* Print the header using the provided functions
*/
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
/**
* Add message to logs & output
*/
2022-03-13 21:49:22 -04:00
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));
}
}