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