38 lines
1.1 KiB
D
38 lines
1.1 KiB
D
|
module utils.log;
|
||
|
|
||
|
import std.stdio;
|
||
|
import std.format;
|
||
|
import std.conv;
|
||
|
import core.time;
|
||
|
import std.typecons;
|
||
|
|
||
|
import grimoire;
|
||
|
|
||
|
enum LogLevel {
|
||
|
INFOLOW, // Reserved for hot loop logging
|
||
|
INFOMED, // Reserved for loop repeated events
|
||
|
INFOHIGH, // Reserved for non loop rare events
|
||
|
DEBUG, // Reserved for sensitive events that may be bugs
|
||
|
WARN, // Reserved for pending bugs
|
||
|
ERROR // Reserved for bugs that may cause a crash in hot loop, should be guarded
|
||
|
}
|
||
|
|
||
|
enum CurrentLogLevel = LogLevel.INFOLOW;
|
||
|
|
||
|
public void log(LogLevel lvl, string data) {
|
||
|
static Nullable!MonoTime start;
|
||
|
if(start.isNull) start = MonoTime.currTime;
|
||
|
if(lvl >= CurrentLogLevel) writeln(format("%7.6f : %s : %s",(MonoTime.currTime - start.get).total!("hnsecs")/10_000_000.0, std.conv.to!string(lvl), data));
|
||
|
}
|
||
|
|
||
|
public void grLog(GrCall call) {
|
||
|
log(call.getEnum!LogLevel(0), "Gr> " ~ call.getString(1));
|
||
|
}
|
||
|
|
||
|
public GrLibrary getLogLibrary() {
|
||
|
auto lib = new GrLibrary();
|
||
|
auto loglvl_t = lib.addEnum("LogLevel", ["INFOLOW", "INFOMED", "INFOHIGH", "DEBUG", "WARN", "ERROR"]);
|
||
|
lib.addFunction(&grLog, "log", [loglvl_t, grString]);
|
||
|
return lib;
|
||
|
}
|