reopen.h 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #pragma once
  2. #include "log.h"
  3. #include "backend.h"
  4. #include <util/generic/fwd.h>
  5. #include <util/generic/ptr.h>
  6. #include <util/generic/size_literals.h>
  7. #include <atomic>
  8. class TReopenLogBackend: public TLogBackend {
  9. public:
  10. explicit TReopenLogBackend(THolder<TLogBackend>&& backend, ui64 bytesWrittenLimit = 1_GB)
  11. : Backend_(std::move(backend)), BytesWrittenLimit_(bytesWrittenLimit), BytesWritten_(0) {
  12. Y_ENSURE(BytesWrittenLimit_ > 0);
  13. }
  14. void WriteData(const TLogRecord& rec) override {
  15. const ui64 prevWritten = BytesWritten_.fetch_add(rec.Len);
  16. if (prevWritten < BytesWrittenLimit_ && prevWritten + rec.Len >= BytesWrittenLimit_) {
  17. try {
  18. ReopenLog();
  19. } catch (...) {
  20. }
  21. }
  22. Backend_->WriteData(rec);
  23. }
  24. void ReopenLog() override {
  25. BytesWritten_.store(0);
  26. Backend_->ReopenLog();
  27. }
  28. private:
  29. const THolder<TLogBackend> Backend_;
  30. const ui64 BytesWrittenLimit_;
  31. std::atomic<ui64> BytesWritten_;
  32. };