From 58939de57eb78a80b97acc2279d92b5ef8e55519 Mon Sep 17 00:00:00 2001 From: orignal Date: Wed, 23 Apr 2014 12:49:02 -0400 Subject: [PATCH] flush log only when queue is empty --- Log.cpp | 15 ++++++++++----- Log.h | 14 +++++++++++++- Queue.h | 8 ++++++++ 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/Log.cpp b/Log.cpp index 4db4920e..92fd8a25 100644 --- a/Log.cpp +++ b/Log.cpp @@ -2,14 +2,19 @@ #include "Daemon.h" -i2p::util::MsgQueue g_Log; +Log g_Log; void LogMsg::Process() { if (Daemon.isLogging == 1 && Daemon.logfile.is_open()) - { Daemon.logfile << s.str(); - Daemon.logfile.flush(); - } + output << s.str(); -} \ No newline at end of file +} + +void Log::Flush () +{ + if (Daemon.isLogging == 1 && Daemon.logfile.is_open()) + Daemon.logfile.flush(); +} + diff --git a/Log.h b/Log.h index acef8f8e..a5856ba7 100644 --- a/Log.h +++ b/Log.h @@ -3,6 +3,7 @@ #include #include +#include #include "Queue.h" struct LogMsg @@ -15,7 +16,18 @@ struct LogMsg void Process(); }; -extern i2p::util::MsgQueue g_Log; +class Log: public i2p::util::MsgQueue +{ + public: + + Log () { SetOnEmpty (std::bind (&Log::Flush, this)); }; + + private: + + void Flush (); +}; + +extern Log g_Log; template void LogPrint (std::stringstream& s, TValue arg) diff --git a/Queue.h b/Queue.h index 52474aff..22f51e69 100644 --- a/Queue.h +++ b/Queue.h @@ -5,6 +5,7 @@ #include #include #include +#include namespace i2p { @@ -104,6 +105,8 @@ namespace util { public: + typedef std::function OnEmpty; + MsgQueue (): m_IsRunning (true), m_Thread (std::bind (&MsgQueue::Run, this)) {}; void Stop() { @@ -112,6 +115,8 @@ namespace util m_Thread.join(); } + void SetOnEmpty (OnEmpty const & e) { m_OnEmpty = e; }; + private: void Run () @@ -123,6 +128,8 @@ namespace util msg->Process (); delete msg; } + if (m_OnEmpty != nullptr) + m_OnEmpty (); Queue::Wait (); } } @@ -131,6 +138,7 @@ namespace util bool m_IsRunning; std::thread m_Thread; + OnEmpty m_OnEmpty; }; } }