From c3298d13a6c19ee171cbf895e4136538c416dade Mon Sep 17 00:00:00 2001 From: elsid Date: Sat, 7 Jul 2018 16:19:24 +0300 Subject: [PATCH] Add log sinks (stdout and file) --- apps/openmw/mwworld/worldimp.cpp | 4 +- components/detournavigator/debug.hpp | 65 ++++++++++++++++++++-------- files/settings-default.cfg | 3 ++ 3 files changed, 52 insertions(+), 20 deletions(-) diff --git a/apps/openmw/mwworld/worldimp.cpp b/apps/openmw/mwworld/worldimp.cpp index 9b8f97e8c..94227a724 100644 --- a/apps/openmw/mwworld/worldimp.cpp +++ b/apps/openmw/mwworld/worldimp.cpp @@ -193,7 +193,9 @@ namespace MWWorld navigatorSettings.mNavMeshPathPrefix = Settings::Manager::getString("nav mesh path prefix", "Navigator"); navigatorSettings.mEnableRecastMeshFileNameRevision = Settings::Manager::getBool("enable recast mesh file name revision", "Navigator"); navigatorSettings.mEnableNavMeshFileNameRevision = Settings::Manager::getBool("enable nav mesh file name revision", "Navigator"); - DetourNavigator::Log::instance().setEnabled(Settings::Manager::getBool("enable log", "Navigator")); + if (Settings::Manager::getBool("enable log", "Navigator")) + DetourNavigator::Log::instance().setSink(std::unique_ptr( + new DetourNavigator::FileSink(Settings::Manager::getString("log path", "Navigator")))); mNavigator.reset(new DetourNavigator::Navigator(navigatorSettings)); mRendering.reset(new MWRender::RenderingManager(viewer, rootNode, resourceSystem, workQueue, &mFallback, resourcePath, *mNavigator)); diff --git a/components/detournavigator/debug.hpp b/components/detournavigator/debug.hpp index bc5254322..2a19bcce8 100644 --- a/components/detournavigator/debug.hpp +++ b/components/detournavigator/debug.hpp @@ -6,11 +6,11 @@ #include #include -#include #include #include #include #include +#include #include #include #include @@ -33,36 +33,64 @@ namespace DetourNavigator << std::chrono::duration_cast(value.time_since_epoch()).count(); } - class Log + struct Sink + { + virtual ~Sink() = default; + virtual void write(const std::string& text) = 0; + }; + + class FileSink final : public Sink { public: - Log() - : mEnabled() + FileSink(std::string path) + : mPath(std::move(path)) { mFile.exceptions(std::ios::failbit | std::ios::badbit); } - void setEnabled(bool value) + void write(const std::string& text) override { - mEnabled = value; + if (!mFile.is_open()) + { + mFile.open(mPath); + } + mFile << text << std::flush; + } + + private: + std::string mPath; + std::ofstream mFile; + }; + + class StdoutSink final : public Sink + { + public: + void write(const std::string& text) override + { + std::cout << text << std::flush; + } + }; + + class Log + { + public: + void setSink(std::unique_ptr sink) + { + const std::lock_guard guard(mMutex); + mSink = std::move(sink); } bool isEnabled() const { - return mEnabled; + const std::lock_guard guard(mMutex); + return bool(mSink); } void write(const std::string& text) { - if (mEnabled) - { - const std::lock_guard lock(mMutex); - if (!mFile.is_open()) - { - mFile.open("detournavigator.log"); - } - mFile << text << std::flush; - } + const std::lock_guard guard(mMutex); + if (mSink) + mSink->write(text); } static Log& instance() @@ -72,9 +100,8 @@ namespace DetourNavigator } private: - std::mutex mMutex; - std::ofstream mFile; - std::atomic_bool mEnabled; + mutable std::mutex mMutex; + std::unique_ptr mSink; }; inline void write(std::ostream& stream) diff --git a/files/settings-default.cfg b/files/settings-default.cfg index f6fc99c0e..1b346dad6 100644 --- a/files/settings-default.cfg +++ b/files/settings-default.cfg @@ -597,6 +597,9 @@ triangles per chunk = 256 # Enable debug log (true, false) enable log = false +# Write debug log to this file +log path = detournavigator.log + # Write recast mesh to file in .obj format for each use to update nav mesh (true, false) enable write recast mesh to file = false