xref: /openbmc/bmcweb/http/logging.hpp (revision 8db83747)
1 #pragma once
2 
3 #include "bmcweb_config.h"
4 
5 #include <boost/system/error_code.hpp>
6 #include <boost/url/pct_string_view.hpp>
7 #include <boost/url/string_view.hpp>
8 #include <boost/url/url_view_base.hpp>
9 #include <nlohmann/json.hpp>
10 
11 #include <bit>
12 #include <format>
13 #include <iostream>
14 #include <source_location>
15 #include <string_view>
16 #include <system_error>
17 
18 // Clang-tidy would rather these be static, but using static causes the template
19 // specialization to not function.  Ignore the warning.
20 // NOLINTBEGIN(readability-convert-member-functions-to-static, cert-dcl58-cpp)
21 template <>
22 struct std::formatter<boost::system::error_code>
23 {
24     constexpr auto parse(std::format_parse_context& ctx)
25     {
26         return ctx.begin();
27     }
28 
29     auto format(const boost::system::error_code& ec, auto& ctx) const
30     {
31         return std::format_to(ctx.out(), "{}", ec.what());
32     }
33 };
34 
35 template <>
36 struct std::formatter<boost::urls::pct_string_view>
37 {
38     constexpr auto parse(std::format_parse_context& ctx)
39     {
40         return ctx.begin();
41     }
42     auto format(const boost::urls::pct_string_view& msg, auto& ctx) const
43     {
44         return std::format_to(ctx.out(), "{}",
45                               std::string_view(msg.data(), msg.size()));
46     }
47 };
48 
49 template <std::derived_from<boost::urls::url_view_base> URL>
50 struct std::formatter<URL>
51 {
52     constexpr auto parse(std::format_parse_context& ctx)
53     {
54         return ctx.begin();
55     }
56     auto format(const URL& msg, auto& ctx) const
57     {
58         return std::format_to(ctx.out(), "{}", std::string_view(msg.buffer()));
59     }
60 };
61 
62 template <>
63 struct std::formatter<boost::core::string_view>
64 {
65     constexpr auto parse(std::format_parse_context& ctx)
66     {
67         return ctx.begin();
68     }
69     auto format(const boost::core::string_view& msg, auto& ctx) const
70     {
71         return std::format_to(ctx.out(), "{}", std::string_view(msg));
72     }
73 };
74 
75 template <>
76 struct std::formatter<void*>
77 {
78     constexpr auto parse(std::format_parse_context& ctx)
79     {
80         return ctx.begin();
81     }
82     auto format(const void*& ptr, auto& ctx) const
83     {
84         return std::format_to(ctx.out(), "{}",
85                               std::to_string(std::bit_cast<size_t>(ptr)));
86     }
87 };
88 
89 template <>
90 struct std::formatter<nlohmann::json::json_pointer>
91 {
92     constexpr auto parse(std::format_parse_context& ctx)
93     {
94         return ctx.begin();
95     }
96     auto format(const nlohmann::json::json_pointer& ptr, auto& ctx) const
97     {
98         return std::format_to(ctx.out(), "{}", ptr.to_string());
99     }
100 };
101 
102 template <>
103 struct std::formatter<nlohmann::json>
104 {
105     static constexpr auto parse(std::format_parse_context& ctx)
106     {
107         return ctx.begin();
108     }
109     auto format(const nlohmann::json& json, auto& ctx) const
110     {
111         return std::format_to(
112             ctx.out(), "{}",
113             json.dump(-1, ' ', false,
114                       nlohmann::json::error_handler_t::replace));
115     }
116 };
117 // NOLINTEND(readability-convert-member-functions-to-static, cert-dcl58-cpp)
118 
119 namespace crow
120 {
121 enum class LogLevel
122 {
123     Disabled = 0,
124     Critical,
125     Error,
126     Warning,
127     Info,
128     Debug,
129     Enabled,
130 };
131 
132 // Mapping of the external loglvl name to internal loglvl
133 constexpr std::array<std::string_view, 7> mapLogLevelFromName{
134     "DISABLED", "CRITICAL", "ERROR", "WARNING", "INFO", "DEBUG", "ENABLED"};
135 
136 constexpr crow::LogLevel getLogLevelFromName(std::string_view name)
137 {
138     const auto* iter = std::ranges::find(mapLogLevelFromName, name);
139     if (iter != mapLogLevelFromName.end())
140     {
141         return static_cast<LogLevel>(iter - mapLogLevelFromName.begin());
142     }
143     return crow::LogLevel::Disabled;
144 }
145 
146 // configured bmcweb LogLevel
147 constexpr crow::LogLevel bmcwebCurrentLoggingLevel =
148     getLogLevelFromName(bmcwebLoggingLevel);
149 
150 template <typename T>
151 const void* logPtr(T p)
152 {
153     static_assert(std::is_pointer<T>::value,
154                   "Can't use logPtr without pointer");
155     return std::bit_cast<const void*>(p);
156 }
157 
158 template <LogLevel level, typename... Args>
159 inline void vlog(std::format_string<Args...> format, Args... args,
160                  const std::source_location& loc) noexcept
161 {
162     if constexpr (bmcwebCurrentLoggingLevel < level)
163     {
164         return;
165     }
166     constexpr size_t stringIndex = static_cast<size_t>(level);
167     static_assert(stringIndex < mapLogLevelFromName.size(),
168                   "Missing string for level");
169     constexpr std::string_view levelString = mapLogLevelFromName[stringIndex];
170     std::string_view filename = loc.file_name();
171     filename = filename.substr(filename.rfind('/') + 1);
172     std::cout << std::format("[{} {}:{}] ", levelString, filename, loc.line())
173               << std::format(format, std::forward<Args>(args)...) << '\n'
174               << std::flush;
175 }
176 } // namespace crow
177 
178 template <typename... Args>
179 struct BMCWEB_LOG_CRITICAL
180 {
181     // NOLINTNEXTLINE(google-explicit-constructor)
182     BMCWEB_LOG_CRITICAL(std::format_string<Args...> format, Args&&... args,
183                         const std::source_location& loc =
184                             std::source_location::current()) noexcept
185     {
186         crow::vlog<crow::LogLevel::Critical, Args...>(format, args..., loc);
187     }
188 };
189 
190 template <typename... Args>
191 struct BMCWEB_LOG_ERROR
192 {
193     // NOLINTNEXTLINE(google-explicit-constructor)
194     BMCWEB_LOG_ERROR(std::format_string<Args...> format, Args&&... args,
195                      const std::source_location& loc =
196                          std::source_location::current()) noexcept
197     {
198         crow::vlog<crow::LogLevel::Error, Args...>(format, args..., loc);
199     }
200 };
201 
202 template <typename... Args>
203 struct BMCWEB_LOG_WARNING
204 {
205     // NOLINTNEXTLINE(google-explicit-constructor)
206     BMCWEB_LOG_WARNING(std::format_string<Args...> format, Args&&... args,
207                        const std::source_location& loc =
208                            std::source_location::current()) noexcept
209     {
210         crow::vlog<crow::LogLevel::Warning, Args...>(format, args..., loc);
211     }
212 };
213 
214 template <typename... Args>
215 struct BMCWEB_LOG_INFO
216 {
217     // NOLINTNEXTLINE(google-explicit-constructor)
218     BMCWEB_LOG_INFO(std::format_string<Args...> format, Args&&... args,
219                     const std::source_location& loc =
220                         std::source_location::current()) noexcept
221     {
222         crow::vlog<crow::LogLevel::Info, Args...>(format, args..., loc);
223     }
224 };
225 
226 template <typename... Args>
227 struct BMCWEB_LOG_DEBUG
228 {
229     // NOLINTNEXTLINE(google-explicit-constructor)
230     BMCWEB_LOG_DEBUG(std::format_string<Args...> format, Args&&... args,
231                      const std::source_location& loc =
232                          std::source_location::current()) noexcept
233     {
234         crow::vlog<crow::LogLevel::Debug, Args...>(format, args..., loc);
235     }
236 };
237 
238 template <typename... Args>
239 BMCWEB_LOG_CRITICAL(std::format_string<Args...>, Args&&...)
240     -> BMCWEB_LOG_CRITICAL<Args...>;
241 
242 template <typename... Args>
243 BMCWEB_LOG_ERROR(std::format_string<Args...>, Args&&...)
244     -> BMCWEB_LOG_ERROR<Args...>;
245 
246 template <typename... Args>
247 BMCWEB_LOG_WARNING(std::format_string<Args...>, Args&&...)
248     -> BMCWEB_LOG_WARNING<Args...>;
249 
250 template <typename... Args>
251 BMCWEB_LOG_INFO(std::format_string<Args...>, Args&&...)
252     -> BMCWEB_LOG_INFO<Args...>;
253 
254 template <typename... Args>
255 BMCWEB_LOG_DEBUG(std::format_string<Args...>, Args&&...)
256     -> BMCWEB_LOG_DEBUG<Args...>;
257