global.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #pragma once
  2. #include "common.h"
  3. #include "rty_formater.h"
  4. // ATTENTION! MUST CALL DoInitGlobalLog BEFORE USAGE
  5. bool GlobalLogInitialized();
  6. void DoInitGlobalLog(const TString& logType, const int logLevel, const bool rotation, const bool startAsDaemon, THolder<ILoggerFormatter> formatter = {}, bool threaded = false);
  7. void DoInitGlobalLog(THolder<TLogBackend> backend, THolder<ILoggerFormatter> formatter = {});
  8. inline void InitGlobalLog2Null() {
  9. DoInitGlobalLog("null", TLOG_EMERG, false, false);
  10. }
  11. inline void InitGlobalLog2Console(int loglevel = TLOG_INFO) {
  12. DoInitGlobalLog("console", loglevel, false, false);
  13. }
  14. class TGlobalLog: public TLog {
  15. public:
  16. TGlobalLog(const TString& logType, ELogPriority priority = LOG_MAX_PRIORITY)
  17. : TLog(logType, priority)
  18. {
  19. }
  20. TGlobalLog(THolder<TLogBackend> backend)
  21. : TLog(std::move(backend))
  22. {
  23. }
  24. };
  25. template <>
  26. TGlobalLog* CreateDefaultLogger<TGlobalLog>();
  27. class TNullLog: public TLog {
  28. public:
  29. TNullLog(const TString& logType, ELogPriority priority = LOG_MAX_PRIORITY)
  30. : TLog(logType, priority)
  31. {
  32. }
  33. TNullLog(THolder<TLogBackend> backend)
  34. : TLog(std::move(backend))
  35. {
  36. }
  37. };
  38. template <>
  39. TNullLog* CreateDefaultLogger<TNullLog>();
  40. template <>
  41. class TSingletonTraits<TLoggerOperator<TGlobalLog>::TPtr> {
  42. public:
  43. static const size_t Priority = NLoggingImpl::SingletonPriority;
  44. };
  45. template <>
  46. class TSingletonTraits<TLoggerOperator<TNullLog>::TPtr> {
  47. public:
  48. static const size_t Priority = NLoggingImpl::SingletonPriority;
  49. };
  50. #define FATAL_LOG SINGLETON_CHECKED_GENERIC_LOG(TGlobalLog, TRTYLogPreprocessor, TLOG_CRIT, "CRITICAL_INFO")
  51. #define ALERT_LOG SINGLETON_CHECKED_GENERIC_LOG(TGlobalLog, TRTYLogPreprocessor, TLOG_ALERT, "ALERT")
  52. #define ERROR_LOG SINGLETON_CHECKED_GENERIC_LOG(TGlobalLog, TRTYLogPreprocessor, TLOG_ERR, "ERROR")
  53. #define WARNING_LOG SINGLETON_CHECKED_GENERIC_LOG(TGlobalLog, TRTYLogPreprocessor, TLOG_WARNING, "WARNING")
  54. #define NOTICE_LOG SINGLETON_CHECKED_GENERIC_LOG(TGlobalLog, TRTYLogPreprocessor, TLOG_NOTICE, "NOTICE")
  55. #define INFO_LOG SINGLETON_CHECKED_GENERIC_LOG(TGlobalLog, TRTYLogPreprocessor, TLOG_INFO, "INFO")
  56. #define DEBUG_LOG SINGLETON_CHECKED_GENERIC_LOG(TGlobalLog, TRTYLogPreprocessor, TLOG_DEBUG, "DEBUG")
  57. #define RESOURCES_LOG SINGLETON_CHECKED_GENERIC_LOG(TGlobalLog, TRTYLogPreprocessor, TLOG_RESOURCES, "RESOURCES")
  58. #define TEMPLATE_LOG(logLevel) SINGLETON_CHECKED_GENERIC_LOG(TGlobalLog, TRTYLogPreprocessor, logLevel, ToString(logLevel).data())
  59. #define IS_LOG_ACTIVE(logLevel) (TLoggerOperator<TGlobalLog>::Log().FiltrationLevel() >= logLevel)
  60. #define RTY_MEM_LOG(Action) \
  61. { NOTICE_LOG << "RESOURCES On " << Action << ": " << NLoggingImpl::GetSystemResources() << Endl; };
  62. #define VERIFY_WITH_LOG(expr, msg, ...) \
  63. do { \
  64. if (Y_UNLIKELY(!(expr))) { \
  65. FATAL_LOG << Sprintf(msg, ##__VA_ARGS__) << Endl; \
  66. Y_ABORT_UNLESS(false, msg, ##__VA_ARGS__); \
  67. }; \
  68. } while (0);
  69. namespace NPrivateGlobalLogger {
  70. class TVerifyEvent: public TStringStream {
  71. public:
  72. ~TVerifyEvent();
  73. template <class T>
  74. inline TVerifyEvent& operator<<(const T& t) {
  75. static_cast<IOutputStream&>(*this) << t;
  76. return *this;
  77. }
  78. };
  79. class TNullStream: public TStringStream {
  80. public:
  81. ~TNullStream() = default;
  82. template <class T>
  83. inline TNullStream& operator<<(const T& /*t*/) {
  84. return *this;
  85. }
  86. };
  87. }
  88. #define CHECK_WITH_LOG(expr) \
  89. Y_UNLIKELY(!(expr)) && NPrivateGlobalLogger::TEatStream() | NPrivateGlobalLogger::TVerifyEvent() << __LOCATION__ << ": " << #expr << "(verification failed!): "
  90. #if !defined(NDEBUG) && !defined(__GCCXML__)
  91. #define ASSERT_WITH_LOG(expr) \
  92. Y_UNLIKELY(!(expr)) && NPrivateGlobalLogger::TEatStream() | NPrivateGlobalLogger::TVerifyEvent() << __LOCATION__ << ": " << #expr << "(verification failed!): "
  93. #else
  94. #define ASSERT_WITH_LOG(expr) \
  95. Y_UNLIKELY(false && !(expr)) && NPrivateGlobalLogger::TEatStream() | NPrivateGlobalLogger::TNullStream()
  96. #endif
  97. #define CHECK_EQ_WITH_LOG(a, b) CHECK_WITH_LOG((a) == (b)) << a << " != " << b;
  98. #define CHECK_LEQ_WITH_LOG(a, b) CHECK_WITH_LOG((a) <= (b)) << a << " > " << b;
  99. #define FAIL_LOG(msg, ...) VERIFY_WITH_LOG(false, msg, ##__VA_ARGS__)
  100. #define S_FAIL_LOG CHECK_WITH_LOG(false)