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