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 = compiler::SelectSize().For64Bit(40).For32Bit(24);
139 alignas(
void*) std::byte content_[kContentSize];
142template <
class NameHolder,
int Line>
143struct EntryStorage
final {
144 static inline StaticLogEntry entry{NameHolder::Get(), Line};
153#ifdef USERVER_FEATURE_ERASE_LOG_WITH_LEVEL
160#define USERVER_IMPL_ERASE_LOG
161 true ? logging::impl::Noop{}
162 : USERVER_NAMESPACE::logging::LogHelper(
163 USERVER_NAMESPACE::logging::GetDefaultLogger(),
164 USERVER_NAMESPACE::logging::Level::kTrace,
165 USERVER_NAMESPACE::utils::impl::SourceLocation::Custom(0
, {}, {})
170#define USERVER_IMPL_LOGS_TRACE_ERASER(X) USERVER_IMPL_ERASE_LOG
172#if USERVER_FEATURE_ERASE_LOG_WITH_LEVEL > 0
174#define USERVER_IMPL_LOGS_DEBUG_ERASER(X) USERVER_IMPL_ERASE_LOG
177#if USERVER_FEATURE_ERASE_LOG_WITH_LEVEL > 1
179#define USERVER_IMPL_LOGS_INFO_ERASER(X) USERVER_IMPL_ERASE_LOG
182#if USERVER_FEATURE_ERASE_LOG_WITH_LEVEL > 2
184#define USERVER_IMPL_LOGS_WARNING_ERASER(X) USERVER_IMPL_ERASE_LOG
187#if USERVER_FEATURE_ERASE_LOG_WITH_LEVEL > 3
189#define USERVER_IMPL_LOGS_ERROR_ERASER(X) USERVER_IMPL_ERASE_LOG
194#ifndef USERVER_IMPL_LOGS_TRACE_ERASER
196#define USERVER_IMPL_LOGS_TRACE_ERASER(X) X
199#ifndef USERVER_IMPL_LOGS_DEBUG_ERASER
201#define USERVER_IMPL_LOGS_DEBUG_ERASER(X) X
204#ifndef USERVER_IMPL_LOGS_INFO_ERASER
206#define USERVER_IMPL_LOGS_INFO_ERASER(X) X
209#ifndef USERVER_IMPL_LOGS_WARNING_ERASER
211#define USERVER_IMPL_LOGS_WARNING_ERASER(X) X
214#ifndef USERVER_IMPL_LOGS_ERROR_ERASER
216#define USERVER_IMPL_LOGS_ERROR_ERASER(X) X
220#define USERVER_IMPL_LOG_TO(logger, level) USERVER_NAMESPACE
::logging::LogHelper(logger, level).AsLvalue()
223#define USERVER_IMPL_DYNAMIC_DEBUG_ENTRY
224 []() noexcept -> const USERVER_NAMESPACE
::logging::impl::StaticLogEntry& {
226 static constexpr const char* Get() noexcept { return USERVER_FILEPATH.data(); }
228 const auto& entry = USERVER_NAMESPACE
::logging::impl::EntryStorage<NameHolder, __LINE__>::entry;
238#define LOG_TO(logger, lvl)
240 USERVER_IMPL_DYNAMIC_DEBUG_ENTRY
().ShouldNotLog((logger), (lvl)),
241 static_cast<int>(lvl) < static_cast<int>(USERVER_NAMESPACE
::logging::Level::kInfo)
243 ? USERVER_NAMESPACE
::logging::impl::Noop{}
244 : USERVER_IMPL_LOG_TO
((logger), (lvl))
250#define LOG(lvl) LOG_TO(USERVER_NAMESPACE
::logging::GetDefaultLogger(), (lvl))
255#define LOG_TRACE() USERVER_IMPL_LOGS_TRACE_ERASER
(LOG(USERVER_NAMESPACE
::logging::Level::kTrace))
260#define LOG_DEBUG() USERVER_IMPL_LOGS_DEBUG_ERASER
(LOG(USERVER_NAMESPACE
::logging::Level::kDebug))
265#define LOG_INFO() USERVER_IMPL_LOGS_INFO_ERASER
(LOG(USERVER_NAMESPACE
::logging::Level::kInfo))
270#define LOG_WARNING() USERVER_IMPL_LOGS_WARNING_ERASER
(LOG(USERVER_NAMESPACE
::logging::Level::kWarning))
275#define LOG_ERROR() USERVER_IMPL_LOGS_ERROR_ERASER
(LOG(USERVER_NAMESPACE
::logging::Level::kError))
280#define LOG_CRITICAL() LOG(USERVER_NAMESPACE
::logging::Level::kCritical)
287#define LOG_TRACE_TO(logger) USERVER_IMPL_LOGS_TRACE_ERASER
(LOG_TO(logger, USERVER_NAMESPACE
::logging::Level::kTrace))
292#define LOG_DEBUG_TO(logger) USERVER_IMPL_LOGS_DEBUG_ERASER
(LOG_TO(logger, USERVER_NAMESPACE
::logging::Level::kDebug))
297#define LOG_INFO_TO(logger) USERVER_IMPL_LOGS_INFO_ERASER
(LOG_TO(logger, USERVER_NAMESPACE
::logging::Level::kInfo))
302#define LOG_WARNING_TO(logger)
303 USERVER_IMPL_LOGS_WARNING_ERASER
(LOG_TO(logger, USERVER_NAMESPACE
::logging::Level::kWarning))
308#define LOG_ERROR_TO(logger) USERVER_IMPL_LOGS_ERROR_ERASER
(LOG_TO(logger, USERVER_NAMESPACE
::logging::Level::kError))
313#define LOG_CRITICAL_TO(logger) LOG_TO(logger, USERVER_NAMESPACE
::logging::Level::kCritical)
324#define LOG_LIMITED_TO(logger, lvl)
325 for (USERVER_NAMESPACE
::logging::impl::RateLimiter log_limited_to_rl{
326 []() -> USERVER_NAMESPACE
::logging::impl::RateLimitData& {
327 thread_local USERVER_NAMESPACE
::logging::impl::RateLimitData rl_data;
331 log_limited_to_rl.ShouldLog();
332 log_limited_to_rl.SetShouldNotLog())
333 LOG_TO((logger), log_limited_to_rl.GetLevel()) << log_limited_to_rl
338#define LOG_LIMITED(lvl) LOG_LIMITED_TO(USERVER_NAMESPACE
::logging::GetDefaultLogger(), lvl)
344#define LOG_LIMITED_TRACE() USERVER_IMPL_LOGS_TRACE_ERASER
(LOG_LIMITED(USERVER_NAMESPACE
::logging::Level::kTrace))
350#define LOG_LIMITED_DEBUG() USERVER_IMPL_LOGS_DEBUG_ERASER
(LOG_LIMITED(USERVER_NAMESPACE
::logging::Level::kDebug))
356#define LOG_LIMITED_INFO() USERVER_IMPL_LOGS_INFO_ERASER
(LOG_LIMITED(USERVER_NAMESPACE
::logging::Level::kInfo))
362#define LOG_LIMITED_WARNING() USERVER_IMPL_LOGS_WARNING_ERASER
(LOG_LIMITED(USERVER_NAMESPACE
::logging::Level::kWarning))
368#define LOG_LIMITED_ERROR() USERVER_IMPL_LOGS_ERROR_ERASER
(LOG_LIMITED(USERVER_NAMESPACE
::logging::Level::kError))
374#define LOG_LIMITED_CRITICAL() LOG_LIMITED(USERVER_NAMESPACE
::logging::Level::kCritical)
382#define LOG_LIMITED_TRACE_TO(logger)
383 USERVER_IMPL_LOGS_TRACE_ERASER
(LOG_LIMITED_TO(logger, USERVER_NAMESPACE
::logging::Level::kTrace))
389#define LOG_LIMITED_DEBUG_TO(logger)
390 USERVER_IMPL_LOGS_DEBUG_ERASER
(LOG_LIMITED_TO(logger, USERVER_NAMESPACE
::logging::Level::kDebug))
396#define LOG_LIMITED_INFO_TO(logger)
397 USERVER_IMPL_LOGS_INFO_ERASER
(LOG_LIMITED_TO(logger, USERVER_NAMESPACE
::logging::Level::kInfo))
403#define LOG_LIMITED_WARNING_TO(logger)
404 USERVER_IMPL_LOGS_WARNING_ERASER
(LOG_LIMITED_TO(logger, USERVER_NAMESPACE
::logging::Level::kWarning))
410#define LOG_LIMITED_ERROR_TO(logger)
411 USERVER_IMPL_LOGS_ERROR_ERASER
(LOG_LIMITED_TO(logger, USERVER_NAMESPACE
::logging::Level::kError))
417#define LOG_LIMITED_CRITICAL_TO(logger) LOG_LIMITED_TO(logger, USERVER_NAMESPACE
::logging::Level::kCritical)