2021-04-12 09:41:28 +00:00
|
|
|
#include "Trace.h"
|
|
|
|
#include "StdString.h"
|
|
|
|
#include "CriticalSection.h"
|
|
|
|
#include "Thread.h"
|
2021-06-28 01:57:35 +00:00
|
|
|
#include "Platform.h"
|
2021-04-12 09:41:28 +00:00
|
|
|
#include <map>
|
|
|
|
#include <vector>
|
2016-01-12 19:48:07 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <Windows.h>
|
2016-01-13 01:05:57 +00:00
|
|
|
#else
|
|
|
|
#include <sys/time.h>
|
2016-01-12 19:48:07 +00:00
|
|
|
#endif
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2015-12-14 10:51:33 +00:00
|
|
|
typedef std::map<uint32_t, stdstr> ModuleNameMap;
|
|
|
|
|
2021-04-12 11:35:39 +00:00
|
|
|
uint32_t * g_ModuleLogLevel = nullptr;
|
2015-12-14 10:51:33 +00:00
|
|
|
static bool g_TraceClosed = false;
|
|
|
|
static ModuleNameMap g_ModuleNames;
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2015-03-17 21:19:42 +00:00
|
|
|
class CTraceLog
|
|
|
|
{
|
2015-12-10 06:38:33 +00:00
|
|
|
std::vector<CTraceModule *> m_Modules;
|
2021-04-12 09:41:28 +00:00
|
|
|
CriticalSection m_CS;
|
2008-09-18 03:15:49 +00:00
|
|
|
|
|
|
|
public:
|
2015-12-10 06:38:33 +00:00
|
|
|
CTraceLog()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
~CTraceLog() { CloseTrace(); }
|
|
|
|
|
2015-12-14 10:51:33 +00:00
|
|
|
void TraceMessage(uint32_t module, uint8_t severity, const char * file, int line, const char * function, const char * Message);
|
|
|
|
|
2015-12-10 06:38:33 +00:00
|
|
|
CTraceModule * AddTraceModule(CTraceModule * TraceModule);
|
|
|
|
CTraceModule * RemoveTraceModule(CTraceModule * TraceModule);
|
2016-12-23 20:51:52 +00:00
|
|
|
void CloseTrace(void);
|
|
|
|
void FlushTrace(void);
|
2008-09-18 03:15:49 +00:00
|
|
|
};
|
|
|
|
|
2015-12-14 10:51:33 +00:00
|
|
|
CTraceLog & GetTraceObjet(void)
|
|
|
|
{
|
|
|
|
static CTraceLog TraceLog;
|
|
|
|
return TraceLog;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TraceSetModuleName(uint8_t module, const char * Name)
|
|
|
|
{
|
|
|
|
g_ModuleNames.insert(ModuleNameMap::value_type(module, Name));
|
|
|
|
}
|
|
|
|
|
|
|
|
void WriteTraceFull(uint32_t module, uint8_t severity, const char * file, int line, const char * function, const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
|
|
|
size_t nlen = _vscprintf(format, args) + 1;
|
|
|
|
char * Message = (char *)alloca(nlen * sizeof(char));
|
|
|
|
Message[nlen - 1] = 0;
|
2021-04-12 11:35:39 +00:00
|
|
|
if (Message != nullptr)
|
2015-12-14 10:51:33 +00:00
|
|
|
{
|
|
|
|
vsprintf(Message, format, args);
|
|
|
|
GetTraceObjet().TraceMessage(module, severity, file, line, function, Message);
|
|
|
|
}
|
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
2016-12-23 20:51:52 +00:00
|
|
|
void TraceFlushLog(void)
|
|
|
|
{
|
|
|
|
GetTraceObjet().FlushTrace();
|
|
|
|
}
|
|
|
|
|
2015-12-14 10:51:33 +00:00
|
|
|
void CloseTrace(void)
|
|
|
|
{
|
|
|
|
g_TraceClosed = true;
|
|
|
|
GetTraceObjet().CloseTrace();
|
|
|
|
|
|
|
|
if (g_ModuleLogLevel)
|
|
|
|
{
|
|
|
|
delete g_ModuleLogLevel;
|
2021-04-12 11:35:39 +00:00
|
|
|
g_ModuleLogLevel = nullptr;
|
2015-12-14 10:51:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TraceSetMaxModule(uint32_t MaxModule, uint8_t DefaultSeverity)
|
|
|
|
{
|
|
|
|
if (g_ModuleLogLevel)
|
|
|
|
{
|
|
|
|
delete g_ModuleLogLevel;
|
2021-04-12 11:35:39 +00:00
|
|
|
g_ModuleLogLevel = nullptr;
|
2015-12-14 10:51:33 +00:00
|
|
|
}
|
|
|
|
g_ModuleLogLevel = new uint32_t[MaxModule];
|
|
|
|
for (uint32_t i = 0; i < MaxModule; i++)
|
|
|
|
{
|
|
|
|
g_ModuleLogLevel[i] = DefaultSeverity;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-06 11:37:21 +00:00
|
|
|
CTraceModule * CTraceLog::AddTraceModule(CTraceModule * TraceModule)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2015-12-10 06:38:33 +00:00
|
|
|
CGuard Guard(m_CS);
|
2015-11-06 11:37:21 +00:00
|
|
|
|
2015-12-10 06:38:33 +00:00
|
|
|
for (int i = 0; i < (int)m_Modules.size(); i++)
|
|
|
|
{
|
|
|
|
if (m_Modules[i] == TraceModule)
|
|
|
|
{
|
|
|
|
return TraceModule;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m_Modules.push_back(TraceModule);
|
|
|
|
return TraceModule;
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
2015-11-06 11:37:21 +00:00
|
|
|
CTraceModule * CTraceLog::RemoveTraceModule(CTraceModule * TraceModule)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2015-12-10 06:38:33 +00:00
|
|
|
CGuard Guard(m_CS);
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2015-12-10 06:38:33 +00:00
|
|
|
for (std::vector<CTraceModule *>::iterator itr = m_Modules.begin(); itr != m_Modules.end(); itr++)
|
|
|
|
{
|
|
|
|
if ((*itr) == TraceModule)
|
|
|
|
{
|
|
|
|
m_Modules.erase(itr);
|
|
|
|
return TraceModule;
|
|
|
|
}
|
|
|
|
}
|
2021-04-12 11:35:39 +00:00
|
|
|
return nullptr;
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
2015-11-06 11:37:21 +00:00
|
|
|
void CTraceLog::CloseTrace(void)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2015-12-10 06:38:33 +00:00
|
|
|
CGuard Guard(m_CS);
|
|
|
|
m_Modules.clear();
|
2018-11-18 01:06:02 +00:00
|
|
|
|
|
|
|
if (g_ModuleLogLevel)
|
|
|
|
{
|
|
|
|
delete g_ModuleLogLevel;
|
2021-04-12 11:35:39 +00:00
|
|
|
g_ModuleLogLevel = nullptr;
|
2018-11-18 01:06:02 +00:00
|
|
|
}
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
2016-12-23 20:51:52 +00:00
|
|
|
void CTraceLog::FlushTrace(void)
|
|
|
|
{
|
|
|
|
CGuard Guard(m_CS);
|
2018-11-18 01:06:02 +00:00
|
|
|
for (size_t i = 0, n = m_Modules.size(); i < n; i++)
|
2016-12-23 20:51:52 +00:00
|
|
|
{
|
|
|
|
m_Modules[i]->FlushTrace();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-12-14 10:51:33 +00:00
|
|
|
void CTraceLog::TraceMessage(uint32_t module, uint8_t severity, const char * file, int line, const char * function, const char * Message)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2015-12-10 06:38:33 +00:00
|
|
|
CGuard Guard(m_CS);
|
|
|
|
|
2018-11-18 01:06:02 +00:00
|
|
|
for (size_t i = 0, n = m_Modules.size(); i < n; i++)
|
2015-12-10 06:38:33 +00:00
|
|
|
{
|
2015-12-14 10:51:33 +00:00
|
|
|
m_Modules[i]->Write(module, severity, file, line, function, Message);
|
2015-12-10 06:38:33 +00:00
|
|
|
}
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
2015-12-14 10:51:33 +00:00
|
|
|
CTraceModule * TraceAddModule(CTraceModule * TraceModule)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2015-12-14 10:51:33 +00:00
|
|
|
if (g_TraceClosed)
|
2015-12-10 06:38:33 +00:00
|
|
|
{
|
2021-04-12 11:35:39 +00:00
|
|
|
return nullptr;
|
2015-12-10 06:38:33 +00:00
|
|
|
}
|
|
|
|
GetTraceObjet().AddTraceModule(TraceModule);
|
|
|
|
return TraceModule;
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
2015-12-14 10:51:33 +00:00
|
|
|
CTraceModule * TraceRemoveModule(CTraceModule * TraceModule)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2015-12-10 06:38:33 +00:00
|
|
|
return GetTraceObjet().RemoveTraceModule(TraceModule);
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
2015-12-14 10:51:33 +00:00
|
|
|
const char * TraceSeverity(uint8_t severity)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2015-12-14 10:51:33 +00:00
|
|
|
switch (severity)
|
|
|
|
{
|
|
|
|
case TraceError: return "Error";
|
|
|
|
case TraceWarning: return "Warning";
|
|
|
|
case TraceNotice: return "Notice";
|
|
|
|
case TraceInfo: return "Info";
|
|
|
|
case TraceDebug: return "Debug";
|
|
|
|
case TraceVerbose: return "Verbose";
|
|
|
|
}
|
|
|
|
|
|
|
|
static stdstr Unknown;
|
|
|
|
Unknown.Format("Unknown (%d)", (int32_t)severity);
|
|
|
|
return Unknown.c_str();
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
2015-12-14 10:51:33 +00:00
|
|
|
const char * TraceModule(uint32_t module)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2015-12-14 10:51:33 +00:00
|
|
|
ModuleNameMap::const_iterator itr = g_ModuleNames.find(module);
|
|
|
|
if (itr != g_ModuleNames.end())
|
|
|
|
{
|
|
|
|
return itr->second.c_str();
|
|
|
|
}
|
|
|
|
static stdstr Unknown;
|
|
|
|
Unknown.Format("Unknown (%d)", module);
|
|
|
|
return Unknown.c_str();
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
2016-04-19 10:37:41 +00:00
|
|
|
CTraceFileLog::CTraceFileLog(const char * FileName, bool FlushFile, CLog::LOG_OPEN_MODE eMode, size_t dwMaxFileSize) :
|
|
|
|
m_FlushFile(FlushFile)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2015-12-10 06:38:33 +00:00
|
|
|
enum { MB = 1024 * 1024 };
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2015-12-10 06:38:33 +00:00
|
|
|
m_hLogFile.SetFlush(false);
|
|
|
|
m_hLogFile.SetTruncateFile(true);
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2015-12-10 06:38:33 +00:00
|
|
|
if (dwMaxFileSize < 3 || dwMaxFileSize > 2047)
|
2021-05-18 11:51:36 +00:00
|
|
|
{ // Clamp file size to 5MB if it exceeds 2047 or falls short of 3
|
2015-12-10 06:38:33 +00:00
|
|
|
dwMaxFileSize = 5;
|
|
|
|
}
|
2018-11-18 01:06:02 +00:00
|
|
|
m_hLogFile.SetMaxFileSize((uint32_t)(dwMaxFileSize * MB));
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2015-12-10 06:38:33 +00:00
|
|
|
m_hLogFile.Open(FileName, eMode);
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CTraceFileLog::~CTraceFileLog()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-12-14 10:51:33 +00:00
|
|
|
void CTraceFileLog::Write(uint32_t module, uint8_t severity, const char * /*file*/, int /*line*/, const char * function, const char * Message)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2015-12-10 06:38:33 +00:00
|
|
|
if (!m_hLogFile.IsOpen()) { return; }
|
2015-11-06 11:37:21 +00:00
|
|
|
|
2016-01-12 19:02:18 +00:00
|
|
|
#ifdef _WIN32
|
2016-01-12 19:48:07 +00:00
|
|
|
SYSTEMTIME sysTime;
|
2015-12-14 10:51:33 +00:00
|
|
|
::GetLocalTime(&sysTime);
|
2016-04-28 07:12:32 +00:00
|
|
|
stdstr_f timestamp("%04d/%02d/%02d %02d:%02d:%02d.%03d %05d,", sysTime.wYear, sysTime.wMonth, sysTime.wDay, sysTime.wHour, sysTime.wMinute, sysTime.wSecond, sysTime.wMilliseconds, CThread::GetCurrentThreadId());
|
2016-01-12 19:02:18 +00:00
|
|
|
#else
|
|
|
|
time_t ltime;
|
|
|
|
ltime=time(<ime);
|
|
|
|
|
2016-01-12 19:48:07 +00:00
|
|
|
struct tm result={0};
|
2016-01-12 19:02:18 +00:00
|
|
|
localtime_r(<ime, &result);
|
|
|
|
|
2016-01-12 19:48:07 +00:00
|
|
|
struct timeval curTime;
|
2021-04-12 11:35:39 +00:00
|
|
|
gettimeofday(&curTime, nullptr);
|
2016-01-12 19:02:18 +00:00
|
|
|
int milliseconds = curTime.tv_usec / 1000;
|
|
|
|
|
2016-04-28 07:12:32 +00:00
|
|
|
stdstr_f timestamp("%04d/%02d/%02d %02d:%02d:%02d.%03d %05d,", result.tm_year+1900, result.tm_mon+1, result.tm_mday, result.tm_hour, result.tm_min, result.tm_sec, milliseconds, CThread::GetCurrentThreadId());
|
2016-01-12 19:02:18 +00:00
|
|
|
#endif
|
2015-12-14 10:51:33 +00:00
|
|
|
|
|
|
|
m_hLogFile.Log(timestamp.c_str());
|
|
|
|
m_hLogFile.Log(TraceSeverity(severity));
|
|
|
|
m_hLogFile.Log(",");
|
|
|
|
m_hLogFile.Log(TraceModule(module));
|
|
|
|
m_hLogFile.Log(",");
|
|
|
|
m_hLogFile.Log(function);
|
|
|
|
m_hLogFile.Log(",");
|
2015-12-10 06:38:33 +00:00
|
|
|
m_hLogFile.Log(Message);
|
2015-12-14 10:51:33 +00:00
|
|
|
m_hLogFile.Log("\r\n");
|
|
|
|
if (m_FlushFile)
|
2015-12-10 06:38:33 +00:00
|
|
|
{
|
2015-12-14 10:51:33 +00:00
|
|
|
m_hLogFile.Flush();
|
2015-12-10 06:38:33 +00:00
|
|
|
}
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
2016-12-23 20:51:52 +00:00
|
|
|
void CTraceFileLog::FlushTrace(void)
|
|
|
|
{
|
|
|
|
m_hLogFile.Flush();
|
|
|
|
}
|
|
|
|
|
2015-11-06 11:37:21 +00:00
|
|
|
void CTraceFileLog::SetFlushFile(bool bFlushFile)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2015-12-10 06:38:33 +00:00
|
|
|
m_FlushFile = bFlushFile;
|
2016-12-23 20:51:52 +00:00
|
|
|
FlushTrace();
|
2021-03-16 23:02:00 +00:00
|
|
|
}
|