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