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