blob: d0a241c709765a12ba92f36a5430928ebfd0e6dd [file] [log] [blame]
Ed Tanous40e9b922024-09-10 13:50:16 -07001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright OpenBMC Authors
Ed Tanous7045c8d2017-04-03 10:04:37 -07003#pragma once
4
Myung Bae662aa6e2023-01-10 14:20:28 -06005#include "bmcweb_config.h"
6
Ed Tanousd7857202025-01-28 15:32:26 -08007#include <algorithm>
8#include <array>
Ed Tanous62598e32023-07-17 17:06:25 -07009#include <bit>
Ed Tanousd7857202025-01-28 15:32:26 -080010#include <cstddef>
11#include <cstdio>
Ed Tanous62598e32023-07-17 17:06:25 -070012#include <format>
Ed Tanous62598e32023-07-17 17:06:25 -070013#include <source_location>
Ed Tanousd7857202025-01-28 15:32:26 -080014#include <string>
Myung Bae662aa6e2023-01-10 14:20:28 -060015#include <string_view>
Ed Tanousd7857202025-01-28 15:32:26 -080016#include <type_traits>
Ed Tanousc35475f2025-02-17 14:30:18 -080017#include <utility>
Ed Tanous62598e32023-07-17 17:06:25 -070018
Ed Tanous62598e32023-07-17 17:06:25 -070019// NOLINTBEGIN(readability-convert-member-functions-to-static, cert-dcl58-cpp)
20template <>
Ed Tanous62598e32023-07-17 17:06:25 -070021struct std::formatter<void*>
22{
23 constexpr auto parse(std::format_parse_context& ctx)
24 {
25 return ctx.begin();
26 }
27 auto format(const void*& ptr, auto& ctx) const
28 {
29 return std::format_to(ctx.out(), "{}",
30 std::to_string(std::bit_cast<size_t>(ptr)));
31 }
32};
Ed Tanous62598e32023-07-17 17:06:25 -070033// NOLINTEND(readability-convert-member-functions-to-static, cert-dcl58-cpp)
Ed Tanous4d92cbf2017-06-22 15:41:02 -070034
Ed Tanous1abe55e2018-09-05 08:30:59 -070035namespace crow
36{
37enum class LogLevel
38{
Myung Bae662aa6e2023-01-10 14:20:28 -060039 Disabled = 0,
Ed Tanous1abe55e2018-09-05 08:30:59 -070040 Critical,
Ed Tanouse7245fe2023-07-24 17:01:38 -070041 Error,
42 Warning,
43 Info,
44 Debug,
45 Enabled,
Ed Tanous1e439872018-05-18 11:48:52 -070046};
Ed Tanous4d92cbf2017-06-22 15:41:02 -070047
Ed Tanousee993dc2024-11-19 19:52:46 -080048constexpr int toSystemdLevel(LogLevel level)
49{
Ed Tanousc35475f2025-02-17 14:30:18 -080050 constexpr std::array<std::pair<LogLevel, int>, 5> mapping{
Ed Tanousee993dc2024-11-19 19:52:46 -080051 {// EMERGENCY 0
52 // ALERT 1
53 {LogLevel::Critical, 2},
54 {LogLevel::Error, 3},
55 {LogLevel::Warning, 4},
56 // NOTICE 5
57 {LogLevel::Info, 6},
58 {LogLevel::Debug, 7}}};
59
Ed Tanousc35475f2025-02-17 14:30:18 -080060 const auto* it = std::ranges::find_if(
Ed Tanousee993dc2024-11-19 19:52:46 -080061 mapping, [level](const std::pair<LogLevel, int>& elem) {
62 return elem.first == level;
63 });
64
65 // Unknown log level. Just assume debug
66 if (it != mapping.end())
67 {
68 return 7;
69 }
70
71 return it->second;
72}
73
Myung Bae662aa6e2023-01-10 14:20:28 -060074// Mapping of the external loglvl name to internal loglvl
Ed Tanouse7245fe2023-07-24 17:01:38 -070075constexpr std::array<std::string_view, 7> mapLogLevelFromName{
76 "DISABLED", "CRITICAL", "ERROR", "WARNING", "INFO", "DEBUG", "ENABLED"};
Myung Bae662aa6e2023-01-10 14:20:28 -060077
78constexpr crow::LogLevel getLogLevelFromName(std::string_view name)
79{
Ed Tanouse7245fe2023-07-24 17:01:38 -070080 const auto* iter = std::ranges::find(mapLogLevelFromName, name);
81 if (iter != mapLogLevelFromName.end())
Myung Bae662aa6e2023-01-10 14:20:28 -060082 {
Ed Tanouse7245fe2023-07-24 17:01:38 -070083 return static_cast<LogLevel>(iter - mapLogLevelFromName.begin());
Myung Bae662aa6e2023-01-10 14:20:28 -060084 }
85 return crow::LogLevel::Disabled;
86}
87
88// configured bmcweb LogLevel
Aushim Nagarkattibd1299b2024-08-12 17:11:04 -070089inline crow::LogLevel& getBmcwebCurrentLoggingLevel()
90{
91 static crow::LogLevel level = getLogLevelFromName(BMCWEB_LOGGING_LEVEL);
92 return level;
93}
94
95struct FormatString
96{
97 std::string_view str;
98 std::source_location loc;
99
100 // NOLINTNEXTLINE(google-explicit-constructor)
101 FormatString(const char* stringIn, const std::source_location& locIn =
102 std::source_location::current()) :
Ed Tanous7a16ddc2024-08-25 12:48:43 -0700103 str(stringIn), loc(locIn)
Aushim Nagarkattibd1299b2024-08-12 17:11:04 -0700104 {}
105};
Myung Bae662aa6e2023-01-10 14:20:28 -0600106
Ed Tanous62598e32023-07-17 17:06:25 -0700107template <typename T>
108const void* logPtr(T p)
109{
110 static_assert(std::is_pointer<T>::value,
111 "Can't use logPtr without pointer");
112 return std::bit_cast<const void*>(p);
113}
114
Ed Tanous6ea90762024-04-07 08:38:44 -0700115template <LogLevel level, typename... Args>
Ed Tanous17c47242024-04-08 17:18:12 -0700116inline void vlog(std::format_string<Args...>&& format, Args&&... args,
Ed Tanous6ea90762024-04-07 08:38:44 -0700117 const std::source_location& loc) noexcept
Ed Tanous62598e32023-07-17 17:06:25 -0700118{
Aushim Nagarkattibd1299b2024-08-12 17:11:04 -0700119 if (getBmcwebCurrentLoggingLevel() < level)
Ed Tanous62598e32023-07-17 17:06:25 -0700120 {
121 return;
122 }
Ed Tanousee993dc2024-11-19 19:52:46 -0800123 constexpr int systemdLevel = toSystemdLevel(level);
Ed Tanous6ea90762024-04-07 08:38:44 -0700124 std::string_view filename = loc.file_name();
Ed Tanousea5e2242024-08-06 10:28:35 -0700125 filename = filename.substr(filename.rfind('/'));
126 if (!filename.empty())
127 {
128 filename.remove_prefix(1);
129 }
Ed Tanous17c47242024-04-08 17:18:12 -0700130 std::string logLocation;
Ed Tanous17c47242024-04-08 17:18:12 -0700131 try
132 {
133 // TODO, multiple static analysis tools flag that this could potentially
134 // throw Based on the documentation, it shouldn't throw, so long as none
135 // of the formatters throw, so unclear at this point why this try/catch
136 // is required, but add it to silence the static analysis tools.
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400137 logLocation =
Ed Tanousee993dc2024-11-19 19:52:46 -0800138 std::format("<{}>[{}:{}] ", systemdLevel, filename, loc.line());
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400139 logLocation +=
140 std::format(std::move(format), std::forward<Args>(args)...);
Ed Tanous17c47242024-04-08 17:18:12 -0700141 }
142 catch (const std::format_error& /*error*/)
143 {
144 logLocation += "Failed to format";
145 // Nothing more we can do here if logging is broken.
146 }
147 logLocation += '\n';
148 // Intentionally ignore error return.
149 fwrite(logLocation.data(), sizeof(std::string::value_type),
150 logLocation.size(), stdout);
151 fflush(stdout);
Ed Tanous62598e32023-07-17 17:06:25 -0700152}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700153} // namespace crow
Ed Tanous4d92cbf2017-06-22 15:41:02 -0700154
Ed Tanous62598e32023-07-17 17:06:25 -0700155template <typename... Args>
Ed Tanous6ea90762024-04-07 08:38:44 -0700156struct BMCWEB_LOG_CRITICAL
Ed Tanous62598e32023-07-17 17:06:25 -0700157{
Ed Tanous6ea90762024-04-07 08:38:44 -0700158 // NOLINTNEXTLINE(google-explicit-constructor)
159 BMCWEB_LOG_CRITICAL(std::format_string<Args...> format, Args&&... args,
160 const std::source_location& loc =
161 std::source_location::current()) noexcept
162 {
Ed Tanous17c47242024-04-08 17:18:12 -0700163 crow::vlog<crow::LogLevel::Critical, Args...>(
164 std::move(format), std::forward<Args>(args)..., loc);
Ed Tanous6ea90762024-04-07 08:38:44 -0700165 }
166};
Patrick Williamseb8a3992023-05-12 09:57:16 -0500167
Ed Tanous62598e32023-07-17 17:06:25 -0700168template <typename... Args>
Ed Tanous6ea90762024-04-07 08:38:44 -0700169struct BMCWEB_LOG_ERROR
Ed Tanous62598e32023-07-17 17:06:25 -0700170{
Ed Tanous6ea90762024-04-07 08:38:44 -0700171 // NOLINTNEXTLINE(google-explicit-constructor)
172 BMCWEB_LOG_ERROR(std::format_string<Args...> format, Args&&... args,
173 const std::source_location& loc =
174 std::source_location::current()) noexcept
175 {
Ed Tanous17c47242024-04-08 17:18:12 -0700176 crow::vlog<crow::LogLevel::Error, Args...>(
177 std::move(format), std::forward<Args>(args)..., loc);
Ed Tanous6ea90762024-04-07 08:38:44 -0700178 }
179};
Ed Tanous600d2392022-01-07 09:32:03 -0800180
Ed Tanous62598e32023-07-17 17:06:25 -0700181template <typename... Args>
Ed Tanous6ea90762024-04-07 08:38:44 -0700182struct BMCWEB_LOG_WARNING
Ed Tanous62598e32023-07-17 17:06:25 -0700183{
Ed Tanous6ea90762024-04-07 08:38:44 -0700184 // NOLINTNEXTLINE(google-explicit-constructor)
185 BMCWEB_LOG_WARNING(std::format_string<Args...> format, Args&&... args,
186 const std::source_location& loc =
187 std::source_location::current()) noexcept
188 {
Ed Tanous17c47242024-04-08 17:18:12 -0700189 crow::vlog<crow::LogLevel::Warning, Args...>(
190 std::move(format), std::forward<Args>(args)..., loc);
Ed Tanous6ea90762024-04-07 08:38:44 -0700191 }
192};
Ed Tanous600d2392022-01-07 09:32:03 -0800193
Ed Tanous62598e32023-07-17 17:06:25 -0700194template <typename... Args>
Ed Tanous6ea90762024-04-07 08:38:44 -0700195struct BMCWEB_LOG_INFO
Ed Tanous62598e32023-07-17 17:06:25 -0700196{
Ed Tanous6ea90762024-04-07 08:38:44 -0700197 // NOLINTNEXTLINE(google-explicit-constructor)
198 BMCWEB_LOG_INFO(std::format_string<Args...> format, Args&&... args,
199 const std::source_location& loc =
200 std::source_location::current()) noexcept
201 {
Ed Tanous17c47242024-04-08 17:18:12 -0700202 crow::vlog<crow::LogLevel::Info, Args...>(
203 std::move(format), std::forward<Args>(args)..., loc);
Ed Tanous6ea90762024-04-07 08:38:44 -0700204 }
205};
Ed Tanous600d2392022-01-07 09:32:03 -0800206
Ed Tanous62598e32023-07-17 17:06:25 -0700207template <typename... Args>
Ed Tanous6ea90762024-04-07 08:38:44 -0700208struct BMCWEB_LOG_DEBUG
Ed Tanous62598e32023-07-17 17:06:25 -0700209{
Ed Tanous6ea90762024-04-07 08:38:44 -0700210 // NOLINTNEXTLINE(google-explicit-constructor)
211 BMCWEB_LOG_DEBUG(std::format_string<Args...> format, Args&&... args,
212 const std::source_location& loc =
213 std::source_location::current()) noexcept
214 {
Ed Tanous17c47242024-04-08 17:18:12 -0700215 crow::vlog<crow::LogLevel::Debug, Args...>(
216 std::move(format), std::forward<Args>(args)..., loc);
Ed Tanous6ea90762024-04-07 08:38:44 -0700217 }
218};
219
220template <typename... Args>
Ed Tanous0f441f02024-12-18 10:57:19 -0800221BMCWEB_LOG_CRITICAL(std::format_string<Args...>, Args&&...)
222 -> BMCWEB_LOG_CRITICAL<Args...>;
Ed Tanous6ea90762024-04-07 08:38:44 -0700223
224template <typename... Args>
Ed Tanous0f441f02024-12-18 10:57:19 -0800225BMCWEB_LOG_ERROR(std::format_string<Args...>, Args&&...)
226 -> BMCWEB_LOG_ERROR<Args...>;
Ed Tanous6ea90762024-04-07 08:38:44 -0700227
228template <typename... Args>
Ed Tanous0f441f02024-12-18 10:57:19 -0800229BMCWEB_LOG_WARNING(std::format_string<Args...>, Args&&...)
230 -> BMCWEB_LOG_WARNING<Args...>;
Ed Tanous6ea90762024-04-07 08:38:44 -0700231
232template <typename... Args>
Ed Tanous0f441f02024-12-18 10:57:19 -0800233BMCWEB_LOG_INFO(std::format_string<Args...>, Args&&...)
234 -> BMCWEB_LOG_INFO<Args...>;
Ed Tanous6ea90762024-04-07 08:38:44 -0700235
236template <typename... Args>
Ed Tanous0f441f02024-12-18 10:57:19 -0800237BMCWEB_LOG_DEBUG(std::format_string<Args...>, Args&&...)
238 -> BMCWEB_LOG_DEBUG<Args...>;