diff options
author | Peter Bell <peterbell10@live.co.uk> | 2020-05-15 04:35:43 +0200 |
---|---|---|
committer | Tiger Wang <ziwei.tiger@outlook.com> | 2020-05-16 10:39:05 +0200 |
commit | 3189a3cbee8eb9d7ded7605776a75f35e5e2f122 (patch) | |
tree | 616ed3031baa99e1f34123b645c5b3cb6ed73223 /src/Logger.cpp | |
parent | Enable some more clang-tidy linter checks (#4738) (diff) | |
download | cuberite-3189a3cbee8eb9d7ded7605776a75f35e5e2f122.tar cuberite-3189a3cbee8eb9d7ded7605776a75f35e5e2f122.tar.gz cuberite-3189a3cbee8eb9d7ded7605776a75f35e5e2f122.tar.bz2 cuberite-3189a3cbee8eb9d7ded7605776a75f35e5e2f122.tar.lz cuberite-3189a3cbee8eb9d7ded7605776a75f35e5e2f122.tar.xz cuberite-3189a3cbee8eb9d7ded7605776a75f35e5e2f122.tar.zst cuberite-3189a3cbee8eb9d7ded7605776a75f35e5e2f122.zip |
Diffstat (limited to 'src/Logger.cpp')
-rw-r--r-- | src/Logger.cpp | 145 |
1 files changed, 71 insertions, 74 deletions
diff --git a/src/Logger.cpp b/src/Logger.cpp index a52ca471d..500007a84 100644 --- a/src/Logger.cpp +++ b/src/Logger.cpp @@ -11,6 +11,38 @@ +static void WriteLogOpener(fmt::memory_buffer & Buffer) +{ + time_t rawtime; + time(&rawtime); + + struct tm * timeinfo; +#ifdef _MSC_VER + struct tm timeinforeal; + timeinfo = &timeinforeal; + localtime_s(timeinfo, &rawtime); +#else + timeinfo = localtime(&rawtime); +#endif + +#ifdef _DEBUG + const auto ThreadID = std::hash<std::thread::id>()(std::this_thread::get_id()); + fmt::format_to( + Buffer, "[{0:04x}|{1:02d}:{2:02d}:{3:02d}] ", + ThreadID, timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec + ); +#else + fmt::format_to( + Buffer, "[{0:02d}:{1:02d}:{2:02d}] ", + timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec + ); +#endif +} + + + + + cLogger & cLogger::GetInstance(void) { static cLogger Instance; @@ -30,32 +62,24 @@ void cLogger::InitiateMultithreading() -void cLogger::LogSimple(const AString & a_Message, eLogLevel a_LogLevel) +void cLogger::LogSimple(std::string_view a_Message, eLogLevel a_LogLevel) { - time_t rawtime; - time(&rawtime); + fmt::memory_buffer Buffer; + WriteLogOpener(Buffer); + fmt::format_to(Buffer, "{0}\n", a_Message); + LogLine(std::string_view(Buffer.data(), Buffer.size()), a_LogLevel); +} + - struct tm * timeinfo; - #ifdef _MSC_VER - struct tm timeinforeal; - timeinfo = &timeinforeal; - localtime_s(timeinfo, &rawtime); - #else - timeinfo = localtime(&rawtime); - #endif - - AString Line; - #ifdef _DEBUG - Printf(Line, "[%04llx|%02d:%02d:%02d] %s\n", static_cast<UInt64>(std::hash<std::thread::id>()(std::this_thread::get_id())), timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec, a_Message); - #else - Printf(Line, "[%02d:%02d:%02d] %s\n", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec, a_Message); - #endif + +void cLogger::LogLine(std::string_view a_Line, eLogLevel a_LogLevel) +{ cCSLock Lock(m_CriticalSection); for (size_t i = 0; i < m_LogListeners.size(); i++) { - m_LogListeners[i]->Log(Line, a_LogLevel); + m_LogListeners[i]->Log(a_Line, a_LogLevel); } } @@ -63,18 +87,32 @@ void cLogger::LogSimple(const AString & a_Message, eLogLevel a_LogLevel) -void cLogger::vLogPrintf(const char * a_Format, eLogLevel a_LogLevel, fmt::printf_args a_ArgList) +void cLogger::LogPrintf( + std::string_view a_Format, eLogLevel a_LogLevel, fmt::printf_args a_ArgList +) { - LogSimple(vPrintf(a_Format, a_ArgList), a_LogLevel); + fmt::memory_buffer Buffer; + WriteLogOpener(Buffer); + fmt::printf(Buffer, fmt::to_string_view(a_Format), a_ArgList); + fmt::format_to(Buffer, "\n"); + + LogLine(std::string_view(Buffer.data(), Buffer.size()), a_LogLevel); } -void cLogger::vLogFormat(const char * a_Format, eLogLevel a_LogLevel, fmt::format_args a_ArgList) +void cLogger::LogFormat( + std::string_view a_Format, eLogLevel a_LogLevel, fmt::format_args a_ArgList +) { - LogSimple(fmt::vformat(a_Format, a_ArgList), a_LogLevel); + fmt::memory_buffer Buffer; + WriteLogOpener(Buffer); + fmt::vformat_to(Buffer, a_Format, a_ArgList); + fmt::format_to(Buffer, "\n"); + + LogLine(std::string_view(Buffer.data(), Buffer.size()), a_LogLevel); } @@ -117,72 +155,31 @@ void cLogger::DetachListener(cListener * a_Listener) //////////////////////////////////////////////////////////////////////////////// // Global functions -void vFLOG(const char * a_Format, fmt::format_args a_ArgList) -{ - cLogger::GetInstance().vLogFormat(a_Format, cLogger::llRegular, a_ArgList); -} - - - - - -void vFLOGINFO(const char * a_Format, fmt::format_args a_ArgList) -{ - cLogger::GetInstance().vLogFormat( a_Format, cLogger::llInfo, a_ArgList); -} - - - - - -void vFLOGWARNING(const char * a_Format, fmt::format_args a_ArgList) -{ - cLogger::GetInstance().vLogFormat( a_Format, cLogger::llWarning, a_ArgList); -} - - - - - -void vFLOGERROR(const char * a_Format, fmt::format_args a_ArgList) -{ - cLogger::GetInstance().vLogFormat(a_Format, cLogger::llError, a_ArgList); -} - - - - - -void vLOG(const char * a_Format, fmt::printf_args a_ArgList) -{ - cLogger::GetInstance().vLogPrintf(a_Format, cLogger::llRegular, a_ArgList); -} - - - - - -void vLOGINFO(const char * a_Format, fmt::printf_args a_ArgList) +void Logger::LogFormat( + std::string_view a_Format, eLogLevel a_LogLevel, fmt::format_args a_ArgList +) { - cLogger::GetInstance().vLogPrintf(a_Format, cLogger::llInfo, a_ArgList); + cLogger::GetInstance().LogFormat(a_Format, a_LogLevel, a_ArgList); } -void vLOGWARNING(const char * a_Format, fmt::printf_args a_ArgList) +void Logger::LogPrintf( + std::string_view a_Format, eLogLevel a_LogLevel, fmt::printf_args a_ArgList +) { - cLogger::GetInstance().vLogPrintf(a_Format, cLogger::llWarning, a_ArgList); + cLogger::GetInstance().LogPrintf(a_Format, a_LogLevel, a_ArgList); } -void vLOGERROR(const char * a_Format, fmt::printf_args a_ArgList) +void Logger::LogSimple(std::string_view a_Message, eLogLevel a_LogLevel) { - cLogger::GetInstance().vLogPrintf( a_Format, cLogger::llError, a_ArgList); + cLogger::GetInstance().LogSimple(a_Message, a_LogLevel); } |