diff --git a/src/kernel/Init.hx b/src/kernel/Init.hx index 59550d1..14ffbc3 100644 --- a/src/kernel/Init.hx +++ b/src/kernel/Init.hx @@ -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(); diff --git a/src/kernel/log/Log.hx b/src/kernel/log/Log.hx index dc35ad5..53d51e7 100644 --- a/src/kernel/log/Log.hx +++ b/src/kernel/log/Log.hx @@ -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 = []; + public final onLog:Signal; + + private final onLogTrigger:SignalTrigger = new SignalTrigger(); + private final logLines:Array = []; + + 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 { + return logLines; } }