mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-02-02 11:04:00 +01:00
flush log only when queue is empty
This commit is contained in:
parent
42f228e75a
commit
58939de57e
15
Log.cpp
15
Log.cpp
|
@ -2,14 +2,19 @@
|
||||||
|
|
||||||
#include "Daemon.h"
|
#include "Daemon.h"
|
||||||
|
|
||||||
i2p::util::MsgQueue<LogMsg> g_Log;
|
Log g_Log;
|
||||||
|
|
||||||
void LogMsg::Process()
|
void LogMsg::Process()
|
||||||
{
|
{
|
||||||
if (Daemon.isLogging == 1 && Daemon.logfile.is_open())
|
if (Daemon.isLogging == 1 && Daemon.logfile.is_open())
|
||||||
{
|
|
||||||
Daemon.logfile << s.str();
|
Daemon.logfile << s.str();
|
||||||
Daemon.logfile.flush();
|
|
||||||
}
|
|
||||||
output << s.str();
|
output << s.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Log::Flush ()
|
||||||
|
{
|
||||||
|
if (Daemon.isLogging == 1 && Daemon.logfile.is_open())
|
||||||
|
Daemon.logfile.flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
14
Log.h
14
Log.h
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
#include <functional>
|
||||||
#include "Queue.h"
|
#include "Queue.h"
|
||||||
|
|
||||||
struct LogMsg
|
struct LogMsg
|
||||||
|
@ -15,7 +16,18 @@ struct LogMsg
|
||||||
void Process();
|
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>
|
template<typename TValue>
|
||||||
void LogPrint (std::stringstream& s, TValue arg)
|
void LogPrint (std::stringstream& s, TValue arg)
|
||||||
|
|
8
Queue.h
8
Queue.h
|
@ -5,6 +5,7 @@
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <condition_variable>
|
#include <condition_variable>
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
namespace i2p
|
namespace i2p
|
||||||
{
|
{
|
||||||
|
@ -104,6 +105,8 @@ namespace util
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
typedef std::function<void()> OnEmpty;
|
||||||
|
|
||||||
MsgQueue (): m_IsRunning (true), m_Thread (std::bind (&MsgQueue<Msg>::Run, this)) {};
|
MsgQueue (): m_IsRunning (true), m_Thread (std::bind (&MsgQueue<Msg>::Run, this)) {};
|
||||||
void Stop()
|
void Stop()
|
||||||
{
|
{
|
||||||
|
@ -112,6 +115,8 @@ namespace util
|
||||||
m_Thread.join();
|
m_Thread.join();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void SetOnEmpty (OnEmpty const & e) { m_OnEmpty = e; };
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
void Run ()
|
void Run ()
|
||||||
|
@ -123,6 +128,8 @@ namespace util
|
||||||
msg->Process ();
|
msg->Process ();
|
||||||
delete msg;
|
delete msg;
|
||||||
}
|
}
|
||||||
|
if (m_OnEmpty != nullptr)
|
||||||
|
m_OnEmpty ();
|
||||||
Queue<Msg>::Wait ();
|
Queue<Msg>::Wait ();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -131,6 +138,7 @@ namespace util
|
||||||
|
|
||||||
bool m_IsRunning;
|
bool m_IsRunning;
|
||||||
std::thread m_Thread;
|
std::thread m_Thread;
|
||||||
|
OnEmpty m_OnEmpty;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue