1
0
Fork 0

first test

This commit is contained in:
Alexander Zhirov 2023-04-27 21:08:12 +03:00
parent cc3a1160e0
commit 0e7ee82265
4 changed files with 112 additions and 50 deletions

2
.gitignore vendored
View File

@ -2,3 +2,5 @@
.dub .dub
*.o *.o
lib lib
bin
test.log

View File

@ -10,6 +10,21 @@
"targetType": "library", "targetType": "library",
"targetPath": "lib", "targetPath": "lib",
"targetName": "singlog", "targetName": "singlog",
"configurations": [
{
"name": "library",
"targetType": "library",
"targetPath": "lib"
},
{
"name": "test",
"targetType": "executable",
"targetPath": "bin",
"targetName": "app",
"importPaths": ["source","tests"],
"sourcePaths": ["tests"]
}
],
"dependencies": { "dependencies": {
"datefmt": "~>1.0.4" "datefmt": "~>1.0.4"
} }

View File

@ -1,9 +1,13 @@
module singlog; module singlog;
version(Windows)
import core.sys.windows.windows;
else
import core.sys.posix.syslog; import core.sys.posix.syslog;
import std.string;
import std.stdio; import std.stdio;
import std.conv; import std.conv;
import std.meta;
import std.file; import std.file;
import std.datetime; import std.datetime;
import datefmt; import datefmt;
@ -40,57 +44,89 @@ alias log = Log.msg;
log.debugging("Debugging message"); log.debugging("Debugging message");
--- ---
+/ +/
class Log class Log {
{ private:
private static Log log; static Log log;
private string path; string path;
private bool writeToFile = true; string nameProgram = "singlog";
private static SysTime time; bool writeToFile = true;
// Target output this() {}
enum {
version(Windows) {
public enum {
DEBUGGING = 0,
ALERT = 1,
CRITICAL = 1,
ERROR = 1,
WARNING = 2,
NOTICE = 3,
INFORMATION = 3,
}
WORD[] sysLevel = [
EVENTLOG_SUCCESS,
EVENTLOG_ERROR_TYPE,
EVENTLOG_WARNING_TYPE,
EVENTLOG_INFORMATION_TYPE
];
void syslog(WORD priority, LPCSTR message) {
HANDLE handleEventLog = RegisterEventSourceA(NULL, this.nameProgram.toStringz());
if (handleEventLog == NULL)
return;
ReportEventA(handleEventLog, priority, 0, 0, NULL, 1, 0, &message, NULL);
DeregisterEventSource(handleEventLog);
}
} else version(Posix) {
public enum {
DEBUGGING = 0,
ALERT = 1,
CRITICAL = 2,
ERROR = 3,
WARNING = 4,
NOTICE = 5,
INFORMATION = 6
}
int[] sysLevel = [
LOG_DEBUG,
LOG_ALERT,
LOG_CRIT,
LOG_ERR,
LOG_WARNING,
LOG_NOTICE,
LOG_INFO
];
}
public enum {
SYSLOG = 1, SYSLOG = 1,
STDOUT = 2, STDOUT = 2,
FILE = 4 FILE = 4
} }
// Message output level
enum {
DEBUG = 0,
CRIT = 1,
ERR = 2,
WARNING = 3,
NOTICE = 4,
INFO = 5,
ALERT = 6
}
int msgOutput = STDOUT; int msgOutput = STDOUT;
int msgLevel = INFO; int msgLevel = INFORMATION;
private this() {} void writeLog(string message, int msgLevel) {
private void writeLog(string message, int msgLevel, int priority)
{
if (this.msgLevel > msgLevel) if (this.msgLevel > msgLevel)
return; return;
if (this.msgOutput & 1) if (this.msgOutput & 1)
syslog(priority, (message ~ "\0").ptr); syslog(sysLevel[msgLevel], message.toStringz());
if (this.msgOutput & 2) if (this.msgOutput & 2)
writeln(message); writeln(message);
if (this.msgOutput & 4) if (this.msgOutput & 4)
writeFile(message); writeFile(message);
} }
private void writeFile(string message) void writeFile(string message) {
{
if (!this.writeToFile) if (!this.writeToFile)
return; return;
if (this.path.exists) if (this.path.exists)
this.writeToFile = true; this.writeToFile = true;
else else {
{
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);
} }
@ -103,16 +139,16 @@ class Log
} 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.critical(e); this.information(e);
return; return;
} }
try { try {
file.writeln(this.time.format("%Y.%m.%d %H:%M:%S: ") ~ message); file.writeln(Clock.currTime().format("%Y.%m.%d %H:%M:%S: ") ~ 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.critical(e); this.information(e);
return; return;
} }
@ -121,33 +157,31 @@ class Log
} 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.critical(e); this.information(e);
return; return;
} }
} }
@property static Log msg() public:
{ @property static Log msg() {
if (this.log is null) if (this.log is null)
{
this.log = new Log; this.log = new Log;
this.time = Clock.currTime();
}
return this.log; return this.log;
} }
void output(int msgOutput) { this.msgOutput = msgOutput; } Log output(int msgOutput) { this.msgOutput = msgOutput; return this.log; }
void level(int msgLevel) { this.msgLevel = msgLevel; } Log name(string nameProgram) { this.nameProgram = nameProgram; return this.log; }
void file(string path) { this.path = path; } 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, LOG_ALERT); } void alert(T)(T message) { writeLog(message.to!string, ALERT); }
void critical(T)(T message) { writeLog(message.to!string, CRIT, LOG_CRIT); } void critical(T)(T message) { writeLog(message.to!string, CRITICAL); }
void error(T)(T message) { writeLog(message.to!string, ERR, LOG_ERR); } void error(T)(T message) { writeLog(message.to!string, ERROR); }
void warning(T)(T message) { writeLog(message.to!string, WARNING, LOG_WARNING); } void warning(T)(T message) { writeLog(message.to!string, WARNING); }
void notice(T)(T message) { writeLog(message.to!string, NOTICE, LOG_NOTICE); } void notice(T)(T message) { writeLog(message.to!string, NOTICE); }
void information(T)(T message) { writeLog(message.to!string, INFO, LOG_INFO); } void information(T)(T message) { writeLog(message.to!string, INFORMATION); }
void debugging(T)(T message) {writeLog(message.to!string, DEBUG, LOG_DEBUG); } void debugging(T)(T message) { writeLog(message.to!string, DEBUG); }
alias a = alert; alias a = alert;
alias c = critical; alias c = critical;

11
tests/test.d Normal file
View File

@ -0,0 +1,11 @@
import singlog;
void main(string[] argv) {
log.output(log.SYSLOG | log.STDOUT | log.FILE)
.name(argv[0])
.level(Log.DEBUGGING)
.file("./test.log");
log.e("hello!");
log.w("hello!");
log.i("hello!");
}