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