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 explicit constexpr 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 VFormat(fmt::string_view(fmt), fmt::make_format_args(args...));
123 bool IsLimitReached()
const noexcept;
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>) {
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>) {
142 }
else if constexpr (meta::kIsRange<T> && !formats::
common::kIsFormatValue<T>) {
146 VFormat(
"{}", fmt::make_format_args(value));
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;
163 LogHelper& operator<<(
const LogExtra& extra)
noexcept;
166 LogHelper& operator<<(LogExtra&& extra)
noexcept;
168 LogHelper& operator<<(Hex hex)
noexcept;
170 LogHelper& operator<<(HexShort hex)
noexcept;
172 LogHelper& operator<<(Quoted value)
noexcept;
174 LogHelper& PutTag(std::string_view key,
const LogExtra::Value& value)
noexcept;
175 LogHelper& PutSwTag(std::string_view key, std::string_view value)
noexcept;
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...));
189 operator impl::Noop()
const noexcept {
return {}; }
194 impl::TagWriter GetTagWriter();
199 friend class impl::TagWriter;
201 void DoLog()
noexcept;
203 void InternalLoggingError(std::string_view message)
noexcept;
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);
214 void PutRaw(std::string_view value_needs_no_escaping);
215 void PutException(
const std::exception& ex);
216 void PutQuoted(std::string_view value);
218 void VFormat(fmt::string_view fmt, fmt::format_args args)
noexcept;
220 template <
typename T>
221 void PutRangeElement(
const T& value);
223 template <
typename T,
typename U>
224 void PutMapElement(
const std::pair<
const T, U>& value);
226 template <
typename T>
227 void PutRange(
const T& range);
229 std::ostream& Stream();
233 std::unique_ptr<Impl> pimpl_;
236inline LogHelper& operator<<(LogHelper& lh, std::error_code ec) {
237 lh << ec.category().name() <<
':' << ec.value() <<
" (" << ec.message() <<
')';
242LogHelper& operator<<(LogHelper& lh,
const std::atomic<T>& value) {
243 return lh << value.load();
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};
259LogHelper& operator<<(LogHelper& lh, T* value) {
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*."
264 return lh <<
static_cast<
const T*>(value);
268LogHelper& operator<<(LogHelper& lh,
const std::optional<T>& value) {
277template <
typename Fun>
278requires std::is_invocable_r_v<
void, Fun, LogHelper&>
279LogHelper& operator<<(LogHelper& lh, Fun&& value) {
280 std::forward<Fun>(value)(lh);
284template <
class Result,
class... Args>
285LogHelper& operator<<(LogHelper& lh, Result (*)(Args...)) {
286 static_assert(!
sizeof(Result),
"Outputting functions or std::ostream formatters is forbidden");
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);
300void LogHelper::PutRangeElement(
const T& value) {
301 if constexpr (std::is_constructible_v<std::string_view, T>) {
302 *
this << Quoted{value};
308template <
typename T,
typename U>
309void LogHelper::PutMapElement(
const std::pair<
const T, U>& value) {
310 PutRangeElement(value.first);
312 PutRangeElement(value.second);
316void LogHelper::PutRange(
const T& range) {
317 static_assert(meta::kIsRange<T>);
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'"
328 constexpr std::string_view kSeparator =
", ";
331 bool is_first =
true;
332 auto curr = begin(range);
333 const auto end_iter = end(range);
335 while (curr != end_iter) {
336 if (IsLimitReached()) {
345 if constexpr (meta::kIsMap<T>) {
346 PutMapElement(*curr);
348 PutRangeElement(*curr);
353 const auto extra_elements = std::distance(curr, end_iter);
355 if (extra_elements != 0) {
359 *
this <<
"..." << extra_elements <<
" more";