From 8266ce11c8c31febd2b9aa6d6f7053a7e449da4c Mon Sep 17 00:00:00 2001 From: William JCM Date: Sun, 24 Apr 2022 16:35:50 +0200 Subject: [PATCH] Logger: improve for GUI display. The biggest change is that the timestamp is pre-computed, because calling strftime() 60 times (or more) per second is sure to have a non-negligible cost. --- src/Logger/Logger.cpp | 17 +++++++++++++---- src/Logger/Logger.h | 6 +++++- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/Logger/Logger.cpp b/src/Logger/Logger.cpp index 577e288..33b3402 100644 --- a/src/Logger/Logger.cpp +++ b/src/Logger/Logger.cpp @@ -45,9 +45,7 @@ inline Debug& operator<<(Debug& out, const LogEntry& entry) { using namespace Containers::Literals; - static char formatted_time[22] = {'\0'}; - std::strftime(&formatted_time[0], sizeof(formatted_time)/sizeof(formatted_time[0]), "[%Y-%m-%d %H:%M:%S]", &entry.timestamp); - out << formatted_time; + out << "["_s << Debug::nospace << entry.timestamp << Debug::nospace << "]"_s; #ifdef SAVETOOL_DEBUG_BUILD #define COLOUR(col) << Debug::color(Debug::Color::col) @@ -88,8 +86,14 @@ addEntry(EntryType type, StringView message) { auto time = std::time(nullptr); + static char formatted_time[20] = {'\0'}; + std::strftime(&formatted_time[0], sizeof(formatted_time)/sizeof(formatted_time[0]), + "%Y-%m-%d %H:%M:%S", // Because MSVCRT (which is the CRT I target by default) is not C99-compliant, + // I can't use "%F %T" as the format string. + std::localtime(&time)); + LogEntry entry{ - *std::localtime(&time), type, + formatted_time, type, (message.back() == '\n') ? String{message.exceptSuffix(1)} : String{message} }; @@ -106,6 +110,11 @@ addEntry(EntryType type, StringView message) { _logMutex.unlock(); } +ArrayView +entries() { + return _entries; +} + } // Logger } // MassBuilderSaveTool diff --git a/src/Logger/Logger.h b/src/Logger/Logger.h index a1abd36..795a9c1 100644 --- a/src/Logger/Logger.h +++ b/src/Logger/Logger.h @@ -19,9 +19,11 @@ #include #include +#include using namespace Corrade; +using Containers::ArrayView; using Containers::String; using Containers::StringView; @@ -35,7 +37,7 @@ enum class EntryType { }; struct LogEntry { - std::tm timestamp; + String timestamp; EntryType type; String message; }; @@ -44,6 +46,8 @@ void initialise(); void addEntry(EntryType type, StringView message); +auto entries() -> ArrayView; + } // Logger } // MassBuilderSaveTool