Skip to content

Commit

Permalink
Avoid extra allocation when creating the LogMetaData
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
ktf authored and rbx committed May 23, 2024
1 parent fdbc047 commit 0ae9a69
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 65 deletions.
102 changes: 51 additions & 51 deletions logger/Logger.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* copied verbatim in the file "LICENSE" *
********************************************************************************/
#include "Logger.h"
#include <string_view>

#if FMT_VERSION < 60000
#include <fmt/time.h>
Expand Down Expand Up @@ -44,58 +45,58 @@ const string Logger::fProcessName = program_invocation_short_name;
const string Logger::fProcessName = "?";
#endif

const unordered_map<string, Verbosity> Logger::fVerbosityMap =
{
{ "veryhigh", Verbosity::veryhigh },
{ "high", Verbosity::high },
{ "medium", Verbosity::medium },
{ "low", Verbosity::low },
{ "verylow", Verbosity::verylow },
{ "VERYHIGH", Verbosity::veryhigh },
{ "HIGH", Verbosity::high },
{ "MEDIUM", Verbosity::medium },
{ "LOW", Verbosity::low },
{ "VERYLOW", Verbosity::verylow },
{ "user1", Verbosity::user1 },
{ "user2", Verbosity::user2 },
{ "user3", Verbosity::user3 },
{ "user4", Verbosity::user4 }
const unordered_map<string_view, Verbosity> Logger::fVerbosityMap =
{
{ {"veryhigh"}, Verbosity::veryhigh },
{ {"high"}, Verbosity::high },
{ {"medium"}, Verbosity::medium },
{ {"low"}, Verbosity::low },
{ {"verylow"}, Verbosity::verylow },
{ {"VERYHIGH"}, Verbosity::veryhigh },
{ {"HIGH"}, Verbosity::high },
{ {"MEDIUM"}, Verbosity::medium },
{ {"LOW"}, Verbosity::low },
{ {"VERYLOW"}, Verbosity::verylow },
{ {"user1"}, Verbosity::user1 },
{ {"user2"}, Verbosity::user2 },
{ {"user3"}, Verbosity::user3 },
{ {"user4"}, Verbosity::user4 }
};

const unordered_map<string, Severity> Logger::fSeverityMap =
{
{ "nolog", Severity::nolog },
{ "NOLOG", Severity::nolog },
{ "fatal", Severity::fatal },
{ "FATAL", Severity::fatal },
{ "error", Severity::error },
{ "ERROR", Severity::error },
{ "alarm", Severity::alarm },
{ "important", Severity::important },
{ "warn", Severity::warn },
{ "WARN", Severity::warn },
{ "warning", Severity::warn },
{ "WARNING", Severity::warn },
{ "state", Severity::state },
{ "STATE", Severity::state },
{ "info", Severity::info },
{ "INFO", Severity::info },
{ "detail", Severity::detail },
{ "debug", Severity::debug },
{ "DEBUG", Severity::debug },
{ "debug1", Severity::debug1 },
{ "DEBUG1", Severity::debug1 },
{ "debug2", Severity::debug2 },
{ "DEBUG2", Severity::debug2 },
{ "debug3", Severity::debug3 },
{ "DEBUG3", Severity::debug3 },
{ "debug4", Severity::debug4 },
{ "DEBUG4", Severity::debug4 },
{ "trace", Severity::trace },
{ "TRACE", Severity::trace }
const unordered_map<string_view, Severity> Logger::fSeverityMap =
{
{ {"nolog"}, Severity::nolog },
{ {"NOLOG"}, Severity::nolog },
{ {"fatal"}, Severity::fatal },
{ {"FATAL"}, Severity::fatal },
{ {"error"}, Severity::error },
{ {"ERROR"}, Severity::error },
{ {"alarm"}, Severity::alarm },
{ {"important"}, Severity::important },
{ {"warn"}, Severity::warn },
{ {"WARN"}, Severity::warn },
{ {"warning"}, Severity::warn },
{ {"WARNING"}, Severity::warn },
{ {"state"}, Severity::state },
{ {"STATE"}, Severity::state },
{ {"info"}, Severity::info },
{ {"INFO"}, Severity::info },
{ {"detail"}, Severity::detail },
{ {"debug"}, Severity::debug },
{ {"DEBUG"}, Severity::debug },
{ {"debug1"}, Severity::debug1 },
{ {"DEBUG1"}, Severity::debug1 },
{ {"debug2"}, Severity::debug2 },
{ {"DEBUG2"}, Severity::debug2 },
{ {"debug3"}, Severity::debug3 },
{ {"DEBUG3"}, Severity::debug3 },
{ {"debug4"}, Severity::debug4 },
{ {"DEBUG4"}, Severity::debug4 },
{ {"trace"}, Severity::trace },
{ {"TRACE"}, Severity::trace }
};

const array<string, 15> Logger::fSeverityNames =
const array<string_view, 15> Logger::fSeverityNames =
{
{
"NOLOG",
Expand All @@ -116,7 +117,7 @@ const array<string, 15> Logger::fSeverityNames =
}
};

const array<string, 9> Logger::fVerbosityNames =
const array<string_view, 9> Logger::fVerbosityNames =
{
{
"verylow",
Expand Down Expand Up @@ -144,12 +145,11 @@ map<Verbosity, VSpec> Logger::fVerbosities =
{ 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)
{
if (!fIsDestructed) {
size_t pos = file.rfind("/");

// fInfos.timestamp is filled conditionally
// fInfos.us is filled conditionally
fInfos.process_name = fProcessName;
Expand Down
29 changes: 15 additions & 14 deletions logger/Logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#include <time.h> // time_t
#include <type_traits> // is_same
#include <unordered_map>
#include <string_view>
#include <utility> // pair

namespace fair
Expand Down Expand Up @@ -175,19 +176,19 @@ struct LogMetaData
{
std::time_t timestamp;
std::chrono::microseconds us;
std::string process_name;
std::string file;
std::string line;
std::string func;
std::string severity_name;
std::string_view process_name;
std::string_view file;
std::string_view line;
std::string_view func;
std::string_view severity_name;
fair::Severity severity;
};

class Logger
{
public:
Logger(Severity severity, Verbosity verbosity, const std::string& file, const std::string& line, const std::string& func);
Logger(Severity severity, 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, std::string_view file, std::string_view line, std::string_view func)
: Logger(severity, fVerbosity, file, line, func)
{}
virtual ~Logger() noexcept(false);
Expand Down Expand Up @@ -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 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 void SetConsoleSeverity(const Severity severity);
Expand Down Expand Up @@ -285,8 +286,8 @@ class Logger

static void RemoveFileSink();

static std::string 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 SeverityName(Severity s) { return fSeverityNames.at(static_cast<size_t>(s)); }
static std::string_view VerbosityName(Verbosity v) { return fVerbosityNames.at(static_cast<size_t>(v)); }

static void OnFatal(std::function<void()> func);

Expand Down Expand Up @@ -322,10 +323,10 @@ class Logger
Logger& operator<<(std::ios_base& (*manip) (std::ios_base&));
Logger& operator<<(std::ostream& (*manip) (std::ostream&));

static const std::unordered_map<std::string, Verbosity> fVerbosityMap;
static const std::unordered_map<std::string, Severity> fSeverityMap;
static const std::array<std::string, 15> fSeverityNames;
static const std::array<std::string, 9> fVerbosityNames;
static const std::unordered_map<std::string_view, Verbosity> fVerbosityMap;
static const std::unordered_map<std::string_view, Severity> fSeverityMap;
static const std::array<std::string_view, 15> fSeverityNames;
static const std::array<std::string_view, 9> fVerbosityNames;

// protection for use after static destruction took place
static bool fIsDestructed;
Expand Down

0 comments on commit 0ae9a69

Please sign in to comment.