Преглед на файлове

fix: fixed issue with overlapping console text in logger

Kristian Vos преди 5 години
родител
ревизия
5e33ac964d
променени са 3 файла, в които са добавени 26 реда и са изтрити 50 реда
  1. 2 13
      backend/core.js
  2. 3 5
      backend/index.js
  3. 21 32
      backend/logic/logger.js

+ 2 - 13
backend/core.js

@@ -25,21 +25,10 @@ module.exports = class {
 			this.setState("INITIALIZED");
 			this.setStage(0);
 			this.moduleManager.printStatus();
-		}).catch(async (err) => {
-			if (this.moduleManager.fancyConsole) {
-				this.moduleManager.replaceLoggerWithConsole();
-			}
-			
+		}).catch(async (err) => {			
 			this.failed = true;
 
-			console.error(`${this.logger.colors.FgRed}MODULE FAILED! Module ${this.name} has failed!${this.logger.colors.Reset}`);
-			console.error(err);
-
-			if (this.moduleManager.fancyConsole) {
-				for(let i = 0; i < this.logger.reservedLines; i++) {
-					process.stdout.write(`\n`);
-				}
-			}
+			this.logger.error(err.stack);
 
 			this.moduleManager.aModuleFailed(this);
 		});

+ 3 - 5
backend/index.js

@@ -82,11 +82,9 @@ class ModuleManager {
 
 		for (let moduleName in this.modules) {
 			let module = this.modules[moduleName];
-			let tabsAmount = 2 - (moduleName.length / 8);
-			
-			let tabs = "";
-			for(let i = 0; i < tabsAmount; i++)
-				tabs += "\t";
+			let tabsAmount = Math.max(0, Math.ceil(2 - (moduleName.length / 8)));
+
+			let tabs = Array(tabsAmount).fill(`\t`).join("");
 
 			let timing = module.timeDifferences.map((timeDifference) => {
 				return `${colors.FgMagenta}${timeDifference}${colors.FgCyan}ms${colors.FgWhite}`;

+ 21 - 32
backend/logic/logger.js

@@ -80,9 +80,7 @@ module.exports = class extends coreClass {
 			fs.appendFile(this.configDirectory + '/debugStation.log', `${time} BACKEND_RESTARTED\n`, ()=>{});
 
 			if (this.moduleManager.fancyConsole) {
-				for(let i = 0; i < this.reservedLines; i++) {
-					process.stdout.write("\n");
-				}
+				process.stdout.write(Array(this.reservedLines).fill(`\n`).join(""));
 			}
 
 			resolve();
@@ -145,41 +143,32 @@ module.exports = class extends coreClass {
 	}
 
 	log(color, message) {
-		this.logCbs.push(() => {
-			this.logCbs.shift();
-			this.logActive = true;
+		if (this.moduleManager.fancyConsole) {
+			const rows = process.stdout.rows;
+			const columns = process.stdout.columns;
+			const lineNumber = rows - this.reservedLines;
 
-			if (this.moduleManager.fancyConsole) {
-				const rows = process.stdout.rows;
-				const columns = process.stdout.columns;
-				const lineNumber = rows - this.reservedLines;
-
-				let lines = Math.floor(message.length / columns) + 1;
-
-				process.stdout.cursorTo(0, lineNumber);
-				process.stdout.write(`${color}${message}${this.colors.Reset}\n`);
+			
+			let lines = 0;
+			
+			message.split("\n").forEach((line) => {
+				lines += Math.floor(line.replace("\t", "    ").length / columns) + 1;
+			});
 
-				process.stdout.cursorTo(0, (rows - this.logger.reservedLines) + lines);
-				process.stdout.clearScreenDown();
+			if (lines > this.logger.reservedLines)
+				lines = this.logger.reservedLines;
 
-				process.stdout.cursorTo(0, process.stdout.rows);
-				for(let i = 0; i < lines; i++) {
-					process.stdout.write(`\n`);
-				}
+			process.stdout.cursorTo(0, rows - this.logger.reservedLines);
+			process.stdout.clearScreenDown();
 
-				this.moduleManager.printStatus();
-			} else console.log(`${color}${message}${this.colors.Reset}`);
+			process.stdout.cursorTo(0, lineNumber);
+			process.stdout.write(`${color}${message}${this.colors.Reset}\n`);
 
-			this.logActive = false;
-			this.nextLog();
-		});
-		this.nextLog();
-	}
+			process.stdout.cursorTo(0, process.stdout.rows);
+			process.stdout.write(Array(lines).fill(`\n!`).join(""));
 
-	nextLog() {
-		if (!this.logActive && this.logCbs.length > 0) {
-			this.logCbs[0]();
-		}
+			this.moduleManager.printStatus();
+		} else console.log(`${color}${message}${this.colors.Reset}`);
 	}
 
 	writeFile(fileName, message) {