From 493942396dce48bb3e30cd978c210e81e6ea1bfc Mon Sep 17 00:00:00 2001 From: Alexander Zhirov Date: Mon, 5 Jun 2023 21:33:02 +0300 Subject: [PATCH] dev color --- source/singlog.d | 28 +++++++++++++++++++--------- tests/test.d | 16 ++++++++++++---- 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/source/singlog.d b/source/singlog.d index 0202885..d4667a2 100644 --- a/source/singlog.d +++ b/source/singlog.d @@ -100,6 +100,16 @@ version(Windows) { ]; } + string[] color = [ + "\u001b[2;30m[DEBUG]:\u001b[0;30m %s\u001b[0;0m", + "\u001b[1;34m[ALERT]:\u001b[0;34m %s\u001b[0;0m", + "\u001b[1;35m[CRITICAL]:\u001b[0;35m %s\u001b[0;0m", + "\u001b[1;31m[ERROR]:\u001b[0;31m %s\u001b[0;0m", + "\u001b[1;33m[WARNING]:\u001b[0;33m %s\u001b[0;0m", + "\u001b[1;36m[NOTICE]:\u001b[0;36m %s\u001b[0;0m", + "\u001b[1;32m[INFO]:\u001b[0;32m %s\u001b[0;0m", + ]; + public enum { SYSLOG = 1, STDOUT = 2, @@ -109,13 +119,13 @@ version(Windows) { int msgOutput = STDOUT; int msgLevel = INFORMATION; - void writeLog(string message, string type, int msgLevel) { + void writeLog(string message, int msgLevel) { if (this.msgLevel > msgLevel) return; if (this.msgOutput & 1) syslog(sysLevel[msgLevel], message.toStringz()); if (this.msgOutput & 6) - message = format("%s [%s]: %s", Clock.currTime().format("%Y.%m.%d %H:%M:%S"), type, message); + message = Clock.currTime().format("%Y.%m.%d %H:%M:%S ") ~ color[msgLevel].format(message); if (this.msgOutput & 2) writeln(message); if (this.msgOutput & 4) @@ -175,13 +185,13 @@ public: Log file(string path) { this.path = path; return this.log; } Log level(int msgLevel) { this.msgLevel = msgLevel; return this.log; } - void alert(T)(T message) { writeLog(message.to!string, "ALERT", ALERT); } - void critical(T)(T message) { writeLog(message.to!string, "CRITICAL", CRITICAL); } - void error(T)(T message) { writeLog(message.to!string, "ERROR", ERROR); } - void warning(T)(T message) { writeLog(message.to!string, "WARNING", WARNING); } - void notice(T)(T message) { writeLog(message.to!string, "NOTICE", NOTICE); } - void information(T)(T message) { writeLog(message.to!string, "INFORMATION", INFORMATION); } - void debugging(T)(T message) { writeLog(message.to!string, "DEBUGGING", DEBUGGING); } + void alert(T)(T message) { writeLog(message.to!string, ALERT); } + void critical(T)(T message) { writeLog(message.to!string, CRITICAL); } + void error(T)(T message) { writeLog(message.to!string, ERROR); } + void warning(T)(T message) { writeLog(message.to!string, WARNING); } + void notice(T)(T message) { writeLog(message.to!string, NOTICE); } + void information(T)(T message) { writeLog(message.to!string, INFORMATION); } + void debugging(T)(T message) { writeLog(message.to!string, DEBUGGING); } alias a = alert; alias c = critical; diff --git a/tests/test.d b/tests/test.d index d272174..fd6b052 100644 --- a/tests/test.d +++ b/tests/test.d @@ -6,8 +6,16 @@ void main(string[] argv) { .level(log.DEBUGGING) // logging level .file("./test.log"); // the path to the log file - log.e("This is an error message"); - log.error("And this is also an error message"); - log.w("This is a warning message"); - log.i("This is an information message"); + // log.e("This is an error message"); + // log.error("And this is also an error message"); + // log.w("This is a warning message"); + // log.i("This is an information message"); + + log.i("information"); + log.n("notice"); + log.w("warning"); + log.e("error"); + log.c("critical"); + log.a("alert"); + log.d("debug"); }