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