Compare commits

..

5 Commits

Author SHA1 Message Date
Alexey Rybalchenko
27527ad87b Add critical severity level between fatal and error 2025-02-06 15:20:41 +01:00
Dennis Klein
f3d68fb4ba build: Adopt all CMake policies up to 3.30 2024-08-20 15:56:57 +02:00
Giulio Eulisse
0ae9a697b4 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).
2024-05-23 19:47:55 +02:00
Giulio Eulisse
fdbc047591 Bump minimum required C++ standard to C++17 2024-05-23 19:47:55 +02:00
Alexey Rybalchenko
ea4067c474 Update CI nodes 2024-02-22 10:31:38 +01:00
9 changed files with 82 additions and 73 deletions

View File

@@ -1,5 +1,5 @@
################################################################################ ################################################################################
# Copyright (C) 2018-2021 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH # # Copyright (C) 2018-2024 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH #
# # # #
# This software is distributed under the terms of the # # This software is distributed under the terms of the #
# GNU Lesser General Public Licence (LGPL) version 3, # # GNU Lesser General Public Licence (LGPL) version 3, #
@@ -7,7 +7,7 @@
################################################################################ ################################################################################
cmake_minimum_required(VERSION 3.9.4 FATAL_ERROR) cmake_minimum_required(VERSION 3.9.4 FATAL_ERROR)
cmake_policy(VERSION 3.9...3.19) cmake_policy(VERSION 3.9...3.30)
# Project ###################################################################### # Project ######################################################################
set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake) set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake)
@@ -54,7 +54,7 @@ add_library(FairLogger
logger/Logger.cxx logger/Logger.cxx
logger/Logger.h logger/Logger.h
) )
target_compile_features(FairLogger PUBLIC cxx_std_14) target_compile_features(FairLogger PUBLIC cxx_std_17)
if(USE_BOOST_PRETTY_FUNCTION) if(USE_BOOST_PRETTY_FUNCTION)
target_link_libraries(FairLogger PUBLIC Boost::boost) target_link_libraries(FairLogger PUBLIC Boost::boost)

View File

@@ -100,6 +100,7 @@ where severity level is one of the following:
"important", "important",
"alarm", "alarm",
"error", "error",
"critical",
"fatal", "fatal",
``` ```

View File

@@ -94,7 +94,7 @@ macro(set_fairlogger_defaults)
endif() endif()
# Handle C++ standard level # Handle C++ standard level
set(PROJECT_MIN_CXX_STANDARD 14) set(PROJECT_MIN_CXX_STANDARD 17)
if(CMAKE_CXX_STANDARD LESS PROJECT_MIN_CXX_STANDARD) if(CMAKE_CXX_STANDARD LESS PROJECT_MIN_CXX_STANDARD)
message(FATAL_ERROR "A minimum CMAKE_CXX_STANDARD of ${PROJECT_MIN_CXX_STANDARD} is required.") message(FATAL_ERROR "A minimum CMAKE_CXX_STANDARD of ${PROJECT_MIN_CXX_STANDARD} is required.")
endif() endif()

View File

@@ -1,11 +1,12 @@
/******************************************************************************** /********************************************************************************
* Copyright (C) 2014-2019 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * * Copyright (C) 2014-2025 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
* * * *
* This software is distributed under the terms of the * * This software is distributed under the terms of the *
* GNU Lesser General Public Licence (LGPL) version 3, * * GNU Lesser General Public Licence (LGPL) version 3, *
* 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,59 @@ 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 }, { {"critical"}, Severity::critical },
{ "ERROR", Severity::error }, { {"error"}, Severity::error },
{ "alarm", Severity::alarm }, { {"ERROR"}, Severity::error },
{ "important", Severity::important }, { {"alarm"}, Severity::alarm },
{ "warn", Severity::warn }, { {"important"}, Severity::important },
{ "WARN", Severity::warn }, { {"warn"}, Severity::warn },
{ "warning", Severity::warn }, { {"WARN"}, Severity::warn },
{ "WARNING", Severity::warn }, { {"warning"}, Severity::warn },
{ "state", Severity::state }, { {"WARNING"}, Severity::warn },
{ "STATE", Severity::state }, { {"state"}, Severity::state },
{ "info", Severity::info }, { {"STATE"}, Severity::state },
{ "INFO", Severity::info }, { {"info"}, Severity::info },
{ "detail", Severity::detail }, { {"INFO"}, Severity::info },
{ "debug", Severity::debug }, { {"detail"}, Severity::detail },
{ "DEBUG", Severity::debug }, { {"debug"}, Severity::debug },
{ "debug1", Severity::debug1 }, { {"DEBUG"}, Severity::debug },
{ "DEBUG1", Severity::debug1 }, { {"debug1"}, Severity::debug1 },
{ "debug2", Severity::debug2 }, { {"DEBUG1"}, Severity::debug1 },
{ "DEBUG2", Severity::debug2 }, { {"debug2"}, Severity::debug2 },
{ "debug3", Severity::debug3 }, { {"DEBUG2"}, Severity::debug2 },
{ "DEBUG3", Severity::debug3 }, { {"debug3"}, Severity::debug3 },
{ "debug4", Severity::debug4 }, { {"DEBUG3"}, Severity::debug3 },
{ "DEBUG4", Severity::debug4 }, { {"debug4"}, Severity::debug4 },
{ "trace", Severity::trace }, { {"DEBUG4"}, Severity::debug4 },
{ "TRACE", Severity::trace } { {"trace"}, Severity::trace },
{ {"TRACE"}, Severity::trace }
}; };
const array<string, 15> Logger::fSeverityNames = const array<string_view, 16> Logger::fSeverityNames =
{ {
{ {
"NOLOG", "NOLOG",
@@ -112,11 +114,12 @@ const array<string, 15> Logger::fSeverityNames =
"IMPORTANT", "IMPORTANT",
"ALARM", "ALARM",
"ERROR", "ERROR",
"CRITICAL",
"FATAL" "FATAL"
} }
}; };
const array<string, 9> Logger::fVerbosityNames = const array<string_view, 9> Logger::fVerbosityNames =
{ {
{ {
"verylow", "verylow",
@@ -144,12 +147,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;
@@ -289,6 +291,7 @@ string Logger::GetColoredSeverityString(Severity severity)
switch (severity) { switch (severity) {
case Severity::nolog: return "\033[01;39mNOLOG\033[0m"; break; case Severity::nolog: return "\033[01;39mNOLOG\033[0m"; break;
case Severity::fatal: return "\033[01;31mFATAL\033[0m"; break; case Severity::fatal: return "\033[01;31mFATAL\033[0m"; break;
case Severity::critical: return "\033[01;31mCRITICAL\033[0m"; break;
case Severity::error: return "\033[01;31mERROR\033[0m"; break; case Severity::error: return "\033[01;31mERROR\033[0m"; break;
case Severity::alarm: return "\033[01;33mALARM\033[0m"; break; case Severity::alarm: return "\033[01;33mALARM\033[0m"; break;
case Severity::important: return "\033[01;32mIMPORTANT\033[0m"; break; case Severity::important: return "\033[01;32mIMPORTANT\033[0m"; break;

View File

@@ -1,5 +1,5 @@
/******************************************************************************** /********************************************************************************
* Copyright (C) 2014-2019 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * * Copyright (C) 2014-2025 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
* * * *
* This software is distributed under the terms of the * * This software is distributed under the terms of the *
* GNU Lesser General Public Licence (LGPL) version 3, * * GNU Lesser General Public Licence (LGPL) version 3, *
@@ -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
@@ -66,7 +67,8 @@ enum class Severity : int
important = 11, important = 11,
alarm = 12, alarm = 12,
error = 13, error = 13,
fatal = 14, critical = 14,
fatal = 15,
// aliases // aliases
warning = warn, warning = warn,
// backwards-compatibility // backwards-compatibility
@@ -175,19 +177,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 +246,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 +287,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 +324,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, 16> 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;

View File

@@ -1,5 +1,5 @@
/******************************************************************************** /********************************************************************************
* Copyright (C) 2015-2020 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * * Copyright (C) 2015-2025 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
* * * *
* This software is distributed under the terms of the * * This software is distributed under the terms of the *
* GNU Lesser General Public Licence (LGPL) version 3, * * GNU Lesser General Public Licence (LGPL) version 3, *
@@ -38,7 +38,7 @@ int main()
Logger::SetConsoleSeverity(Severity::fatal); Logger::SetConsoleSeverity(Severity::fatal);
cout << "initial severity >" << Logger::GetConsoleSeverity() << "<" << endl << endl; cout << "initial severity >" << Logger::GetConsoleSeverity() << "<" << endl << endl;
array<Severity, 15> severitiesUp{{ Severity::nolog, Severity::trace, Severity::debug4, Severity::debug3, Severity::debug2, Severity::debug1, Severity::debug, Severity::detail, Severity::info, Severity::state, Severity::warn, Severity::important, Severity::alarm, Severity::error, Severity::fatal }}; array<Severity, 16> severitiesUp{{ Severity::nolog, Severity::trace, Severity::debug4, Severity::debug3, Severity::debug2, Severity::debug1, Severity::debug, Severity::detail, Severity::info, Severity::state, Severity::warn, Severity::important, Severity::alarm, Severity::error, Severity::critical, Severity::fatal }};
#ifdef FAIR_MIN_SEVERITY #ifdef FAIR_MIN_SEVERITY
for (unsigned int i = static_cast<int>(Severity::FAIR_MIN_SEVERITY); i < severitiesUp.size(); ++i) { for (unsigned int i = static_cast<int>(Severity::FAIR_MIN_SEVERITY); i < severitiesUp.size(); ++i) {
#else #else
@@ -57,7 +57,7 @@ int main()
Logger::SetConsoleSeverity(Severity::fatal); Logger::SetConsoleSeverity(Severity::fatal);
cout << "initial severity >" << Logger::GetConsoleSeverity() << "<" << endl << endl; cout << "initial severity >" << Logger::GetConsoleSeverity() << "<" << endl << endl;
array<Severity, 15> severitiesDown{{ Severity::error, Severity::alarm, Severity::important, Severity::warn, Severity::state, Severity::info, Severity::detail, Severity::debug, Severity::debug1, Severity::debug2, Severity::debug3, Severity::debug4, Severity::trace, Severity::nolog, Severity::fatal }}; array<Severity, 16> severitiesDown{{ Severity::critical, Severity::error, Severity::alarm, Severity::important, Severity::warn, Severity::state, Severity::info, Severity::detail, Severity::debug, Severity::debug1, Severity::debug2, Severity::debug3, Severity::debug4, Severity::trace, Severity::nolog, Severity::fatal }};
#ifdef FAIR_MIN_SEVERITY #ifdef FAIR_MIN_SEVERITY
for (unsigned int i = 0; i < severitiesDown.size() - static_cast<int>(Severity::FAIR_MIN_SEVERITY) - 1; ++i) { for (unsigned int i = 0; i < severitiesDown.size() - static_cast<int>(Severity::FAIR_MIN_SEVERITY) - 1; ++i) {
#else #else

View File

@@ -31,6 +31,7 @@ void printEverySeverity()
LOG(important) << "important message, counter: " << i++; LOG(important) << "important message, counter: " << i++;
LOG(alarm) << "alarm message, counter: " << i++; LOG(alarm) << "alarm message, counter: " << i++;
LOG(error) << "error message, counter: " << i++; LOG(error) << "error message, counter: " << i++;
LOG(critical) << "critical message, counter: " << i++;
LOG(fatal) << "fatal message, counter: " << i++; LOG(fatal) << "fatal message, counter: " << i++;
} }

View File

@@ -27,6 +27,7 @@ void printEverySeverity()
LOG(important) << "important message "; LOG(important) << "important message ";
LOG(alarm) << "alarm message "; LOG(alarm) << "alarm message ";
LOG(error) << "error message "; LOG(error) << "error message ";
LOG(critical) << "critical message ";
} }
void silentlyPrintAllVerbositiesWithSeverity(Severity sev) void silentlyPrintAllVerbositiesWithSeverity(Severity sev)

View File

@@ -34,6 +34,7 @@ uint32_t printEverySeverity(uint32_t i)
LOG(important) << "important message, counter: " << i++; LOG(important) << "important message, counter: " << i++;
LOG(alarm) << "alarm message, counter: " << i++; LOG(alarm) << "alarm message, counter: " << i++;
LOG(error) << "error message, counter: " << i++; LOG(error) << "error message, counter: " << i++;
LOG(critical) << "critical message, counter: " << i++;
LOG(fatal) << "fatal message, counter: " << i++; LOG(fatal) << "fatal message, counter: " << i++;
return i; return i;