diff --git a/src/Lib/Log.cc b/src/Lib/Log.cc index e09f7480d8931c4fcfab8f11eb7e5fa05d0e6188..795786f859ee678c56d23415ca36a3f2e36eaa6c 100644 --- a/src/Lib/Log.cc +++ b/src/Lib/Log.cc @@ -69,6 +69,23 @@ LogSink::closeLoggerClients() noexcept [](const std::weak_ptr<Logger> &weakPtr) { return !weakPtr.expired(); })); } +std::shared_ptr<Logger> +LogSink::getLoggerClient(const std::string_view category) const +{ + auto end = std::end(clientLoggers); + auto it = std::find_if(std::begin(clientLoggers), end, + [category](const std::weak_ptr<Logger> &weakPtr) { + std::shared_ptr<Logger> sharedPtr = weakPtr.lock(); + return sharedPtr && (sharedPtr->getCategoryView() == category); + }); + if (it == end) + throw std::logic_error("Logger was not found"); + std::shared_ptr<Logger> sharedPtr = (*it).lock(); + if (sharedPtr == nullptr) + throw std::logic_error("Logger has expired"); + return sharedPtr; +} + LogMessage Logger::logEvent(const char *fileName, const char *functionName, const int lineNumber, const LogLevel logSeverity) noexcept diff --git a/src/Lib/Log.hh b/src/Lib/Log.hh index a9d4a22b090eef9557316cb8df5be14aa7367248..64cd059d81b8c611004ba6ebafc93d2616eead8b 100644 --- a/src/Lib/Log.hh +++ b/src/Lib/Log.hh @@ -53,10 +53,11 @@ public: static std::shared_ptr<LogSink> newSink() noexcept; ~LogSink() noexcept; + void recieveLogMessage(const Logger *const, LogMessage const &) noexcept; + void registerLoggerClient(std::shared_ptr<Logger>) noexcept; void closeLoggerClients() noexcept; - - void recieveLogMessage(const Logger *const, LogMessage const &) noexcept; + std::shared_ptr<Logger> getLoggerClient(const std::string_view category) const; private: std::mutex messageQueueLock{}; @@ -126,7 +127,7 @@ class Logger : public std::enable_shared_from_this<Logger> { public: // Templated constructor, construct from anything that can be considered as // a string. - explicit Logger(LogSink *const sink, const std::string_view category) noexcept + explicit Logger(LogSink *const sink, const StringType auto category) noexcept : parentLogSink(sink) , logCategory(anyStringToStdString(category)) {