improved logging system

This commit is contained in:
Niklas Kapelle 2024-08-17 00:47:15 +02:00
parent afc0309222
commit 39a7da716c
Signed by: niklas
GPG Key ID: 4EB651B36D841D16
3 changed files with 122 additions and 22 deletions

View File

@ -1,7 +1,8 @@
package kernel.log; package kernel.log;
import haxe.ds.ReadOnlyArray; import haxe.ds.ReadOnlyArray;
#if webconsole import haxe.macro.Context;
#if (webconsole && !macro)
import lib.Debug; import lib.Debug;
#end #end
@ -24,35 +25,59 @@ class Log {
#if debug #if debug
haxe.Log.trace = function(v:Dynamic, ?infos:haxe.PosInfos) { haxe.Log.trace = function(v:Dynamic, ?infos:haxe.PosInfos) {
Log.debug(v, infos); Log._debug(v, infos.className);
} }
#end #end
} }
public static function info(msg:Dynamic, ?pos:haxe.PosInfos) { public static function _info(msg:Dynamic, ?origin:String) {
log({level: Info, message: Std.string(msg), time: 0}, pos); log({
level: Info,
message: Std.string(msg),
time: 0,
origin: origin
});
} }
public static function warn(msg:Dynamic, ?pos:haxe.PosInfos) { public static function _warn(msg:Dynamic, ?origin:String) {
log({level: Warn, message: Std.string(msg), time: 0}, pos); log({
level: Warn,
message: Std.string(msg),
time: 0,
origin: origin
});
} }
public static function error(msg:Dynamic, ?pos:haxe.PosInfos) { public static function _error(msg:Dynamic, ?origin:String) {
log({level: Error, message: Std.string(msg), time: 0}, pos); 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 #if debug
log({level: Debug, message: Std.string(msg), time: 0}, pos); log({
level: Debug,
message: Std.string(msg),
time: 0,
origin: origin
});
#end #end
} }
public static function silly(msg:Dynamic, ?pos:haxe.PosInfos) { public static function _silly(msg:Dynamic, ?origin:String) {
log({level: Silly, message: Std.string(msg), time: 0}, pos); log({
level: Silly,
message: Std.string(msg),
time: 0,
origin: origin
});
} }
private static function log(line:LogLine, ?pos:haxe.PosInfos) { private static function log(line:LogLine) {
line.origin = pos.className;
logLines.push(line); logLines.push(line);
if (logLines.length > MAX_LINES) { if (logLines.length > MAX_LINES) {
@ -60,12 +85,49 @@ class Log {
} }
onLogTrigger.trigger(line); onLogTrigger.trigger(line);
#if webconsole #if (webconsole && !macro)
Debug.printWeb('[${Std.string(line.level)}][${line.origin}] ${line.message}'); Debug.logToWebconsole(line);
#end #end
} }
public static function getLines():ReadOnlyArray<LogLine> { public static function getLines():ReadOnlyArray<LogLine> {
return logLines; 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()});
}
}
} }

View File

@ -1,7 +1,7 @@
package lib; package lib;
import kernel.http.HTTPRequest.Http;
import lua.TableTools; import lua.TableTools;
import kernel.KernelEvents;
import kernel.log.Log; import kernel.log.Log;
import lua.NativeStringTools; import lua.NativeStringTools;
import lib.ui.Canvas; import lib.ui.Canvas;
@ -9,6 +9,7 @@ import cc.ComputerCraft;
#if webconsole #if webconsole
import cc.HTTP; import cc.HTTP;
import kernel.net.Net; import kernel.net.Net;
import kernel.log.LogLine;
#end #end
class Debug { class Debug {
@ -66,8 +67,8 @@ class Debug {
#end #end
#if webconsole #if webconsole
public static function printWeb(msg:String) { public static function logToWebconsole(line:LogLine) {
HTTP.request("http://127.0.0.1:8080/" + Net.networkID, msg); Http.request('http://127.0.0.1:8080/log/${Net.networkID}/${line.level.getIndex()}', '[${line.origin}] ${line.message}').eager();
} }
#end #end
} }

View File

@ -6,6 +6,41 @@ function time() {
return `${now.getHours().toString().padStart(2, "0")}:${now.getMinutes().toString().padStart(2, 0)}:${now.getSeconds().toString().padStart(2, 0)}`; return `${now.getHours().toString().padStart(2, "0")}:${now.getMinutes().toString().padStart(2, 0)}:${now.getSeconds().toString().padStart(2, 0)}`;
} }
const Reset = "\x1b[0m";
const FgBlack = "\x1b[30m"
const FgRed = "\x1b[31m"
const FgGreen = "\x1b[32m"
const FgYellow = "\x1b[33m"
const FgBlue = "\x1b[34m"
const FgMagenta = "\x1b[35m"
const FgCyan = "\x1b[36m"
const FgWhite = "\x1b[37m"
const FgGray = "\x1b[90m"
function log(id, lvl, msg) {
var logLvl = "UNKN";
switch (lvl) {
case "0":
logLvl = "INFO";
break;
case "1":
logLvl = FgYellow + "WARN" + Reset;
break;
case "2":
logLvl = FgRed + "ERRO" + Reset;
break;
case "3":
logLvl = FgGray + "DEBG" + Reset;
break;
case "4":
logLvl = FgWhite + "SILY" + Reset;
break;
}
console.log(`(${id})[${logLvl}]${msg}`);
}
const server = http.createServer((req, res) => { const server = http.createServer((req, res) => {
if (req.method != "POST") { if (req.method != "POST") {
@ -13,7 +48,9 @@ const server = http.createServer((req, res) => {
return; return;
} }
var id = req.url.substring(1); var urlParts = req.url.split("/");
var id = urlParts[2];
var lvl = urlParts[3];
let data = ""; let data = "";
@ -22,11 +59,11 @@ const server = http.createServer((req, res) => {
}) })
req.on('end', () => { req.on('end', () => {
console.log(`[${time()}][${id}]${data}`); log(id, lvl, data);
res.writeHead(200); res.writeHead(200);
res.end(); res.end();
}) })
}); });
console.log("Listening on port 8080") console.log("Webconsole running on port 8080");
server.listen(8080); server.listen(8080);