Avoid extra allocation when creating the LogMetaData

By using a std::string_view for LogMetaData strings we avoid an implicit
memory allocation when passing __file__, etc. which are char const*
(notice this is also the case for the standardized
std::source_location::file_name etc).
This commit is contained in:
Giulio Eulisse 2024-05-23 09:17:34 +02:00
parent d24e2cbeda
commit 635f184a6b
2 changed files with 64 additions and 63 deletions

View File

@ -6,6 +6,7 @@
* copied verbatim in the file "LICENSE" * * copied verbatim in the file "LICENSE" *
********************************************************************************/ ********************************************************************************/
#include "Logger.h" #include "Logger.h"
#include <string_view>
#if FMT_VERSION < 60000 #if FMT_VERSION < 60000
#include <fmt/time.h> #include <fmt/time.h>
@ -44,58 +45,58 @@ const string Logger::fProcessName = program_invocation_short_name;
const string Logger::fProcessName = "?"; const string Logger::fProcessName = "?";
#endif #endif
const unordered_map<string, Verbosity> Logger::fVerbosityMap = const unordered_map<string_view, Verbosity> Logger::fVerbosityMap =
{ {
{ "veryhigh", Verbosity::veryhigh }, { {"veryhigh"}, Verbosity::veryhigh },
{ "high", Verbosity::high }, { {"high"}, Verbosity::high },
{ "medium", Verbosity::medium }, { {"medium"}, Verbosity::medium },
{ "low", Verbosity::low }, { {"low"}, Verbosity::low },
{ "verylow", Verbosity::verylow }, { {"verylow"}, Verbosity::verylow },
{ "VERYHIGH", Verbosity::veryhigh }, { {"VERYHIGH"}, Verbosity::veryhigh },
{ "HIGH", Verbosity::high }, { {"HIGH"}, Verbosity::high },
{ "MEDIUM", Verbosity::medium }, { {"MEDIUM"}, Verbosity::medium },
{ "LOW", Verbosity::low }, { {"LOW"}, Verbosity::low },
{ "VERYLOW", Verbosity::verylow }, { {"VERYLOW"}, Verbosity::verylow },
{ "user1", Verbosity::user1 }, { {"user1"}, Verbosity::user1 },
{ "user2", Verbosity::user2 }, { {"user2"}, Verbosity::user2 },
{ "user3", Verbosity::user3 }, { {"user3"}, Verbosity::user3 },
{ "user4", Verbosity::user4 } { {"user4"}, Verbosity::user4 }
}; };
const unordered_map<string, Severity> Logger::fSeverityMap = const unordered_map<string_view, Severity> Logger::fSeverityMap =
{ {
{ "nolog", Severity::nolog }, { {"nolog"}, Severity::nolog },
{ "NOLOG", Severity::nolog }, { {"NOLOG"}, Severity::nolog },
{ "fatal", Severity::fatal }, { {"fatal"}, Severity::fatal },
{ "FATAL", Severity::fatal }, { {"FATAL"}, Severity::fatal },
{ "error", Severity::error }, { {"error"}, Severity::error },
{ "ERROR", Severity::error }, { {"ERROR"}, Severity::error },
{ "alarm", Severity::alarm }, { {"alarm"}, Severity::alarm },
{ "important", Severity::important }, { {"important"}, Severity::important },
{ "warn", Severity::warn }, { {"warn"}, Severity::warn },
{ "WARN", Severity::warn }, { {"WARN"}, Severity::warn },
{ "warning", Severity::warn }, { {"warning"}, Severity::warn },
{ "WARNING", Severity::warn }, { {"WARNING"}, Severity::warn },
{ "state", Severity::state }, { {"state"}, Severity::state },
{ "STATE", Severity::state }, { {"STATE"}, Severity::state },
{ "info", Severity::info }, { {"info"}, Severity::info },
{ "INFO", Severity::info }, { {"INFO"}, Severity::info },
{ "detail", Severity::detail }, { {"detail"}, Severity::detail },
{ "debug", Severity::debug }, { {"debug"}, Severity::debug },
{ "DEBUG", Severity::debug }, { {"DEBUG"}, Severity::debug },
{ "debug1", Severity::debug1 }, { {"debug1"}, Severity::debug1 },
{ "DEBUG1", Severity::debug1 }, { {"DEBUG1"}, Severity::debug1 },
{ "debug2", Severity::debug2 }, { {"debug2"}, Severity::debug2 },
{ "DEBUG2", Severity::debug2 }, { {"DEBUG2"}, Severity::debug2 },
{ "debug3", Severity::debug3 }, { {"debug3"}, Severity::debug3 },
{ "DEBUG3", Severity::debug3 }, { {"DEBUG3"}, Severity::debug3 },
{ "debug4", Severity::debug4 }, { {"debug4"}, Severity::debug4 },
{ "DEBUG4", Severity::debug4 }, { {"DEBUG4"}, Severity::debug4 },
{ "trace", Severity::trace }, { {"trace"}, Severity::trace },
{ "TRACE", Severity::trace } { {"TRACE"}, Severity::trace }
}; };
const array<string, 15> Logger::fSeverityNames = const array<string_view, 15> Logger::fSeverityNames =
{ {
{ {
"NOLOG", "NOLOG",
@ -116,7 +117,7 @@ const array<string, 15> Logger::fSeverityNames =
} }
}; };
const array<string, 9> Logger::fVerbosityNames = const array<string_view, 9> Logger::fVerbosityNames =
{ {
{ {
"verylow", "verylow",
@ -144,12 +145,11 @@ map<Verbosity, VSpec> Logger::fVerbosities =
{ Verbosity::user4, VSpec::Make(VSpec::Info::severity) } { Verbosity::user4, VSpec::Make(VSpec::Info::severity) }
}; };
Logger::Logger(Severity severity, Verbosity verbosity, const string& file, const string& line, const string& func) Logger::Logger(Severity severity, Verbosity verbosity, std::string_view file, std::string_view line, std::string_view func)
: fTimeCalculated(false) : fTimeCalculated(false)
{ {
if (!fIsDestructed) { if (!fIsDestructed) {
size_t pos = file.rfind("/"); size_t pos = file.rfind("/");
// fInfos.timestamp is filled conditionally // fInfos.timestamp is filled conditionally
// fInfos.us is filled conditionally // fInfos.us is filled conditionally
fInfos.process_name = fProcessName; fInfos.process_name = fProcessName;

View File

@ -45,6 +45,7 @@
#include <time.h> // time_t #include <time.h> // time_t
#include <type_traits> // is_same #include <type_traits> // is_same
#include <unordered_map> #include <unordered_map>
#include <string_view>
#include <utility> // pair #include <utility> // pair
namespace fair namespace fair
@ -175,19 +176,19 @@ struct LogMetaData
{ {
std::time_t timestamp; std::time_t timestamp;
std::chrono::microseconds us; std::chrono::microseconds us;
std::string process_name; std::string_view process_name;
std::string file; std::string_view file;
std::string line; std::string_view line;
std::string func; std::string_view func;
std::string severity_name; std::string_view severity_name;
fair::Severity severity; fair::Severity severity;
}; };
class Logger class Logger
{ {
public: public:
Logger(Severity severity, Verbosity verbosity, const std::string& file, const std::string& line, const std::string& func); Logger(Severity severity, Verbosity verbosity, std::string_view file, std::string_view line, std::string_view func);
Logger(Severity severity, const std::string& file, const std::string& line, const std::string& func) Logger(Severity severity, std::string_view file, std::string_view line, std::string_view func)
: Logger(severity, fVerbosity, file, line, func) : Logger(severity, fVerbosity, file, line, func)
{} {}
virtual ~Logger() noexcept(false); virtual ~Logger() noexcept(false);
@ -244,7 +245,7 @@ class Logger
static std::string startColor(Color color) { return fmt::format("\033[01;{}m", static_cast<int>(color)); } static std::string startColor(Color color) { return fmt::format("\033[01;{}m", static_cast<int>(color)); }
static std::string endColor() { return "\033[0m"; } static std::string endColor() { return "\033[0m"; }
static std::string ColorOut(Color c, const std::string& s) { return fmt::format("\033[01;{}m{}\033[0m", static_cast<int>(c), s); } static std::string ColorOut(Color c, std::string_view s) { return fmt::format("\033[01;{}m{}\033[0m", static_cast<int>(c), s); }
static std::string GetColoredSeverityString(Severity severity); static std::string GetColoredSeverityString(Severity severity);
static void SetConsoleSeverity(const Severity severity); static void SetConsoleSeverity(const Severity severity);
@ -285,8 +286,8 @@ class Logger
static void RemoveFileSink(); static void RemoveFileSink();
static std::string SeverityName(Severity s) { return fSeverityNames.at(static_cast<size_t>(s)); } static std::string_view SeverityName(Severity s) { return fSeverityNames.at(static_cast<size_t>(s)); }
static std::string VerbosityName(Verbosity v) { return fVerbosityNames.at(static_cast<size_t>(v)); } static std::string_view VerbosityName(Verbosity v) { return fVerbosityNames.at(static_cast<size_t>(v)); }
static void OnFatal(std::function<void()> func); static void OnFatal(std::function<void()> func);
@ -322,10 +323,10 @@ class Logger
Logger& operator<<(std::ios_base& (*manip) (std::ios_base&)); Logger& operator<<(std::ios_base& (*manip) (std::ios_base&));
Logger& operator<<(std::ostream& (*manip) (std::ostream&)); Logger& operator<<(std::ostream& (*manip) (std::ostream&));
static const std::unordered_map<std::string, Verbosity> fVerbosityMap; static const std::unordered_map<std::string_view, Verbosity> fVerbosityMap;
static const std::unordered_map<std::string, Severity> fSeverityMap; static const std::unordered_map<std::string_view, Severity> fSeverityMap;
static const std::array<std::string, 15> fSeverityNames; static const std::array<std::string_view, 15> fSeverityNames;
static const std::array<std::string, 9> fVerbosityNames; static const std::array<std::string_view, 9> fVerbosityNames;
// protection for use after static destruction took place // protection for use after static destruction took place
static bool fIsDestructed; static bool fIsDestructed;