2014-08-17 05:35:09 +00:00
|
|
|
#include <boost/date_time/posix_time/posix_time.hpp>
|
2015-05-06 15:24:35 +00:00
|
|
|
#include "Log.h"
|
2013-12-10 13:00:13 +00:00
|
|
|
|
2014-07-02 17:48:45 +00:00
|
|
|
Log * g_Log = nullptr;
|
2014-04-22 22:10:21 +00:00
|
|
|
|
2014-10-28 20:36:17 +00:00
|
|
|
static const char * g_LogLevelStr[eNumLogLevels] =
|
|
|
|
{
|
|
|
|
"error", // eLogError
|
|
|
|
"warn", // eLogWarning
|
|
|
|
"info", // eLogInfo
|
|
|
|
"debug" // eLogDebug
|
|
|
|
};
|
|
|
|
|
2014-04-22 22:10:21 +00:00
|
|
|
void LogMsg::Process()
|
|
|
|
{
|
2016-02-04 17:36:58 +00:00
|
|
|
auto stream = log ? log->GetLogStream () : nullptr;
|
|
|
|
auto& output = stream ? *stream : std::cout;
|
2015-05-06 15:24:35 +00:00
|
|
|
if (log)
|
|
|
|
output << log->GetTimestamp ();
|
|
|
|
else
|
|
|
|
output << boost::posix_time::second_clock::local_time().time_of_day ();
|
|
|
|
output << "/" << g_LogLevelStr[level] << " - ";
|
2014-04-22 22:10:21 +00:00
|
|
|
output << s.str();
|
2014-04-23 16:49:02 +00:00
|
|
|
}
|
|
|
|
|
2015-05-06 15:24:35 +00:00
|
|
|
const std::string& Log::GetTimestamp ()
|
|
|
|
{
|
2015-11-20 15:02:54 +00:00
|
|
|
#if (__GNUC__ == 4) && (__GNUC_MINOR__ <= 6) && !defined(__clang__)
|
2015-05-06 23:18:00 +00:00
|
|
|
auto ts = std::chrono::monotonic_clock::now ();
|
|
|
|
#else
|
2015-05-06 15:24:35 +00:00
|
|
|
auto ts = std::chrono::steady_clock::now ();
|
2015-05-06 23:18:00 +00:00
|
|
|
#endif
|
2015-05-06 15:24:35 +00: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 16:49:02 +00:00
|
|
|
void Log::Flush ()
|
|
|
|
{
|
2014-12-08 21:27:10 +00:00
|
|
|
if (m_LogStream)
|
|
|
|
m_LogStream->flush();
|
2014-04-23 16:49:02 +00:00
|
|
|
}
|
|
|
|
|
2014-04-24 15:10:46 +00:00
|
|
|
void Log::SetLogFile (const std::string& fullFilePath)
|
|
|
|
{
|
2016-02-04 18:53:38 +00:00
|
|
|
m_FullFilePath = fullFilePath;
|
2016-02-04 17:36:58 +00:00
|
|
|
auto logFile = std::make_shared<std::ofstream> (fullFilePath, std::ofstream::out | std::ofstream::binary | std::ofstream::trunc);
|
2014-12-08 21:27:10 +00:00
|
|
|
if (logFile->is_open ())
|
|
|
|
{
|
|
|
|
SetLogStream (logFile);
|
2015-12-18 14:08:35 +00:00
|
|
|
LogPrint(eLogInfo, "Log: will send messages to ", fullFilePath);
|
2014-12-08 21:27:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-04 18:53:38 +00:00
|
|
|
void Log::ReopenLogFile ()
|
|
|
|
{
|
|
|
|
if (m_FullFilePath.length () > 0)
|
|
|
|
{
|
|
|
|
SetLogFile (m_FullFilePath);
|
|
|
|
LogPrint(eLogInfo, "Log: file ", m_FullFilePath, " reopen");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-12-28 00:00:00 +00:00
|
|
|
void Log::SetLogLevel (const std::string& level)
|
|
|
|
{
|
|
|
|
if (level == "error") { m_MinLevel = eLogError; }
|
|
|
|
else if (level == "warn") { m_MinLevel = eLogWarning; }
|
|
|
|
else if (level == "info") { m_MinLevel = eLogInfo; }
|
|
|
|
else if (level == "debug") { m_MinLevel = eLogDebug; }
|
|
|
|
else {
|
|
|
|
LogPrint(eLogError, "Log: Unknown loglevel: ", level);
|
|
|
|
return;
|
|
|
|
}
|
2016-02-04 17:36:58 +00:00
|
|
|
LogPrint(eLogInfo, "Log: min msg level set to ", level);
|
2015-12-28 00:00:00 +00:00
|
|
|
}
|
|
|
|
|
2016-02-04 17:36:58 +00:00
|
|
|
void Log::SetLogStream (std::shared_ptr<std::ostream> logStream)
|
2014-12-08 21:27:10 +00:00
|
|
|
{
|
|
|
|
m_LogStream = logStream;
|
2014-04-24 15:10:46 +00:00
|
|
|
}
|