控制台输出彩色log

阿甘 24天前 290

记录一下,可以借鉴 python 的 loguru 库

namespace logger
import console;
import debug;
import preg;

class Logger{
	ctor(){
		..console.open();
		this.queryinfo = "select source,function,upvars,name,currentline,activelines";
		this.regex = ..preg("(?<=//).+(?=\.\.\.)");
		this.type_enum = {
			[1] = "INFO   ";
			[2] = "DEBUG  ";
			[3] = "WARNING";
			[4] = "ERROR  ";
			[5] = "SUCCESS";
		}
		this.type = 1;
		this.color = ..console.color.white;
	};
	logMessage = function(msg, dInfo){
    	var _time = tostring(..time());
    	//var _file = this.regex.match(dInfo["source"]["src"]);
    	var _line = dInfo["currentline"];
    	var _function = dInfo["name"] == null ? "module" : dInfo["name"];
    	..console.writeColorText(_time, ..console.color.darkGreen);
    	..console.writeColorText(" | ");
    	..console.writeColorText(this.type_enum[this.type], this.color);
    	..console.writeColorText(" | ");
    	..console.writeColorText(_function++":"++_line, ..console.color.cyan);
    	..console.writeColorText(' - ');
    	..console.writeColorText(msg++'\n', this.color);
	}
	
	info = function(msg){
		this.type = 1;
		this.color = ..console.color.white;
    	var dInfo = ..debug.queryinfo(2, this.queryinfo);
    	this.logMessage(msg, dInfo);
	}
	debug = function(msg){
		this.type = 2;
		this.color = ..console.color.blue;
    	var dInfo = ..debug.queryinfo(2, this.queryinfo);
    	this.logMessage(msg, dInfo);
	}
	warning = function(msg){
		this.type = 3;
		this.color = ..console.color.darkGray;
    	var dInfo = ..debug.queryinfo(2, this.queryinfo);
    	this.logMessage(msg, dInfo);
	}
	error = function(msg){
		this.type = 4;
		this.color = ..console.color.red;
    	var dInfo = ..debug.queryinfo(2, this.queryinfo);
    	this.logMessage(msg, dInfo);
	}
	success = function(msg){
		this.type = 5;
		this.color = ..console.color.green;
    	var dInfo = ..debug.queryinfo(2, this.queryinfo);
    	this.logMessage(msg, dInfo);
	}
}
//test.aardio
import logger;
logger = logger.Logger();

var testFunc = function(){
	logger.info("Hello world!");
	logger.debug("Hello world!");
	logger.warning("Hello world!");
	logger.error("Hello world!");
	logger.success("Hello world!");
}
testFunc();

console.askYesNo("按Y键继续,按N键取消");


最新回复 (2)
  • 光庆 23天前
    0 2

    好东西,顶


  • kio 23天前
    0 3
    漂亮
返回