1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
|
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright OpenBMC Authors
#pragma once
#include "bmcweb_config.h"
#include <algorithm>
#include <array>
#include <bit>
#include <cstddef>
#include <cstdio>
#include <format>
#include <source_location>
#include <string>
#include <string_view>
#include <type_traits>
#include <utility>
// NOLINTBEGIN(readability-convert-member-functions-to-static, cert-dcl58-cpp)
template <>
struct std::formatter<void*>
{
constexpr auto parse(std::format_parse_context& ctx)
{
return ctx.begin();
}
auto format(const void*& ptr, auto& ctx) const
{
return std::format_to(ctx.out(), "{}",
std::to_string(std::bit_cast<size_t>(ptr)));
}
};
// NOLINTEND(readability-convert-member-functions-to-static, cert-dcl58-cpp)
namespace crow
{
enum class LogLevel
{
Disabled = 0,
Critical,
Error,
Warning,
Info,
Debug,
Enabled,
};
constexpr int toSystemdLevel(LogLevel level)
{
constexpr std::array<std::pair<LogLevel, int>, 5> mapping{
{// EMERGENCY 0
// ALERT 1
{LogLevel::Critical, 2},
{LogLevel::Error, 3},
{LogLevel::Warning, 4},
// NOTICE 5
{LogLevel::Info, 6},
// Note, debug here is actually mapped to info level, because OpenBMC
// has a MaxLevelSyslog and MaxLevelStore of info, so DEBUG level will
// never be stored.
{LogLevel::Debug, 6}}};
const auto* it = std::ranges::find_if(
mapping, [level](const std::pair<LogLevel, int>& elem) {
return elem.first == level;
});
// Unknown log level. Just assume debug
if (it == mapping.end())
{
return 6;
}
return it->second;
}
// Mapping of the external loglvl name to internal loglvl
constexpr std::array<std::string_view, 7> mapLogLevelFromName{
"DISABLED", "CRITICAL", "ERROR", "WARNING", "INFO", "DEBUG", "ENABLED"};
constexpr crow::LogLevel getLogLevelFromName(std::string_view name)
{
const auto* iter = std::ranges::find(mapLogLevelFromName, name);
if (iter != mapLogLevelFromName.end())
{
return static_cast<LogLevel>(iter - mapLogLevelFromName.begin());
}
return crow::LogLevel::Disabled;
}
// configured bmcweb LogLevel
inline crow::LogLevel& getBmcwebCurrentLoggingLevel()
{
static crow::LogLevel level = getLogLevelFromName(BMCWEB_LOGGING_LEVEL);
return level;
}
struct FormatString
{
std::string_view str;
std::source_location loc;
// NOLINTNEXTLINE(google-explicit-constructor)
FormatString(const char* stringIn, const std::source_location& locIn =
std::source_location::current()) :
str(stringIn), loc(locIn)
{}
};
template <typename T>
const void* logPtr(T p)
{
static_assert(std::is_pointer<T>::value,
"Can't use logPtr without pointer");
return std::bit_cast<const void*>(p);
}
template <LogLevel level, typename... Args>
inline void vlog(std::format_string<Args...>&& format, Args&&... args,
const std::source_location& loc) noexcept
{
if (getBmcwebCurrentLoggingLevel() < level)
{
return;
}
constexpr int systemdLevel = toSystemdLevel(level);
std::string_view filename = loc.file_name();
filename = filename.substr(filename.rfind('/'));
if (!filename.empty())
{
filename.remove_prefix(1);
}
std::string logLocation;
try
{
// TODO, multiple static analysis tools flag that this could potentially
// throw Based on the documentation, it shouldn't throw, so long as none
// of the formatters throw, so unclear at this point why this try/catch
// is required, but add it to silence the static analysis tools.
logLocation =
std::format("<{}>[{}:{}] ", systemdLevel, filename, loc.line());
logLocation +=
std::format(std::move(format), std::forward<Args>(args)...);
}
catch (const std::format_error& /*error*/)
{
logLocation += "Failed to format";
// Nothing more we can do here if logging is broken.
}
logLocation += '\n';
// Intentionally ignore error return.
fwrite(logLocation.data(), sizeof(std::string::value_type),
logLocation.size(), stdout);
fflush(stdout);
}
} // namespace crow
template <typename... Args>
struct BMCWEB_LOG_CRITICAL
{
// NOLINTNEXTLINE(google-explicit-constructor)
BMCWEB_LOG_CRITICAL(std::format_string<Args...> format, Args&&... args,
const std::source_location& loc =
std::source_location::current()) noexcept
{
crow::vlog<crow::LogLevel::Critical, Args...>(
std::move(format), std::forward<Args>(args)..., loc);
}
};
template <typename... Args>
struct BMCWEB_LOG_ERROR
{
// NOLINTNEXTLINE(google-explicit-constructor)
BMCWEB_LOG_ERROR(std::format_string<Args...> format, Args&&... args,
const std::source_location& loc =
std::source_location::current()) noexcept
{
crow::vlog<crow::LogLevel::Error, Args...>(
std::move(format), std::forward<Args>(args)..., loc);
}
};
template <typename... Args>
struct BMCWEB_LOG_WARNING
{
// NOLINTNEXTLINE(google-explicit-constructor)
BMCWEB_LOG_WARNING(std::format_string<Args...> format, Args&&... args,
const std::source_location& loc =
std::source_location::current()) noexcept
{
crow::vlog<crow::LogLevel::Warning, Args...>(
std::move(format), std::forward<Args>(args)..., loc);
}
};
template <typename... Args>
struct BMCWEB_LOG_INFO
{
// NOLINTNEXTLINE(google-explicit-constructor)
BMCWEB_LOG_INFO(std::format_string<Args...> format, Args&&... args,
const std::source_location& loc =
std::source_location::current()) noexcept
{
crow::vlog<crow::LogLevel::Info, Args...>(
std::move(format), std::forward<Args>(args)..., loc);
}
};
template <typename... Args>
struct BMCWEB_LOG_DEBUG
{
// NOLINTNEXTLINE(google-explicit-constructor)
BMCWEB_LOG_DEBUG(std::format_string<Args...> format, Args&&... args,
const std::source_location& loc =
std::source_location::current()) noexcept
{
crow::vlog<crow::LogLevel::Debug, Args...>(
std::move(format), std::forward<Args>(args)..., loc);
}
};
template <typename... Args>
BMCWEB_LOG_CRITICAL(std::format_string<Args...>, Args&&...)
-> BMCWEB_LOG_CRITICAL<Args...>;
template <typename... Args>
BMCWEB_LOG_ERROR(std::format_string<Args...>, Args&&...)
-> BMCWEB_LOG_ERROR<Args...>;
template <typename... Args>
BMCWEB_LOG_WARNING(std::format_string<Args...>, Args&&...)
-> BMCWEB_LOG_WARNING<Args...>;
template <typename... Args>
BMCWEB_LOG_INFO(std::format_string<Args...>, Args&&...)
-> BMCWEB_LOG_INFO<Args...>;
template <typename... Args>
BMCWEB_LOG_DEBUG(std::format_string<Args...>, Args&&...)
-> BMCWEB_LOG_DEBUG<Args...>;
|