flush log only when queue is empty

This commit is contained in:
orignal 2014-04-23 12:49:02 -04:00
parent 42f228e75a
commit 58939de57e
3 changed files with 31 additions and 6 deletions

15
Log.cpp
View File

@ -2,14 +2,19 @@
#include "Daemon.h"
i2p::util::MsgQueue<LogMsg> 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();
}
}
void Log::Flush ()
{
if (Daemon.isLogging == 1 && Daemon.logfile.is_open())
Daemon.logfile.flush();
}

14
Log.h
View File

@ -3,6 +3,7 @@
#include <iostream>
#include <sstream>
#include <functional>
#include "Queue.h"
struct LogMsg
@ -15,7 +16,18 @@ struct LogMsg
void Process();
};
extern i2p::util::MsgQueue<LogMsg> g_Log;
class Log: public i2p::util::MsgQueue<LogMsg>
{
public:
Log () { SetOnEmpty (std::bind (&Log::Flush, this)); };
private:
void Flush ();
};
extern Log g_Log;
template<typename TValue>
void LogPrint (std::stringstream& s, TValue arg)

View File

@ -5,6 +5,7 @@
#include <mutex>
#include <thread>
#include <condition_variable>
#include <functional>
namespace i2p
{
@ -104,6 +105,8 @@ namespace util
{
public:
typedef std::function<void()> OnEmpty;
MsgQueue (): m_IsRunning (true), m_Thread (std::bind (&MsgQueue<Msg>::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<Msg>::Wait ();
}
}
@ -131,6 +138,7 @@ namespace util
bool m_IsRunning;
std::thread m_Thread;
OnEmpty m_OnEmpty;
};
}
}