logging.hpp (62598e31d0988d589506d5091bd38f72d61faf5e) | logging.hpp (e7245fe847e7282522a7979f816fd76f925348d3) |
---|---|
1#pragma once 2 3#include "bmcweb_config.h" 4 5#include <boost/system/error_code.hpp> 6#include <boost/url/pct_string_view.hpp> 7#include <boost/url/string_view.hpp> 8#include <boost/url/url.hpp> --- 107 unchanged lines hidden (view full) --- 116}; 117// NOLINTEND(readability-convert-member-functions-to-static, cert-dcl58-cpp) 118 119namespace crow 120{ 121enum class LogLevel 122{ 123 Disabled = 0, | 1#pragma once 2 3#include "bmcweb_config.h" 4 5#include <boost/system/error_code.hpp> 6#include <boost/url/pct_string_view.hpp> 7#include <boost/url/string_view.hpp> 8#include <boost/url/url.hpp> --- 107 unchanged lines hidden (view full) --- 116}; 117// NOLINTEND(readability-convert-member-functions-to-static, cert-dcl58-cpp) 118 119namespace crow 120{ 121enum class LogLevel 122{ 123 Disabled = 0, |
124 Debug, 125 Info, 126 Warning, 127 Error, | |
128 Critical, | 124 Critical, |
125 Error, 126 Warning, 127 Info, 128 Debug, 129 Enabled, |
|
129}; 130 131// Mapping of the external loglvl name to internal loglvl | 130}; 131 132// Mapping of the external loglvl name to internal loglvl |
132constexpr std::array<std::pair<std::string_view, crow::LogLevel>, 7> 133 mapLogLevelFromName{{{"disabled", crow::LogLevel::Disabled}, 134 {"enabled", crow::LogLevel::Debug}, 135 {"debug", crow::LogLevel::Debug}, 136 {"info", crow::LogLevel::Info}, 137 {"warning", crow::LogLevel::Warning}, 138 {"error", crow::LogLevel::Error}, 139 {"critical", crow::LogLevel::Critical}}}; | 133constexpr std::array<std::string_view, 7> mapLogLevelFromName{ 134 "DISABLED", "CRITICAL", "ERROR", "WARNING", "INFO", "DEBUG", "ENABLED"}; |
140 141constexpr crow::LogLevel getLogLevelFromName(std::string_view name) 142{ | 135 136constexpr crow::LogLevel getLogLevelFromName(std::string_view name) 137{ |
143 const auto* iter = 144 std::find_if(begin(mapLogLevelFromName), end(mapLogLevelFromName), 145 [&name](const auto& v) { return v.first == name; }); 146 if (iter != end(mapLogLevelFromName)) | 138 const auto* iter = std::ranges::find(mapLogLevelFromName, name); 139 if (iter != mapLogLevelFromName.end()) |
147 { | 140 { |
148 return iter->second; | 141 return static_cast<LogLevel>(iter - mapLogLevelFromName.begin()); |
149 } 150 return crow::LogLevel::Disabled; 151} 152 153// configured bmcweb LogLevel 154constexpr crow::LogLevel bmcwebCurrentLoggingLevel = 155 getLogLevelFromName(bmcwebLoggingLevel); 156 --- 16 unchanged lines hidden (view full) --- 173 static_assert(std::is_pointer<T>::value, 174 "Can't use logPtr without pointer"); 175 return std::bit_cast<const void*>(p); 176} 177 178template <LogLevel level> 179inline void vlog(const FormatString& format, std::format_args&& args) 180{ | 142 } 143 return crow::LogLevel::Disabled; 144} 145 146// configured bmcweb LogLevel 147constexpr crow::LogLevel bmcwebCurrentLoggingLevel = 148 getLogLevelFromName(bmcwebLoggingLevel); 149 --- 16 unchanged lines hidden (view full) --- 166 static_assert(std::is_pointer<T>::value, 167 "Can't use logPtr without pointer"); 168 return std::bit_cast<const void*>(p); 169} 170 171template <LogLevel level> 172inline void vlog(const FormatString& format, std::format_args&& args) 173{ |
181 if constexpr (bmcwebCurrentLoggingLevel > level) | 174 if constexpr (bmcwebCurrentLoggingLevel <= level) |
182 { 183 return; 184 } 185 constexpr size_t stringIndex = static_cast<size_t>(level); 186 static_assert(stringIndex < mapLogLevelFromName.size(), 187 "Missing string for level"); | 175 { 176 return; 177 } 178 constexpr size_t stringIndex = static_cast<size_t>(level); 179 static_assert(stringIndex < mapLogLevelFromName.size(), 180 "Missing string for level"); |
188 constexpr std::string_view levelString = 189 mapLogLevelFromName[stringIndex].first; | 181 constexpr std::string_view levelString = mapLogLevelFromName[stringIndex]; |
190 std::string_view filename = format.loc.file_name(); | 182 std::string_view filename = format.loc.file_name(); |
191 if (filename.starts_with("../")) 192 { 193 filename = filename.substr(3); 194 } | 183 filename = filename.substr(filename.rfind('/') + 1); |
195 std::cout << std::format("[{} {}:{}] ", levelString, filename, | 184 std::cout << std::format("[{} {}:{}] ", levelString, filename, |
196 format.loc.line()); 197 std::cout << std::vformat(format.str, args); 198 std::putc('\n', stdout); | 185 format.loc.line()) 186 << std::vformat(format.str, args) << '\n'; |
199} 200} // namespace crow 201 202template <typename... Args> 203inline void BMCWEB_LOG_CRITICAL(const crow::FormatString& format, 204 Args&&... args) 205{ 206 crow::vlog<crow::LogLevel::Critical>( --- 30 unchanged lines hidden --- | 187} 188} // namespace crow 189 190template <typename... Args> 191inline void BMCWEB_LOG_CRITICAL(const crow::FormatString& format, 192 Args&&... args) 193{ 194 crow::vlog<crow::LogLevel::Critical>( --- 30 unchanged lines hidden --- |