logging.hpp (25b54dba775b31021a3a4677eb79e9771bcb97f7) | logging.hpp (17c472452c9a3518aeac636d01b8e2f01c4fa21f) |
---|---|
1#pragma once 2 3#include "bmcweb_config.h" 4 5#include <bit> 6#include <format> 7#include <iostream> 8#include <source_location> --- 51 unchanged lines hidden (view full) --- 60const void* logPtr(T p) 61{ 62 static_assert(std::is_pointer<T>::value, 63 "Can't use logPtr without pointer"); 64 return std::bit_cast<const void*>(p); 65} 66 67template <LogLevel level, typename... Args> | 1#pragma once 2 3#include "bmcweb_config.h" 4 5#include <bit> 6#include <format> 7#include <iostream> 8#include <source_location> --- 51 unchanged lines hidden (view full) --- 60const void* logPtr(T p) 61{ 62 static_assert(std::is_pointer<T>::value, 63 "Can't use logPtr without pointer"); 64 return std::bit_cast<const void*>(p); 65} 66 67template <LogLevel level, typename... Args> |
68inline void vlog(std::format_string<Args...> format, Args... args, | 68inline void vlog(std::format_string<Args...>&& format, Args&&... args, |
69 const std::source_location& loc) noexcept 70{ 71 if constexpr (bmcwebCurrentLoggingLevel < level) 72 { 73 return; 74 } 75 constexpr size_t stringIndex = static_cast<size_t>(level); 76 static_assert(stringIndex < mapLogLevelFromName.size(), 77 "Missing string for level"); 78 constexpr std::string_view levelString = mapLogLevelFromName[stringIndex]; 79 std::string_view filename = loc.file_name(); 80 filename = filename.substr(filename.rfind('/') + 1); | 69 const std::source_location& loc) noexcept 70{ 71 if constexpr (bmcwebCurrentLoggingLevel < level) 72 { 73 return; 74 } 75 constexpr size_t stringIndex = static_cast<size_t>(level); 76 static_assert(stringIndex < mapLogLevelFromName.size(), 77 "Missing string for level"); 78 constexpr std::string_view levelString = mapLogLevelFromName[stringIndex]; 79 std::string_view filename = loc.file_name(); 80 filename = filename.substr(filename.rfind('/') + 1); |
81 std::cout << std::format("[{} {}:{}] ", levelString, filename, loc.line()) 82 << std::format(format, std::forward<Args>(args)...) << '\n' 83 << std::flush; | 81 std::string logLocation; 82 logLocation = std::format("[{} {}:{}] ", levelString, filename, loc.line()); 83 try 84 { 85 // TODO, multiple static analysis tools flag that this could potentially 86 // throw Based on the documentation, it shouldn't throw, so long as none 87 // of the formatters throw, so unclear at this point why this try/catch 88 // is required, but add it to silence the static analysis tools. 89 logLocation += std::format(std::move(format), 90 std::forward<Args>(args)...); 91 } 92 catch (const std::format_error& /*error*/) 93 { 94 logLocation += "Failed to format"; 95 // Nothing more we can do here if logging is broken. 96 } 97 logLocation += '\n'; 98 // Intentionally ignore error return. 99 fwrite(logLocation.data(), sizeof(std::string::value_type), 100 logLocation.size(), stdout); 101 fflush(stdout); |
84} 85} // namespace crow 86 87template <typename... Args> 88struct BMCWEB_LOG_CRITICAL 89{ 90 // NOLINTNEXTLINE(google-explicit-constructor) 91 BMCWEB_LOG_CRITICAL(std::format_string<Args...> format, Args&&... args, 92 const std::source_location& loc = 93 std::source_location::current()) noexcept 94 { | 102} 103} // namespace crow 104 105template <typename... Args> 106struct BMCWEB_LOG_CRITICAL 107{ 108 // NOLINTNEXTLINE(google-explicit-constructor) 109 BMCWEB_LOG_CRITICAL(std::format_string<Args...> format, Args&&... args, 110 const std::source_location& loc = 111 std::source_location::current()) noexcept 112 { |
95 crow::vlog<crow::LogLevel::Critical, Args...>(format, args..., loc); | 113 crow::vlog<crow::LogLevel::Critical, Args...>( 114 std::move(format), std::forward<Args>(args)..., loc); |
96 } 97}; 98 99template <typename... Args> 100struct BMCWEB_LOG_ERROR 101{ 102 // NOLINTNEXTLINE(google-explicit-constructor) 103 BMCWEB_LOG_ERROR(std::format_string<Args...> format, Args&&... args, 104 const std::source_location& loc = 105 std::source_location::current()) noexcept 106 { | 115 } 116}; 117 118template <typename... Args> 119struct BMCWEB_LOG_ERROR 120{ 121 // NOLINTNEXTLINE(google-explicit-constructor) 122 BMCWEB_LOG_ERROR(std::format_string<Args...> format, Args&&... args, 123 const std::source_location& loc = 124 std::source_location::current()) noexcept 125 { |
107 crow::vlog<crow::LogLevel::Error, Args...>(format, args..., loc); | 126 crow::vlog<crow::LogLevel::Error, Args...>( 127 std::move(format), std::forward<Args>(args)..., loc); |
108 } 109}; 110 111template <typename... Args> 112struct BMCWEB_LOG_WARNING 113{ 114 // NOLINTNEXTLINE(google-explicit-constructor) 115 BMCWEB_LOG_WARNING(std::format_string<Args...> format, Args&&... args, 116 const std::source_location& loc = 117 std::source_location::current()) noexcept 118 { | 128 } 129}; 130 131template <typename... Args> 132struct BMCWEB_LOG_WARNING 133{ 134 // NOLINTNEXTLINE(google-explicit-constructor) 135 BMCWEB_LOG_WARNING(std::format_string<Args...> format, Args&&... args, 136 const std::source_location& loc = 137 std::source_location::current()) noexcept 138 { |
119 crow::vlog<crow::LogLevel::Warning, Args...>(format, args..., loc); | 139 crow::vlog<crow::LogLevel::Warning, Args...>( 140 std::move(format), std::forward<Args>(args)..., loc); |
120 } 121}; 122 123template <typename... Args> 124struct BMCWEB_LOG_INFO 125{ 126 // NOLINTNEXTLINE(google-explicit-constructor) 127 BMCWEB_LOG_INFO(std::format_string<Args...> format, Args&&... args, 128 const std::source_location& loc = 129 std::source_location::current()) noexcept 130 { | 141 } 142}; 143 144template <typename... Args> 145struct BMCWEB_LOG_INFO 146{ 147 // NOLINTNEXTLINE(google-explicit-constructor) 148 BMCWEB_LOG_INFO(std::format_string<Args...> format, Args&&... args, 149 const std::source_location& loc = 150 std::source_location::current()) noexcept 151 { |
131 crow::vlog<crow::LogLevel::Info, Args...>(format, args..., loc); | 152 crow::vlog<crow::LogLevel::Info, Args...>( 153 std::move(format), std::forward<Args>(args)..., loc); |
132 } 133}; 134 135template <typename... Args> 136struct BMCWEB_LOG_DEBUG 137{ 138 // NOLINTNEXTLINE(google-explicit-constructor) 139 BMCWEB_LOG_DEBUG(std::format_string<Args...> format, Args&&... args, 140 const std::source_location& loc = 141 std::source_location::current()) noexcept 142 { | 154 } 155}; 156 157template <typename... Args> 158struct BMCWEB_LOG_DEBUG 159{ 160 // NOLINTNEXTLINE(google-explicit-constructor) 161 BMCWEB_LOG_DEBUG(std::format_string<Args...> format, Args&&... args, 162 const std::source_location& loc = 163 std::source_location::current()) noexcept 164 { |
143 crow::vlog<crow::LogLevel::Debug, Args...>(format, args..., loc); | 165 crow::vlog<crow::LogLevel::Debug, Args...>( 166 std::move(format), std::forward<Args>(args)..., loc); |
144 } 145}; 146 147template <typename... Args> 148BMCWEB_LOG_CRITICAL(std::format_string<Args...>, Args&&...) 149 -> BMCWEB_LOG_CRITICAL<Args...>; 150 151template <typename... Args> --- 14 unchanged lines hidden --- | 167 } 168}; 169 170template <typename... Args> 171BMCWEB_LOG_CRITICAL(std::format_string<Args...>, Args&&...) 172 -> BMCWEB_LOG_CRITICAL<Args...>; 173 174template <typename... Args> --- 14 unchanged lines hidden --- |