From 82a3db85138c91df397fd820a3b5d1a0b5c21ef9 Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Thu, 16 Nov 2017 16:27:47 +0000 Subject: Asynchronous logging added --- yage/core/.#logmessage.h | 1 + yage/core/logger.cpp | 15 ++++++++++----- yage/core/logger.h | 4 ++++ yage/core/logsink.cpp | 2 +- yage/core/logsink.h | 8 ++++---- 5 files changed, 20 insertions(+), 10 deletions(-) create mode 120000 yage/core/.#logmessage.h (limited to 'yage/core') diff --git a/yage/core/.#logmessage.h b/yage/core/.#logmessage.h new file mode 120000 index 00000000..c4ccefcf --- /dev/null +++ b/yage/core/.#logmessage.h @@ -0,0 +1 @@ +yannherklotz@yann-arch.2036:1510699559 \ No newline at end of file diff --git a/yage/core/logger.cpp b/yage/core/logger.cpp index 9fba2239..12a23dfc 100644 --- a/yage/core/logger.cpp +++ b/yage/core/logger.cpp @@ -17,7 +17,7 @@ namespace yage { -Logger::Logger() +Logger::Logger() : active_(Active::create()) { add(makeConsoleSink()); } @@ -31,9 +31,14 @@ void Logger::flush(const LogMessage *msg) { std::string asString(msg->buffer_.str()); - for (auto &&sink : sinks_) { - sink.write(msg->meta_, asString); - } + auto &&sinks = sinks_; + auto &&meta = msg->meta_; + + active_->send([=] { + for (auto &&sink : sinks) { + sink.write(meta, asString); + } + }); } void Logger::add(const LogSink &sink) @@ -45,7 +50,7 @@ void Logger::remove(const LogSink &sink) { auto it = std::find(std::begin(sinks_), std::end(sinks_), sink); - if(it != std::end(sinks_)) { + if (it != std::end(sinks_)) { sinks_.erase(it); } } diff --git a/yage/core/logger.h b/yage/core/logger.h index 2c70fd04..30b06b98 100644 --- a/yage/core/logger.h +++ b/yage/core/logger.h @@ -9,6 +9,9 @@ #ifndef YAGE_CORE_LOGGER_H #define YAGE_CORE_LOGGER_H +#include + +#include #include #include @@ -34,6 +37,7 @@ public: private: std::vector sinks_; + std::unique_ptr active_; }; } // namespace yage diff --git a/yage/core/logsink.cpp b/yage/core/logsink.cpp index 987a260d..a8f4d7b4 100644 --- a/yage/core/logsink.cpp +++ b/yage/core/logsink.cpp @@ -34,7 +34,7 @@ bool LogSink::operator==(const LogSink &sink) return (wrapper_.get() == sink.wrapper_.get()); } -void LogSink::write(const LogMessage::Meta &meta, const std::string &msg) +void LogSink::write(const LogMessage::Meta &meta, const std::string &msg) const { wrapper_->write(meta, msg); } diff --git a/yage/core/logsink.h b/yage/core/logsink.h index f18a6d37..526c862e 100644 --- a/yage/core/logsink.h +++ b/yage/core/logsink.h @@ -30,7 +30,7 @@ public: LogSink &operator=(LogSink &&sink); bool operator==(const LogSink &sink); - void write(const LogMessage::Meta &meta, const std::string &msg); + void write(const LogMessage::Meta &meta, const std::string &msg) const; private: struct Concept { @@ -38,7 +38,7 @@ private: virtual Concept *clone() const = 0; virtual void write(const LogMessage::Meta &meta, - const std::string &msg) = 0; + const std::string &msg) const = 0; }; template @@ -46,7 +46,7 @@ private: Model(T impl_i); virtual Concept *clone() const override; virtual void write(const LogMessage::Meta &meta, - const std::string &msg) override; + const std::string &msg) const override; T impl; }; @@ -79,7 +79,7 @@ LogSink::Concept *LogSink::Model::clone() const template void LogSink::Model::write(const LogMessage::Meta &meta, - const std::string &msg) + const std::string &msg) const { impl(meta, msg); } -- cgit