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 <bit> |
| 6 | #include <format> |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame] | 7 | #include <iostream> |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 8 | #include <source_location> |
Myung Bae | 662aa6e | 2023-01-10 14:20:28 -0600 | [diff] [blame] | 9 | #include <string_view> |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 10 | #include <system_error> |
| 11 | |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 12 | // NOLINTBEGIN(readability-convert-member-functions-to-static, cert-dcl58-cpp) |
| 13 | template <> |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 14 | struct std::formatter<void*> |
| 15 | { |
| 16 | constexpr auto parse(std::format_parse_context& ctx) |
| 17 | { |
| 18 | return ctx.begin(); |
| 19 | } |
| 20 | auto format(const void*& ptr, auto& ctx) const |
| 21 | { |
| 22 | return std::format_to(ctx.out(), "{}", |
| 23 | std::to_string(std::bit_cast<size_t>(ptr))); |
| 24 | } |
| 25 | }; |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 26 | // NOLINTEND(readability-convert-member-functions-to-static, cert-dcl58-cpp) |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame] | 27 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 28 | namespace crow |
| 29 | { |
| 30 | enum class LogLevel |
| 31 | { |
Myung Bae | 662aa6e | 2023-01-10 14:20:28 -0600 | [diff] [blame] | 32 | Disabled = 0, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 33 | Critical, |
Ed Tanous | e7245fe | 2023-07-24 17:01:38 -0700 | [diff] [blame] | 34 | Error, |
| 35 | Warning, |
| 36 | Info, |
| 37 | Debug, |
| 38 | Enabled, |
Ed Tanous | 1e43987 | 2018-05-18 11:48:52 -0700 | [diff] [blame] | 39 | }; |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame] | 40 | |
Myung Bae | 662aa6e | 2023-01-10 14:20:28 -0600 | [diff] [blame] | 41 | // Mapping of the external loglvl name to internal loglvl |
Ed Tanous | e7245fe | 2023-07-24 17:01:38 -0700 | [diff] [blame] | 42 | constexpr std::array<std::string_view, 7> mapLogLevelFromName{ |
| 43 | "DISABLED", "CRITICAL", "ERROR", "WARNING", "INFO", "DEBUG", "ENABLED"}; |
Myung Bae | 662aa6e | 2023-01-10 14:20:28 -0600 | [diff] [blame] | 44 | |
| 45 | constexpr crow::LogLevel getLogLevelFromName(std::string_view name) |
| 46 | { |
Ed Tanous | e7245fe | 2023-07-24 17:01:38 -0700 | [diff] [blame] | 47 | const auto* iter = std::ranges::find(mapLogLevelFromName, name); |
| 48 | if (iter != mapLogLevelFromName.end()) |
Myung Bae | 662aa6e | 2023-01-10 14:20:28 -0600 | [diff] [blame] | 49 | { |
Ed Tanous | e7245fe | 2023-07-24 17:01:38 -0700 | [diff] [blame] | 50 | return static_cast<LogLevel>(iter - mapLogLevelFromName.begin()); |
Myung Bae | 662aa6e | 2023-01-10 14:20:28 -0600 | [diff] [blame] | 51 | } |
| 52 | return crow::LogLevel::Disabled; |
| 53 | } |
| 54 | |
| 55 | // configured bmcweb LogLevel |
Aushim Nagarkatti | bd1299b | 2024-08-12 17:11:04 -0700 | [diff] [blame] | 56 | inline crow::LogLevel& getBmcwebCurrentLoggingLevel() |
| 57 | { |
| 58 | static crow::LogLevel level = getLogLevelFromName(BMCWEB_LOGGING_LEVEL); |
| 59 | return level; |
| 60 | } |
| 61 | |
| 62 | struct FormatString |
| 63 | { |
| 64 | std::string_view str; |
| 65 | std::source_location loc; |
| 66 | |
| 67 | // NOLINTNEXTLINE(google-explicit-constructor) |
| 68 | FormatString(const char* stringIn, const std::source_location& locIn = |
| 69 | std::source_location::current()) : |
Ed Tanous | 7a16ddc | 2024-08-25 12:48:43 -0700 | [diff] [blame] | 70 | str(stringIn), loc(locIn) |
Aushim Nagarkatti | bd1299b | 2024-08-12 17:11:04 -0700 | [diff] [blame] | 71 | {} |
| 72 | }; |
Myung Bae | 662aa6e | 2023-01-10 14:20:28 -0600 | [diff] [blame] | 73 | |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 74 | template <typename T> |
| 75 | const void* logPtr(T p) |
| 76 | { |
| 77 | static_assert(std::is_pointer<T>::value, |
| 78 | "Can't use logPtr without pointer"); |
| 79 | return std::bit_cast<const void*>(p); |
| 80 | } |
| 81 | |
Ed Tanous | 6ea9076 | 2024-04-07 08:38:44 -0700 | [diff] [blame] | 82 | template <LogLevel level, typename... Args> |
Ed Tanous | 17c4724 | 2024-04-08 17:18:12 -0700 | [diff] [blame] | 83 | inline void vlog(std::format_string<Args...>&& format, Args&&... args, |
Ed Tanous | 6ea9076 | 2024-04-07 08:38:44 -0700 | [diff] [blame] | 84 | const std::source_location& loc) noexcept |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 85 | { |
Aushim Nagarkatti | bd1299b | 2024-08-12 17:11:04 -0700 | [diff] [blame] | 86 | if (getBmcwebCurrentLoggingLevel() < level) |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 87 | { |
| 88 | return; |
| 89 | } |
| 90 | constexpr size_t stringIndex = static_cast<size_t>(level); |
| 91 | static_assert(stringIndex < mapLogLevelFromName.size(), |
| 92 | "Missing string for level"); |
Ed Tanous | e7245fe | 2023-07-24 17:01:38 -0700 | [diff] [blame] | 93 | constexpr std::string_view levelString = mapLogLevelFromName[stringIndex]; |
Ed Tanous | 6ea9076 | 2024-04-07 08:38:44 -0700 | [diff] [blame] | 94 | std::string_view filename = loc.file_name(); |
Ed Tanous | ea5e224 | 2024-08-06 10:28:35 -0700 | [diff] [blame] | 95 | filename = filename.substr(filename.rfind('/')); |
| 96 | if (!filename.empty()) |
| 97 | { |
| 98 | filename.remove_prefix(1); |
| 99 | } |
Ed Tanous | 17c4724 | 2024-04-08 17:18:12 -0700 | [diff] [blame] | 100 | std::string logLocation; |
Ed Tanous | 17c4724 | 2024-04-08 17:18:12 -0700 | [diff] [blame] | 101 | try |
| 102 | { |
| 103 | // TODO, multiple static analysis tools flag that this could potentially |
| 104 | // throw Based on the documentation, it shouldn't throw, so long as none |
| 105 | // of the formatters throw, so unclear at this point why this try/catch |
| 106 | // is required, but add it to silence the static analysis tools. |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 107 | logLocation = |
| 108 | std::format("[{} {}:{}] ", levelString, filename, loc.line()); |
| 109 | logLocation += |
| 110 | std::format(std::move(format), std::forward<Args>(args)...); |
Ed Tanous | 17c4724 | 2024-04-08 17:18:12 -0700 | [diff] [blame] | 111 | } |
| 112 | catch (const std::format_error& /*error*/) |
| 113 | { |
| 114 | logLocation += "Failed to format"; |
| 115 | // Nothing more we can do here if logging is broken. |
| 116 | } |
| 117 | logLocation += '\n'; |
| 118 | // Intentionally ignore error return. |
| 119 | fwrite(logLocation.data(), sizeof(std::string::value_type), |
| 120 | logLocation.size(), stdout); |
| 121 | fflush(stdout); |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 122 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 123 | } // namespace crow |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame] | 124 | |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 125 | template <typename... Args> |
Ed Tanous | 6ea9076 | 2024-04-07 08:38:44 -0700 | [diff] [blame] | 126 | struct BMCWEB_LOG_CRITICAL |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 127 | { |
Ed Tanous | 6ea9076 | 2024-04-07 08:38:44 -0700 | [diff] [blame] | 128 | // NOLINTNEXTLINE(google-explicit-constructor) |
| 129 | BMCWEB_LOG_CRITICAL(std::format_string<Args...> format, Args&&... args, |
| 130 | const std::source_location& loc = |
| 131 | std::source_location::current()) noexcept |
| 132 | { |
Ed Tanous | 17c4724 | 2024-04-08 17:18:12 -0700 | [diff] [blame] | 133 | crow::vlog<crow::LogLevel::Critical, Args...>( |
| 134 | std::move(format), std::forward<Args>(args)..., loc); |
Ed Tanous | 6ea9076 | 2024-04-07 08:38:44 -0700 | [diff] [blame] | 135 | } |
| 136 | }; |
Patrick Williams | eb8a399 | 2023-05-12 09:57:16 -0500 | [diff] [blame] | 137 | |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 138 | template <typename... Args> |
Ed Tanous | 6ea9076 | 2024-04-07 08:38:44 -0700 | [diff] [blame] | 139 | struct BMCWEB_LOG_ERROR |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 140 | { |
Ed Tanous | 6ea9076 | 2024-04-07 08:38:44 -0700 | [diff] [blame] | 141 | // NOLINTNEXTLINE(google-explicit-constructor) |
| 142 | BMCWEB_LOG_ERROR(std::format_string<Args...> format, Args&&... args, |
| 143 | const std::source_location& loc = |
| 144 | std::source_location::current()) noexcept |
| 145 | { |
Ed Tanous | 17c4724 | 2024-04-08 17:18:12 -0700 | [diff] [blame] | 146 | crow::vlog<crow::LogLevel::Error, Args...>( |
| 147 | std::move(format), std::forward<Args>(args)..., loc); |
Ed Tanous | 6ea9076 | 2024-04-07 08:38:44 -0700 | [diff] [blame] | 148 | } |
| 149 | }; |
Ed Tanous | 600d239 | 2022-01-07 09:32:03 -0800 | [diff] [blame] | 150 | |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 151 | template <typename... Args> |
Ed Tanous | 6ea9076 | 2024-04-07 08:38:44 -0700 | [diff] [blame] | 152 | struct BMCWEB_LOG_WARNING |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 153 | { |
Ed Tanous | 6ea9076 | 2024-04-07 08:38:44 -0700 | [diff] [blame] | 154 | // NOLINTNEXTLINE(google-explicit-constructor) |
| 155 | BMCWEB_LOG_WARNING(std::format_string<Args...> format, Args&&... args, |
| 156 | const std::source_location& loc = |
| 157 | std::source_location::current()) noexcept |
| 158 | { |
Ed Tanous | 17c4724 | 2024-04-08 17:18:12 -0700 | [diff] [blame] | 159 | crow::vlog<crow::LogLevel::Warning, Args...>( |
| 160 | std::move(format), std::forward<Args>(args)..., loc); |
Ed Tanous | 6ea9076 | 2024-04-07 08:38:44 -0700 | [diff] [blame] | 161 | } |
| 162 | }; |
Ed Tanous | 600d239 | 2022-01-07 09:32:03 -0800 | [diff] [blame] | 163 | |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 164 | template <typename... Args> |
Ed Tanous | 6ea9076 | 2024-04-07 08:38:44 -0700 | [diff] [blame] | 165 | struct BMCWEB_LOG_INFO |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 166 | { |
Ed Tanous | 6ea9076 | 2024-04-07 08:38:44 -0700 | [diff] [blame] | 167 | // NOLINTNEXTLINE(google-explicit-constructor) |
| 168 | BMCWEB_LOG_INFO(std::format_string<Args...> format, Args&&... args, |
| 169 | const std::source_location& loc = |
| 170 | std::source_location::current()) noexcept |
| 171 | { |
Ed Tanous | 17c4724 | 2024-04-08 17:18:12 -0700 | [diff] [blame] | 172 | crow::vlog<crow::LogLevel::Info, Args...>( |
| 173 | std::move(format), std::forward<Args>(args)..., loc); |
Ed Tanous | 6ea9076 | 2024-04-07 08:38:44 -0700 | [diff] [blame] | 174 | } |
| 175 | }; |
Ed Tanous | 600d239 | 2022-01-07 09:32:03 -0800 | [diff] [blame] | 176 | |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 177 | template <typename... Args> |
Ed Tanous | 6ea9076 | 2024-04-07 08:38:44 -0700 | [diff] [blame] | 178 | struct BMCWEB_LOG_DEBUG |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 179 | { |
Ed Tanous | 6ea9076 | 2024-04-07 08:38:44 -0700 | [diff] [blame] | 180 | // NOLINTNEXTLINE(google-explicit-constructor) |
| 181 | BMCWEB_LOG_DEBUG(std::format_string<Args...> format, Args&&... args, |
| 182 | const std::source_location& loc = |
| 183 | std::source_location::current()) noexcept |
| 184 | { |
Ed Tanous | 17c4724 | 2024-04-08 17:18:12 -0700 | [diff] [blame] | 185 | crow::vlog<crow::LogLevel::Debug, Args...>( |
| 186 | std::move(format), std::forward<Args>(args)..., loc); |
Ed Tanous | 6ea9076 | 2024-04-07 08:38:44 -0700 | [diff] [blame] | 187 | } |
| 188 | }; |
| 189 | |
| 190 | template <typename... Args> |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 191 | BMCWEB_LOG_CRITICAL(std::format_string<Args...>, |
| 192 | Args&&...) -> BMCWEB_LOG_CRITICAL<Args...>; |
Ed Tanous | 6ea9076 | 2024-04-07 08:38:44 -0700 | [diff] [blame] | 193 | |
| 194 | template <typename... Args> |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 195 | BMCWEB_LOG_ERROR(std::format_string<Args...>, |
| 196 | Args&&...) -> BMCWEB_LOG_ERROR<Args...>; |
Ed Tanous | 6ea9076 | 2024-04-07 08:38:44 -0700 | [diff] [blame] | 197 | |
| 198 | template <typename... Args> |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 199 | BMCWEB_LOG_WARNING(std::format_string<Args...>, |
| 200 | Args&&...) -> BMCWEB_LOG_WARNING<Args...>; |
Ed Tanous | 6ea9076 | 2024-04-07 08:38:44 -0700 | [diff] [blame] | 201 | |
| 202 | template <typename... Args> |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 203 | BMCWEB_LOG_INFO(std::format_string<Args...>, |
| 204 | Args&&...) -> BMCWEB_LOG_INFO<Args...>; |
Ed Tanous | 6ea9076 | 2024-04-07 08:38:44 -0700 | [diff] [blame] | 205 | |
| 206 | template <typename... Args> |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 207 | BMCWEB_LOG_DEBUG(std::format_string<Args...>, |
| 208 | Args&&...) -> BMCWEB_LOG_DEBUG<Args...>; |