From 4911ee6977284533aedd55a25e3b2aa2bd314f4f Mon Sep 17 00:00:00 2001 From: Nomango Date: Thu, 30 Jul 2020 16:05:45 +0800 Subject: [PATCH] update LogFormater --- src/kiwano/utils/Logger.cpp | 83 ++++++++++++++++++++++++++++++++----- src/kiwano/utils/Logger.h | 4 +- 2 files changed, 74 insertions(+), 13 deletions(-) diff --git a/src/kiwano/utils/Logger.cpp b/src/kiwano/utils/Logger.cpp index 47503dcf..ddecb5ba 100644 --- a/src/kiwano/utils/Logger.cpp +++ b/src/kiwano/utils/Logger.cpp @@ -52,19 +52,80 @@ String LogFormater::GetLevelLabel(LogLevel level) const class TextFormater : public LogFormater { -public: - void FormatHeader(std::ostream& out, LogLevel level, Time time) override +private: + struct TimeFormater { - // get timestamp - time_t unix = std::time(nullptr); - std::tm tmbuf; - localtime_s(&tmbuf, &unix); + TimeFormater() + { + time_t ctime = std::time(nullptr); + prev_sec_ = ctime; + prev_min_ = ctime / 60; + RefreshLocalTime(&ctime); + ResetFormat(); + } + + const char* Format(ClockTime* current_time) + { + time_t ctime = current_time->GetCTime(); + if (ctime != prev_sec_) + { + prev_sec_ = ctime; + tmbuf_.tm_sec = static_cast(ctime % 60); + time_t ctime_min = ctime / 60; + if (ctime_min != prev_min_) + { + prev_min_ = ctime_min; + + RefreshLocalTime(&ctime); + ResetFormat(); + } + else + { + ResetFormatSec(); + } + } + return time_format_; + } + + private: + void ResetFormat() + { + std::snprintf(time_format_, 20, "%d-%02d-%02d %02d:%02d:%02d", tmbuf_.tm_year + 1900, tmbuf_.tm_mon + 1, + tmbuf_.tm_mday, tmbuf_.tm_hour, tmbuf_.tm_min, tmbuf_.tm_sec); + } + + void ResetFormatSec() + { + std::snprintf(time_format_ + 17, 3, "%02d", tmbuf_.tm_sec); + } + + void RefreshLocalTime(const time_t* ptime) + { +#if defined(KGE_PLATFORM_WINDOWS) + ::localtime_s(&tmbuf_, ptime); +#else + std::tm* ptm = std::localtime(ptime); + ::memcpy(&tmbuf_, ptm, sizeof(std::tm)); +#endif + } + + time_t prev_sec_ = 0; + time_t prev_min_ = 0; + std::tm tmbuf_ = {}; + char time_format_[20] = {}; + }; + + TimeFormater tf_; + +public: + void FormatHeader(std::ostream& out, LogLevel level, ClockTime time) override + { // build message - out << GetLevelLabel(level) << std::put_time(&tmbuf, " %H:%M:%S "); + out << GetLevelLabel(level) << ' ' << tf_.Format(&time); } - void FormatFooter(std::ostream& out, LogLevel level, Time time) override + void FormatFooter(std::ostream& out, LogLevel level) override { out << "\n"; } @@ -510,7 +571,7 @@ std::iostream& Logger::GetFormatedStream(LogLevel level, LogBuffer* buffer) if (formater_) { - formater_->FormatHeader(stream_, level, Time::Now()); + formater_->FormatHeader(stream_, level, ClockTime::Now()); } return stream_; } @@ -534,7 +595,7 @@ void Logger::Logf(LogLevel level, const char* format, ...) // build message auto& stream = this->GetFormatedStream(level, &buffer_); - stream << strings::FormatArgs(format, args); + stream << ' ' << strings::FormatArgs(format, args); va_end(args); @@ -582,7 +643,7 @@ void Logger::WriteToProviders(LogLevel level, LogBuffer* buffer) // format footer if (formater_) { - formater_->FormatFooter(stream_, level, Time::Now()); + formater_->FormatFooter(stream_, level); } // write message diff --git a/src/kiwano/utils/Logger.h b/src/kiwano/utils/Logger.h index 81bf0a7e..591cf84e 100644 --- a/src/kiwano/utils/Logger.h +++ b/src/kiwano/utils/Logger.h @@ -132,9 +132,9 @@ enum class LogLevel class KGE_API LogFormater : public ObjectBase { public: - virtual void FormatHeader(std::ostream& out, LogLevel level, Time time) = 0; + virtual void FormatHeader(std::ostream& out, LogLevel level, ClockTime time) = 0; - virtual void FormatFooter(std::ostream& out, LogLevel level, Time time) = 0; + virtual void FormatFooter(std::ostream& out, LogLevel level) = 0; String GetLevelLabel(LogLevel level) const; };