mirror of https://github.com/PurpleI2P/i2pd.git
I2P: End-to-End encrypted and anonymous Internet
https://i2pd.website/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
63 lines
1.5 KiB
63 lines
1.5 KiB
10 years ago
|
#include <boost/date_time/posix_time/posix_time.hpp>
|
||
10 years ago
|
#include "Log.h"
|
||
11 years ago
|
|
||
11 years ago
|
Log * g_Log = nullptr;
|
||
11 years ago
|
|
||
10 years ago
|
static const char * g_LogLevelStr[eNumLogLevels] =
|
||
|
{
|
||
|
"error", // eLogError
|
||
|
"warn", // eLogWarning
|
||
|
"info", // eLogInfo
|
||
10 years ago
|
"debug" // eLogDebug
|
||
10 years ago
|
};
|
||
|
|
||
11 years ago
|
void LogMsg::Process()
|
||
|
{
|
||
10 years ago
|
auto& output = (log && log->GetLogStream ()) ? *log->GetLogStream () : std::cerr;
|
||
|
if (log)
|
||
10 years ago
|
output << log->GetTimestamp ();
|
||
|
else
|
||
|
output << boost::posix_time::second_clock::local_time().time_of_day ();
|
||
|
output << "/" << g_LogLevelStr[level] << " - ";
|
||
11 years ago
|
output << s.str();
|
||
11 years ago
|
}
|
||
|
|
||
10 years ago
|
const std::string& Log::GetTimestamp ()
|
||
|
{
|
||
10 years ago
|
#if (__GNUC__ == 4) && (__GNUC_MINOR__ <= 6) && !defined(__clang__)
|
||
10 years ago
|
auto ts = std::chrono::monotonic_clock::now ();
|
||
|
#else
|
||
|
auto ts = std::chrono::steady_clock::now ();
|
||
|
#endif
|
||
10 years ago
|
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 ());
|
||
10 years ago
|
}
|
||
10 years ago
|
return m_Timestamp;
|
||
|
}
|
||
|
|
||
11 years ago
|
void Log::Flush ()
|
||
|
{
|
||
10 years ago
|
if (m_LogStream)
|
||
|
m_LogStream->flush();
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
void Log::SetLogFile (const std::string& fullFilePath)
|
||
|
{
|
||
10 years ago
|
auto logFile = new std::ofstream (fullFilePath, std::ofstream::out | std::ofstream::binary | std::ofstream::trunc);
|
||
|
if (logFile->is_open ())
|
||
|
{
|
||
|
SetLogStream (logFile);
|
||
11 years ago
|
LogPrint("Logging to file ", fullFilePath, " enabled.");
|
||
10 years ago
|
}
|
||
11 years ago
|
else
|
||
10 years ago
|
delete logFile;
|
||
|
}
|
||
|
|
||
|
void Log::SetLogStream (std::ostream * logStream)
|
||
|
{
|
||
10 years ago
|
if (m_LogStream) delete m_LogStream;
|
||
10 years ago
|
m_LogStream = logStream;
|
||
11 years ago
|
}
|