| 10 | #include <atomic> |
| 11 | |
| 12 | class TReopenLogBackend: public TLogBackend { |
| 13 | public: |
| 14 | explicit TReopenLogBackend(THolder<TLogBackend>&& backend, ui64 bytesWrittenLimit = 1_GB) |
| 15 | : Backend_(std::move(backend)), BytesWrittenLimit_(bytesWrittenLimit), BytesWritten_(0) { |
| 16 | Y_ENSURE(BytesWrittenLimit_ > 0); |
| 17 | } |
| 18 | |
| 19 | void WriteData(const TLogRecord& rec) override { |
| 20 | const ui64 prevWritten = BytesWritten_.fetch_add(rec.Len); |
| 21 | if (prevWritten < BytesWrittenLimit_ && prevWritten + rec.Len >= BytesWrittenLimit_) { |
| 22 | try { |
| 23 | ReopenLog(); |
| 24 | } catch (...) { |
| 25 | } |
| 26 | } |
| 27 | Backend_->WriteData(rec); |
| 28 | } |
| 29 | |
| 30 | void ReopenLog() override { |
| 31 | BytesWritten_.store(0); |
| 32 | Backend_->ReopenLog(); |
| 33 | } |
| 34 | |
| 35 | private: |
| 36 | const THolder<TLogBackend> Backend_; |
| 37 | |
| 38 | const ui64 BytesWrittenLimit_; |
| 39 | std::atomic<ui64> BytesWritten_; |
| 40 | }; |