0.5.0-dev

This commit is contained in:
Alexander Zhirov 2023-07-18 21:01:43 +03:00
parent f9b4718549
commit d354a60dbc
2 changed files with 94 additions and 12 deletions

View File

@ -114,6 +114,13 @@ version(Windows) {
defaultTextOutput(time, wMessage, priority); 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) { } else version(Posix) {
int[] _sysPriority = [0, 1, 2, 3, 4, 5, 6]; 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) { void writesyslog(string message, int priority) {
syslog(priority, message.toStringz()); syslog(priority, message.toStringz());
} }
@ -172,23 +187,29 @@ version(Windows) {
public enum { public enum {
SYSLOG = 1, SYSLOG = 1,
STDOUT = 2, STDOUT = 2,
FILE = 4 STDERR = 4,
FILE = 8
} }
int _nowoutput = 0;
int _output = STDOUT; int _output = STDOUT;
int _priority = INFORMATION; int _priority = INFORMATION;
void writelog(string message, int priority) { void writelog(string message, int priority) {
string time; string time;
int output = this._nowoutput ? this._nowoutput : this._output;
this._nowoutput = 0;
if (this._priority > priority) if (this._priority > priority)
return; return;
if (this._output & 1) if (output & 1)
writesyslog(message, _sysPriorityOS[_sysPriority[priority]]); writesyslog(message, _sysPriorityOS[_sysPriority[priority]]);
if (this._output & 6) if (output & 14)
time = Clock.currTime().format("%Y.%m.%d %H:%M:%S"); time = Clock.currTime().format("%Y.%m.%d %H:%M:%S");
if (this._output & 2) if (output & 2 && priority >= WARNING)
writestdout(time, message, priority); writestdout(time, message, priority);
if (this._output & 4) if (output & 4 && priority <= ERROR)
writestderr(time, message, priority);
if (output & 8)
writefile(time, message, priority); writefile(time, message, priority);
} }
@ -208,7 +229,7 @@ version(Windows) {
this._writeToFile = true; this._writeToFile = true;
} catch (Exception e) { } catch (Exception e) {
this._writeToFile = false; 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); this.information(e);
return; return;
} }
@ -217,7 +238,7 @@ version(Windows) {
file.writefln("%s %s %s", time, this._type[priority], message); file.writefln("%s %s %s", time, this._type[priority], message);
} catch (Exception e) { } catch (Exception e) {
this._writeToFile = false; 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); this.information(e);
return; return;
} }
@ -226,12 +247,65 @@ version(Windows) {
file.close(); file.close();
} catch (Exception e) { } catch (Exception e) {
this._writeToFile = false; 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); this.information(e);
return; 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: public:
@property static Log msg() { @property static Log msg() {
if (this._log is null) if (this._log is null)
@ -240,7 +314,12 @@ public:
return this._log; 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 program(string name) { this._name = name.to!wstring; return this._log; }
Log file(string path) { this._path = path; return this._log; } Log file(string path) { this._path = path; return this._log; }
Log level(int priority) { this._priority = priority; return this._log; } Log level(int priority) { this._priority = priority; return this._log; }

View File

@ -1,9 +1,9 @@
import singlog; import singlog;
void main(string[] argv) { 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) .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 .color(true) // color text output
.file("./test.log"); // the path to the log file .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.c("This is a critical message");
log.a("This is an alert message"); log.a("This is an alert message");
log.d("This is a debug 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");
} }