Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Myung Bae | 662aa6e | 2023-01-10 14:20:28 -0600 | [diff] [blame] | 3 | #include "bmcweb_config.h" |
| 4 | |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 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> |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame] | 13 | #include <iostream> |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 14 | #include <source_location> |
Myung Bae | 662aa6e | 2023-01-10 14:20:28 -0600 | [diff] [blame] | 15 | #include <string_view> |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 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 <> |
Ed Tanous | a716aa7 | 2023-08-01 11:35:53 -0700 | [diff] [blame] | 50 | struct std::formatter<boost::urls::url_view> |
| 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 <> |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 63 | struct std::formatter<boost::urls::url> |
| 64 | { |
| 65 | constexpr auto parse(std::format_parse_context& ctx) |
| 66 | { |
| 67 | return ctx.begin(); |
| 68 | } |
| 69 | auto format(const boost::urls::url& msg, auto& ctx) const |
| 70 | { |
| 71 | return std::format_to(ctx.out(), "{}", std::string_view(msg.buffer())); |
| 72 | } |
| 73 | }; |
| 74 | |
| 75 | template <> |
| 76 | struct std::formatter<boost::core::string_view> |
| 77 | { |
| 78 | constexpr auto parse(std::format_parse_context& ctx) |
| 79 | { |
| 80 | return ctx.begin(); |
| 81 | } |
| 82 | auto format(const boost::core::string_view& msg, auto& ctx) const |
| 83 | { |
| 84 | return std::format_to(ctx.out(), "{}", std::string_view(msg)); |
| 85 | } |
| 86 | }; |
| 87 | |
| 88 | template <> |
| 89 | struct std::formatter<void*> |
| 90 | { |
| 91 | constexpr auto parse(std::format_parse_context& ctx) |
| 92 | { |
| 93 | return ctx.begin(); |
| 94 | } |
| 95 | auto format(const void*& ptr, auto& ctx) const |
| 96 | { |
| 97 | return std::format_to(ctx.out(), "{}", |
| 98 | std::to_string(std::bit_cast<size_t>(ptr))); |
| 99 | } |
| 100 | }; |
| 101 | |
| 102 | template <> |
| 103 | struct std::formatter<nlohmann::json::json_pointer> |
| 104 | { |
| 105 | constexpr auto parse(std::format_parse_context& ctx) |
| 106 | { |
| 107 | return ctx.begin(); |
| 108 | } |
| 109 | auto format(const nlohmann::json::json_pointer& ptr, auto& ctx) const |
| 110 | { |
| 111 | return std::format_to(ctx.out(), "{}", ptr.to_string()); |
| 112 | } |
| 113 | }; |
| 114 | |
| 115 | template <> |
| 116 | struct std::formatter<nlohmann::json> |
| 117 | { |
| 118 | static constexpr auto parse(std::format_parse_context& ctx) |
| 119 | { |
| 120 | return ctx.begin(); |
| 121 | } |
| 122 | auto format(const nlohmann::json& json, auto& ctx) const |
| 123 | { |
| 124 | return std::format_to( |
| 125 | ctx.out(), "{}", |
| 126 | json.dump(-1, ' ', false, |
| 127 | nlohmann::json::error_handler_t::replace)); |
| 128 | } |
| 129 | }; |
| 130 | // NOLINTEND(readability-convert-member-functions-to-static, cert-dcl58-cpp) |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame] | 131 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 132 | namespace crow |
| 133 | { |
| 134 | enum class LogLevel |
| 135 | { |
Myung Bae | 662aa6e | 2023-01-10 14:20:28 -0600 | [diff] [blame] | 136 | Disabled = 0, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 137 | Critical, |
Ed Tanous | e7245fe | 2023-07-24 17:01:38 -0700 | [diff] [blame] | 138 | Error, |
| 139 | Warning, |
| 140 | Info, |
| 141 | Debug, |
| 142 | Enabled, |
Ed Tanous | 1e43987 | 2018-05-18 11:48:52 -0700 | [diff] [blame] | 143 | }; |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame] | 144 | |
Myung Bae | 662aa6e | 2023-01-10 14:20:28 -0600 | [diff] [blame] | 145 | // Mapping of the external loglvl name to internal loglvl |
Ed Tanous | e7245fe | 2023-07-24 17:01:38 -0700 | [diff] [blame] | 146 | constexpr std::array<std::string_view, 7> mapLogLevelFromName{ |
| 147 | "DISABLED", "CRITICAL", "ERROR", "WARNING", "INFO", "DEBUG", "ENABLED"}; |
Myung Bae | 662aa6e | 2023-01-10 14:20:28 -0600 | [diff] [blame] | 148 | |
| 149 | constexpr crow::LogLevel getLogLevelFromName(std::string_view name) |
| 150 | { |
Ed Tanous | e7245fe | 2023-07-24 17:01:38 -0700 | [diff] [blame] | 151 | const auto* iter = std::ranges::find(mapLogLevelFromName, name); |
| 152 | if (iter != mapLogLevelFromName.end()) |
Myung Bae | 662aa6e | 2023-01-10 14:20:28 -0600 | [diff] [blame] | 153 | { |
Ed Tanous | e7245fe | 2023-07-24 17:01:38 -0700 | [diff] [blame] | 154 | return static_cast<LogLevel>(iter - mapLogLevelFromName.begin()); |
Myung Bae | 662aa6e | 2023-01-10 14:20:28 -0600 | [diff] [blame] | 155 | } |
| 156 | return crow::LogLevel::Disabled; |
| 157 | } |
| 158 | |
| 159 | // configured bmcweb LogLevel |
| 160 | constexpr crow::LogLevel bmcwebCurrentLoggingLevel = |
| 161 | getLogLevelFromName(bmcwebLoggingLevel); |
| 162 | |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 163 | struct FormatString |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 164 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 165 | std::string_view str; |
| 166 | std::source_location loc; |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame] | 167 | |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 168 | // NOLINTNEXTLINE(google-explicit-constructor) |
Ed Tanous | 8ece0e4 | 2024-01-02 13:16:50 -0800 | [diff] [blame^] | 169 | FormatString(const char* stringIn, const std::source_location& locIn = |
| 170 | std::source_location::current()) : |
| 171 | str(stringIn), |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 172 | loc(locIn) |
| 173 | {} |
Ed Tanous | 1e43987 | 2018-05-18 11:48:52 -0700 | [diff] [blame] | 174 | }; |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 175 | |
| 176 | template <typename T> |
| 177 | const void* logPtr(T p) |
| 178 | { |
| 179 | static_assert(std::is_pointer<T>::value, |
| 180 | "Can't use logPtr without pointer"); |
| 181 | return std::bit_cast<const void*>(p); |
| 182 | } |
| 183 | |
| 184 | template <LogLevel level> |
| 185 | inline void vlog(const FormatString& format, std::format_args&& args) |
| 186 | { |
Myung Bae | d518a9b | 2023-08-01 16:12:35 -0400 | [diff] [blame] | 187 | if constexpr (bmcwebCurrentLoggingLevel < level) |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 188 | { |
| 189 | return; |
| 190 | } |
| 191 | constexpr size_t stringIndex = static_cast<size_t>(level); |
| 192 | static_assert(stringIndex < mapLogLevelFromName.size(), |
| 193 | "Missing string for level"); |
Ed Tanous | e7245fe | 2023-07-24 17:01:38 -0700 | [diff] [blame] | 194 | constexpr std::string_view levelString = mapLogLevelFromName[stringIndex]; |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 195 | std::string_view filename = format.loc.file_name(); |
Ed Tanous | e7245fe | 2023-07-24 17:01:38 -0700 | [diff] [blame] | 196 | filename = filename.substr(filename.rfind('/') + 1); |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 197 | std::cout << std::format("[{} {}:{}] ", levelString, filename, |
Ed Tanous | e7245fe | 2023-07-24 17:01:38 -0700 | [diff] [blame] | 198 | format.loc.line()) |
Jonathan Doman | e334466 | 2023-08-18 16:11:25 -0700 | [diff] [blame] | 199 | << std::vformat(format.str, args) << std::endl; |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 200 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 201 | } // namespace crow |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame] | 202 | |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 203 | template <typename... Args> |
| 204 | inline void BMCWEB_LOG_CRITICAL(const crow::FormatString& format, |
| 205 | Args&&... args) |
| 206 | { |
| 207 | crow::vlog<crow::LogLevel::Critical>( |
| 208 | format, std::make_format_args(std::forward<Args>(args)...)); |
| 209 | } |
Patrick Williams | eb8a399 | 2023-05-12 09:57:16 -0500 | [diff] [blame] | 210 | |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 211 | template <typename... Args> |
| 212 | inline void BMCWEB_LOG_ERROR(const crow::FormatString& format, Args&&... args) |
| 213 | { |
| 214 | crow::vlog<crow::LogLevel::Error>( |
| 215 | format, std::make_format_args(std::forward<Args>(args)...)); |
| 216 | } |
Ed Tanous | 600d239 | 2022-01-07 09:32:03 -0800 | [diff] [blame] | 217 | |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 218 | template <typename... Args> |
| 219 | inline void BMCWEB_LOG_WARNING(const crow::FormatString& format, Args&&... args) |
| 220 | { |
| 221 | crow::vlog<crow::LogLevel::Warning>( |
| 222 | format, std::make_format_args(std::forward<Args>(args)...)); |
| 223 | } |
Ed Tanous | 600d239 | 2022-01-07 09:32:03 -0800 | [diff] [blame] | 224 | |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 225 | template <typename... Args> |
| 226 | inline void BMCWEB_LOG_INFO(const crow::FormatString& format, Args&&... args) |
| 227 | { |
| 228 | crow::vlog<crow::LogLevel::Info>( |
| 229 | format, std::make_format_args(std::forward<Args>(args)...)); |
| 230 | } |
Ed Tanous | 600d239 | 2022-01-07 09:32:03 -0800 | [diff] [blame] | 231 | |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 232 | template <typename... Args> |
| 233 | inline void BMCWEB_LOG_DEBUG(const crow::FormatString& format, Args&&... args) |
| 234 | { |
| 235 | crow::vlog<crow::LogLevel::Debug>( |
| 236 | format, std::make_format_args(std::forward<Args>(args)...)); |
| 237 | } |