2014-08-12 11:05:04 -04:00
|
|
|
|
|
|
|
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
|
2015-08-30 17:57:43 -04:00
|
|
|
#include "Logger.h"
|
2014-08-12 11:05:04 -04:00
|
|
|
|
|
|
|
#include "OSSupport/IsThread.h"
|
2014-08-12 16:43:04 -04:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <time.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-08-12 11:05:04 -04:00
|
|
|
|
|
|
|
|
|
|
|
cLogger & cLogger::GetInstance(void)
|
|
|
|
{
|
|
|
|
static cLogger Instance;
|
|
|
|
return Instance;
|
|
|
|
}
|
|
|
|
|
2014-08-12 16:43:04 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-08-12 11:05:04 -04:00
|
|
|
void cLogger::InitiateMultithreading()
|
|
|
|
{
|
|
|
|
GetInstance();
|
|
|
|
}
|
|
|
|
|
2014-08-12 16:43:04 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-08-12 11:05:04 -04:00
|
|
|
void cLogger::LogSimple(AString a_Message, eLogLevel a_LogLevel)
|
|
|
|
{
|
|
|
|
time_t rawtime;
|
2014-08-12 16:43:04 -04:00
|
|
|
time(&rawtime);
|
2014-08-12 11:05:04 -04:00
|
|
|
|
2014-08-12 16:43:04 -04:00
|
|
|
struct tm * timeinfo;
|
2014-08-12 11:05:04 -04:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
struct tm timeinforeal;
|
|
|
|
timeinfo = &timeinforeal;
|
|
|
|
localtime_s(timeinfo, &rawtime);
|
|
|
|
#else
|
2014-08-12 16:43:04 -04:00
|
|
|
timeinfo = localtime(&rawtime);
|
2014-08-12 11:05:04 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
AString Line;
|
|
|
|
#ifdef _DEBUG
|
2014-12-07 11:28:55 -05:00
|
|
|
Printf(Line, "[%04llx|%02d:%02d:%02d] %s\n", static_cast<UInt64>(std::hash<std::thread::id>()(std::this_thread::get_id())), timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec, a_Message.c_str());
|
2014-08-12 11:05:04 -04:00
|
|
|
#else
|
2014-08-12 16:43:04 -04:00
|
|
|
Printf(Line, "[%02d:%02d:%02d] %s\n", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec, a_Message.c_str());
|
2014-08-12 11:05:04 -04:00
|
|
|
#endif
|
|
|
|
|
2014-12-07 11:28:55 -05:00
|
|
|
|
2014-08-12 11:05:04 -04:00
|
|
|
cCSLock Lock(m_CriticalSection);
|
2014-08-12 16:43:04 -04:00
|
|
|
for (size_t i = 0; i < m_LogListeners.size(); i++)
|
2014-08-12 11:05:04 -04:00
|
|
|
{
|
2014-08-13 07:27:56 -04:00
|
|
|
m_LogListeners[i]->Log(Line, a_LogLevel);
|
2014-08-12 11:05:04 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void cLogger::Log(const char * a_Format, eLogLevel a_LogLevel, va_list a_ArgList)
|
|
|
|
{
|
|
|
|
AString Message;
|
|
|
|
AppendVPrintf(Message, a_Format, a_ArgList);
|
|
|
|
LogSimple(Message, a_LogLevel);
|
|
|
|
}
|
|
|
|
|
2014-08-12 16:43:04 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-08-30 17:57:43 -04:00
|
|
|
cLogger::cAttachment cLogger::AttachListener(std::unique_ptr<cListener> a_Listener)
|
2014-08-12 11:05:04 -04:00
|
|
|
{
|
2015-08-30 17:57:43 -04:00
|
|
|
auto nonOwning = a_Listener.get();
|
|
|
|
{
|
|
|
|
cCSLock Lock(m_CriticalSection);
|
|
|
|
m_LogListeners.push_back(std::move(a_Listener));
|
|
|
|
}
|
|
|
|
return cAttachment{nonOwning};
|
2014-08-12 11:05:04 -04:00
|
|
|
}
|
|
|
|
|
2014-08-12 16:43:04 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-08-12 11:05:04 -04:00
|
|
|
void cLogger::DetachListener(cListener * a_Listener)
|
|
|
|
{
|
|
|
|
cCSLock Lock(m_CriticalSection);
|
2015-08-30 17:57:43 -04:00
|
|
|
m_LogListeners.erase(
|
|
|
|
std::remove_if(
|
|
|
|
m_LogListeners.begin(),
|
|
|
|
m_LogListeners.end(),
|
|
|
|
[=](std::unique_ptr<cListener> & a_OtherListener) -> bool
|
|
|
|
{
|
|
|
|
return a_OtherListener.get() == a_Listener;
|
|
|
|
}
|
|
|
|
)
|
|
|
|
);
|
2014-08-12 11:05:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-08-12 16:43:04 -04:00
|
|
|
|
|
|
|
|
2014-08-12 11:05:04 -04:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Global functions
|
|
|
|
|
2014-08-12 16:43:04 -04:00
|
|
|
void LOG(const char * a_Format, ...)
|
2014-08-12 11:05:04 -04:00
|
|
|
{
|
|
|
|
va_list argList;
|
|
|
|
va_start(argList, a_Format);
|
|
|
|
cLogger::GetInstance().Log(a_Format, cLogger::llRegular, argList);
|
|
|
|
va_end(argList);
|
|
|
|
}
|
|
|
|
|
2014-08-12 16:43:04 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void LOGINFO(const char * a_Format, ...)
|
2014-08-12 11:05:04 -04:00
|
|
|
{
|
|
|
|
va_list argList;
|
|
|
|
va_start(argList, a_Format);
|
|
|
|
cLogger::GetInstance().Log( a_Format, cLogger::llInfo, argList);
|
|
|
|
va_end(argList);
|
|
|
|
}
|
|
|
|
|
2014-08-12 16:43:04 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2015-08-30 17:57:43 -04:00
|
|
|
void LOGWARNING(const char * a_Format, ...)
|
2014-08-12 11:05:04 -04:00
|
|
|
{
|
|
|
|
va_list argList;
|
|
|
|
va_start(argList, a_Format);
|
|
|
|
cLogger::GetInstance().Log( a_Format, cLogger::llWarning, argList);
|
|
|
|
va_end(argList);
|
|
|
|
}
|
|
|
|
|
2014-08-12 16:43:04 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void LOGERROR(const char * a_Format, ...)
|
2014-08-12 11:05:04 -04:00
|
|
|
{
|
|
|
|
va_list argList;
|
|
|
|
va_start(argList, a_Format);
|
|
|
|
cLogger::GetInstance().Log( a_Format, cLogger::llError, argList);
|
|
|
|
va_end(argList);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|