forked from dlang/singlog
linux version devel
This commit is contained in:
parent
493942396d
commit
43302ab0ea
152
source/singlog.d
152
source/singlog.d
|
@ -19,7 +19,9 @@ alias log = Log.msg;
|
||||||
|
|
||||||
---
|
---
|
||||||
// Setting the name of the logged program
|
// Setting the name of the logged program
|
||||||
log.name("My program");
|
log.program("My program");
|
||||||
|
// Setting the status of color text output
|
||||||
|
log.color(true);
|
||||||
// Setting the error output level
|
// Setting the error output level
|
||||||
log.level(log.DEBUGGING);
|
log.level(log.DEBUGGING);
|
||||||
log.level(log.ALERT);
|
log.level(log.ALERT);
|
||||||
|
@ -46,24 +48,18 @@ alias log = Log.msg;
|
||||||
+/
|
+/
|
||||||
class Log {
|
class Log {
|
||||||
private:
|
private:
|
||||||
static Log log;
|
static Log _log;
|
||||||
string path;
|
string _path;
|
||||||
string nameProgram = "singlog";
|
string _name = "singlog";
|
||||||
bool writeToFile = true;
|
bool _writeToFile = true;
|
||||||
|
bool _ccolor = false;
|
||||||
|
|
||||||
this() {}
|
this() {}
|
||||||
|
|
||||||
version(Windows) {
|
version(Windows) {
|
||||||
public enum {
|
int[] _sysLevel = [0, 1, 1, 1, 2, 3, 4];
|
||||||
DEBUGGING = 0,
|
|
||||||
ALERT = 1,
|
WORD[] _sysLevelOS = [
|
||||||
CRITICAL = 1,
|
|
||||||
ERROR = 1,
|
|
||||||
WARNING = 2,
|
|
||||||
NOTICE = 3,
|
|
||||||
INFORMATION = 3,
|
|
||||||
}
|
|
||||||
WORD[] sysLevel = [
|
|
||||||
EVENTLOG_SUCCESS,
|
EVENTLOG_SUCCESS,
|
||||||
EVENTLOG_ERROR_TYPE,
|
EVENTLOG_ERROR_TYPE,
|
||||||
EVENTLOG_WARNING_TYPE,
|
EVENTLOG_WARNING_TYPE,
|
||||||
|
@ -80,6 +76,38 @@ version(Windows) {
|
||||||
DeregisterEventSource(handleEventLog);
|
DeregisterEventSource(handleEventLog);
|
||||||
}
|
}
|
||||||
} else version(Posix) {
|
} else version(Posix) {
|
||||||
|
int[] _sysLevel = [0, 1, 2, 3, 4, 5, 6];
|
||||||
|
|
||||||
|
int[] _sysLevelOS = [
|
||||||
|
LOG_DEBUG,
|
||||||
|
LOG_ALERT,
|
||||||
|
LOG_CRIT,
|
||||||
|
LOG_ERR,
|
||||||
|
LOG_WARNING,
|
||||||
|
LOG_NOTICE,
|
||||||
|
LOG_INFO
|
||||||
|
];
|
||||||
|
|
||||||
|
string[] _color = [
|
||||||
|
"\x1b[1;32m%s\x1b[0;32m %s\x1b[0;0m", // green
|
||||||
|
"\x1b[1;34m%s\x1b[0;34m %s\x1b[0;0m", // blue
|
||||||
|
"\x1b[1;35m%s\x1b[0;35m %s\x1b[0;0m", // magenta
|
||||||
|
"\x1b[1;31m%s\x1b[0;31m %s\x1b[0;0m", // red
|
||||||
|
"\x1b[1;33m%s\x1b[0;33m %s\x1b[0;0m", // yellow
|
||||||
|
"\x1b[1;36m%s\x1b[0;36m %s\x1b[0;0m", // cyan
|
||||||
|
"\x1b[1;97m%s\x1b[0;97m %s\x1b[0;0m", // white
|
||||||
|
];
|
||||||
|
|
||||||
|
void writestdout(string time, string message, int level) {
|
||||||
|
writeln("%s %s".format(time,
|
||||||
|
this._ccolor ?
|
||||||
|
this._color[level].format(this._type[level], message) :
|
||||||
|
"%s %s".format(this._type[level], message)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public enum {
|
public enum {
|
||||||
DEBUGGING = 0,
|
DEBUGGING = 0,
|
||||||
ALERT = 1,
|
ALERT = 1,
|
||||||
|
@ -89,25 +117,15 @@ version(Windows) {
|
||||||
NOTICE = 5,
|
NOTICE = 5,
|
||||||
INFORMATION = 6
|
INFORMATION = 6
|
||||||
}
|
}
|
||||||
int[] sysLevel = [
|
|
||||||
LOG_DEBUG,
|
|
||||||
LOG_ALERT,
|
|
||||||
LOG_CRIT,
|
|
||||||
LOG_ERR,
|
|
||||||
LOG_WARNING,
|
|
||||||
LOG_NOTICE,
|
|
||||||
LOG_INFO
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
string[] color = [
|
string[] _type = [
|
||||||
"\u001b[2;30m[DEBUG]:\u001b[0;30m %s\u001b[0;0m",
|
"[DEBUG]:",
|
||||||
"\u001b[1;34m[ALERT]:\u001b[0;34m %s\u001b[0;0m",
|
"[ALERT]:",
|
||||||
"\u001b[1;35m[CRITICAL]:\u001b[0;35m %s\u001b[0;0m",
|
"[CRITICAL]:",
|
||||||
"\u001b[1;31m[ERROR]:\u001b[0;31m %s\u001b[0;0m",
|
"[ERROR]:",
|
||||||
"\u001b[1;33m[WARNING]:\u001b[0;33m %s\u001b[0;0m",
|
"[WARNING]:",
|
||||||
"\u001b[1;36m[NOTICE]:\u001b[0;36m %s\u001b[0;0m",
|
"[NOTICE]:",
|
||||||
"\u001b[1;32m[INFO]:\u001b[0;32m %s\u001b[0;0m",
|
"[INFO]:"
|
||||||
];
|
];
|
||||||
|
|
||||||
public enum {
|
public enum {
|
||||||
|
@ -116,48 +134,49 @@ version(Windows) {
|
||||||
FILE = 4
|
FILE = 4
|
||||||
}
|
}
|
||||||
|
|
||||||
int msgOutput = STDOUT;
|
int _output = STDOUT;
|
||||||
int msgLevel = INFORMATION;
|
int _level = INFORMATION;
|
||||||
|
|
||||||
void writeLog(string message, int msgLevel) {
|
void writeLog(string message, int level) {
|
||||||
if (this.msgLevel > msgLevel)
|
string time;
|
||||||
|
if (this._level > level)
|
||||||
return;
|
return;
|
||||||
if (this.msgOutput & 1)
|
if (this._output & 1)
|
||||||
syslog(sysLevel[msgLevel], message.toStringz());
|
syslog(_sysLevelOS[_sysLevel[level]], message.toStringz());
|
||||||
if (this.msgOutput & 6)
|
if (this._output & 6)
|
||||||
message = Clock.currTime().format("%Y.%m.%d %H:%M:%S ") ~ color[msgLevel].format(message);
|
time = Clock.currTime().format("%Y.%m.%d %H:%M:%S");
|
||||||
if (this.msgOutput & 2)
|
if (this._output & 2)
|
||||||
writeln(message);
|
writestdout(time, message, level);
|
||||||
if (this.msgOutput & 4)
|
if (this._output & 4)
|
||||||
writeFile(message);
|
writeFile(time, message, level);
|
||||||
}
|
}
|
||||||
|
|
||||||
void writeFile(string message) {
|
void writeFile(string time, string message, int level) {
|
||||||
if (!this.writeToFile)
|
if (!this._writeToFile)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (!this.path.exists) {
|
if (!this._path.exists) {
|
||||||
this.writeToFile = false;
|
this._writeToFile = false;
|
||||||
this.warning("The log file does not exist: " ~ this.path);
|
this.warning("The log file does not exist: " ~ this._path);
|
||||||
}
|
}
|
||||||
|
|
||||||
File file;
|
File file;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
file = File(this.path, "a+");
|
file = File(this._path, "a+");
|
||||||
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.error("Unable to open the log file " ~ this._path);
|
||||||
this.information(e);
|
this.information(e);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
file.writeln(message);
|
file.writeln("%s %s %s".format(time, this._type[level], 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.error("Unable to write to the log file " ~ this._path);
|
||||||
this.information(e);
|
this.information(e);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -165,8 +184,8 @@ version(Windows) {
|
||||||
try {
|
try {
|
||||||
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.error("Unable to close the log file " ~ this._path);
|
||||||
this.information(e);
|
this.information(e);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -174,16 +193,17 @@ version(Windows) {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
@property static Log msg() {
|
@property static Log msg() {
|
||||||
if (this.log is null)
|
if (this._log is null)
|
||||||
this.log = new Log;
|
this._log = new Log;
|
||||||
|
|
||||||
return this.log;
|
return this._log;
|
||||||
}
|
}
|
||||||
|
|
||||||
Log output(int msgOutput) { this.msgOutput = msgOutput; return this.log; }
|
Log output(int outs) { this._output = outs; return this._log; }
|
||||||
Log name(string nameProgram) { this.nameProgram = nameProgram; return this.log; }
|
Log program(string name) { this._name = name; 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 msgLevel) { this.msgLevel = msgLevel; return this.log; }
|
Log level(int lvl) { this._level = lvl; return this._log; }
|
||||||
|
Log color(bool condition) { this._ccolor = condition; return this._log; }
|
||||||
|
|
||||||
void alert(T)(T message) { writeLog(message.to!string, ALERT); }
|
void alert(T)(T message) { writeLog(message.to!string, ALERT); }
|
||||||
void critical(T)(T message) { writeLog(message.to!string, CRITICAL); }
|
void critical(T)(T message) { writeLog(message.to!string, CRITICAL); }
|
||||||
|
|
22
tests/test.d
22
tests/test.d
|
@ -2,20 +2,16 @@ 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.SYSLOG | log.STDOUT | log.FILE) // write to syslog, standard output stream and file
|
||||||
.name(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.DEBUGGING) // logging level
|
||||||
|
.color(true) // color text output
|
||||||
.file("./test.log"); // the path to the log file
|
.file("./test.log"); // the path to the log file
|
||||||
|
|
||||||
// log.e("This is an error message");
|
log.i("This is an information message");
|
||||||
// log.error("And this is also an error message");
|
log.n("This is a notice message");
|
||||||
// log.w("This is a warning message");
|
log.w("This is a warning message");
|
||||||
// log.i("This is an information message");
|
log.e("This is an error message");
|
||||||
|
log.c("This is a critical message");
|
||||||
log.i("information");
|
log.a("This is an alert message");
|
||||||
log.n("notice");
|
log.d("This is a debug message");
|
||||||
log.w("warning");
|
|
||||||
log.e("error");
|
|
||||||
log.c("critical");
|
|
||||||
log.a("alert");
|
|
||||||
log.d("debug");
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue