59 lines
1.5 KiB
D
59 lines
1.5 KiB
D
module angel.utils.logging;
|
|
|
|
// Internal imports
|
|
import angel.config : config;
|
|
import angel.utils.constants;
|
|
// External imports
|
|
import std.stdio;
|
|
import std.datetime;
|
|
import std.file;
|
|
import std.format;
|
|
import std.string;
|
|
|
|
enum LogLevel {
|
|
Debug,
|
|
Event,
|
|
Warning,
|
|
Error
|
|
}
|
|
|
|
class Logger {
|
|
private static string getTimestamp() {
|
|
auto now = Clock.currTime();
|
|
return format!"%04d-%02d-%02d %02d:%02d:%02d"(
|
|
now.year, now.month, now.day,
|
|
now.hour, now.minute, now.second
|
|
);
|
|
}
|
|
|
|
private static string getLogSymbol(LogLevel level) {
|
|
switch (level) {
|
|
case LogLevel.Debug: return "[*]";
|
|
case LogLevel.Event: return "[i]";
|
|
case LogLevel.Warning: return "[!]";
|
|
case LogLevel.Error: return "[E]";
|
|
default: return "[?]";
|
|
}
|
|
}
|
|
|
|
private static void writeToFile(string message) {
|
|
auto file = File(Constants.logFilePath, "a");
|
|
file.write(message ~ "\n");
|
|
file.close();
|
|
}
|
|
|
|
public static void log(LogLevel level, string message) {
|
|
auto timestamp = getTimestamp();
|
|
auto symbol = getLogSymbol(level);
|
|
|
|
auto logMessage = timestamp ~ " " ~ symbol ~ " " ~ message;
|
|
|
|
writeToFile(logMessage);
|
|
if (level == LogLevel.Debug && !config.debug_mode) {
|
|
return;
|
|
} else {
|
|
writeln(logMessage);
|
|
}
|
|
}
|
|
}
|