2007-12-09 03:26:33 +00:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include "Log.h"
|
2012-12-24 15:47:47 +00:00
|
|
|
#include "AppConfig.h"
|
|
|
|
#include "PathUtils.h"
|
|
|
|
#include "StdStreamUtils.h"
|
2007-12-09 03:26:33 +00:00
|
|
|
|
2012-12-24 15:47:47 +00:00
|
|
|
#define LOG_PATH "logs"
|
2007-12-09 03:26:33 +00:00
|
|
|
|
2018-05-25 16:37:10 +00:00
|
|
|
#define PREF_LOG_SHOWPRINTS "log.showprints"
|
|
|
|
|
2007-12-09 03:26:33 +00:00
|
|
|
CLog::CLog()
|
|
|
|
{
|
2013-04-21 05:07:30 +00:00
|
|
|
#ifndef DISABLE_LOGGING
|
2012-12-24 15:47:47 +00:00
|
|
|
m_logBasePath = CAppConfig::GetBasePath() / LOG_PATH;
|
|
|
|
Framework::PathUtils::EnsurePathExists(m_logBasePath);
|
2018-05-25 16:37:10 +00:00
|
|
|
CAppConfig::GetInstance().RegisterPreferenceBoolean(PREF_LOG_SHOWPRINTS, false);
|
|
|
|
m_showPrints = CAppConfig::GetInstance().GetPreferenceBoolean(PREF_LOG_SHOWPRINTS);
|
2013-04-21 05:07:30 +00:00
|
|
|
#endif
|
2007-12-09 03:26:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CLog::Print(const char* logName, const char* format, ...)
|
|
|
|
{
|
2013-04-21 05:07:30 +00:00
|
|
|
#if defined(_DEBUG) && !defined(DISABLE_LOGGING)
|
2018-05-25 16:37:10 +00:00
|
|
|
if(!m_showPrints) return;
|
2012-12-24 15:47:47 +00:00
|
|
|
auto& logStream(GetLog(logName));
|
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
|
|
|
vfprintf(logStream, format, args);
|
|
|
|
va_end(args);
|
|
|
|
logStream.Flush();
|
2007-12-12 03:09:57 +00:00
|
|
|
#endif
|
2007-12-09 03:26:33 +00:00
|
|
|
}
|
|
|
|
|
2018-05-24 16:54:10 +00:00
|
|
|
void CLog::Warn(const char* logName, const char* format, ...)
|
|
|
|
{
|
|
|
|
#if defined(_DEBUG) && !defined(DISABLE_LOGGING)
|
|
|
|
auto& logStream(GetLog(logName));
|
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
|
|
|
vfprintf(logStream, format, args);
|
|
|
|
va_end(args);
|
|
|
|
logStream.Flush();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2012-12-24 15:47:47 +00:00
|
|
|
Framework::CStdStream& CLog::GetLog(const char* logName)
|
2007-12-09 03:26:33 +00:00
|
|
|
{
|
2012-12-24 15:47:47 +00:00
|
|
|
auto logIterator(m_logs.find(logName));
|
|
|
|
if(logIterator == std::end(m_logs))
|
|
|
|
{
|
|
|
|
auto logPath = m_logBasePath / (std::string(logName) + ".log");
|
|
|
|
auto logStream = Framework::CreateOutputStdStream(logPath.native());
|
|
|
|
m_logs[logName] = std::move(logStream);
|
|
|
|
logIterator = m_logs.find(logName);
|
|
|
|
}
|
|
|
|
return logIterator->second;
|
2007-12-09 03:26:33 +00:00
|
|
|
}
|