From 1c8203d83263d5e82ffb27aeccd56522069216c7 Mon Sep 17 00:00:00 2001 From: Nomango Date: Wed, 29 Jul 2020 15:52:49 +0800 Subject: [PATCH 1/3] disable imgui demo windows --- src/3rd-party/imgui/imconfig.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/3rd-party/imgui/imconfig.h b/src/3rd-party/imgui/imconfig.h index 825505bf..ba0a9599 100644 --- a/src/3rd-party/imgui/imconfig.h +++ b/src/3rd-party/imgui/imconfig.h @@ -26,7 +26,7 @@ //---- Don't implement demo windows functionality (ShowDemoWindow()/ShowStyleEditor()/ShowUserGuide() methods will be empty) //---- It is very strongly recommended to NOT disable the demo windows during development. Please read the comments in imgui_demo.cpp. -//#define IMGUI_DISABLE_DEMO_WINDOWS +#define IMGUI_DISABLE_DEMO_WINDOWS //---- Don't implement some functions to reduce linkage requirements. //#define IMGUI_DISABLE_WIN32_DEFAULT_CLIPBOARD_FUNCTIONS // [Win32] Don't implement default clipboard handler. Won't use and link with OpenClipboard/GetClipboardData/CloseClipboard etc. From a9c42d781fac51b71cf978f5e31e481a1f8353ee Mon Sep 17 00:00:00 2001 From: Nomango Date: Thu, 30 Jul 2020 16:04:28 +0800 Subject: [PATCH 2/3] update Singleton --- src/kiwano/core/Singleton.h | 42 +++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 23 deletions(-) diff --git a/src/kiwano/core/Singleton.h b/src/kiwano/core/Singleton.h index c067e1d4..70158f86 100644 --- a/src/kiwano/core/Singleton.h +++ b/src/kiwano/core/Singleton.h @@ -20,6 +20,7 @@ #pragma once #include +#include namespace kiwano { @@ -27,23 +28,6 @@ namespace kiwano template class Singleton { -protected: - Singleton() = default; - Singleton(const Singleton&) = delete; - Singleton& operator=(const Singleton&) = delete; - -private: - struct InstanceCreator - { - InstanceCreator() - { - (void)Singleton<_Ty>::GetInstancePtr(); - } - - inline void Dummy() const {} - }; - static InstanceCreator creator_; - public: using object_type = _Ty; @@ -56,11 +40,7 @@ public: static inline object_type* GetInstancePtr() { - creator_.Dummy(); - if (!instance_ptr_) - { - instance_ptr_.reset(new object_type); - } + std::call_once(once_, Singleton::Init); return instance_ptr_.get(); } @@ -68,10 +48,26 @@ public: { instance_ptr_.reset(); } + +protected: + Singleton() = default; + Singleton(const Singleton&) = delete; + Singleton& operator=(const Singleton&) = delete; + +private: + static inline void Init() + { + if (!instance_ptr_) + { + instance_ptr_.reset(new object_type); + } + } + + static std::once_flag once_; }; template -typename Singleton<_Ty>::InstanceCreator Singleton<_Ty>::creator_; +std::once_flag Singleton<_Ty>::once_; template typename std::unique_ptr<_Ty> Singleton<_Ty>::instance_ptr_; From 4911ee6977284533aedd55a25e3b2aa2bd314f4f Mon Sep 17 00:00:00 2001 From: Nomango Date: Thu, 30 Jul 2020 16:05:45 +0800 Subject: [PATCH 3/3] 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; };