diff --git a/logger/Logger.cxx b/logger/Logger.cxx index 8ac5d1e..3d6cd3e 100644 --- a/logger/Logger.cxx +++ b/logger/Logger.cxx @@ -6,6 +6,7 @@ * copied verbatim in the file "LICENSE" * ********************************************************************************/ #include "Logger.h" +#include #if FMT_VERSION < 60000 #include @@ -44,58 +45,58 @@ const string Logger::fProcessName = program_invocation_short_name; const string Logger::fProcessName = "?"; #endif -const unordered_map 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 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 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 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 Logger::fSeverityNames = +const array Logger::fSeverityNames = { { "NOLOG", @@ -116,7 +117,7 @@ const array Logger::fSeverityNames = } }; -const array Logger::fVerbosityNames = +const array Logger::fVerbosityNames = { { "verylow", @@ -144,12 +145,11 @@ map 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; diff --git a/logger/Logger.h b/logger/Logger.h index e1c8a78..a49501d 100644 --- a/logger/Logger.h +++ b/logger/Logger.h @@ -45,6 +45,7 @@ #include // time_t #include // is_same #include +#include #include // pair namespace fair @@ -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); @@ -244,7 +245,7 @@ class Logger static std::string startColor(Color color) { return fmt::format("\033[01;{}m", static_cast(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(c), s); } + static std::string ColorOut(Color c, std::string_view s) { return fmt::format("\033[01;{}m{}\033[0m", static_cast(c), s); } static std::string GetColoredSeverityString(Severity severity); static void SetConsoleSeverity(const Severity severity); @@ -285,8 +286,8 @@ class Logger static void RemoveFileSink(); - static std::string SeverityName(Severity s) { return fSeverityNames.at(static_cast(s)); } - static std::string VerbosityName(Verbosity v) { return fVerbosityNames.at(static_cast(v)); } + static std::string_view SeverityName(Severity s) { return fSeverityNames.at(static_cast(s)); } + static std::string_view VerbosityName(Verbosity v) { return fVerbosityNames.at(static_cast(v)); } static void OnFatal(std::function func); @@ -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 fVerbosityMap; - static const std::unordered_map fSeverityMap; - static const std::array fSeverityNames; - static const std::array fVerbosityNames; + static const std::unordered_map fVerbosityMap; + static const std::unordered_map fSeverityMap; + static const std::array fSeverityNames; + static const std::array fVerbosityNames; // protection for use after static destruction took place static bool fIsDestructed;