stream_log_manager.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #include "stream_log_manager.h"
  2. #include <library/cpp/yt/logging/logger.h>
  3. #include <library/cpp/yt/logging/plain_text_formatter/formatter.h>
  4. #include <library/cpp/yt/string/raw_formatter.h>
  5. #include <library/cpp/yt/threading/fork_aware_spin_lock.h>
  6. namespace NYT::NLogging {
  7. ////////////////////////////////////////////////////////////////////////////////
  8. class TStreamLogManager
  9. : public ILogManager
  10. {
  11. public:
  12. explicit TStreamLogManager(IOutputStream* output)
  13. : Output_(output)
  14. { }
  15. void RegisterStaticAnchor(
  16. TLoggingAnchor* anchor,
  17. ::TSourceLocation /*sourceLocation*/,
  18. TStringBuf /*anchorMessage*/) override
  19. {
  20. anchor->Registered = true;
  21. }
  22. virtual void UpdateAnchor(TLoggingAnchor* anchor) override
  23. {
  24. anchor->Enabled = true;
  25. }
  26. virtual void Enqueue(TLogEvent&& event) override
  27. {
  28. Buffer_.Reset();
  29. EventFormatter_.Format(&Buffer_, event);
  30. *Output_ << Buffer_.GetBuffer() << Endl;
  31. }
  32. virtual const TLoggingCategory* GetCategory(TStringBuf categoryName) override
  33. {
  34. if (!categoryName) {
  35. return nullptr;
  36. }
  37. auto guard = Guard(SpinLock_);
  38. auto it = NameToCategory_.find(categoryName);
  39. if (it == NameToCategory_.end()) {
  40. auto category = std::make_unique<TLoggingCategory>();
  41. category->Name = categoryName;
  42. category->ActualVersion = &Version_;
  43. category->CurrentVersion = Version_.load();
  44. it = NameToCategory_.emplace(categoryName, std::move(category)).first;
  45. }
  46. return it->second.get();
  47. }
  48. virtual void UpdateCategory(TLoggingCategory* /*category*/) override
  49. { }
  50. virtual bool GetAbortOnAlert() const override
  51. {
  52. return false;
  53. }
  54. private:
  55. IOutputStream* const Output_;
  56. NThreading::TForkAwareSpinLock SpinLock_;
  57. THashMap<TString, std::unique_ptr<TLoggingCategory>> NameToCategory_;
  58. std::atomic<int> Version_ = 1;
  59. TPlainTextEventFormatter EventFormatter_{/*enableSourceLocation*/ false};
  60. TRawFormatter<MessageBufferSize> Buffer_;
  61. };
  62. std::unique_ptr<ILogManager> CreateStreamLogManager(IOutputStream* output)
  63. {
  64. return std::make_unique<TStreamLogManager>(output);
  65. }
  66. ////////////////////////////////////////////////////////////////////////////////
  67. } // namespace NYT::NLogging