mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-01-22 12:24:19 +00:00
don't recalculate timestamp for each log message
This commit is contained in:
parent
0ae7bbd34d
commit
7c13194d5a
21
Log.cpp
21
Log.cpp
@ -1,5 +1,5 @@
|
|||||||
#include "Log.h"
|
|
||||||
#include <boost/date_time/posix_time/posix_time.hpp>
|
#include <boost/date_time/posix_time/posix_time.hpp>
|
||||||
|
#include "Log.h"
|
||||||
|
|
||||||
Log * g_Log = nullptr;
|
Log * g_Log = nullptr;
|
||||||
|
|
||||||
@ -13,11 +13,26 @@ static const char * g_LogLevelStr[eNumLogLevels] =
|
|||||||
|
|
||||||
void LogMsg::Process()
|
void LogMsg::Process()
|
||||||
{
|
{
|
||||||
output << boost::posix_time::second_clock::local_time().time_of_day () <<
|
auto& output = (log && log->GetLogStream ()) ? *log->GetLogStream () : std::cout;
|
||||||
"/" << g_LogLevelStr[level] << " - ";
|
if (log)
|
||||||
|
output << log->GetTimestamp ();
|
||||||
|
else
|
||||||
|
output << boost::posix_time::second_clock::local_time().time_of_day ();
|
||||||
|
output << "/" << g_LogLevelStr[level] << " - ";
|
||||||
output << s.str();
|
output << s.str();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const std::string& Log::GetTimestamp ()
|
||||||
|
{
|
||||||
|
auto ts = std::chrono::steady_clock::now ();
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
void Log::Flush ()
|
void Log::Flush ()
|
||||||
{
|
{
|
||||||
if (m_LogStream)
|
if (m_LogStream)
|
||||||
|
12
Log.h
12
Log.h
@ -6,6 +6,7 @@
|
|||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
#include <chrono>
|
||||||
#include "Queue.h"
|
#include "Queue.h"
|
||||||
|
|
||||||
enum LogLevel
|
enum LogLevel
|
||||||
@ -17,13 +18,14 @@ enum LogLevel
|
|||||||
eNumLogLevels
|
eNumLogLevels
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class Log;
|
||||||
struct LogMsg
|
struct LogMsg
|
||||||
{
|
{
|
||||||
std::stringstream s;
|
std::stringstream s;
|
||||||
std::ostream& output;
|
Log * log;
|
||||||
LogLevel level;
|
LogLevel level;
|
||||||
|
|
||||||
LogMsg (std::ostream& o = std::cout, LogLevel l = eLogInfo): output (o), level (l) {};
|
LogMsg (Log * l = nullptr, LogLevel lv = eLogInfo): log (l), level (lv) {};
|
||||||
|
|
||||||
void Process();
|
void Process();
|
||||||
};
|
};
|
||||||
@ -38,6 +40,7 @@ class Log: public i2p::util::MsgQueue<LogMsg>
|
|||||||
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 ();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
@ -46,6 +49,8 @@ class Log: public i2p::util::MsgQueue<LogMsg>
|
|||||||
private:
|
private:
|
||||||
|
|
||||||
std::ostream * m_LogStream;
|
std::ostream * m_LogStream;
|
||||||
|
std::string m_Timestamp;
|
||||||
|
std::chrono::steady_clock::time_point m_LastTimestampUpdate;
|
||||||
};
|
};
|
||||||
|
|
||||||
extern Log * g_Log;
|
extern Log * g_Log;
|
||||||
@ -95,8 +100,7 @@ void LogPrint (std::stringstream& s, TValue arg, TArgs... args)
|
|||||||
template<typename... TArgs>
|
template<typename... TArgs>
|
||||||
void LogPrint (LogLevel level, TArgs... args)
|
void LogPrint (LogLevel level, TArgs... args)
|
||||||
{
|
{
|
||||||
LogMsg * msg = (g_Log && g_Log->GetLogStream ()) ? new LogMsg (*g_Log->GetLogStream (), level) :
|
LogMsg * msg = new LogMsg (g_Log, level);
|
||||||
new LogMsg (std::cout, level);
|
|
||||||
LogPrint (msg->s, args...);
|
LogPrint (msg->s, args...);
|
||||||
msg->s << std::endl;
|
msg->s << std::endl;
|
||||||
if (g_Log)
|
if (g_Log)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user