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:
parent
dd6d2491fd
commit
8266ce11c8
2 changed files with 18 additions and 5 deletions
|
@ -45,9 +45,7 @@ inline Debug&
|
||||||
operator<<(Debug& out, const LogEntry& entry) {
|
operator<<(Debug& out, const LogEntry& entry) {
|
||||||
using namespace Containers::Literals;
|
using namespace Containers::Literals;
|
||||||
|
|
||||||
static char formatted_time[22] = {'\0'};
|
out << "["_s << Debug::nospace << entry.timestamp << Debug::nospace << "]"_s;
|
||||||
std::strftime(&formatted_time[0], sizeof(formatted_time)/sizeof(formatted_time[0]), "[%Y-%m-%d %H:%M:%S]", &entry.timestamp);
|
|
||||||
out << formatted_time;
|
|
||||||
|
|
||||||
#ifdef SAVETOOL_DEBUG_BUILD
|
#ifdef SAVETOOL_DEBUG_BUILD
|
||||||
#define COLOUR(col) << Debug::color(Debug::Color::col)
|
#define COLOUR(col) << Debug::color(Debug::Color::col)
|
||||||
|
@ -88,8 +86,14 @@ addEntry(EntryType type, StringView message) {
|
||||||
|
|
||||||
auto time = std::time(nullptr);
|
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{
|
LogEntry entry{
|
||||||
*std::localtime(&time), type,
|
formatted_time, type,
|
||||||
(message.back() == '\n') ? String{message.exceptSuffix(1)} : String{message}
|
(message.back() == '\n') ? String{message.exceptSuffix(1)} : String{message}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -106,6 +110,11 @@ addEntry(EntryType type, StringView message) {
|
||||||
_logMutex.unlock();
|
_logMutex.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ArrayView<const LogEntry>
|
||||||
|
entries() {
|
||||||
|
return _entries;
|
||||||
|
}
|
||||||
|
|
||||||
} // Logger
|
} // Logger
|
||||||
|
|
||||||
} // MassBuilderSaveTool
|
} // MassBuilderSaveTool
|
||||||
|
|
|
@ -19,9 +19,11 @@
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
|
|
||||||
#include <Corrade/Containers/String.h>
|
#include <Corrade/Containers/String.h>
|
||||||
|
#include <Corrade/Containers/ArrayView.h>
|
||||||
|
|
||||||
using namespace Corrade;
|
using namespace Corrade;
|
||||||
|
|
||||||
|
using Containers::ArrayView;
|
||||||
using Containers::String;
|
using Containers::String;
|
||||||
using Containers::StringView;
|
using Containers::StringView;
|
||||||
|
|
||||||
|
@ -35,7 +37,7 @@ enum class EntryType {
|
||||||
};
|
};
|
||||||
|
|
||||||
struct LogEntry {
|
struct LogEntry {
|
||||||
std::tm timestamp;
|
String timestamp;
|
||||||
EntryType type;
|
EntryType type;
|
||||||
String message;
|
String message;
|
||||||
};
|
};
|
||||||
|
@ -44,6 +46,8 @@ void initialise();
|
||||||
|
|
||||||
void addEntry(EntryType type, StringView message);
|
void addEntry(EntryType type, StringView message);
|
||||||
|
|
||||||
|
auto entries() -> ArrayView<const LogEntry>;
|
||||||
|
|
||||||
} // Logger
|
} // Logger
|
||||||
|
|
||||||
} // MassBuilderSaveTool
|
} // MassBuilderSaveTool
|
||||||
|
|
Loading…
Reference in a new issue