From d354a60dbc8eca546a76d9f1e0184a784a8bf263 Mon Sep 17 00:00:00 2001 From: Alexander Zhirov Date: Tue, 18 Jul 2023 21:01:43 +0300 Subject: [PATCH] 0.5.0-dev --- source/singlog.d | 99 +++++++++++++++++++++++++++++++++++++++++++----- tests/test.d | 7 +++- 2 files changed, 94 insertions(+), 12 deletions(-) diff --git a/source/singlog.d b/source/singlog.d index 5caa979..627836e 100644 --- a/source/singlog.d +++ b/source/singlog.d @@ -114,6 +114,13 @@ version(Windows) { defaultTextOutput(time, wMessage, priority); } + // void writestderr(string time, string message, int priority) { + // wstring wMessage = " %s\n".format(message).to!wstring; + // this._ccolor ? + // colorTextOutput(time, wMessage, priority) : + // defaultTextOutput(time, wMessage, priority); + // } + } else version(Posix) { int[] _sysPriority = [0, 1, 2, 3, 4, 5, 6]; @@ -144,6 +151,14 @@ version(Windows) { ); } + void writestderr(string time, string message, int priority) { + // stderr.writefln("%s %s %s", time, this._type[priority], message); + stderr.writefln("%s %s", + time, + (this._ccolor ? this._color[priority] : "%s %s").format(this._type[priority], message) + ); + } + void writesyslog(string message, int priority) { syslog(priority, message.toStringz()); } @@ -172,23 +187,29 @@ version(Windows) { public enum { SYSLOG = 1, STDOUT = 2, - FILE = 4 + STDERR = 4, + FILE = 8 } + int _nowoutput = 0; int _output = STDOUT; int _priority = INFORMATION; void writelog(string message, int priority) { string time; + int output = this._nowoutput ? this._nowoutput : this._output; + this._nowoutput = 0; if (this._priority > priority) return; - if (this._output & 1) + if (output & 1) writesyslog(message, _sysPriorityOS[_sysPriority[priority]]); - if (this._output & 6) + if (output & 14) time = Clock.currTime().format("%Y.%m.%d %H:%M:%S"); - if (this._output & 2) + if (output & 2 && priority >= WARNING) writestdout(time, message, priority); - if (this._output & 4) + if (output & 4 && priority <= ERROR) + writestderr(time, message, priority); + if (output & 8) writefile(time, message, priority); } @@ -208,7 +229,7 @@ version(Windows) { this._writeToFile = true; } catch (Exception e) { this._writeToFile = false; - this.error("Unable to open the log file " ~ this._path); + this.now(output.stderr).error("Unable to open the log file " ~ this._path); this.information(e); return; } @@ -217,7 +238,7 @@ version(Windows) { file.writefln("%s %s %s", time, this._type[priority], message); } catch (Exception e) { this._writeToFile = false; - this.error("Unable to write to the log file " ~ this._path); + this.now(output.stderr).error("Unable to write to the log file " ~ this._path); this.information(e); return; } @@ -226,12 +247,65 @@ version(Windows) { file.close(); } catch (Exception e) { this._writeToFile = false; - this.error("Unable to close the log file " ~ this._path); + this.now(output.stderr).error("Unable to close the log file " ~ this._path); this.information(e); return; } } - + + struct Output { + int _output = STDOUT; + int _newoutput = 0; + + int output() { return this._newoutput ? this._newoutput : this._output; } + public: + Output syslog() { this._newoutput |= SYSLOG; return this; } + Output stdout() { this._newoutput |= STDOUT; return this; } + Output stderr() { this._newoutput |= STDERR; return this; } + Output file() { this._newoutput |= FILE; return this; } + } + + struct Level { + public: + int debugging() { return DEBUGGING; } + int alert() { return ALERT; } + int critical() { return CRITICAL; } + int error() { return ERROR; } + int warning() { return WARNING; } + int notice() { return NOTICE; } + int information() { return INFORMATION; } + + alias d = debugging; + alias a = alert; + alias c = critical; + alias e = error; + alias w = warning; + alias n = notice; + alias i = information; + } + + struct Now { + this(Output outs) { + _log._nowoutput = outs.output(); + } + + public: + void alert(T)(T message) { _log.alert(message); } + void critical(T)(T message) { _log.critical(message); } + void error(T)(T message) { _log.error(message); } + void warning(T)(T message) { _log.warning(message); } + void notice(T)(T message) { _log.notice(message); } + void information(T)(T message) { _log.information(message); } + void debugging(T)(T message) { _log.debugging(message); } + + alias a = alert; + alias c = critical; + alias e = error; + alias w = warning; + alias n = notice; + alias i = information; + alias d = debugging; + } public: @property static Log msg() { if (this._log is null) @@ -240,7 +314,12 @@ public: return this._log; } - Log output(int outs) { this._output = outs; return this._log; } + Output output() { return Output(); } + Level level() { return Level(); } + Now now(Output outs) { return Now(outs); } + + Log output(Output outs) { this._output = outs.output(); return this._log; } + deprecated Log output(int outs) { this._output = outs; return this._log; } Log program(string name) { this._name = name.to!wstring; return this._log; } Log file(string path) { this._path = path; return this._log; } Log level(int priority) { this._priority = priority; return this._log; } diff --git a/tests/test.d b/tests/test.d index d3c46aa..3df1929 100644 --- a/tests/test.d +++ b/tests/test.d @@ -1,9 +1,9 @@ import singlog; void main(string[] argv) { - log.output(log.SYSLOG | log.STDOUT | log.FILE) // write to syslog, standard output stream and file + log.output(log.output.syslog.stderr.file) // write to syslog, standard error stream and file .program(argv[0]) // program name as an identifier (for Windows OS) - .level(log.DEBUGGING) // logging level + .level(log.level.debugging) // logging level .color(true) // color text output .file("./test.log"); // the path to the log file @@ -14,4 +14,7 @@ void main(string[] argv) { log.c("This is a critical message"); log.a("This is an alert message"); log.d("This is a debug message"); + + log.now(log.output.stdout).n("This error message will only be written to the standard output stream"); + log.now(log.output.syslog.file).c("This error message will only be written to the syslog and file"); }