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(); Peripheral.instance = new Peripheral();
WindowManager.instance = new WindowManager(); WindowManager.instance = new WindowManager();
MainTerm.instance = new MainTerm();
Log.init();
if (Turtle.isTurtle()){ if (Turtle.isTurtle()){
Turtle.instance = new Turtle(); Turtle.instance = new Turtle();

View File

@ -1,11 +1,6 @@
package kernel; package kernel;
import kernel.ui.WindowContext;
import kernel.ui.WindowManager;
import kernel.ui.TermWriteable;
import lib.TermIO;
#if webconsole #if webconsole
import kernel.net.Net;
import lib.Debug; import lib.Debug;
#end #end
@ -13,68 +8,41 @@ import lib.Debug;
Log messages to specified output. Log messages to specified output.
**/ **/
class Log { class Log {
private static var context:WindowContext; private static inline final MAX_LINES:Int = 100;
private static var writer:TermIO;
/** private static final logLines:Array<LogLine> = [];
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);
}
public static function info(msg:Dynamic, ?pos:haxe.PosInfos) { public static function info(msg:Dynamic, ?pos:haxe.PosInfos) {
writer.writeLn(logLine("INFO",pos,msg)); log({level: Info, message: Std.string(msg),time: 0});
#if webconsole
Debug.printWeb(logLine("INFO",pos,msg));
#end
} }
public static function warn(msg:Dynamic, ?pos:haxe.PosInfos) { public static function warn(msg:Dynamic, ?pos:haxe.PosInfos) {
writer.writeLn(logLine("WARN",pos,msg), Yellow); log({level: Warn, message: Std.string(msg),time: 0});
#if webconsole
Debug.printWeb(logLine("WARN",pos,msg));
#end
} }
public static function error(msg:Dynamic, ?pos:haxe.PosInfos) { public static function error(msg:Dynamic, ?pos:haxe.PosInfos) {
writer.writeLn(logLine("ERRO",pos,msg), Red); log({level: Error, message: Std.string(msg),time: 0});
#if webconsole
Debug.printWeb(logLine("ERRO",pos,msg));
#end
} }
public static function debug(msg:Dynamic, ?pos:haxe.PosInfos) { public static function debug(msg:Dynamic, ?pos:haxe.PosInfos) {
#if debug #if debug
writer.writeLn(logLine("DEBG",pos,msg), Gray); log({level: Debug, message: Std.string(msg),time: 0});
#if webconsole
Debug.printWeb(logLine("DEBG",pos,msg));
#end
#end #end
} }
public static function silly(msg:Dynamic, ?pos:haxe.PosInfos) { 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 #if webconsole
Debug.printWeb(logLine("SILY",pos,msg)); Debug.printWeb('[${Std.string(line.level)}] ${line.message}');
#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)}';
#end #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,
}