Added initial implementation.

This commit is contained in:
Andrew Lalis 2023-03-02 10:08:47 +01:00
parent 530d02d462
commit 044247da83
6 changed files with 118 additions and 0 deletions

16
.gitignore vendored Normal file
View File

@ -0,0 +1,16 @@
.dub
docs.json
__dummy.html
docs/
/slf4d-dlog
slf4d-dlog.so
slf4d-dlog.dylib
slf4d-dlog.dll
slf4d-dlog.a
slf4d-dlog.lib
slf4d-dlog-test-*
*.exe
*.o
*.obj
*.lst
*.a

13
dub.json Normal file
View File

@ -0,0 +1,13 @@
{
"authors": [
"Andrew Lalis"
],
"copyright": "Copyright © 2023, Andrew Lalis",
"dependencies": {
"dlog": "~>0.2.1",
"slf4d": "~>1.3.0"
},
"description": "DLog provider implementation for SLF4D",
"license": "MIT",
"name": "slf4d-dlog"
}

7
dub.selections.json Normal file
View File

@ -0,0 +1,7 @@
{
"fileVersion": 1,
"versions": {
"dlog": "0.2.1",
"slf4d": "1.3.0"
}
}

View File

@ -0,0 +1,45 @@
module slf4d_dlog.handler;
import slf4d.handler;
import slf4d.logger;
import dlog;
/**
* An SLF4D LogHandler implementation for DLog, which just maps all log calls
* to an internal `dlog.Logger`. Note that because DLog's Logger class does not
* support shared methods (which are needed to ensure safe operation from
* multiple threads), this handler synchronizes calls to handle log messages.
*/
class DLogHandler : LogHandler {
private shared dlog.Logger dlogLogger;
public shared this() {
this(new DefaultLogger());
}
public shared this(dlog.MessageTransform messageTransform) {
// TODO: Implement this once it is done in DLog.
import std.stdio;
stderr.writeln("Warning: Constructing a DLogHandler with a MessageTransform is not supported.");
this(new DefaultLogger());
}
public shared this(dlog.Logger logger) {
this.dlogLogger = cast(shared) logger;
}
public shared void handle(LogMessage msg) {
synchronized(this.dlogLogger) {
auto unsharedLogger = cast(dlog.Logger) this.dlogLogger;
unsharedLogger.log3!(const(string))(
msg.message,
"",
"",
0,
cast(string) msg.sourceContext.moduleName.dup,
"",
cast(string) msg.sourceContext.functionName.dup
);
}
}
}

View File

@ -0,0 +1,7 @@
/**
* This module contains the DLog provider for SLF4D.
*/
module slf4d_dlog;
public import slf4d_dlog.handler;
public import slf4d_dlog.provider;

View File

@ -0,0 +1,30 @@
module slf4d_dlog.provider;
import slf4d.provider;
import slf4d.factory;
import slf4d.level;
import slf4d.default_provider : DefaultLoggerFactory;
import dlog;
import slf4d_dlog.handler;
class DLogProvider : LoggingProvider {
private shared DefaultLoggerFactory loggerFactory;
public shared this(dlog.Logger logger, Level loggingLevel = Levels.INFO) {
this.loggerFactory = new shared DefaultLoggerFactory(
new shared DLogHandler(logger),
loggingLevel
);
}
public shared this(Level loggingLevel = Levels.INFO) {
this.loggerFactory = new shared DefaultLoggerFactory(
new shared DLogHandler(),
loggingLevel
);
}
public shared shared(DefaultLoggerFactory) getLoggerFactory() {
return this.loggerFactory;
}
}