13#include <system_error>
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>
26USERVER_NAMESPACE_BEGIN
39 template <
typename Unsigned>
40 requires std::is_unsigned_v<Unsigned>
41 constexpr explicit HexBase(Unsigned value)
noexcept : value(value) {
42 static_assert(
sizeof(Unsigned) <=
sizeof(value));
46 explicit HexBase(T* pointer)
noexcept : HexBase(
reinterpret_cast<std::uintptr_t>(pointer)) {
47 static_assert(
sizeof(std::uintptr_t) <=
sizeof(value));
54struct Hex
final : impl::HexBase {
55 using impl::HexBase::HexBase;
60struct HexShort
final : impl::HexBase {
61 using impl::HexBase::HexBase;
66 std::string_view string;
79class LogHelper
final {
89 LogClass log_class = LogClass::kLog,
90 const utils::impl::SourceLocation& location =
utils::impl::SourceLocation::Current()
99 const LoggerPtr& logger,
101 LogClass log_class = LogClass::kLog,
102 const utils::impl::SourceLocation& location =
utils::impl::SourceLocation::Current()
107 LogHelper(LogHelper&&) =
delete;
108 LogHelper(
const LogHelper&) =
delete;
109 LogHelper& operator=(LogHelper&&) =
delete;
110 LogHelper& operator=(
const LogHelper&) =
delete;
113 LogHelper& AsLvalue()
noexcept {
return *
this; }
116 template <
typename... Args>
117 USERVER_IMPL_NODEBUG_INLINE_FUNC LogHelper& AsLvalue(fmt::format_string<Args...> fmt, Args&&... args)
noexcept {
118 const fmt::string_view fmt_string =
119#if FMT_VERSION
>= 120200
122 static_cast<fmt::string_view>(fmt);
124 VFormat(fmt_string, fmt::make_format_args(args...));
129 bool IsLimitReached()
const noexcept;
131 template <
typename T>
132 USERVER_IMPL_NODEBUG_INLINE_FUNC LogHelper& operator<<(
const T& value) {
133 if constexpr (std::is_constructible_v<std::string_view, T>) {
135 *
this << std::string_view{value};
136 }
else if constexpr (std::is_signed_v<T>) {
137 using LongLong =
long long;
138 *
this << LongLong{value};
139 }
else if constexpr (std::is_unsigned_v<T>) {
140 using UnsignedLongLong =
unsigned long long;
141 *
this << UnsignedLongLong{value};
142 }
else if constexpr (std::is_base_of_v<std::exception, T>) {
143 *
this <<
static_cast<
const std::exception&>(value);
144 }
else if constexpr (meta::IsOstreamWritable<T>) {
148 }
else if constexpr (meta::kIsRange<T> && !formats::
common::IsFormatValue<T>) {
152 VFormat(
"{}", fmt::make_format_args(value));
158 LogHelper& operator<<(
char value)
noexcept;
159 LogHelper& operator<<(std::string_view value)
noexcept;
160 LogHelper& operator<<(
float value)
noexcept;
161 LogHelper& operator<<(
double value)
noexcept;
162 LogHelper& operator<<(
long double value)
noexcept;
163 LogHelper& operator<<(
unsigned long long value)
noexcept;
164 LogHelper& operator<<(
long long value)
noexcept;
165 LogHelper& operator<<(
bool value)
noexcept;
166 LogHelper& operator<<(
const std::exception& value)
noexcept;
169 LogHelper& operator<<(
const LogExtra& extra)
noexcept;
172 LogHelper& operator<<(LogExtra&& extra)
noexcept;
174 LogHelper& operator<<(Hex hex)
noexcept;
176 LogHelper& operator<<(HexShort hex)
noexcept;
178 LogHelper& operator<<(Quoted value)
noexcept;
180 LogHelper& PutTag(std::string_view key,
const LogExtra::Value& value)
noexcept;
181 LogHelper& PutSwTag(std::string_view key, std::string_view value)
noexcept;
187 template <
typename... Args>
188 USERVER_IMPL_NODEBUG_INLINE_FUNC LogHelper&
Format(fmt::format_string<Args...> fmt, Args&&... args)
noexcept {
189 const fmt::string_view fmt_string =
190#if FMT_VERSION
>= 120200
193 static_cast<fmt::string_view>(fmt);
195 VFormat(fmt_string, fmt::make_format_args(args...));
201 operator impl::Noop()
const noexcept {
return {}; }
206 impl::TagWriter GetTagWriter();
211 friend class impl::TagWriter;
213 void DoLog()
noexcept;
215 void InternalLoggingError(std::string_view message)
noexcept;
217 void PutFloatingPoint(
float value);
218 void PutFloatingPoint(
double value);
219 void PutFloatingPoint(
long double value);
220 void PutUnsigned(
unsigned long long value);
221 void PutSigned(
long long value);
222 void PutBoolean(
bool value);
223 void Put(std::string_view value);
224 void Put(
char value);
226 void PutRaw(std::string_view value_needs_no_escaping);
227 void PutException(
const std::exception& ex);
228 void PutQuoted(std::string_view value);
230 void VFormat(fmt::string_view fmt, fmt::format_args args)
noexcept;
232 template <
typename T>
233 void PutRangeElement(
const T& value);
235 template <
typename T,
typename U>
236 void PutMapElement(
const std::pair<
const T, U>& value);
238 template <
typename T>
239 void PutRange(
const T& range);
241 std::ostream& Stream();
245 std::unique_ptr<Impl> pimpl_;
248inline LogHelper& operator<<(LogHelper& lh, std::error_code ec) {
249 lh << ec.category().name() <<
':' << ec.value() <<
" (" << ec.message() <<
')';
254LogHelper& operator<<(LogHelper& lh,
const std::atomic<T>& value) {
255 return lh << value.load();
259LogHelper& operator<<(LogHelper& lh,
const T* value)
noexcept {
260 if (value ==
nullptr) {
261 lh << std::string_view{
"(null)"};
262 }
else if constexpr (std::is_same_v<T,
char>) {
263 lh << std::string_view{value};
271LogHelper& operator<<(LogHelper& lh, T* value) {
273 !std::is_function_v<T>,
274 "An attempt to log the function address is denied. If you really know what you're doing, cast it to void*."
276 return lh <<
static_cast<
const T*>(value);
280LogHelper& operator<<(LogHelper& lh,
const std::optional<T>& value) {
289template <
typename Fun>
290requires std::is_invocable_r_v<
void, Fun, LogHelper&>
291LogHelper& operator<<(LogHelper& lh, Fun&& value) {
292 std::forward<Fun>(value)(lh);
296template <
class Result,
class... Args>
297LogHelper& operator<<(LogHelper& lh, Result (*)(Args...)) {
298 static_assert(!
sizeof(Result),
"Outputting functions or std::ostream formatters is forbidden");
302LogHelper& operator<<(LogHelper& lh, std::chrono::system_clock::time_point tp);
303LogHelper& operator<<(LogHelper& lh, std::chrono::seconds value);
304LogHelper& operator<<(LogHelper& lh, std::chrono::milliseconds value);
305LogHelper& operator<<(LogHelper& lh, std::chrono::microseconds value);
306LogHelper& operator<<(LogHelper& lh, std::chrono::nanoseconds value);
307LogHelper& operator<<(LogHelper& lh, std::chrono::minutes value);
308LogHelper& operator<<(LogHelper& lh, std::chrono::nanoseconds value);
309LogHelper& operator<<(LogHelper& lh, std::chrono::hours value);
312void LogHelper::PutRangeElement(
const T& value) {
313 if constexpr (std::is_constructible_v<std::string_view, T>) {
314 *
this << Quoted{value};
320template <
typename T,
typename U>
321void LogHelper::PutMapElement(
const std::pair<
const T, U>& value) {
322 PutRangeElement(value.first);
324 PutRangeElement(value.second);
328void LogHelper::PutRange(
const T& range) {
329 static_assert(meta::kIsRange<T>);
334 !std::is_same_v<meta::RangeValueType<T>,
char>,
335 "You should either manually convert type to 'std::string_view' or provide 'operator<<' specialization for your "
336 "type: 'logging::LogHelper& operator<<(logging::LogHelper& lh, const T& value)' or make your type convertible "
337 "to 'std::string_view'"
340 constexpr std::string_view kSeparator =
", ";
343 bool is_first =
true;
344 auto curr = begin(range);
345 const auto end_iter = end(range);
347 while (curr != end_iter) {
348 if (IsLimitReached()) {
357 if constexpr (meta::kIsMap<T>) {
358 PutMapElement(*curr);
360 PutRangeElement(*curr);
365 const auto extra_elements = std::distance(curr, end_iter);
367 if (extra_elements != 0) {
371 *
this <<
"..." << extra_elements <<
" more";