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#define USERVER_IMPL_LOG_TO(logger, level)
155 USERVER_NAMESPACE
::logging::LogHelper(logger, level).AsLvalue()
158#define USERVER_IMPL_DYNAMIC_DEBUG_ENTRY
159 []() noexcept -> const USERVER_NAMESPACE
::logging::impl::StaticLogEntry& {
161 static constexpr const char* Get() noexcept {
166 USERVER_NAMESPACE
::logging::impl::EntryStorage<NameHolder,
177#define LOG_TO(logger, lvl)
179 USERVER_IMPL_DYNAMIC_DEBUG_ENTRY
().ShouldNotLog((logger), (lvl)),
180 static_cast<int>(lvl) <
181 static_cast<int>(USERVER_NAMESPACE
::logging::Level::kInfo))
182 ? USERVER_NAMESPACE
::logging::impl::Noop{}
183 : USERVER_IMPL_LOG_TO
((logger), (lvl))
189#define LOG(lvl) LOG_TO(USERVER_NAMESPACE
::logging::GetDefaultLogger(), (lvl))
194#define LOG_TRACE() LOG(USERVER_NAMESPACE
::logging::Level::kTrace)
199#define LOG_DEBUG() LOG(USERVER_NAMESPACE
::logging::Level::kDebug)
204#define LOG_INFO() LOG(USERVER_NAMESPACE
::logging::Level::kInfo)
209#define LOG_WARNING() LOG(USERVER_NAMESPACE
::logging::Level::kWarning)
214#define LOG_ERROR() LOG(USERVER_NAMESPACE
::logging::Level::kError)
219#define LOG_CRITICAL() LOG(USERVER_NAMESPACE
::logging::Level::kCritical)
226#define LOG_TRACE_TO(logger)
227 LOG_TO(logger, USERVER_NAMESPACE
::logging::Level::kTrace)
232#define LOG_DEBUG_TO(logger)
233 LOG_TO(logger, USERVER_NAMESPACE
::logging::Level::kDebug)
238#define LOG_INFO_TO(logger)
239 LOG_TO(logger, USERVER_NAMESPACE
::logging::Level::kInfo)
244#define LOG_WARNING_TO(logger)
245 LOG_TO(logger, USERVER_NAMESPACE
::logging::Level::kWarning)
250#define LOG_ERROR_TO(logger)
251 LOG_TO(logger, USERVER_NAMESPACE
::logging::Level::kError)
256#define LOG_CRITICAL_TO(logger)
257 LOG_TO(logger, USERVER_NAMESPACE
::logging::Level::kCritical)
268#define LOG_LIMITED_TO(logger, lvl)
269 for (USERVER_NAMESPACE
::logging::impl::RateLimiter log_limited_to_rl{
270 []() -> USERVER_NAMESPACE
::logging::impl::RateLimitData& {
271 thread_local USERVER_NAMESPACE
::logging::impl::RateLimitData
276 log_limited_to_rl.ShouldLog(); log_limited_to_rl.SetShouldNotLog())
277 LOG_TO((logger), log_limited_to_rl.GetLevel()) << log_limited_to_rl
282#define LOG_LIMITED(lvl)
283 LOG_LIMITED_TO(USERVER_NAMESPACE
::logging::GetDefaultLogger(), lvl)
289#define LOG_LIMITED_TRACE()
290 LOG_LIMITED(USERVER_NAMESPACE
::logging::Level::kTrace)
296#define LOG_LIMITED_DEBUG()
297 LOG_LIMITED(USERVER_NAMESPACE
::logging::Level::kDebug)
303#define LOG_LIMITED_INFO() LOG_LIMITED(USERVER_NAMESPACE
::logging::Level::kInfo)
309#define LOG_LIMITED_WARNING()
310 LOG_LIMITED(USERVER_NAMESPACE
::logging::Level::kWarning)
316#define LOG_LIMITED_ERROR()
317 LOG_LIMITED(USERVER_NAMESPACE
::logging::Level::kError)
323#define LOG_LIMITED_CRITICAL()
324 LOG_LIMITED(USERVER_NAMESPACE
::logging::Level::kCritical)
332#define LOG_LIMITED_TRACE_TO(logger)
339#define LOG_LIMITED_DEBUG_TO(logger)
346#define LOG_LIMITED_INFO_TO(logger)
353#define LOG_LIMITED_WARNING_TO(logger)
354 LOG_LIMITED_TO(logger, USERVER_NAMESPACE
::logging::Level::kWarning)
360#define LOG_LIMITED_ERROR_TO(logger)
367#define LOG_LIMITED_CRITICAL_TO(logger)
368 LOG_LIMITED_TO(logger, USERVER_NAMESPACE
::logging::Level::kCritical)