blob: 9b1a36b143becf58c3254d47dd5c541a59348bea [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 =
57 getLogLevelFromName(bmcwebLoggingLevel);
58
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>
68inline void vlog(std::format_string<Args...> format, Args... args,
69 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 Tanouse7245fe2023-07-24 17:01:38 -070080 filename = filename.substr(filename.rfind('/') + 1);
Ed Tanous6ea90762024-04-07 08:38:44 -070081 std::cout << std::format("[{} {}:{}] ", levelString, filename, loc.line())
82 << std::format(format, std::forward<Args>(args)...) << '\n'
Ed Tanousdce4d232024-04-03 10:52:39 -070083 << std::flush;
Ed Tanous62598e32023-07-17 17:06:25 -070084}
Ed Tanous1abe55e2018-09-05 08:30:59 -070085} // namespace crow
Ed Tanous4d92cbf2017-06-22 15:41:02 -070086
Ed Tanous62598e32023-07-17 17:06:25 -070087template <typename... Args>
Ed Tanous6ea90762024-04-07 08:38:44 -070088struct BMCWEB_LOG_CRITICAL
Ed Tanous62598e32023-07-17 17:06:25 -070089{
Ed Tanous6ea90762024-04-07 08:38:44 -070090 // NOLINTNEXTLINE(google-explicit-constructor)
91 BMCWEB_LOG_CRITICAL(std::format_string<Args...> format, Args&&... args,
92 const std::source_location& loc =
93 std::source_location::current()) noexcept
94 {
95 crow::vlog<crow::LogLevel::Critical, Args...>(format, args..., loc);
96 }
97};
Patrick Williamseb8a3992023-05-12 09:57:16 -050098
Ed Tanous62598e32023-07-17 17:06:25 -070099template <typename... Args>
Ed Tanous6ea90762024-04-07 08:38:44 -0700100struct BMCWEB_LOG_ERROR
Ed Tanous62598e32023-07-17 17:06:25 -0700101{
Ed Tanous6ea90762024-04-07 08:38:44 -0700102 // NOLINTNEXTLINE(google-explicit-constructor)
103 BMCWEB_LOG_ERROR(std::format_string<Args...> format, Args&&... args,
104 const std::source_location& loc =
105 std::source_location::current()) noexcept
106 {
107 crow::vlog<crow::LogLevel::Error, Args...>(format, args..., loc);
108 }
109};
Ed Tanous600d2392022-01-07 09:32:03 -0800110
Ed Tanous62598e32023-07-17 17:06:25 -0700111template <typename... Args>
Ed Tanous6ea90762024-04-07 08:38:44 -0700112struct BMCWEB_LOG_WARNING
Ed Tanous62598e32023-07-17 17:06:25 -0700113{
Ed Tanous6ea90762024-04-07 08:38:44 -0700114 // NOLINTNEXTLINE(google-explicit-constructor)
115 BMCWEB_LOG_WARNING(std::format_string<Args...> format, Args&&... args,
116 const std::source_location& loc =
117 std::source_location::current()) noexcept
118 {
119 crow::vlog<crow::LogLevel::Warning, Args...>(format, args..., loc);
120 }
121};
Ed Tanous600d2392022-01-07 09:32:03 -0800122
Ed Tanous62598e32023-07-17 17:06:25 -0700123template <typename... Args>
Ed Tanous6ea90762024-04-07 08:38:44 -0700124struct BMCWEB_LOG_INFO
Ed Tanous62598e32023-07-17 17:06:25 -0700125{
Ed Tanous6ea90762024-04-07 08:38:44 -0700126 // NOLINTNEXTLINE(google-explicit-constructor)
127 BMCWEB_LOG_INFO(std::format_string<Args...> format, Args&&... args,
128 const std::source_location& loc =
129 std::source_location::current()) noexcept
130 {
131 crow::vlog<crow::LogLevel::Info, Args...>(format, args..., loc);
132 }
133};
Ed Tanous600d2392022-01-07 09:32:03 -0800134
Ed Tanous62598e32023-07-17 17:06:25 -0700135template <typename... Args>
Ed Tanous6ea90762024-04-07 08:38:44 -0700136struct BMCWEB_LOG_DEBUG
Ed Tanous62598e32023-07-17 17:06:25 -0700137{
Ed Tanous6ea90762024-04-07 08:38:44 -0700138 // NOLINTNEXTLINE(google-explicit-constructor)
139 BMCWEB_LOG_DEBUG(std::format_string<Args...> format, Args&&... args,
140 const std::source_location& loc =
141 std::source_location::current()) noexcept
142 {
143 crow::vlog<crow::LogLevel::Debug, Args...>(format, args..., loc);
144 }
145};
146
147template <typename... Args>
148BMCWEB_LOG_CRITICAL(std::format_string<Args...>, Args&&...)
149 -> BMCWEB_LOG_CRITICAL<Args...>;
150
151template <typename... Args>
152BMCWEB_LOG_ERROR(std::format_string<Args...>, Args&&...)
153 -> BMCWEB_LOG_ERROR<Args...>;
154
155template <typename... Args>
156BMCWEB_LOG_WARNING(std::format_string<Args...>, Args&&...)
157 -> BMCWEB_LOG_WARNING<Args...>;
158
159template <typename... Args>
160BMCWEB_LOG_INFO(std::format_string<Args...>, Args&&...)
161 -> BMCWEB_LOG_INFO<Args...>;
162
163template <typename... Args>
164BMCWEB_LOG_DEBUG(std::format_string<Args...>, Args&&...)
165 -> BMCWEB_LOG_DEBUG<Args...>;