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;
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()});
}
}
}

View File

@ -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
}

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)}`;
}
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) => {
if (req.method != "POST") {
@ -13,7 +48,9 @@ const server = http.createServer((req, res) => {
return;
}
var id = req.url.substring(1);
var urlParts = req.url.split("/");
var id = urlParts[2];
var lvl = urlParts[3];
let data = "";
@ -22,11 +59,11 @@ const server = http.createServer((req, res) => {
})
req.on('end', () => {
console.log(`[${time()}][${id}]${data}`);
log(id, lvl, data);
res.writeHead(200);
res.end();
})
});
console.log("Listening on port 8080")
console.log("Webconsole running on port 8080");
server.listen(8080);