logger.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #pragma once
  2. #include <library/cpp/unified_agent_client/f_maybe.h>
  3. #include <library/cpp/unified_agent_client/throttling.h>
  4. #include <library/cpp/logger/log.h>
  5. #include <library/cpp/monlib/dynamic_counters/counters.h>
  6. #include <util/generic/string.h>
  7. #include <util/string/join.h>
  8. #include <util/string/printf.h>
  9. #include <util/system/file.h>
  10. #include <atomic>
  11. #define YLOG(logPriority, message, logger) \
  12. do { \
  13. const auto __logPriority = logPriority; \
  14. if (auto* log = logger.Accept(__logPriority, false); log != nullptr) { \
  15. logger.Log(*log, __logPriority, message); \
  16. } \
  17. } while (false)
  18. #define YLOG_EMERG(msg) YLOG(TLOG_EMERG, msg, Logger)
  19. #define YLOG_ALERT(msg) YLOG(TLOG_ALERT, msg, Logger)
  20. #define YLOG_CRIT(msg) YLOG(TLOG_CRIT, msg, Logger)
  21. #define YLOG_ERR(msg) YLOG(TLOG_ERR, msg, Logger)
  22. #define YLOG_WARNING(msg) YLOG(TLOG_WARNING, msg, Logger)
  23. #define YLOG_NOTICE(msg) YLOG(TLOG_NOTICE, msg, Logger)
  24. #define YLOG_INFO(msg) YLOG(TLOG_INFO, msg, Logger)
  25. #define YLOG_DEBUG(msg) YLOG(TLOG_DEBUG, msg, Logger)
  26. #define YLOG_RESOURCES(msg) YLOG(TLOG_RESOURCES , msg, Logger)
  27. #define YLOG_FATAL(msg) \
  28. YLOG(TLOG_CRIT, msg, Logger); \
  29. _Exit(1);
  30. namespace NUnifiedAgent {
  31. class TScopeLogger;
  32. class TLogger {
  33. public:
  34. TLogger(TLog& log, TFMaybe<size_t> rateLimitBytes);
  35. void StartTracing(ELogPriority logPriority) noexcept;
  36. void FinishTracing() noexcept;
  37. inline TScopeLogger Child(const TString& v, NMonitoring::TDeprecatedCounter* errors = nullptr);
  38. inline void SetErrorsCounter(NMonitoring::TDeprecatedCounter* counter) noexcept {
  39. Errors = counter;
  40. }
  41. inline void SetDroppedBytesCounter(NMonitoring::TDeprecatedCounter* counter) noexcept {
  42. DroppedBytes = counter;
  43. }
  44. inline bool HasRateLimit() const noexcept {
  45. return Throttler != nullptr;
  46. }
  47. friend class TScopeLogger;
  48. private:
  49. void Log(TLog& log, ELogPriority logPriority, const TStringBuf message, const TString& scope) const;
  50. inline TLog* Accept(ELogPriority logPriority, NMonitoring::TDeprecatedCounter* errors) const noexcept {
  51. if ((logPriority <= TLOG_ERR) && (errors != nullptr)) {
  52. ++(*errors);
  53. }
  54. auto* result = CurrentLogContext_.load(std::memory_order_acquire);
  55. return result != nullptr && static_cast<int>(logPriority) <= static_cast<int>(result->Priority)
  56. ? &result->Log
  57. : nullptr;
  58. }
  59. private:
  60. struct TLogContext {
  61. TLog Log;
  62. ELogPriority Priority;
  63. };
  64. class TThrottlerWithLock {
  65. public:
  66. explicit TThrottlerWithLock(size_t rateLimitBytes);
  67. bool TryConsume(double tokens);
  68. private:
  69. TThrottler Throttler;
  70. TAdaptiveLock Lock;
  71. };
  72. private:
  73. void SetCurrentLogContext(TLogContext& logContext);
  74. TLogContext& GetOrCreateTracingLogContext(ELogPriority logPriority);
  75. void SetTracing(TLogContext& logContext, const char* action);
  76. private:
  77. TLogContext DefaultLogContext;
  78. TVector<THolder<TLogContext>> TracingLogContexts;
  79. std::atomic<TLogContext*> CurrentLogContext_;
  80. NMonitoring::TDeprecatedCounter* Errors;
  81. NMonitoring::TDeprecatedCounter* DroppedBytes;
  82. const THolder<TThrottlerWithLock> Throttler;
  83. TAdaptiveLock Lock;
  84. };
  85. class TScopeLogger {
  86. public:
  87. TScopeLogger();
  88. inline void Log(TLog& log, ELogPriority logPriority, const TStringBuf message) const {
  89. if (Logger) {
  90. Logger->Log(log, logPriority, message, Scope);
  91. }
  92. }
  93. inline TLog* Accept(ELogPriority logPriority, bool silent) const noexcept {
  94. return Logger ? Logger->Accept(logPriority, silent ? nullptr : Errors) : nullptr;
  95. }
  96. inline TScopeLogger Child(const TString& v, NMonitoring::TDeprecatedCounter* errors = nullptr) {
  97. return Logger
  98. ? Logger->Child(Join('/', Scope, v), errors == nullptr ? Errors : errors)
  99. : TScopeLogger();
  100. }
  101. inline TLogger* Unwrap() noexcept {
  102. return Logger;
  103. }
  104. ELogPriority FiltrationLevel() const {
  105. if (Logger) {
  106. return Logger->CurrentLogContext_.load()->Priority;
  107. }
  108. return LOG_DEF_PRIORITY;
  109. }
  110. friend class TLogger;
  111. private:
  112. TScopeLogger(TLogger* logger,
  113. const TString& scope,
  114. NMonitoring::TDeprecatedCounter* errors);
  115. private:
  116. TLogger* Logger;
  117. TString Scope;
  118. NMonitoring::TDeprecatedCounter* Errors;
  119. };
  120. inline TScopeLogger TLogger::Child(const TString& v, NMonitoring::TDeprecatedCounter* errors) {
  121. return TScopeLogger(this, v, errors == nullptr ? Errors : errors);
  122. }
  123. inline ELogPriority ToLogPriority(int level) noexcept {
  124. const auto result = ClampVal(level, 0, static_cast<int>(TLOG_RESOURCES));
  125. return static_cast<ELogPriority>(result);
  126. }
  127. }