2014-08-17 09:35:09 +04:00
|
|
|
#include <boost/date_time/posix_time/posix_time.hpp>
|
2015-05-06 18:24:35 +03:00
|
|
|
#include "Log.h"
|
2013-12-10 17:00:13 +04:00
|
|
|
|
2014-07-02 21:48:45 +04:00
|
|
|
Log * g_Log = nullptr;
|
2014-04-23 02:10:21 +04:00
|
|
|
|
2014-10-28 23:36:17 +03:00
|
|
|
static const char * g_LogLevelStr[eNumLogLevels] =
|
|
|
|
{
|
|
|
|
"error", // eLogError
|
|
|
|
"warn", // eLogWarning
|
|
|
|
"info", // eLogInfo
|
|
|
|
"debug" // eLogDebug
|
|
|
|
};
|
|
|
|
|
2014-04-23 02:10:21 +04:00
|
|
|
void LogMsg::Process()
|
|
|
|
{
|
2015-05-12 10:27:02 +03:00
|
|
|
auto& output = (log && log->GetLogStream ()) ? *log->GetLogStream () : std::cerr;
|
2015-05-06 18:24:35 +03:00
|
|
|
if (log)
|
|
|
|
output << log->GetTimestamp ();
|
|
|
|
else
|
|
|
|
output << boost::posix_time::second_clock::local_time().time_of_day ();
|
|
|
|
output << "/" << g_LogLevelStr[level] << " - ";
|
2014-04-23 02:10:21 +04:00
|
|
|
output << s.str();
|
2014-04-23 20:49:02 +04:00
|
|
|
}
|
|
|
|
|
2015-05-06 18:24:35 +03:00
|
|
|
const std::string& Log::GetTimestamp ()
|
|
|
|
{
|
2015-05-07 02:18:00 +03:00
|
|
|
#if (__GNUC__ == 4) && (__GNUC_MINOR__ <= 6)
|
|
|
|
auto ts = std::chrono::monotonic_clock::now ();
|
|
|
|
#else
|
2015-05-06 18:24:35 +03:00
|
|
|
auto ts = std::chrono::steady_clock::now ();
|
2015-05-07 02:18:00 +03:00
|
|
|
#endif
|
2015-05-06 18:24:35 +03:00
|
|
|
if (ts > m_LastTimestampUpdate + std::chrono::milliseconds (500)) // 0.5 second
|
|
|
|
{
|
|
|
|
m_LastTimestampUpdate = ts;
|
|
|
|
m_Timestamp = boost::posix_time::to_simple_string (boost::posix_time::second_clock::local_time().time_of_day ());
|
|
|
|
}
|
|
|
|
return m_Timestamp;
|
|
|
|
}
|
|
|
|
|
2014-04-23 20:49:02 +04:00
|
|
|
void Log::Flush ()
|
|
|
|
{
|
2014-12-09 00:27:10 +03:00
|
|
|
if (m_LogStream)
|
|
|
|
m_LogStream->flush();
|
2014-04-23 20:49:02 +04:00
|
|
|
}
|
|
|
|
|
2014-04-24 19:10:46 +04:00
|
|
|
void Log::SetLogFile (const std::string& fullFilePath)
|
|
|
|
{
|
2014-12-09 00:27:10 +03:00
|
|
|
auto logFile = new std::ofstream (fullFilePath, std::ofstream::out | std::ofstream::binary | std::ofstream::trunc);
|
|
|
|
if (logFile->is_open ())
|
|
|
|
{
|
|
|
|
SetLogStream (logFile);
|
2014-04-24 19:10:46 +04:00
|
|
|
LogPrint("Logging to file ", fullFilePath, " enabled.");
|
2014-12-09 00:27:10 +03:00
|
|
|
}
|
2014-04-24 19:10:46 +04:00
|
|
|
else
|
2014-12-09 00:27:10 +03:00
|
|
|
delete logFile;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Log::SetLogStream (std::ostream * logStream)
|
|
|
|
{
|
|
|
|
if (m_LogStream) delete m_LogStream;
|
|
|
|
m_LogStream = logStream;
|
2014-04-24 19:10:46 +04:00
|
|
|
}
|