Przeglądaj źródła

refactor: improved logger and printStatus

Kristian Vos 5 lat temu
rodzic
commit
60f43e2972
2 zmienionych plików z 60 dodań i 9 usunięć
  1. 18 3
      backend/index.js
  2. 42 6
      backend/logic/logger.js

+ 18 - 3
backend/index.js

@@ -28,7 +28,12 @@ class ModuleManager {
 	initialize() {
 		if (!this.modules["logger"]) return console.error("There is no logger module");
 		this.logger = this.modules["logger"];
-		this.logger.reservedLines = Object.keys(this.modules).length + 2;
+		console.log = (...args) => this.logger.debug(args.join(" "));
+		console.debug = (...args) => this.logger.debug(args.join(" "));
+		console.info = (...args) => this.logger.debug(args.join(" "));
+		console.warn = (...args) => this.logger.debug(args.join(" "));
+		console.error = (...args) => this.logger.error("CONSOLE", args.join(" "));
+		this.logger.reservedLines = Object.keys(this.modules).length + 5;
 		
 		for (let moduleName in this.modules) {
 			let module = this.modules[moduleName];
@@ -60,9 +65,13 @@ class ModuleManager {
 		
 		let colors = this.logger.colors;
 
-		process.stdout.moveCursor(0, -this.logger.reservedLines);
+		const rows = process.stdout.rows;
+
+		process.stdout.cursorTo(0, rows - this.logger.reservedLines);
 		process.stdout.clearScreenDown();
-		process.stdout.write(`\n`);
+
+		process.stdout.cursorTo(0, (rows - this.logger.reservedLines) + 2);
+
 		process.stdout.write(`${colors.FgYellow}Modules${colors.FgWhite}:\n`);
 
 		for (let moduleName in this.modules) {
@@ -147,3 +156,9 @@ process.stdin.on("data", function (data) {
        	moduleManager._lockdown();
     }
 });
+
+const rows = process.stdout.rows;
+
+for(let i = 0; i < rows; i++) {
+	process.stdout.write("\n");
+}

+ 42 - 6
backend/logic/logger.js

@@ -38,6 +38,8 @@ module.exports = class extends coreClass {
 
 			let time = getTimeFormatted();
 
+			this.logCbs = [];
+
 			this.colors = {
 				Reset: "\x1b[0m",
 				Bright: "\x1b[1m",
@@ -115,6 +117,15 @@ module.exports = class extends coreClass {
 		if (display) this.log(this.colors.FgCyan, message);
 	}
 
+	async debug(text, display = true) {
+		try { await this._validateHook(); } catch { return; }
+
+		const time = getTimeFormatted();
+		const message = `${time} DEBUG - ${text}`;
+
+		if (display) this.log(this.colors.FgMagenta, message);
+	}
+
 	async stationIssue(text, display = false) {
 		try { await this._validateHook(); } catch { return; }
 
@@ -127,14 +138,39 @@ module.exports = class extends coreClass {
 	}
 
 	log(color, message) {
-		process.stdout.moveCursor(0, -this.reservedLines);
-		process.stdout.write(`${color}${message}${this.colors.Reset}\n`);
+		this.logCbs.push(() => {
+			this.logCbs.shift();
+			this.logActive = true;
 
-		for(let i = 0; i < this.reservedLines; i++) {
-			process.stdout.write("\n");
-		}
+			const rows = process.stdout.rows;
+			const columns = process.stdout.columns;
+			const lineNumber = rows - this.reservedLines;
+
+			let lines = Math.floor(message.length / columns) + 1;
 
-		this.moduleManager.printStatus();
+			process.stdout.cursorTo(0, lineNumber);
+			process.stdout.write(`${color}${message}${this.colors.Reset}\n`);
+
+			process.stdout.cursorTo(0, (rows - this.logger.reservedLines) + lines);
+			process.stdout.clearScreenDown();
+
+			process.stdout.cursorTo(0, process.stdout.rows);
+			for(let i = 0; i < lines; i++) {
+				process.stdout.write(`\n`);
+			}
+
+			this.moduleManager.printStatus();
+
+			this.logActive = false;
+			this.nextLog();
+		});
+		this.nextLog();
+	}
+
+	nextLog() {
+		if (!this.logActive && this.logCbs.length > 0) {
+			this.logCbs[0]();
+		}
 	}
 
 	writeFile(fileName, message) {