Improved Log system

This commit is contained in:
Djeeberjr 2023-03-20 01:15:07 +01:00
parent b0be0ba8b4
commit 84d437104d
2 changed files with 27 additions and 7 deletions

View File

@ -1,5 +1,6 @@
package kernel;
import kernel.log.Log;
import kernel.turtle.Turtle;
import haxe.MainLoop;
import kernel.net.Routing;
@ -12,6 +13,7 @@ import kernel.net.Net;
class Init {
public static function initKernel() {
// Init singeltons here because haxe is confused about the order to create them.
Log.instance = new Log();
KernelEvents.instance = new KernelEvents();
Peripheral.instance = new Peripheral();

View File

@ -1,40 +1,52 @@
package kernel.log;
import haxe.ds.ReadOnlyArray;
#if webconsole
import lib.Debug;
#end
using tink.CoreApi;
/**
Log messages to specified output.
**/
class Log {
public static var instance:Log;
private static inline final MAX_LINES:Int = 100;
private static final logLines:Array<LogLine> = [];
public final onLog:Signal<LogLine>;
private final onLogTrigger:SignalTrigger<LogLine> = new SignalTrigger();
private final logLines:Array<LogLine> = [];
public function new() {
onLog = onLogTrigger.asSignal();
}
public static function info(msg:Dynamic, ?pos:haxe.PosInfos) {
log({level: Info, message: Std.string(msg),time: 0});
instance.log({level: Info, message: Std.string(msg),time: 0});
}
public static function warn(msg:Dynamic, ?pos:haxe.PosInfos) {
log({level: Warn, message: Std.string(msg),time: 0});
instance.log({level: Warn, message: Std.string(msg),time: 0});
}
public static function error(msg:Dynamic, ?pos:haxe.PosInfos) {
log({level: Error, message: Std.string(msg),time: 0});
instance.log({level: Error, message: Std.string(msg),time: 0});
}
public static function debug(msg:Dynamic, ?pos:haxe.PosInfos) {
#if debug
log({level: Debug, message: Std.string(msg),time: 0});
instance.log({level: Debug, message: Std.string(msg),time: 0});
#end
}
public static function silly(msg:Dynamic, ?pos:haxe.PosInfos) {
log({level: Silly, message: Std.string(msg),time: 0});
instance.log({level: Silly, message: Std.string(msg),time: 0});
}
private static function log(line: LogLine, ?pos:haxe.PosInfos) {
private function log(line: LogLine, ?pos:haxe.PosInfos) {
logLines.push(line);
if (logLines.length > MAX_LINES) {
@ -44,5 +56,11 @@ class Log {
#if webconsole
Debug.printWeb('[${Std.string(line.level)}] ${line.message}');
#end
onLogTrigger.trigger(line);
}
public function getLines():ReadOnlyArray<LogLine> {
return logLines;
}
}