From 4f20086480ce27db439df7af1f3284ae88ca9422 Mon Sep 17 00:00:00 2001 From: Andrew Lalis Date: Wed, 1 Mar 2023 23:00:12 +0100 Subject: [PATCH] Fixed testing provider constructors. --- source/slf4d/handler.d | 32 ++++++++++++++++---------------- source/slf4d/testing_provider.d | 17 +++++++++++++++-- 2 files changed, 31 insertions(+), 18 deletions(-) diff --git a/source/slf4d/handler.d b/source/slf4d/handler.d index fe532ed..3dc4163 100644 --- a/source/slf4d/handler.d +++ b/source/slf4d/handler.d @@ -1,4 +1,4 @@ -/** +/** * The handler module defines the `LogHandler` interface, as well as several * simple handler implementations that can be used or extended by SLF4D * providers. @@ -8,7 +8,7 @@ module slf4d.handler; import slf4d.logger : LogMessage; import slf4d.level; -/** +/** * The interface for any component that consumes log messages generated by a * `Logger`. Only messages whose level is greater than or equal to the logger's * level will be sent to handlers. For example, a Logger configured at an INFO @@ -31,7 +31,7 @@ shared interface LogHandler { shared void handle(LogMessage msg); } -/** +/** * A log handler that discards all messages. Useful for testing. */ class DiscardingLogHandler : LogHandler { @@ -40,17 +40,17 @@ class DiscardingLogHandler : LogHandler { } } -/** +/** * A log handler that simply appends all messages it receives to an internal * array. This can be useful for testing. */ synchronized class CachingLogHandler : LogHandler { - /** + /** * The internal cache of messages. */ private shared LogMessage[] messages; - /** + /** * "Handles" a log message by appending it to this handler's internal list * of messages, for later inspection. * Params: @@ -60,14 +60,14 @@ synchronized class CachingLogHandler : LogHandler { this.messages ~= msg; } - /** + /** * Resets this handler's internal message cache to an empty list. */ public shared void reset() { this.messages = []; } - /** + /** * Gets an immutable duplication of the list of messages this handler has * handled since the last `reset()` call. * Returns: The list of messages. @@ -76,7 +76,7 @@ synchronized class CachingLogHandler : LogHandler { return cast(LogMessage[]) messages.idup; } - /** + /** * Gets the number of messages that this handler has handled since the last * `reset()` call. * Returns: The number of messages. @@ -85,7 +85,7 @@ synchronized class CachingLogHandler : LogHandler { return messages.length; } - /** + /** * Determines if no messages have been logged since the last time `reset()` * was called. * Returns: True if there are no messages, or false otherwise. @@ -104,7 +104,7 @@ unittest { assert(handler.getMessages().length == 1); } -/** +/** * A log handler that simply passes any log message it receives to a list of * other handlers. Note that handlers should only be added at application * startup, because the `handle` method is not synchronized to improve @@ -113,7 +113,7 @@ unittest { class MultiLogHandler : LogHandler { private shared LogHandler[] handlers; - /** + /** * Constructs this multi-log handler using the given list of sub-handlers. * Params: * handlers = The handlers that should each handle every message this @@ -123,7 +123,7 @@ class MultiLogHandler : LogHandler { this.handlers = handlers; } - /** + /** * Adds a handler to this multi-handler's list of handlers. * Params: * handler = The handler to add. @@ -134,7 +134,7 @@ class MultiLogHandler : LogHandler { return this; } - /** + /** * Handles a log message by calling each of its sub-handlers' `handle` * methods on it. * Params: @@ -158,7 +158,7 @@ unittest { assert(h2.getMessages().length == 1); } -/** +/** * A handler that applies a filter to log messages, and only passes messages to * its internal handler if the filter returns `true`. */ @@ -194,7 +194,7 @@ unittest { assert(baseHandler.getMessages().length == 1); } -/** +/** * A handler that sends log messages to different handlers depending on the * level of the message. For example, you could create a level-mapped log * handler that sends INFO messages to stdout, but sends ERROR messages to diff --git a/source/slf4d/testing_provider.d b/source/slf4d/testing_provider.d index 2094029..1eff5bc 100644 --- a/source/slf4d/testing_provider.d +++ b/source/slf4d/testing_provider.d @@ -1,3 +1,8 @@ +/** + * An SLF4D provider that is intended to be used for testing. The provider + * class, `TestingLoggingProvider`, includes some convenience methods to + * inspect the log messages generated while it's active. + */ module slf4d.testing_provider; import slf4d.factory; @@ -15,7 +20,11 @@ class TestingLoggingProvider : LoggingProvider { /** * The logger factory that this provider uses. */ - public shared TestingLoggerFactory factory = new TestingLoggerFactory(); + public shared TestingLoggerFactory factory; + + public shared this() { + this.factory = new shared TestingLoggerFactory(); + } public shared shared(TestingLoggerFactory) getLoggerFactory() { return this.factory; @@ -43,9 +52,13 @@ class TestingLoggingProvider : LoggingProvider { * manner. */ class TestingLoggerFactory : LoggerFactory { - public shared CachingLogHandler handler = new shared CachingLogHandler(); + public shared CachingLogHandler handler; public Level logLevel = Levels.TRACE; + public shared this() { + this.handler = new shared CachingLogHandler(); + } + shared Logger getLogger(string name = __MODULE__) { return Logger(this.handler, this.logLevel, name); }