improved logging system
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
package kernel.log;
|
||||
|
||||
import haxe.ds.ReadOnlyArray;
|
||||
#if webconsole
|
||||
import haxe.macro.Context;
|
||||
#if (webconsole && !macro)
|
||||
import lib.Debug;
|
||||
#end
|
||||
|
||||
@@ -24,35 +25,59 @@ class Log {
|
||||
|
||||
#if debug
|
||||
haxe.Log.trace = function(v:Dynamic, ?infos:haxe.PosInfos) {
|
||||
Log.debug(v, infos);
|
||||
Log._debug(v, infos.className);
|
||||
}
|
||||
#end
|
||||
}
|
||||
|
||||
public static function info(msg:Dynamic, ?pos:haxe.PosInfos) {
|
||||
log({level: Info, message: Std.string(msg), time: 0}, pos);
|
||||
public static function _info(msg:Dynamic, ?origin:String) {
|
||||
log({
|
||||
level: Info,
|
||||
message: Std.string(msg),
|
||||
time: 0,
|
||||
origin: origin
|
||||
});
|
||||
}
|
||||
|
||||
public static function warn(msg:Dynamic, ?pos:haxe.PosInfos) {
|
||||
log({level: Warn, message: Std.string(msg), time: 0}, pos);
|
||||
public static function _warn(msg:Dynamic, ?origin:String) {
|
||||
log({
|
||||
level: Warn,
|
||||
message: Std.string(msg),
|
||||
time: 0,
|
||||
origin: origin
|
||||
});
|
||||
}
|
||||
|
||||
public static function error(msg:Dynamic, ?pos:haxe.PosInfos) {
|
||||
log({level: Error, message: Std.string(msg), time: 0}, pos);
|
||||
public static function _error(msg:Dynamic, ?origin:String) {
|
||||
log({
|
||||
level: Error,
|
||||
message: Std.string(msg),
|
||||
time: 0,
|
||||
origin: origin
|
||||
});
|
||||
}
|
||||
|
||||
public static function debug(msg:Dynamic, ?pos:haxe.PosInfos) {
|
||||
public static function _debug(msg:Dynamic, ?origin:String) {
|
||||
#if debug
|
||||
log({level: Debug, message: Std.string(msg), time: 0}, pos);
|
||||
log({
|
||||
level: Debug,
|
||||
message: Std.string(msg),
|
||||
time: 0,
|
||||
origin: origin
|
||||
});
|
||||
#end
|
||||
}
|
||||
|
||||
public static function silly(msg:Dynamic, ?pos:haxe.PosInfos) {
|
||||
log({level: Silly, message: Std.string(msg), time: 0}, pos);
|
||||
public static function _silly(msg:Dynamic, ?origin:String) {
|
||||
log({
|
||||
level: Silly,
|
||||
message: Std.string(msg),
|
||||
time: 0,
|
||||
origin: origin
|
||||
});
|
||||
}
|
||||
|
||||
private static function log(line:LogLine, ?pos:haxe.PosInfos) {
|
||||
line.origin = pos.className;
|
||||
private static function log(line:LogLine) {
|
||||
logLines.push(line);
|
||||
|
||||
if (logLines.length > MAX_LINES) {
|
||||
@@ -60,12 +85,49 @@ class Log {
|
||||
}
|
||||
onLogTrigger.trigger(line);
|
||||
|
||||
#if webconsole
|
||||
Debug.printWeb('[${Std.string(line.level)}][${line.origin}] ${line.message}');
|
||||
#if (webconsole && !macro)
|
||||
Debug.logToWebconsole(line);
|
||||
#end
|
||||
}
|
||||
|
||||
public static function getLines():ReadOnlyArray<LogLine> {
|
||||
return logLines;
|
||||
}
|
||||
|
||||
#if macro
|
||||
private static function getOrigin():String {
|
||||
var localClass = Context.getLocalClass().get();
|
||||
return localClass.name;
|
||||
}
|
||||
#end
|
||||
|
||||
public macro static function info(msg:ExprOf<Dynamic>) {
|
||||
return macro {
|
||||
Log._info(${msg}, $v{getOrigin()});
|
||||
}
|
||||
}
|
||||
|
||||
public macro static function warn(msg:ExprOf<Dynamic>) {
|
||||
return macro {
|
||||
Log._warn(${msg}, $v{getOrigin()});
|
||||
}
|
||||
}
|
||||
|
||||
public macro static function error(msg:ExprOf<Dynamic>) {
|
||||
return macro {
|
||||
Log._error(${msg}, $v{getOrigin()});
|
||||
}
|
||||
}
|
||||
|
||||
public macro static function debug(msg:ExprOf<Dynamic>) {
|
||||
return macro {
|
||||
Log._debug(${msg}, $v{getOrigin()});
|
||||
}
|
||||
}
|
||||
|
||||
public macro static function silly(msg:ExprOf<Dynamic>) {
|
||||
return macro {
|
||||
Log._silly(${msg}, $v{getOrigin()});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package lib;
|
||||
|
||||
import kernel.http.HTTPRequest.Http;
|
||||
import lua.TableTools;
|
||||
import kernel.KernelEvents;
|
||||
import kernel.log.Log;
|
||||
import lua.NativeStringTools;
|
||||
import lib.ui.Canvas;
|
||||
@@ -9,6 +9,7 @@ import cc.ComputerCraft;
|
||||
#if webconsole
|
||||
import cc.HTTP;
|
||||
import kernel.net.Net;
|
||||
import kernel.log.LogLine;
|
||||
#end
|
||||
|
||||
class Debug {
|
||||
@@ -66,8 +67,8 @@ class Debug {
|
||||
#end
|
||||
|
||||
#if webconsole
|
||||
public static function printWeb(msg:String) {
|
||||
HTTP.request("http://127.0.0.1:8080/" + Net.networkID, msg);
|
||||
public static function logToWebconsole(line:LogLine) {
|
||||
Http.request('http://127.0.0.1:8080/log/${Net.networkID}/${line.level.getIndex()}', '[${line.origin}] ${line.message}').eager();
|
||||
}
|
||||
#end
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user