Added logger.js

This commit is contained in:
Zakary Timson 2022-03-14 01:49:22 +00:00
parent 2c646e42ec
commit c777efc79d
2 changed files with 50 additions and 0 deletions

49
scripts/lib/logger.js Normal file
View File

@ -0,0 +1,49 @@
export class Logger {
historyLen = 17;
history = [];
/**
* Create a nicer log with a banner.
* @param ns {NS} - BitBurner API
* @param titleFn {Function} - Function to generate title
* @param extraFns {Function[]} - Extra info to put in the header
*/
constructor(ns, titleFn, extraFns = []) {
this.ns = ns;
this.title = titleFn;
this.extra = extraFns;
this.historyLen -= extraFns.length * 2;
this.history = Array(this.historyLen).fill('');
}
/**
* Add a linebreak
*/
lineBreak() {
this.ns.print('===================================================');
}
/**
* Print the header using the provided functions
*/
header() {
this.lineBreak();
this.ns.print(this.title());
this.lineBreak();
this.extra.forEach(extra => {
this.ns.print(extra());
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));
}
}

View File

@ -129,6 +129,7 @@ export async function main(ns) {
const dest = '/scripts/';
const fileList = [
'lib/arg-parser.js',
'lib/logger.js',
'lib/utils.js',
'connect.js',
'crawler.js',