refactored Logging

This commit is contained in:
Djeeberjr 2023-01-30 02:47:54 +01:00
parent 632173d122
commit 391c6b19fd
4 changed files with 35 additions and 51 deletions

View File

@ -16,8 +16,6 @@ class Init {
Peripheral.instance = new Peripheral();
WindowManager.instance = new WindowManager();
MainTerm.instance = new MainTerm();
Log.init();
if (Turtle.isTurtle()){
Turtle.instance = new Turtle();

View File

@ -1,11 +1,6 @@
package kernel;
import kernel.ui.WindowContext;
import kernel.ui.WindowManager;
import kernel.ui.TermWriteable;
import lib.TermIO;
#if webconsole
import kernel.net.Net;
import lib.Debug;
#end
@ -13,68 +8,41 @@ import lib.Debug;
Log messages to specified output.
**/
class Log {
private static var context:WindowContext;
private static var writer:TermIO;
private static inline final MAX_LINES:Int = 100;
/**
Depends on: WindowManager
**/
@:allow(kernel.Init)
private static function init() {
Log.context = WindowManager.instance.createNewContext();
Log.writer = new TermIO(Log.context);
}
private static function setMainoutout(newOutput:TermWriteable) {
writer = new TermIO(newOutput);
}
private static final logLines:Array<LogLine> = [];
public static function info(msg:Dynamic, ?pos:haxe.PosInfos) {
writer.writeLn(logLine("INFO",pos,msg));
#if webconsole
Debug.printWeb(logLine("INFO",pos,msg));
#end
log({level: Info, message: Std.string(msg),time: 0});
}
public static function warn(msg:Dynamic, ?pos:haxe.PosInfos) {
writer.writeLn(logLine("WARN",pos,msg), Yellow);
#if webconsole
Debug.printWeb(logLine("WARN",pos,msg));
#end
log({level: Warn, message: Std.string(msg),time: 0});
}
public static function error(msg:Dynamic, ?pos:haxe.PosInfos) {
writer.writeLn(logLine("ERRO",pos,msg), Red);
#if webconsole
Debug.printWeb(logLine("ERRO",pos,msg));
#end
log({level: Error, message: Std.string(msg),time: 0});
}
public static function debug(msg:Dynamic, ?pos:haxe.PosInfos) {
#if debug
writer.writeLn(logLine("DEBG",pos,msg), Gray);
#if webconsole
Debug.printWeb(logLine("DEBG",pos,msg));
#end
log({level: Debug, message: Std.string(msg),time: 0});
#end
}
public static function silly(msg:Dynamic, ?pos:haxe.PosInfos) {
writer.writeLn(logLine("SILY",pos,msg), LightGray);
log({level: Silly, message: Std.string(msg),time: 0});
}
private static function log(line: LogLine, ?pos:haxe.PosInfos) {
logLines.push(line);
if (logLines.length > MAX_LINES) {
logLines.shift();
}
#if webconsole
Debug.printWeb(logLine("SILY",pos,msg));
#end
}
public static function moveToOutput(addr:String) {
WindowManager.instance.focusContextToOutput(context, addr);
}
private static function logLine(tag: String,pos: haxe.PosInfos,msg: Dynamic): String {
#if debug
return '[$tag][${pos.className}:${pos.lineNumber}]: ${Std.string(msg)}';
#else
return '[$tag]: ${Std.string(msg)}';
Debug.printWeb('[${Std.string(line.level)}] ${line.message}');
#end
}
}

9
src/kernel/LogLevel.hx Normal file
View File

@ -0,0 +1,9 @@
package kernel;
enum LogLevel {
Info;
Warn;
Error;
Debug;
Silly;
}

9
src/kernel/LogLine.hx Normal file
View File

@ -0,0 +1,9 @@
package kernel;
import kernel.LogLevel;
typedef LogLine = {
level: LogLevel,
message: String,
time: Int,
}