8#include <userver/compiler/select.hpp>
9#include <userver/logging/fwd.hpp>
10#include <userver/logging/level.hpp>
11#include <userver/logging/log_filepath.hpp>
12#include <userver/logging/log_helper.hpp>
14USERVER_NAMESPACE_BEGIN
20void SetDefaultLoggerRef(LoggerRef new_logger)
noexcept;
22extern bool has_background_threads_which_can_log;
38class DefaultLoggerGuard
final {
47 DefaultLoggerGuard(DefaultLoggerGuard&&) =
delete;
48 DefaultLoggerGuard& operator=(DefaultLoggerGuard&&) =
delete;
50 ~DefaultLoggerGuard();
53 LoggerRef logger_prev_;
54 const Level level_prev_;
55 LoggerPtr logger_new_;
60class DefaultLoggerLevelScope
final {
64 DefaultLoggerLevelScope(DefaultLoggerLevelScope&&) =
delete;
65 DefaultLoggerLevelScope& operator=(DefaultLoggerLevelScope&&) =
delete;
67 ~DefaultLoggerLevelScope();
70 impl::LoggerBase& logger_;
71 const Level level_initial_;
88bool LoggerShouldLog(LoggerRef logger,
Level level)
noexcept;
90bool LoggerShouldLog(
const LoggerPtr& logger,
Level level)
noexcept;
92Level GetLoggerLevel(LoggerRef logger)
noexcept;
105 uint64_t count_since_reset = 0;
106 uint64_t dropped_count = 0;
107 std::chrono::steady_clock::time_point last_reset_time{};
113 RateLimiter(RateLimitData& data,
Level level)
noexcept;
114 bool ShouldLog()
const {
return should_log_; }
115 void SetShouldNotLog() { should_log_ =
false; }
116 Level GetLevel()
const {
return level_; }
117 friend LogHelper& operator<<(LogHelper& lh,
const RateLimiter& rl)
noexcept;
121 bool should_log_{
true};
122 uint64_t dropped_count_{0};
126class StaticLogEntry
final {
128 StaticLogEntry(
const char* path,
int line)
noexcept;
130 StaticLogEntry(StaticLogEntry&&) =
delete;
131 StaticLogEntry& operator=(StaticLogEntry&&) =
delete;
133 bool ShouldNotLog(LoggerRef logger,
Level level)
const noexcept;
134 bool ShouldNotLog(
const LoggerPtr& logger,
Level level)
const noexcept;
137 static constexpr std::size_t kContentSize =
138 compiler::SelectSize().For64Bit(40).For32Bit(24);
140 alignas(
void*) std::byte content_[kContentSize];
143template <
class NameHolder,
int Line>
144struct EntryStorage
final {
145 static inline StaticLogEntry entry{NameHolder::Get(), Line};
154#ifdef USERVER_FEATURE_ERASE_LOG_WITH_LEVEL
161#define USERVER_IMPL_ERASE_LOG
163 ? logging::impl::Noop{}
164 : USERVER_NAMESPACE::logging::LogHelper(
165 USERVER_NAMESPACE::logging::GetDefaultLogger(),
166 USERVER_NAMESPACE::logging::Level::kTrace,
167 USERVER_NAMESPACE::utils::impl::SourceLocation::Custom(0
, {}, {}))
171#define USERVER_IMPL_LOGS_TRACE_ERASER(X) USERVER_IMPL_ERASE_LOG
173#if USERVER_FEATURE_ERASE_LOG_WITH_LEVEL > 0
175#define USERVER_IMPL_LOGS_DEBUG_ERASER(X) USERVER_IMPL_ERASE_LOG
178#if USERVER_FEATURE_ERASE_LOG_WITH_LEVEL > 1
180#define USERVER_IMPL_LOGS_INFO_ERASER(X) USERVER_IMPL_ERASE_LOG
183#if USERVER_FEATURE_ERASE_LOG_WITH_LEVEL > 2
185#define USERVER_IMPL_LOGS_WARNING_ERASER(X) USERVER_IMPL_ERASE_LOG
188#if USERVER_FEATURE_ERASE_LOG_WITH_LEVEL > 3
190#define USERVER_IMPL_LOGS_ERROR_ERASER(X) USERVER_IMPL_ERASE_LOG
195#ifndef USERVER_IMPL_LOGS_TRACE_ERASER
197#define USERVER_IMPL_LOGS_TRACE_ERASER(X) X
200#ifndef USERVER_IMPL_LOGS_DEBUG_ERASER
202#define USERVER_IMPL_LOGS_DEBUG_ERASER(X) X
205#ifndef USERVER_IMPL_LOGS_INFO_ERASER
207#define USERVER_IMPL_LOGS_INFO_ERASER(X) X
210#ifndef USERVER_IMPL_LOGS_WARNING_ERASER
212#define USERVER_IMPL_LOGS_WARNING_ERASER(X) X
215#ifndef USERVER_IMPL_LOGS_ERROR_ERASER
217#define USERVER_IMPL_LOGS_ERROR_ERASER(X) X
221#define USERVER_IMPL_LOG_TO(logger, level)
222 USERVER_NAMESPACE
::logging::LogHelper(logger, level).AsLvalue()
225#define USERVER_IMPL_DYNAMIC_DEBUG_ENTRY
226 []() noexcept -> const USERVER_NAMESPACE
::logging::impl::StaticLogEntry& {
228 static constexpr const char* Get() noexcept {
233 USERVER_NAMESPACE
::logging::impl::EntryStorage<NameHolder,
244#define LOG_TO(logger, lvl)
246 USERVER_IMPL_DYNAMIC_DEBUG_ENTRY
().ShouldNotLog((logger), (lvl)),
247 static_cast<int>(lvl) <
248 static_cast<int>(USERVER_NAMESPACE
::logging::Level::kInfo))
249 ? USERVER_NAMESPACE
::logging::impl::Noop{}
250 : USERVER_IMPL_LOG_TO
((logger), (lvl))
256#define LOG(lvl) LOG_TO(USERVER_NAMESPACE
::logging::GetDefaultLogger(), (lvl))
262 USERVER_IMPL_LOGS_TRACE_ERASER
(LOG(USERVER_NAMESPACE
::logging::Level::kTrace))
268 USERVER_IMPL_LOGS_DEBUG_ERASER
(LOG(USERVER_NAMESPACE
::logging::Level::kDebug))
274 USERVER_IMPL_LOGS_INFO_ERASER
(LOG(USERVER_NAMESPACE
::logging::Level::kInfo))
280 USERVER_IMPL_LOGS_WARNING_ERASER
(
281 LOG(USERVER_NAMESPACE
::logging::Level::kWarning))
287 USERVER_IMPL_LOGS_ERROR_ERASER
(LOG(USERVER_NAMESPACE
::logging::Level::kError))
292#define LOG_CRITICAL() LOG(USERVER_NAMESPACE
::logging::Level::kCritical)
299#define LOG_TRACE_TO(logger)
300 USERVER_IMPL_LOGS_TRACE_ERASER
(
301 LOG_TO(logger, USERVER_NAMESPACE
::logging::Level::kTrace))
306#define LOG_DEBUG_TO(logger)
307 USERVER_IMPL_LOGS_DEBUG_ERASER
(
308 LOG_TO(logger, USERVER_NAMESPACE
::logging::Level::kDebug))
313#define LOG_INFO_TO(logger)
314 USERVER_IMPL_LOGS_INFO_ERASER
(
315 LOG_TO(logger, USERVER_NAMESPACE
::logging::Level::kInfo))
320#define LOG_WARNING_TO(logger)
321 USERVER_IMPL_LOGS_WARNING_ERASER
(
322 LOG_TO(logger, USERVER_NAMESPACE
::logging::Level::kWarning))
327#define LOG_ERROR_TO(logger)
328 USERVER_IMPL_LOGS_ERROR_ERASER
(
329 LOG_TO(logger, USERVER_NAMESPACE
::logging::Level::kError))
334#define LOG_CRITICAL_TO(logger)
335 LOG_TO(logger, USERVER_NAMESPACE
::logging::Level::kCritical)
346#define LOG_LIMITED_TO(logger, lvl)
347 for (USERVER_NAMESPACE
::logging::impl::RateLimiter log_limited_to_rl{
348 []() -> USERVER_NAMESPACE
::logging::impl::RateLimitData& {
349 thread_local USERVER_NAMESPACE
::logging::impl::RateLimitData
354 log_limited_to_rl.ShouldLog(); log_limited_to_rl.SetShouldNotLog())
355 LOG_TO((logger), log_limited_to_rl.GetLevel()) << log_limited_to_rl
360#define LOG_LIMITED(lvl)
361 LOG_LIMITED_TO(USERVER_NAMESPACE
::logging::GetDefaultLogger(), lvl)
367#define LOG_LIMITED_TRACE()
368 USERVER_IMPL_LOGS_TRACE_ERASER
(
369 LOG_LIMITED(USERVER_NAMESPACE
::logging::Level::kTrace))
375#define LOG_LIMITED_DEBUG()
376 USERVER_IMPL_LOGS_DEBUG_ERASER
(
377 LOG_LIMITED(USERVER_NAMESPACE
::logging::Level::kDebug))
383#define LOG_LIMITED_INFO()
384 USERVER_IMPL_LOGS_INFO_ERASER
(
385 LOG_LIMITED(USERVER_NAMESPACE
::logging::Level::kInfo))
391#define LOG_LIMITED_WARNING()
392 USERVER_IMPL_LOGS_WARNING_ERASER
(
393 LOG_LIMITED(USERVER_NAMESPACE
::logging::Level::kWarning))
399#define LOG_LIMITED_ERROR()
400 USERVER_IMPL_LOGS_ERROR_ERASER
(
401 LOG_LIMITED(USERVER_NAMESPACE
::logging::Level::kError))
407#define LOG_LIMITED_CRITICAL()
408 LOG_LIMITED(USERVER_NAMESPACE
::logging::Level::kCritical)
416#define LOG_LIMITED_TRACE_TO(logger)
417 USERVER_IMPL_LOGS_TRACE_ERASER
(
418 LOG_LIMITED_TO(logger, USERVER_NAMESPACE
::logging::Level::kTrace))
424#define LOG_LIMITED_DEBUG_TO(logger)
425 USERVER_IMPL_LOGS_DEBUG_ERASER
(
426 LOG_LIMITED_TO(logger, USERVER_NAMESPACE
::logging::Level::kDebug))
432#define LOG_LIMITED_INFO_TO(logger)
433 USERVER_IMPL_LOGS_INFO_ERASER
(
440#define LOG_LIMITED_WARNING_TO(logger)
441 USERVER_IMPL_LOGS_WARNING_ERASER
(
442 LOG_LIMITED_TO(logger, USERVER_NAMESPACE
::logging::Level::kWarning))
448#define LOG_LIMITED_ERROR_TO(logger)
449 USERVER_IMPL_LOGS_ERROR_ERASER
(
450 LOG_LIMITED_TO(logger, USERVER_NAMESPACE
::logging::Level::kError))
456#define LOG_LIMITED_CRITICAL_TO(logger)
457 LOG_LIMITED_TO(logger, USERVER_NAMESPACE
::logging::Level::kCritical)