userver: userver/logging/log_helper.hpp Source File
Loading...
Searching...
No Matches
log_helper.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/logging/log_helper.hpp
4/// @brief @copybrief logging::LogHelper
5
6#include <chrono>
7#include <exception>
8#include <iosfwd>
9#include <iterator>
10#include <memory>
11#include <optional>
12#include <string_view>
13#include <system_error>
14#include <type_traits>
15
16#include <fmt/core.h>
17
18#include <userver/compiler/impl/nodebug.hpp>
19#include <userver/formats/common/meta.hpp>
20#include <userver/logging/fwd.hpp>
21#include <userver/logging/level.hpp>
22#include <userver/logging/log_extra.hpp>
23#include <userver/utils/impl/source_location.hpp>
24#include <userver/utils/meta.hpp>
25
26USERVER_NAMESPACE_BEGIN
27
28namespace logging {
29
30namespace impl {
31
32class TagWriter;
33
34struct Noop {};
35
36struct HexBase {
37 std::uint64_t value;
38
39 template <typename Unsigned>
40 requires std::is_unsigned_v<Unsigned>
41 explicit constexpr HexBase(Unsigned value) noexcept : value(value) {
42 static_assert(sizeof(Unsigned) <= sizeof(value));
43 }
44
45 template <typename T>
46 explicit HexBase(T* pointer) noexcept : HexBase(reinterpret_cast<std::uintptr_t>(pointer)) {
47 static_assert(sizeof(std::uintptr_t) <= sizeof(value));
48 }
49};
50
51} // namespace impl
52
53/// Formats value in a hex mode with the fixed length representation.
54struct Hex final : impl::HexBase {
55 using impl::HexBase::HexBase;
56};
57
58/// Formats value in a hex mode with the shortest representation, just like
59/// std::to_chars does.
60struct HexShort final : impl::HexBase {
61 using impl::HexBase::HexBase;
62};
63
64/// Formats a string as quoted, escaping the '\' and '"' symbols.
65struct Quoted final {
66 std::string_view string;
67};
68
69enum class LogClass {
70 kLog,
71 kTrace,
72};
73
74/// Stream-like tskv-formatted log message builder.
75///
76/// Users can add LogHelper& operator<<(LogHelper&, ) overloads to use a faster
77/// localeless logging, rather than outputting data through the ostream
78/// operator.
79class LogHelper final {
80public:
81 /// @brief Constructs LogHelper with span logging
82 /// @param logger to log to
83 /// @param level message log level
84 /// @param log_class whether this LogHelper will be used to write a log record or a span
85 /// @param location source location that will be written to logs
87 LoggerRef logger,
88 Level level,
89 LogClass log_class = LogClass::kLog,
90 const utils::impl::SourceLocation& location = utils::impl::SourceLocation::Current()
91 ) noexcept;
92
93 /// @brief Constructs LogHelper with span logging
94 /// @param logger to log to (logging to nullptr does not output messages)
95 /// @param level message log level
96 /// @param log_class whether this LogHelper will be used to write a log record or a span
97 /// @param location source location that will be written to logs
99 const LoggerPtr& logger,
100 Level level,
101 LogClass log_class = LogClass::kLog,
102 const utils::impl::SourceLocation& location = utils::impl::SourceLocation::Current()
103 ) noexcept;
104
105 ~LogHelper();
106
107 LogHelper(LogHelper&&) = delete;
108 LogHelper(const LogHelper&) = delete;
109 LogHelper& operator=(LogHelper&&) = delete;
110 LogHelper& operator=(const LogHelper&) = delete;
111
112 // Helper function that could be called on LogHelper&& to get LogHelper&.
113 LogHelper& AsLvalue() noexcept { return *this; }
114
115 /// @cond
116 template <typename... Args>
117 USERVER_IMPL_NODEBUG_INLINE_FUNC LogHelper& AsLvalue(fmt::format_string<Args...> fmt, Args&&... args) noexcept {
118 VFormat(fmt::string_view(fmt), fmt::make_format_args(args...));
119 return *this;
120 }
121 /// @endcond
122
123 bool IsLimitReached() const noexcept;
124
125 template <typename T>
126 USERVER_IMPL_NODEBUG_INLINE_FUNC LogHelper& operator<<(const T& value) {
127 if constexpr (std::is_constructible_v<std::string_view, T>) {
128 // noexcept if the conversion is noexcept
129 *this << std::string_view{value};
130 } else if constexpr (std::is_signed_v<T>) {
131 using LongLong = long long;
132 *this << LongLong{value};
133 } else if constexpr (std::is_unsigned_v<T>) {
134 using UnsignedLongLong = unsigned long long;
135 *this << UnsignedLongLong{value};
136 } else if constexpr (std::is_base_of_v<std::exception, T>) {
137 *this << static_cast<const std::exception&>(value);
138 } else if constexpr (meta::IsOstreamWritable<T>) {
139 // may throw a non std::exception based exception
140 Stream() << value;
141 FlushStream();
142 } else if constexpr (meta::kIsRange<T> && !formats::common::kIsFormatValue<T>) {
143 // may throw a non std::exception based exception
144 PutRange(value);
145 } else {
146 VFormat("{}", fmt::make_format_args(value));
147 }
148
149 return *this;
150 }
151
152 LogHelper& operator<<(char value) noexcept;
153 LogHelper& operator<<(std::string_view value) noexcept;
154 LogHelper& operator<<(float value) noexcept;
155 LogHelper& operator<<(double value) noexcept;
156 LogHelper& operator<<(long double value) noexcept;
157 LogHelper& operator<<(unsigned long long value) noexcept;
158 LogHelper& operator<<(long long value) noexcept;
159 LogHelper& operator<<(bool value) noexcept;
160 LogHelper& operator<<(const std::exception& value) noexcept;
161
162 /// Extends internal LogExtra
163 LogHelper& operator<<(const LogExtra& extra) noexcept;
164
165 /// Extends internal LogExtra
166 LogHelper& operator<<(LogExtra&& extra) noexcept;
167
168 LogHelper& operator<<(Hex hex) noexcept;
169
170 LogHelper& operator<<(HexShort hex) noexcept;
171
172 LogHelper& operator<<(Quoted value) noexcept;
173
174 LogHelper& PutTag(std::string_view key, const LogExtra::Value& value) noexcept;
175 LogHelper& PutSwTag(std::string_view key, std::string_view value) noexcept;
176
177 /// @brief Formats a log message using the specified format string and arguments.
178 /// @param fmt The format string as per fmtlib.
179 /// @param args Arguments to be formatted into the log message.
180 /// @return A reference to the LogHelper object for chaining.
181 template <typename... Args>
182 USERVER_IMPL_NODEBUG_INLINE_FUNC LogHelper& Format(fmt::format_string<Args...> fmt, Args&&... args) noexcept {
183 VFormat(fmt::string_view(fmt), fmt::make_format_args(args...));
184 return *this;
185 }
186
187 /// @cond
188 // For internal use only!
189 operator impl::Noop() const noexcept { return {}; }
190
191 struct InternalTag;
192
193 // For internal use only!
194 impl::TagWriter GetTagWriter();
195
196 /// @endcond
197
198private:
199 friend class impl::TagWriter;
200
201 void DoLog() noexcept;
202
203 void InternalLoggingError(std::string_view message) noexcept;
204
205 void PutFloatingPoint(float value);
206 void PutFloatingPoint(double value);
207 void PutFloatingPoint(long double value);
208 void PutUnsigned(unsigned long long value);
209 void PutSigned(long long value);
210 void PutBoolean(bool value);
211 void Put(std::string_view value);
212 void Put(char value);
213
214 void PutRaw(std::string_view value_needs_no_escaping);
215 void PutException(const std::exception& ex);
216 void PutQuoted(std::string_view value);
217
218 void VFormat(fmt::string_view fmt, fmt::format_args args) noexcept;
219
220 template <typename T>
221 void PutRangeElement(const T& value);
222
223 template <typename T, typename U>
224 void PutMapElement(const std::pair<const T, U>& value);
225
226 template <typename T>
227 void PutRange(const T& range);
228
229 std::ostream& Stream();
230 void FlushStream();
231
232 class Impl;
233 std::unique_ptr<Impl> pimpl_;
234};
235
236inline LogHelper& operator<<(LogHelper& lh, std::error_code ec) {
237 lh << ec.category().name() << ':' << ec.value() << " (" << ec.message() << ')';
238 return lh;
239}
240
241template <typename T>
242LogHelper& operator<<(LogHelper& lh, const std::atomic<T>& value) {
243 return lh << value.load();
244}
245
246template <typename T>
247LogHelper& operator<<(LogHelper& lh, const T* value) noexcept {
248 if (value == nullptr) {
249 lh << std::string_view{"(null)"};
250 } else if constexpr (std::is_same_v<T, char>) {
251 lh << std::string_view{value};
252 } else {
253 lh << Hex{value};
254 }
255 return lh;
256}
257
258template <typename T>
259LogHelper& operator<<(LogHelper& lh, T* value) {
260 static_assert(
261 !std::is_function_v<T>,
262 "An attempt to log the function address is denied. If you really know what you're doing, cast it to void*."
263 );
264 return lh << static_cast<const T*>(value);
265}
266
267template <typename T>
268LogHelper& operator<<(LogHelper& lh, const std::optional<T>& value) {
269 if (value) {
270 lh << *value;
271 } else {
272 lh << "(none)";
273 }
274 return lh;
275}
276
277template <typename Fun>
278requires std::is_invocable_r_v<void, Fun, LogHelper&>
279LogHelper& operator<<(LogHelper& lh, Fun&& value) {
280 std::forward<Fun>(value)(lh);
281 return lh;
282}
283
284template <class Result, class... Args>
285LogHelper& operator<<(LogHelper& lh, Result (*)(Args...)) {
286 static_assert(!sizeof(Result), "Outputting functions or std::ostream formatters is forbidden");
287 return lh;
288}
289
290LogHelper& operator<<(LogHelper& lh, std::chrono::system_clock::time_point tp);
291LogHelper& operator<<(LogHelper& lh, std::chrono::seconds value);
292LogHelper& operator<<(LogHelper& lh, std::chrono::milliseconds value);
293LogHelper& operator<<(LogHelper& lh, std::chrono::microseconds value);
294LogHelper& operator<<(LogHelper& lh, std::chrono::nanoseconds value);
295LogHelper& operator<<(LogHelper& lh, std::chrono::minutes value);
296LogHelper& operator<<(LogHelper& lh, std::chrono::nanoseconds value);
297LogHelper& operator<<(LogHelper& lh, std::chrono::hours value);
298
299template <typename T>
300void LogHelper::PutRangeElement(const T& value) {
301 if constexpr (std::is_constructible_v<std::string_view, T>) {
302 *this << Quoted{value};
303 } else {
304 *this << value;
305 }
306}
307
308template <typename T, typename U>
309void LogHelper::PutMapElement(const std::pair<const T, U>& value) {
310 PutRangeElement(value.first);
311 *this << ": ";
312 PutRangeElement(value.second);
313}
314
315template <typename T>
316void LogHelper::PutRange(const T& range) {
317 static_assert(meta::kIsRange<T>);
318 using std::begin;
319 using std::end;
320
321 static_assert(
322 !std::is_same_v<meta::RangeValueType<T>, char>,
323 "You should either manually convert type to 'std::string_view' or provide 'operator<<' specialization for your "
324 "type: 'logging::LogHelper& operator<<(logging::LogHelper& lh, const T& value)' or make your type convertible "
325 "to 'std::string_view'"
326 );
327
328 constexpr std::string_view kSeparator = ", ";
329 *this << '[';
330
331 bool is_first = true;
332 auto curr = begin(range);
333 const auto end_iter = end(range);
334
335 while (curr != end_iter) {
336 if (IsLimitReached()) {
337 break;
338 }
339 if (is_first) {
340 is_first = false;
341 } else {
342 *this << kSeparator;
343 }
344
345 if constexpr (meta::kIsMap<T>) {
346 PutMapElement(*curr);
347 } else {
348 PutRangeElement(*curr);
349 }
350 ++curr;
351 }
352
353 const auto extra_elements = std::distance(curr, end_iter);
354
355 if (extra_elements != 0) {
356 if (!is_first) {
357 *this << kSeparator;
358 }
359 *this << "..." << extra_elements << " more";
360 }
361
362 *this << ']';
363}
364
365} // namespace logging
366
367USERVER_NAMESPACE_END