yt_log.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #include "yt_log.h"
  2. #include "logger.h"
  3. #include <util/generic/guid.h>
  4. #include <util/system/mutex.h>
  5. namespace NYT {
  6. using namespace NLogging;
  7. ////////////////////////////////////////////////////////////////////////////////
  8. namespace {
  9. class TLogManager
  10. : public ILogManager
  11. {
  12. public:
  13. static constexpr TStringBuf CategoryName = "Wrapper";
  14. public:
  15. void RegisterStaticAnchor(
  16. TLoggingAnchor* anchor,
  17. ::TSourceLocation sourceLocation,
  18. TStringBuf anchorMessage) override
  19. {
  20. if (anchor->Registered.exchange(true)) {
  21. return;
  22. }
  23. anchor->Enabled.store(true);
  24. auto guard = Guard(Mutex_);
  25. anchor->SourceLocation = sourceLocation;
  26. anchor->AnchorMessage = anchorMessage;
  27. }
  28. void UpdateAnchor(TLoggingAnchor* /*position*/) override
  29. { }
  30. void Enqueue(TLogEvent&& event) override
  31. {
  32. auto message = TString(event.MessageRef.ToStringBuf());
  33. LogMessage(
  34. ToImplLevel(event.Level),
  35. ::TSourceLocation(event.SourceFile, event.SourceLine),
  36. "%.*s",
  37. event.MessageRef.size(),
  38. event.MessageRef.begin());
  39. }
  40. const TLoggingCategory* GetCategory(TStringBuf categoryName) override
  41. {
  42. Y_ABORT_UNLESS(categoryName == CategoryName);
  43. return &Category_;
  44. }
  45. void UpdateCategory(TLoggingCategory* /*category*/) override
  46. {
  47. Y_ABORT();
  48. }
  49. bool GetAbortOnAlert() const override
  50. {
  51. return false;
  52. }
  53. private:
  54. static ILogger::ELevel ToImplLevel(ELogLevel level)
  55. {
  56. switch (level) {
  57. case ELogLevel::Minimum:
  58. case ELogLevel::Trace:
  59. case ELogLevel::Debug:
  60. return ILogger::ELevel::DEBUG;
  61. case ELogLevel::Info:
  62. return ILogger::ELevel::INFO;
  63. case ELogLevel::Warning:
  64. case ELogLevel::Error:
  65. return ILogger::ELevel::ERROR;
  66. case ELogLevel::Alert:
  67. case ELogLevel::Fatal:
  68. case ELogLevel::Maximum:
  69. return ILogger::ELevel::FATAL;
  70. }
  71. }
  72. static void LogMessage(ILogger::ELevel level, const ::TSourceLocation& sourceLocation, const char* format, ...)
  73. {
  74. va_list args;
  75. va_start(args, format);
  76. GetLogger()->Log(level, sourceLocation, format, args);
  77. va_end(args);
  78. }
  79. private:
  80. ::TMutex Mutex_;
  81. std::atomic<int> ActualVersion_{1};
  82. const TLoggingCategory Category_{
  83. .Name{CategoryName},
  84. .MinPlainTextLevel{ELogLevel::Minimum},
  85. .CurrentVersion{1},
  86. .ActualVersion = &ActualVersion_,
  87. };
  88. };
  89. TLogManager LogManager;
  90. } // namespace
  91. ////////////////////////////////////////////////////////////////////////////////
  92. TLogger Logger(&LogManager, TLogManager::CategoryName);
  93. ////////////////////////////////////////////////////////////////////////////////
  94. void FormatValue(TStringBuilderBase* builder, const TGUID& value, TStringBuf /*format*/)
  95. {
  96. builder->AppendString(GetGuidAsString(value));
  97. }
  98. ////////////////////////////////////////////////////////////////////////////////
  99. } // namespace NYT