|
|
@ -58,13 +58,29 @@ namespace log { |
|
|
|
|
|
|
|
|
|
|
|
Log::Log(): |
|
|
|
Log::Log(): |
|
|
|
m_Destination(eLogStdout), m_MinLevel(eLogInfo), |
|
|
|
m_Destination(eLogStdout), m_MinLevel(eLogInfo), |
|
|
|
m_LogStream (nullptr), m_Logfile(""), m_IsReady(false), m_HasColors(true) |
|
|
|
m_LogStream (nullptr), m_Logfile(""), m_HasColors(true), |
|
|
|
|
|
|
|
m_IsRunning (false), m_Thread (nullptr) |
|
|
|
{ |
|
|
|
{ |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Log::~Log () |
|
|
|
Log::~Log () |
|
|
|
{ |
|
|
|
{ |
|
|
|
switch (m_Destination) { |
|
|
|
delete m_Thread; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Log::Start () |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
if (!m_IsRunning) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
m_IsRunning = true; |
|
|
|
|
|
|
|
m_Thread = new std::thread (std::bind (&Log::Run, this)); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Log::Stop () |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
switch (m_Destination) |
|
|
|
|
|
|
|
{ |
|
|
|
#ifndef _WIN32 |
|
|
|
#ifndef _WIN32 |
|
|
|
case eLogSyslog : |
|
|
|
case eLogSyslog : |
|
|
|
closelog(); |
|
|
|
closelog(); |
|
|
@ -78,7 +94,14 @@ namespace log { |
|
|
|
/* do nothing */ |
|
|
|
/* do nothing */ |
|
|
|
break; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
Process(); |
|
|
|
m_IsRunning = false; |
|
|
|
|
|
|
|
m_Queue.WakeUp (); |
|
|
|
|
|
|
|
if (m_Thread) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
m_Thread->join (); |
|
|
|
|
|
|
|
delete m_Thread; |
|
|
|
|
|
|
|
m_Thread = nullptr; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void Log::SetLogLevel (const std::string& level) { |
|
|
|
void Log::SetLogLevel (const std::string& level) { |
|
|
@ -106,14 +129,11 @@ namespace log { |
|
|
|
* Unfortunately, with current startup process with late fork() this |
|
|
|
* Unfortunately, with current startup process with late fork() this |
|
|
|
* will give us nothing but pain. Maybe later. See in NetDb as example. |
|
|
|
* will give us nothing but pain. Maybe later. See in NetDb as example. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
void Log::Process() { |
|
|
|
void Log::Process(std::shared_ptr<LogMsg> msg) |
|
|
|
std::unique_lock<std::mutex> l(m_OutputLock); |
|
|
|
{ |
|
|
|
|
|
|
|
if (!msg) return; |
|
|
|
std::hash<std::thread::id> hasher; |
|
|
|
std::hash<std::thread::id> hasher; |
|
|
|
unsigned short short_tid; |
|
|
|
unsigned short short_tid; |
|
|
|
while (1) { |
|
|
|
|
|
|
|
auto msg = m_Queue.GetNextWithTimeout (1); |
|
|
|
|
|
|
|
if (!msg) |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
short_tid = (short) (hasher(msg->tid) % 1000); |
|
|
|
short_tid = (short) (hasher(msg->tid) % 1000); |
|
|
|
switch (m_Destination) { |
|
|
|
switch (m_Destination) { |
|
|
|
#ifndef _WIN32 |
|
|
|
#ifndef _WIN32 |
|
|
@ -137,14 +157,24 @@ namespace log { |
|
|
|
<< " - " << msg->text << std::endl; |
|
|
|
<< " - " << msg->text << std::endl; |
|
|
|
break; |
|
|
|
break; |
|
|
|
} // switch
|
|
|
|
} // switch
|
|
|
|
} // while
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void Log::Append(std::shared_ptr<i2p::log::LogMsg> & msg) { |
|
|
|
void Log::Run () |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
while (m_IsRunning) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
std::shared_ptr<LogMsg> msg; |
|
|
|
|
|
|
|
while (msg = m_Queue.Get ()) |
|
|
|
|
|
|
|
Process (msg); |
|
|
|
|
|
|
|
if (m_LogStream) m_LogStream->flush(); |
|
|
|
|
|
|
|
if (m_IsRunning) |
|
|
|
|
|
|
|
m_Queue.Wait (); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Log::Append(std::shared_ptr<i2p::log::LogMsg> & msg) |
|
|
|
|
|
|
|
{ |
|
|
|
m_Queue.Put(msg); |
|
|
|
m_Queue.Put(msg); |
|
|
|
if (!m_IsReady) |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
Process(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void Log::SendTo (const std::string& path) |
|
|
|
void Log::SendTo (const std::string& path) |
|
|
|