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()) : |
| 70 | str(stringIn), |
| 71 | loc(locIn) |
| 72 | {} |
| 73 | }; |
Myung Bae | 662aa6e | 2023-01-10 14:20:28 -0600 | [diff] [blame] | 74 | |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 75 | template <typename T> |
| 76 | const void* logPtr(T p) |
| 77 | { |
| 78 | static_assert(std::is_pointer<T>::value, |
| 79 | "Can't use logPtr without pointer"); |
| 80 | return std::bit_cast<const void*>(p); |
| 81 | } |
| 82 | |
Ed Tanous | 6ea9076 | 2024-04-07 08:38:44 -0700 | [diff] [blame] | 83 | template <LogLevel level, typename... Args> |
Ed Tanous | 17c4724 | 2024-04-08 17:18:12 -0700 | [diff] [blame] | 84 | inline void vlog(std::format_string<Args...>&& format, Args&&... args, |
Ed Tanous | 6ea9076 | 2024-04-07 08:38:44 -0700 | [diff] [blame] | 85 | const std::source_location& loc) noexcept |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 86 | { |
Aushim Nagarkatti | bd1299b | 2024-08-12 17:11:04 -0700 | [diff] [blame] | 87 | if (getBmcwebCurrentLoggingLevel() < level) |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 88 | { |
| 89 | return; |
| 90 | } |
| 91 | constexpr size_t stringIndex = static_cast<size_t>(level); |
| 92 | static_assert(stringIndex < mapLogLevelFromName.size(), |
| 93 | "Missing string for level"); |
Ed Tanous | e7245fe | 2023-07-24 17:01:38 -0700 | [diff] [blame] | 94 | constexpr std::string_view levelString = mapLogLevelFromName[stringIndex]; |
Ed Tanous | 6ea9076 | 2024-04-07 08:38:44 -0700 | [diff] [blame] | 95 | std::string_view filename = loc.file_name(); |
Ed Tanous | ea5e224 | 2024-08-06 10:28:35 -0700 | [diff] [blame] | 96 | filename = filename.substr(filename.rfind('/')); |
| 97 | if (!filename.empty()) |
| 98 | { |
| 99 | filename.remove_prefix(1); |
| 100 | } |
Ed Tanous | 17c4724 | 2024-04-08 17:18:12 -0700 | [diff] [blame] | 101 | std::string logLocation; |
Ed Tanous | 17c4724 | 2024-04-08 17:18:12 -0700 | [diff] [blame] | 102 | try |
| 103 | { |
| 104 | // TODO, multiple static analysis tools flag that this could potentially |
| 105 | // throw Based on the documentation, it shouldn't throw, so long as none |
| 106 | // of the formatters throw, so unclear at this point why this try/catch |
| 107 | // is required, but add it to silence the static analysis tools. |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 108 | logLocation = |
| 109 | std::format("[{} {}:{}] ", levelString, filename, loc.line()); |
| 110 | logLocation += |
| 111 | std::format(std::move(format), std::forward<Args>(args)...); |
Ed Tanous | 17c4724 | 2024-04-08 17:18:12 -0700 | [diff] [blame] | 112 | } |
| 113 | catch (const std::format_error& /*error*/) |
| 114 | { |
| 115 | logLocation += "Failed to format"; |
| 116 | // Nothing more we can do here if logging is broken. |
| 117 | } |
| 118 | logLocation += '\n'; |
| 119 | // Intentionally ignore error return. |
| 120 | fwrite(logLocation.data(), sizeof(std::string::value_type), |
| 121 | logLocation.size(), stdout); |
| 122 | fflush(stdout); |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 123 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 124 | } // namespace crow |
Ed Tanous | 4d92cbf | 2017-06-22 15:41:02 -0700 | [diff] [blame] | 125 | |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 126 | template <typename... Args> |
Ed Tanous | 6ea9076 | 2024-04-07 08:38:44 -0700 | [diff] [blame] | 127 | struct BMCWEB_LOG_CRITICAL |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 128 | { |
Ed Tanous | 6ea9076 | 2024-04-07 08:38:44 -0700 | [diff] [blame] | 129 | // NOLINTNEXTLINE(google-explicit-constructor) |
| 130 | BMCWEB_LOG_CRITICAL(std::format_string<Args...> format, Args&&... args, |
| 131 | const std::source_location& loc = |
| 132 | std::source_location::current()) noexcept |
| 133 | { |
Ed Tanous | 17c4724 | 2024-04-08 17:18:12 -0700 | [diff] [blame] | 134 | crow::vlog<crow::LogLevel::Critical, Args...>( |
| 135 | std::move(format), std::forward<Args>(args)..., loc); |
Ed Tanous | 6ea9076 | 2024-04-07 08:38:44 -0700 | [diff] [blame] | 136 | } |
| 137 | }; |
Patrick Williams | eb8a399 | 2023-05-12 09:57:16 -0500 | [diff] [blame] | 138 | |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 139 | template <typename... Args> |
Ed Tanous | 6ea9076 | 2024-04-07 08:38:44 -0700 | [diff] [blame] | 140 | struct BMCWEB_LOG_ERROR |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 141 | { |
Ed Tanous | 6ea9076 | 2024-04-07 08:38:44 -0700 | [diff] [blame] | 142 | // NOLINTNEXTLINE(google-explicit-constructor) |
| 143 | BMCWEB_LOG_ERROR(std::format_string<Args...> format, Args&&... args, |
| 144 | const std::source_location& loc = |
| 145 | std::source_location::current()) noexcept |
| 146 | { |
Ed Tanous | 17c4724 | 2024-04-08 17:18:12 -0700 | [diff] [blame] | 147 | crow::vlog<crow::LogLevel::Error, Args...>( |
| 148 | std::move(format), std::forward<Args>(args)..., loc); |
Ed Tanous | 6ea9076 | 2024-04-07 08:38:44 -0700 | [diff] [blame] | 149 | } |
| 150 | }; |
Ed Tanous | 600d239 | 2022-01-07 09:32:03 -0800 | [diff] [blame] | 151 | |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 152 | template <typename... Args> |
Ed Tanous | 6ea9076 | 2024-04-07 08:38:44 -0700 | [diff] [blame] | 153 | struct BMCWEB_LOG_WARNING |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 154 | { |
Ed Tanous | 6ea9076 | 2024-04-07 08:38:44 -0700 | [diff] [blame] | 155 | // NOLINTNEXTLINE(google-explicit-constructor) |
| 156 | BMCWEB_LOG_WARNING(std::format_string<Args...> format, Args&&... args, |
| 157 | const std::source_location& loc = |
| 158 | std::source_location::current()) noexcept |
| 159 | { |
Ed Tanous | 17c4724 | 2024-04-08 17:18:12 -0700 | [diff] [blame] | 160 | crow::vlog<crow::LogLevel::Warning, Args...>( |
| 161 | std::move(format), std::forward<Args>(args)..., loc); |
Ed Tanous | 6ea9076 | 2024-04-07 08:38:44 -0700 | [diff] [blame] | 162 | } |
| 163 | }; |
Ed Tanous | 600d239 | 2022-01-07 09:32:03 -0800 | [diff] [blame] | 164 | |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 165 | template <typename... Args> |
Ed Tanous | 6ea9076 | 2024-04-07 08:38:44 -0700 | [diff] [blame] | 166 | struct BMCWEB_LOG_INFO |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 167 | { |
Ed Tanous | 6ea9076 | 2024-04-07 08:38:44 -0700 | [diff] [blame] | 168 | // NOLINTNEXTLINE(google-explicit-constructor) |
| 169 | BMCWEB_LOG_INFO(std::format_string<Args...> format, Args&&... args, |
| 170 | const std::source_location& loc = |
| 171 | std::source_location::current()) noexcept |
| 172 | { |
Ed Tanous | 17c4724 | 2024-04-08 17:18:12 -0700 | [diff] [blame] | 173 | crow::vlog<crow::LogLevel::Info, Args...>( |
| 174 | std::move(format), std::forward<Args>(args)..., loc); |
Ed Tanous | 6ea9076 | 2024-04-07 08:38:44 -0700 | [diff] [blame] | 175 | } |
| 176 | }; |
Ed Tanous | 600d239 | 2022-01-07 09:32:03 -0800 | [diff] [blame] | 177 | |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 178 | template <typename... Args> |
Ed Tanous | 6ea9076 | 2024-04-07 08:38:44 -0700 | [diff] [blame] | 179 | struct BMCWEB_LOG_DEBUG |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 180 | { |
Ed Tanous | 6ea9076 | 2024-04-07 08:38:44 -0700 | [diff] [blame] | 181 | // NOLINTNEXTLINE(google-explicit-constructor) |
| 182 | BMCWEB_LOG_DEBUG(std::format_string<Args...> format, Args&&... args, |
| 183 | const std::source_location& loc = |
| 184 | std::source_location::current()) noexcept |
| 185 | { |
Ed Tanous | 17c4724 | 2024-04-08 17:18:12 -0700 | [diff] [blame] | 186 | crow::vlog<crow::LogLevel::Debug, Args...>( |
| 187 | std::move(format), std::forward<Args>(args)..., loc); |
Ed Tanous | 6ea9076 | 2024-04-07 08:38:44 -0700 | [diff] [blame] | 188 | } |
| 189 | }; |
| 190 | |
| 191 | template <typename... Args> |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 192 | BMCWEB_LOG_CRITICAL(std::format_string<Args...>, |
| 193 | Args&&...) -> BMCWEB_LOG_CRITICAL<Args...>; |
Ed Tanous | 6ea9076 | 2024-04-07 08:38:44 -0700 | [diff] [blame] | 194 | |
| 195 | template <typename... Args> |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 196 | BMCWEB_LOG_ERROR(std::format_string<Args...>, |
| 197 | Args&&...) -> BMCWEB_LOG_ERROR<Args...>; |
Ed Tanous | 6ea9076 | 2024-04-07 08:38:44 -0700 | [diff] [blame] | 198 | |
| 199 | template <typename... Args> |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 200 | BMCWEB_LOG_WARNING(std::format_string<Args...>, |
| 201 | Args&&...) -> BMCWEB_LOG_WARNING<Args...>; |
Ed Tanous | 6ea9076 | 2024-04-07 08:38:44 -0700 | [diff] [blame] | 202 | |
| 203 | template <typename... Args> |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 204 | BMCWEB_LOG_INFO(std::format_string<Args...>, |
| 205 | Args&&...) -> BMCWEB_LOG_INFO<Args...>; |
Ed Tanous | 6ea9076 | 2024-04-07 08:38:44 -0700 | [diff] [blame] | 206 | |
| 207 | template <typename... Args> |
Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 208 | BMCWEB_LOG_DEBUG(std::format_string<Args...>, |
| 209 | Args&&...) -> BMCWEB_LOG_DEBUG<Args...>; |