ソースを参照

refactor: added way to disable fancy console, added way to see what error failed module gives

Kristian Vos 5 年 前
コミット
55554a6920
4 ファイル変更79 行追加27 行削除
  1. 2 1
      backend/config/template.json
  2. 17 1
      backend/core.js
  3. 41 10
      backend/index.js
  4. 19 15
      backend/logic/logger.js

+ 2 - 1
backend/config/template.json

@@ -5,7 +5,8 @@
 	"frontendPort": 80,
 	"serverDomain": "http://localhost:8080",
   	"serverPort": 8080,
-  	"isDocker": true,
+	"isDocker": true,
+	"fancyConsole": true,
 	"apis": {
 		"youtube": {
 			"key": ""

+ 17 - 1
backend/core.js

@@ -14,6 +14,7 @@ module.exports = class {
 		this.lastTime = 0;
 		this.totalTimeInitialize = 0;
 		this.timeDifferences = [];
+		this.failed = false;
 	}
 
 	_initialize() {
@@ -24,7 +25,22 @@ module.exports = class {
 			this.setState("INITIALIZED");
 			this.setStage(0);
 			this.moduleManager.printStatus();
-		}).catch(() => {
+		}).catch(async (err) => {
+			if (this.moduleManager.fancyConsole) {
+				this.moduleManager.replaceLoggerWithConsole();
+			}
+			
+			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.moduleManager.aModuleFailed(this);
 		});
 	}

+ 41 - 10
backend/index.js

@@ -4,11 +4,15 @@ const util = require("util");
 
 process.env.NODE_CONFIG_DIR = `${__dirname}/config`;
 
+const config = require("config");
+
 process.on('uncaughtException', err => {
 	if (err.code === 'ECONNREFUSED' || err.code === 'UNCERTAIN_STATE') return;
 	console.log(`UNCAUGHT EXCEPTION: ${err.stack}`);
 });
 
+const fancyConsole = config.get("fancyConsole");
+
 class ModuleManager {
 	constructor() {
 		this.modules = {};
@@ -17,6 +21,7 @@ class ModuleManager {
 		this.modulesLeft = [];
 		this.i = 0;
 		this.lockdown = false;
+		this.fancyConsole = fancyConsole;
 	}
 
 	addModule(moduleName) {
@@ -30,12 +35,10 @@ class ModuleManager {
 	initialize() {
 		if (!this.modules["logger"]) return console.error("There is no logger module");
 		this.logger = this.modules["logger"];
-		console.log = (...args) => this.logger.debug(args.map(arg => util.format(arg)));
-		console.debug = (...args) => this.logger.debug(args.map(arg => util.format(arg)));
-		console.info = (...args) => this.logger.debug(args.map(arg => util.format(arg)));
-		console.warn = (...args) => this.logger.debug(args.map(arg => util.format(arg)));
-		console.error = (...args) => this.logger.error("CONSOLE", args.map(arg => util.format(arg)));
-		this.logger.reservedLines = Object.keys(this.modules).length + 5;
+		if (this.fancyConsole) {
+			this.replaceConsoleWithLogger();
+			this.logger.reservedLines = Object.keys(this.modules).length + 5;
+		}
 		
 		for (let moduleName in this.modules) {
 			let module = this.modules[moduleName];
@@ -64,6 +67,7 @@ class ModuleManager {
 
 	async printStatus() {
 		try { await Promise.race([this.logger._onInitialize, this.logger._isInitialized]); } catch { return; }
+		if (!this.fancyConsole) return;
 		
 		let colors = this.logger.colors;
 
@@ -92,7 +96,8 @@ class ModuleManager {
 			if (module.state === "NOT_INITIALIZED") stateColor = colors.FgWhite;
 			if (module.state === "INITIALIZING") stateColor = colors.FgYellow;
 			if (module.state === "INITIALIZED") stateColor = colors.FgGreen;
-			if (module.state === "LOCKDOWN") stateColor = colors.FgRed;
+			if (module.state === "LOCKDOWN" && !module.failed) stateColor = colors.FgRed;
+			if (module.state === "LOCKDOWN" && module.failed) stateColor = colors.FgMagenta;
 			
 			process.stdout.write(`${moduleName}${tabs}${stateColor}${module.state}\t${colors.FgYellow}Stage: ${colors.FgRed}${module.stage}${colors.FgWhite}. ${colors.FgYellow}Timing${colors.FgWhite}: [${timing}]${colors.FgWhite}${colors.FgWhite}. ${colors.FgYellow}Total time${colors.FgWhite}: ${colors.FgRed}${module.totalTimeInitialize}${colors.FgCyan}ms${colors.Reset}\n`);
 		}
@@ -119,6 +124,29 @@ class ModuleManager {
 		this._lockdown();
 	}
 
+	replaceConsoleWithLogger() {
+		this.oldConsole = {
+			log: console.log,
+			debug: console.debug,
+			info: console.info,
+			warn: console.warn,
+			error: console.error
+		};
+		console.log = (...args) => this.logger.debug(args.map(arg => util.format(arg)));
+		console.debug = (...args) => this.logger.debug(args.map(arg => util.format(arg)));
+		console.info = (...args) => this.logger.debug(args.map(arg => util.format(arg)));
+		console.warn = (...args) => this.logger.debug(args.map(arg => util.format(arg)));
+		console.error = (...args) => this.logger.error("CONSOLE", args.map(arg => util.format(arg)));
+	}
+
+	replaceLoggerWithConsole() {
+		console.log = this.oldConsole.log;
+		console.debug = this.oldConsole.debug;
+		console.info = this.oldConsole.info;
+		console.warn = this.oldConsole.warn;
+		console.error = this.oldConsole.error;
+	}
+
 	_lockdown() {
 		this.lockdown = true;
 		
@@ -159,8 +187,11 @@ process.stdin.on("data", function (data) {
     }
 });
 
-const rows = process.stdout.rows;
 
-for(let i = 0; i < rows; i++) {
-	process.stdout.write("\n");
+if (fancyConsole) {
+	const rows = process.stdout.rows;
+
+	for(let i = 0; i < rows; i++) {
+		process.stdout.write("\n");
+	}
 }

+ 19 - 15
backend/logic/logger.js

@@ -74,8 +74,10 @@ module.exports = class extends coreClass {
 			fs.appendFile(this.configDirectory + '/info.log', `${time} BACKEND_RESTARTED\n`, ()=>{});
 			fs.appendFile(this.configDirectory + '/debugStation.log', `${time} BACKEND_RESTARTED\n`, ()=>{});
 
-			for(let i = 0; i < this.reservedLines; i++) {
-				process.stdout.write("\n");
+			if (this.moduleManager.fancyConsole) {
+				for(let i = 0; i < this.reservedLines; i++) {
+					process.stdout.write("\n");
+				}
 			}
 
 			resolve();
@@ -142,24 +144,26 @@ module.exports = class extends coreClass {
 			this.logCbs.shift();
 			this.logActive = true;
 
-			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;
+				let lines = Math.floor(message.length / columns) + 1;
 
-			process.stdout.cursorTo(0, lineNumber);
-			process.stdout.write(`${color}${message}${this.colors.Reset}\n`);
+				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, (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`);
-			}
+				process.stdout.cursorTo(0, process.stdout.rows);
+				for(let i = 0; i < lines; i++) {
+					process.stdout.write(`\n`);
+				}
 
-			this.moduleManager.printStatus();
+				this.moduleManager.printStatus();
+			} else console.log(`${color}${message}${this.colors.Reset}`);
 
 			this.logActive = false;
 			this.nextLog();