diff options
author | Yann Herklotz <ymherklotz@gmail.com> | 2017-11-16 16:27:47 +0000 |
---|---|---|
committer | Yann Herklotz <ymherklotz@gmail.com> | 2017-11-16 16:27:47 +0000 |
commit | 82a3db85138c91df397fd820a3b5d1a0b5c21ef9 (patch) | |
tree | 6c6435962594df18eb2b6ed1d07740aecd0778ff /yage | |
parent | 443ae47fc210bcfe10f6f6c5ac8aa3453e1d29d2 (diff) | |
download | YAGE-82a3db85138c91df397fd820a3b5d1a0b5c21ef9.tar.gz YAGE-82a3db85138c91df397fd820a3b5d1a0b5c21ef9.zip |
Asynchronous logging added
Diffstat (limited to 'yage')
-rw-r--r-- | yage/CMakeLists.txt | 2 | ||||
l--------- | yage/core/.#logmessage.h | 1 | ||||
-rw-r--r-- | yage/core/logger.cpp | 15 | ||||
-rw-r--r-- | yage/core/logger.h | 4 | ||||
-rw-r--r-- | yage/core/logsink.cpp | 2 | ||||
-rw-r--r-- | yage/core/logsink.h | 8 | ||||
-rw-r--r-- | yage/util/active.cpp | 37 | ||||
-rw-r--r-- | yage/util/active.h | 29 |
8 files changed, 87 insertions, 11 deletions
diff --git a/yage/CMakeLists.txt b/yage/CMakeLists.txt index 5a5674ec..83a6e054 100644 --- a/yage/CMakeLists.txt +++ b/yage/CMakeLists.txt @@ -3,12 +3,14 @@ cmake_policy(SET CMP0048 NEW) file(GLOB YAGE_CORE_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} core/*.cpp) file(GLOB YAGE_MATH_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} math/*.cpp) file(GLOB YAGE_PHYSICS_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} physics/*.cpp) +file(GLOB YAGE_UTIL_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} util/*.cpp) file(GLOB YAGE_CURRENT_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp) set(YAGE_SOURCES ${YAGE_CORE_SOURCES} ${YAGE_PHYSICS_SOURCES} ${YAGE_MATH_SOURCES} + ${YAGE_UTIL_SOURCES} ${YAGE_CURRENT_SOURCES}) set(${PROJECT_NAME}_DIR 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 <yage/util/active.h> + +#include <memory> #include <string> #include <vector> @@ -34,6 +37,7 @@ public: private: std::vector<LogSink> sinks_; + std::unique_ptr<Active> 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 <typename T> @@ -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<T>::clone() const template <typename T> void LogSink::Model<T>::write(const LogMessage::Meta &meta, - const std::string &msg) + const std::string &msg) const { impl(meta, msg); } diff --git a/yage/util/active.cpp b/yage/util/active.cpp new file mode 100644 index 00000000..13e7fc38 --- /dev/null +++ b/yage/util/active.cpp @@ -0,0 +1,37 @@ +#include "active.h" + +namespace yage +{ + +Active::Active() : running_(true) {} + +Active::~Active() +{ + send([this] { running_ = false; }); + thread_.join(); +} + +std::unique_ptr<Active> Active::create() +{ + std::unique_ptr<Active> result(new Active); + + result->thread_ = std::thread(&Active::run, result.get()); + + return result; +} + +void Active::send(Callback message) +{ + queue_.push(message); +} + +void Active::run() +{ + Callback fn; + while (running_) { + queue_.pop(fn); + fn(); + } +} + +} // namespace yage diff --git a/yage/util/active.h b/yage/util/active.h index 877ab75e..ca8d30ad 100644 --- a/yage/util/active.h +++ b/yage/util/active.h @@ -1,11 +1,38 @@ #ifndef YAGE_UTIL_ACTIVE_H #define YAGE_UTIL_ACTIVE_H +#include "syncqueue.h" + +#include <functional> +#include <memory> +#include <thread> + +namespace yage +{ + class Active { public: + typedef std::function<void()> Callback; + + Active(const Active &) = delete; + Active &operator=(const Active &) = delete; + + ~Active(); + + static std::unique_ptr<Active> create(); + + void send(Callback message); + +private: Active(); - virtual ~Active(); + void run(); + + bool running_; + SyncQueue<Callback> queue_; + std::thread thread_; }; +} // namespace yage + #endif |