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.
This commit is contained in:
Guillaume Jacquemin 2022-04-24 16:35:50 +02:00
parent dd6d2491fd
commit 8266ce11c8
2 changed files with 18 additions and 5 deletions

View file

@ -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<const LogEntry>
entries() {
return _entries;
}
} // Logger
} // MassBuilderSaveTool

View file

@ -19,9 +19,11 @@
#include <ctime>
#include <Corrade/Containers/String.h>
#include <Corrade/Containers/ArrayView.h>
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<const LogEntry>;
} // Logger
} // MassBuilderSaveTool