Browse Source

Fixed build issue with Mac OSX, which don't support std::chrono::monotonic_clock.

pull/225/head
Mikal Villa 10 years ago
parent
commit
5f644b1b04
  1. 26
      Log.cpp
  2. 44
      Log.h

26
Log.cpp

@ -8,13 +8,13 @@ static const char * g_LogLevelStr[eNumLogLevels] = @@ -8,13 +8,13 @@ static const char * g_LogLevelStr[eNumLogLevels] =
"error", // eLogError
"warn", // eLogWarning
"info", // eLogInfo
"debug" // eLogDebug
"debug" // eLogDebug
};
void LogMsg::Process()
{
auto& output = (log && log->GetLogStream ()) ? *log->GetLogStream () : std::cerr;
if (log)
auto& output = (log && log->GetLogStream ()) ? *log->GetLogStream () : std::cerr;
if (log)
output << log->GetTimestamp ();
else
output << boost::posix_time::second_clock::local_time().time_of_day ();
@ -24,16 +24,20 @@ void LogMsg::Process() @@ -24,16 +24,20 @@ void LogMsg::Process()
const std::string& Log::GetTimestamp ()
{
#if (__GNUC__ == 4) && (__GNUC_MINOR__ <= 6)
auto ts = std::chrono::monotonic_clock::now ();
#else
auto ts = std::chrono::steady_clock::now ();
#endif
#if !defined(__APPLE__)
#if (__GNUC__ == 4) && (__GNUC_MINOR__ <= 6) && !defined(__APPLE__)
auto ts = std::chrono::monotonic_clock::now ();
#else
auto ts = std::chrono::steady_clock::now ();
#endif
#else
auto ts = std::chrono::steady_clock::now ();
#endif
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;
}
@ -50,13 +54,13 @@ void Log::SetLogFile (const std::string& fullFilePath) @@ -50,13 +54,13 @@ void Log::SetLogFile (const std::string& fullFilePath)
{
SetLogStream (logFile);
LogPrint("Logging to file ", fullFilePath, " enabled.");
}
}
else
delete logFile;
}
void Log::SetLogStream (std::ostream * logStream)
{
if (m_LogStream) delete m_LogStream;
if (m_LogStream) delete m_LogStream;
m_LogStream = logStream;
}

44
Log.h

@ -14,7 +14,7 @@ enum LogLevel @@ -14,7 +14,7 @@ enum LogLevel
eLogError = 0,
eLogWarning,
eLogInfo,
eLogDebug,
eLogDebug,
eNumLogLevels
};
@ -22,11 +22,11 @@ class Log; @@ -22,11 +22,11 @@ class Log;
struct LogMsg
{
std::stringstream s;
Log * log;
Log * log;
LogLevel level;
LogMsg (Log * l = nullptr, LogLevel lv = eLogInfo): log (l), level (lv) {};
void Process();
};
@ -39,7 +39,7 @@ class Log: public i2p::util::MsgQueue<LogMsg> @@ -39,7 +39,7 @@ class Log: public i2p::util::MsgQueue<LogMsg>
void SetLogFile (const std::string& fullFilePath);
void SetLogStream (std::ostream * logStream);
std::ostream * GetLogStream () const { return m_LogStream; };
std::ostream * GetLogStream () const { return m_LogStream; };
const std::string& GetTimestamp ();
private:
@ -47,14 +47,18 @@ class Log: public i2p::util::MsgQueue<LogMsg> @@ -47,14 +47,18 @@ class Log: public i2p::util::MsgQueue<LogMsg>
void Flush ();
private:
std::ostream * m_LogStream;
std::string m_Timestamp;
#if !defined(__APPLE__)
#if (__GNUC__ == 4) && (__GNUC_MINOR__ <= 6) // gcc 4.6
std::chrono::monotonic_clock::time_point m_LastTimestampUpdate;
#else
std::chrono::steady_clock::time_point m_LastTimestampUpdate;
#endif
#else
std::chrono::steady_clock::time_point m_LastTimestampUpdate;
#endif
#else
std::chrono::steady_clock::time_point m_LastTimestampUpdate;
#endif
};
extern Log * g_Log;
@ -62,23 +66,23 @@ extern Log * g_Log; @@ -62,23 +66,23 @@ extern Log * g_Log;
inline void StartLog (const std::string& fullFilePath)
{
if (!g_Log)
{
{
auto log = new Log ();
if (fullFilePath.length () > 0)
log->SetLogFile (fullFilePath);
g_Log = log;
}
}
}
inline void StartLog (std::ostream * s)
{
if (!g_Log)
{
{
auto log = new Log ();
if (s)
log->SetLogStream (s);
g_Log = log;
}
}
}
inline void StopLog ()
@ -89,29 +93,29 @@ inline void StopLog () @@ -89,29 +93,29 @@ inline void StopLog ()
g_Log = nullptr;
log->Stop ();
delete log;
}
}
}
template<typename TValue>
void LogPrint (std::stringstream& s, TValue arg)
void LogPrint (std::stringstream& s, TValue arg)
{
s << arg;
}
template<typename TValue, typename... TArgs>
void LogPrint (std::stringstream& s, TValue arg, TArgs... args)
void LogPrint (std::stringstream& s, TValue arg, TArgs... args)
{
LogPrint (s, arg);
LogPrint (s, args...);
}
template<typename... TArgs>
void LogPrint (LogLevel level, TArgs... args)
void LogPrint (LogLevel level, TArgs... args)
{
LogMsg * msg = new LogMsg (g_Log, level);
LogPrint (msg->s, args...);
msg->s << std::endl;
if (g_Log)
if (g_Log)
g_Log->Put (msg);
else
{
@ -121,9 +125,9 @@ void LogPrint (LogLevel level, TArgs... args) @@ -121,9 +125,9 @@ void LogPrint (LogLevel level, TArgs... args)
}
template<typename... TArgs>
void LogPrint (TArgs... args)
void LogPrint (TArgs... args)
{
LogPrint (eLogInfo, args...);
}
}
#endif

Loading…
Cancel
Save