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> 9 #include <nlohmann/json.hpp> 10 11 #include <bit> 12 #include <format> 13 #include <iostream> 14 #include <source_location> 15 #include <string_view> 16 #include <system_error> 17 18 // Clang-tidy would rather these be static, but using static causes the template 19 // specialization to not function. Ignore the warning. 20 // NOLINTBEGIN(readability-convert-member-functions-to-static, cert-dcl58-cpp) 21 template <> 22 struct std::formatter<boost::system::error_code> 23 { 24 constexpr auto parse(std::format_parse_context& ctx) 25 { 26 return ctx.begin(); 27 } 28 29 auto format(const boost::system::error_code& ec, auto& ctx) const 30 { 31 return std::format_to(ctx.out(), "{}", ec.what()); 32 } 33 }; 34 35 template <> 36 struct std::formatter<boost::urls::pct_string_view> 37 { 38 constexpr auto parse(std::format_parse_context& ctx) 39 { 40 return ctx.begin(); 41 } 42 auto format(const boost::urls::pct_string_view& msg, auto& ctx) const 43 { 44 return std::format_to(ctx.out(), "{}", 45 std::string_view(msg.data(), msg.size())); 46 } 47 }; 48 49 template <> 50 struct std::formatter<boost::urls::url> 51 { 52 constexpr auto parse(std::format_parse_context& ctx) 53 { 54 return ctx.begin(); 55 } 56 auto format(const boost::urls::url& msg, auto& ctx) const 57 { 58 return std::format_to(ctx.out(), "{}", std::string_view(msg.buffer())); 59 } 60 }; 61 62 template <> 63 struct std::formatter<boost::core::string_view> 64 { 65 constexpr auto parse(std::format_parse_context& ctx) 66 { 67 return ctx.begin(); 68 } 69 auto format(const boost::core::string_view& msg, auto& ctx) const 70 { 71 return std::format_to(ctx.out(), "{}", std::string_view(msg)); 72 } 73 }; 74 75 template <> 76 struct std::formatter<void*> 77 { 78 constexpr auto parse(std::format_parse_context& ctx) 79 { 80 return ctx.begin(); 81 } 82 auto format(const void*& ptr, auto& ctx) const 83 { 84 return std::format_to(ctx.out(), "{}", 85 std::to_string(std::bit_cast<size_t>(ptr))); 86 } 87 }; 88 89 template <> 90 struct std::formatter<nlohmann::json::json_pointer> 91 { 92 constexpr auto parse(std::format_parse_context& ctx) 93 { 94 return ctx.begin(); 95 } 96 auto format(const nlohmann::json::json_pointer& ptr, auto& ctx) const 97 { 98 return std::format_to(ctx.out(), "{}", ptr.to_string()); 99 } 100 }; 101 102 template <> 103 struct std::formatter<nlohmann::json> 104 { 105 static constexpr auto parse(std::format_parse_context& ctx) 106 { 107 return ctx.begin(); 108 } 109 auto format(const nlohmann::json& json, auto& ctx) const 110 { 111 return std::format_to( 112 ctx.out(), "{}", 113 json.dump(-1, ' ', false, 114 nlohmann::json::error_handler_t::replace)); 115 } 116 }; 117 // NOLINTEND(readability-convert-member-functions-to-static, cert-dcl58-cpp) 118 119 namespace crow 120 { 121 enum class LogLevel 122 { 123 Disabled = 0, 124 Critical, 125 Error, 126 Warning, 127 Info, 128 Debug, 129 Enabled, 130 }; 131 132 // Mapping of the external loglvl name to internal loglvl 133 constexpr std::array<std::string_view, 7> mapLogLevelFromName{ 134 "DISABLED", "CRITICAL", "ERROR", "WARNING", "INFO", "DEBUG", "ENABLED"}; 135 136 constexpr crow::LogLevel getLogLevelFromName(std::string_view name) 137 { 138 const auto* iter = std::ranges::find(mapLogLevelFromName, name); 139 if (iter != mapLogLevelFromName.end()) 140 { 141 return static_cast<LogLevel>(iter - mapLogLevelFromName.begin()); 142 } 143 return crow::LogLevel::Disabled; 144 } 145 146 // configured bmcweb LogLevel 147 constexpr crow::LogLevel bmcwebCurrentLoggingLevel = 148 getLogLevelFromName(bmcwebLoggingLevel); 149 150 struct FormatString 151 { 152 std::string_view str; 153 std::source_location loc; 154 155 // NOLINTNEXTLINE(google-explicit-constructor) 156 FormatString(const char* strIn, const std::source_location& locIn = 157 std::source_location::current()) : 158 str(strIn), 159 loc(locIn) 160 {} 161 }; 162 163 template <typename T> 164 const void* logPtr(T p) 165 { 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 171 template <LogLevel level> 172 inline void vlog(const FormatString& format, std::format_args&& args) 173 { 174 if constexpr (bmcwebCurrentLoggingLevel < 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"); 181 constexpr std::string_view levelString = mapLogLevelFromName[stringIndex]; 182 std::string_view filename = format.loc.file_name(); 183 filename = filename.substr(filename.rfind('/') + 1); 184 std::cout << std::format("[{} {}:{}] ", levelString, filename, 185 format.loc.line()) 186 << std::vformat(format.str, args) << '\n'; 187 } 188 } // namespace crow 189 190 template <typename... Args> 191 inline void BMCWEB_LOG_CRITICAL(const crow::FormatString& format, 192 Args&&... args) 193 { 194 crow::vlog<crow::LogLevel::Critical>( 195 format, std::make_format_args(std::forward<Args>(args)...)); 196 } 197 198 template <typename... Args> 199 inline void BMCWEB_LOG_ERROR(const crow::FormatString& format, Args&&... args) 200 { 201 crow::vlog<crow::LogLevel::Error>( 202 format, std::make_format_args(std::forward<Args>(args)...)); 203 } 204 205 template <typename... Args> 206 inline void BMCWEB_LOG_WARNING(const crow::FormatString& format, Args&&... args) 207 { 208 crow::vlog<crow::LogLevel::Warning>( 209 format, std::make_format_args(std::forward<Args>(args)...)); 210 } 211 212 template <typename... Args> 213 inline void BMCWEB_LOG_INFO(const crow::FormatString& format, Args&&... args) 214 { 215 crow::vlog<crow::LogLevel::Info>( 216 format, std::make_format_args(std::forward<Args>(args)...)); 217 } 218 219 template <typename... Args> 220 inline void BMCWEB_LOG_DEBUG(const crow::FormatString& format, Args&&... args) 221 { 222 crow::vlog<crow::LogLevel::Debug>( 223 format, std::make_format_args(std::forward<Args>(args)...)); 224 } 225