blob: e2b9fefd272f1f42c5fa4ebd6c5f2e0bf8ca8bee [file] [log] [blame]
Ed Tanous7045c8d2017-04-03 10:04:37 -07001#pragma once
2
Myung Bae662aa6e2023-01-10 14:20:28 -06003#include "bmcweb_config.h"
4
Ed Tanous62598e32023-07-17 17:06:25 -07005#include <bit>
6#include <format>
Ed Tanous4d92cbf2017-06-22 15:41:02 -07007#include <iostream>
Ed Tanous62598e32023-07-17 17:06:25 -07008#include <source_location>
Myung Bae662aa6e2023-01-10 14:20:28 -06009#include <string_view>
Ed Tanous62598e32023-07-17 17:06:25 -070010#include <system_error>
11
Ed Tanous62598e32023-07-17 17:06:25 -070012// NOLINTBEGIN(readability-convert-member-functions-to-static, cert-dcl58-cpp)
13template <>
Ed Tanous62598e32023-07-17 17:06:25 -070014struct 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 Tanous62598e32023-07-17 17:06:25 -070026// NOLINTEND(readability-convert-member-functions-to-static, cert-dcl58-cpp)
Ed Tanous4d92cbf2017-06-22 15:41:02 -070027
Ed Tanous1abe55e2018-09-05 08:30:59 -070028namespace crow
29{
30enum class LogLevel
31{
Myung Bae662aa6e2023-01-10 14:20:28 -060032 Disabled = 0,
Ed Tanous1abe55e2018-09-05 08:30:59 -070033 Critical,
Ed Tanouse7245fe2023-07-24 17:01:38 -070034 Error,
35 Warning,
36 Info,
37 Debug,
38 Enabled,
Ed Tanous1e439872018-05-18 11:48:52 -070039};
Ed Tanous4d92cbf2017-06-22 15:41:02 -070040
Myung Bae662aa6e2023-01-10 14:20:28 -060041// Mapping of the external loglvl name to internal loglvl
Ed Tanouse7245fe2023-07-24 17:01:38 -070042constexpr std::array<std::string_view, 7> mapLogLevelFromName{
43 "DISABLED", "CRITICAL", "ERROR", "WARNING", "INFO", "DEBUG", "ENABLED"};
Myung Bae662aa6e2023-01-10 14:20:28 -060044
45constexpr crow::LogLevel getLogLevelFromName(std::string_view name)
46{
Ed Tanouse7245fe2023-07-24 17:01:38 -070047 const auto* iter = std::ranges::find(mapLogLevelFromName, name);
48 if (iter != mapLogLevelFromName.end())
Myung Bae662aa6e2023-01-10 14:20:28 -060049 {
Ed Tanouse7245fe2023-07-24 17:01:38 -070050 return static_cast<LogLevel>(iter - mapLogLevelFromName.begin());
Myung Bae662aa6e2023-01-10 14:20:28 -060051 }
52 return crow::LogLevel::Disabled;
53}
54
55// configured bmcweb LogLevel
56constexpr crow::LogLevel bmcwebCurrentLoggingLevel =
Ed Tanous25b54db2024-04-17 15:40:31 -070057 getLogLevelFromName(BMCWEB_LOGGING_LEVEL);
Myung Bae662aa6e2023-01-10 14:20:28 -060058
Ed Tanous62598e32023-07-17 17:06:25 -070059template <typename T>
60const 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 Tanous6ea90762024-04-07 08:38:44 -070067template <LogLevel level, typename... Args>
Ed Tanous17c47242024-04-08 17:18:12 -070068inline void vlog(std::format_string<Args...>&& format, Args&&... args,
Ed Tanous6ea90762024-04-07 08:38:44 -070069 const std::source_location& loc) noexcept
Ed Tanous62598e32023-07-17 17:06:25 -070070{
Myung Baed518a9b2023-08-01 16:12:35 -040071 if constexpr (bmcwebCurrentLoggingLevel < level)
Ed Tanous62598e32023-07-17 17:06:25 -070072 {
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 Tanouse7245fe2023-07-24 17:01:38 -070078 constexpr std::string_view levelString = mapLogLevelFromName[stringIndex];
Ed Tanous6ea90762024-04-07 08:38:44 -070079 std::string_view filename = loc.file_name();
Ed Tanousea5e2242024-08-06 10:28:35 -070080 filename = filename.substr(filename.rfind('/'));
81 if (!filename.empty())
82 {
83 filename.remove_prefix(1);
84 }
Ed Tanous17c47242024-04-08 17:18:12 -070085 std::string logLocation;
Ed Tanous17c47242024-04-08 17:18:12 -070086 try
87 {
88 // TODO, multiple static analysis tools flag that this could potentially
89 // throw Based on the documentation, it shouldn't throw, so long as none
90 // of the formatters throw, so unclear at this point why this try/catch
91 // is required, but add it to silence the static analysis tools.
Patrick Williamsbd79bce2024-08-16 15:22:20 -040092 logLocation =
93 std::format("[{} {}:{}] ", levelString, filename, loc.line());
94 logLocation +=
95 std::format(std::move(format), std::forward<Args>(args)...);
Ed Tanous17c47242024-04-08 17:18:12 -070096 }
97 catch (const std::format_error& /*error*/)
98 {
99 logLocation += "Failed to format";
100 // Nothing more we can do here if logging is broken.
101 }
102 logLocation += '\n';
103 // Intentionally ignore error return.
104 fwrite(logLocation.data(), sizeof(std::string::value_type),
105 logLocation.size(), stdout);
106 fflush(stdout);
Ed Tanous62598e32023-07-17 17:06:25 -0700107}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700108} // namespace crow
Ed Tanous4d92cbf2017-06-22 15:41:02 -0700109
Ed Tanous62598e32023-07-17 17:06:25 -0700110template <typename... Args>
Ed Tanous6ea90762024-04-07 08:38:44 -0700111struct BMCWEB_LOG_CRITICAL
Ed Tanous62598e32023-07-17 17:06:25 -0700112{
Ed Tanous6ea90762024-04-07 08:38:44 -0700113 // NOLINTNEXTLINE(google-explicit-constructor)
114 BMCWEB_LOG_CRITICAL(std::format_string<Args...> format, Args&&... args,
115 const std::source_location& loc =
116 std::source_location::current()) noexcept
117 {
Ed Tanous17c47242024-04-08 17:18:12 -0700118 crow::vlog<crow::LogLevel::Critical, Args...>(
119 std::move(format), std::forward<Args>(args)..., loc);
Ed Tanous6ea90762024-04-07 08:38:44 -0700120 }
121};
Patrick Williamseb8a3992023-05-12 09:57:16 -0500122
Ed Tanous62598e32023-07-17 17:06:25 -0700123template <typename... Args>
Ed Tanous6ea90762024-04-07 08:38:44 -0700124struct BMCWEB_LOG_ERROR
Ed Tanous62598e32023-07-17 17:06:25 -0700125{
Ed Tanous6ea90762024-04-07 08:38:44 -0700126 // NOLINTNEXTLINE(google-explicit-constructor)
127 BMCWEB_LOG_ERROR(std::format_string<Args...> format, Args&&... args,
128 const std::source_location& loc =
129 std::source_location::current()) noexcept
130 {
Ed Tanous17c47242024-04-08 17:18:12 -0700131 crow::vlog<crow::LogLevel::Error, Args...>(
132 std::move(format), std::forward<Args>(args)..., loc);
Ed Tanous6ea90762024-04-07 08:38:44 -0700133 }
134};
Ed Tanous600d2392022-01-07 09:32:03 -0800135
Ed Tanous62598e32023-07-17 17:06:25 -0700136template <typename... Args>
Ed Tanous6ea90762024-04-07 08:38:44 -0700137struct BMCWEB_LOG_WARNING
Ed Tanous62598e32023-07-17 17:06:25 -0700138{
Ed Tanous6ea90762024-04-07 08:38:44 -0700139 // NOLINTNEXTLINE(google-explicit-constructor)
140 BMCWEB_LOG_WARNING(std::format_string<Args...> format, Args&&... args,
141 const std::source_location& loc =
142 std::source_location::current()) noexcept
143 {
Ed Tanous17c47242024-04-08 17:18:12 -0700144 crow::vlog<crow::LogLevel::Warning, Args...>(
145 std::move(format), std::forward<Args>(args)..., loc);
Ed Tanous6ea90762024-04-07 08:38:44 -0700146 }
147};
Ed Tanous600d2392022-01-07 09:32:03 -0800148
Ed Tanous62598e32023-07-17 17:06:25 -0700149template <typename... Args>
Ed Tanous6ea90762024-04-07 08:38:44 -0700150struct BMCWEB_LOG_INFO
Ed Tanous62598e32023-07-17 17:06:25 -0700151{
Ed Tanous6ea90762024-04-07 08:38:44 -0700152 // NOLINTNEXTLINE(google-explicit-constructor)
153 BMCWEB_LOG_INFO(std::format_string<Args...> format, Args&&... args,
154 const std::source_location& loc =
155 std::source_location::current()) noexcept
156 {
Ed Tanous17c47242024-04-08 17:18:12 -0700157 crow::vlog<crow::LogLevel::Info, Args...>(
158 std::move(format), std::forward<Args>(args)..., loc);
Ed Tanous6ea90762024-04-07 08:38:44 -0700159 }
160};
Ed Tanous600d2392022-01-07 09:32:03 -0800161
Ed Tanous62598e32023-07-17 17:06:25 -0700162template <typename... Args>
Ed Tanous6ea90762024-04-07 08:38:44 -0700163struct BMCWEB_LOG_DEBUG
Ed Tanous62598e32023-07-17 17:06:25 -0700164{
Ed Tanous6ea90762024-04-07 08:38:44 -0700165 // NOLINTNEXTLINE(google-explicit-constructor)
166 BMCWEB_LOG_DEBUG(std::format_string<Args...> format, Args&&... args,
167 const std::source_location& loc =
168 std::source_location::current()) noexcept
169 {
Ed Tanous17c47242024-04-08 17:18:12 -0700170 crow::vlog<crow::LogLevel::Debug, Args...>(
171 std::move(format), std::forward<Args>(args)..., loc);
Ed Tanous6ea90762024-04-07 08:38:44 -0700172 }
173};
174
175template <typename... Args>
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400176BMCWEB_LOG_CRITICAL(std::format_string<Args...>,
177 Args&&...) -> BMCWEB_LOG_CRITICAL<Args...>;
Ed Tanous6ea90762024-04-07 08:38:44 -0700178
179template <typename... Args>
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400180BMCWEB_LOG_ERROR(std::format_string<Args...>,
181 Args&&...) -> BMCWEB_LOG_ERROR<Args...>;
Ed Tanous6ea90762024-04-07 08:38:44 -0700182
183template <typename... Args>
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400184BMCWEB_LOG_WARNING(std::format_string<Args...>,
185 Args&&...) -> BMCWEB_LOG_WARNING<Args...>;
Ed Tanous6ea90762024-04-07 08:38:44 -0700186
187template <typename... Args>
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400188BMCWEB_LOG_INFO(std::format_string<Args...>,
189 Args&&...) -> BMCWEB_LOG_INFO<Args...>;
Ed Tanous6ea90762024-04-07 08:38:44 -0700190
191template <typename... Args>
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400192BMCWEB_LOG_DEBUG(std::format_string<Args...>,
193 Args&&...) -> BMCWEB_LOG_DEBUG<Args...>;