From 4b28e15652a7672062edb6804bc985eedc333257 Mon Sep 17 00:00:00 2001 From: danibw Date: Thu, 10 Feb 2022 16:50:12 +0300 Subject: Restoring authorship annotation for . Commit 2 of 2. --- library/cpp/logger/rotating_file.cpp | 64 +++++++++++++++---------------- library/cpp/logger/rotating_file.h | 2 +- library/cpp/logger/rotating_file_ut.cpp | 68 ++++++++++++++++----------------- 3 files changed, 67 insertions(+), 67 deletions(-) (limited to 'library/cpp/logger') diff --git a/library/cpp/logger/rotating_file.cpp b/library/cpp/logger/rotating_file.cpp index 3391d260025..a62f48f25db 100644 --- a/library/cpp/logger/rotating_file.cpp +++ b/library/cpp/logger/rotating_file.cpp @@ -2,7 +2,7 @@ #include "file.h" #include "record.h" -#include +#include #include #include #include @@ -11,36 +11,36 @@ /* * rotating file log - * if Size_ > MaxSizeBytes - * Path.(N-1) -> Path.N - * Path.(N-2) -> Path.(N-1) - * ... - * Path.1 -> Path.2 - * Path -> Path.1 + * if Size_ > MaxSizeBytes + * Path.(N-1) -> Path.N + * Path.(N-2) -> Path.(N-1) + * ... + * Path.1 -> Path.2 + * Path -> Path.1 */ class TRotatingFileLogBackend::TImpl { public: - inline TImpl(const TString& path, const ui64 maxSizeBytes, const ui32 rotatedFilesCount) - : Log_(path) - , Path_(path) - , MaxSizeBytes_(maxSizeBytes) - , Size_(TFileStat(Path_).Size) - , RotatedFilesCount_(rotatedFilesCount) - { - Y_ENSURE(RotatedFilesCount_ != 0); - } - + inline TImpl(const TString& path, const ui64 maxSizeBytes, const ui32 rotatedFilesCount) + : Log_(path) + , Path_(path) + , MaxSizeBytes_(maxSizeBytes) + , Size_(TFileStat(Path_).Size) + , RotatedFilesCount_(rotatedFilesCount) + { + Y_ENSURE(RotatedFilesCount_ != 0); + } + inline void WriteData(const TLogRecord& rec) { if (static_cast(AtomicGet(Size_)) > MaxSizeBytes_) { TWriteGuard guard(Lock_); if (static_cast(AtomicGet(Size_)) > MaxSizeBytes_) { - TString newLogPath(TStringBuilder{} << Path_ << "." << RotatedFilesCount_); - for (size_t fileId = RotatedFilesCount_ - 1; fileId; --fileId) { - TString oldLogPath(TStringBuilder{} << Path_ << "." << fileId); - NFs::Rename(oldLogPath, newLogPath); - newLogPath = oldLogPath; - } - NFs::Rename(Path_, newLogPath); + TString newLogPath(TStringBuilder{} << Path_ << "." << RotatedFilesCount_); + for (size_t fileId = RotatedFilesCount_ - 1; fileId; --fileId) { + TString oldLogPath(TStringBuilder{} << Path_ << "." << fileId); + NFs::Rename(oldLogPath, newLogPath); + newLogPath = oldLogPath; + } + NFs::Rename(Path_, newLogPath); Log_.ReopenLog(); AtomicSet(Size_, 0); } @@ -54,23 +54,23 @@ public: TWriteGuard guard(Lock_); Log_.ReopenLog(); - AtomicSet(Size_, TFileStat(Path_).Size); + AtomicSet(Size_, TFileStat(Path_).Size); } private: TRWMutex Lock_; TFileLogBackend Log_; - const TString Path_; + const TString Path_; const ui64 MaxSizeBytes_; TAtomic Size_; - const ui32 RotatedFilesCount_; + const ui32 RotatedFilesCount_; }; -TRotatingFileLogBackend::TRotatingFileLogBackend(const TString& path, const ui64 maxSizeByte, const ui32 rotatedFilesCount) - : Impl_(new TImpl(path, maxSizeByte, rotatedFilesCount)) -{ -} - +TRotatingFileLogBackend::TRotatingFileLogBackend(const TString& path, const ui64 maxSizeByte, const ui32 rotatedFilesCount) + : Impl_(new TImpl(path, maxSizeByte, rotatedFilesCount)) +{ +} + TRotatingFileLogBackend::~TRotatingFileLogBackend() { } diff --git a/library/cpp/logger/rotating_file.h b/library/cpp/logger/rotating_file.h index 8f5f834fbbc..cb047f25fbd 100644 --- a/library/cpp/logger/rotating_file.h +++ b/library/cpp/logger/rotating_file.h @@ -8,7 +8,7 @@ class TRotatingFileLogBackend: public TLogBackend { public: TRotatingFileLogBackend(const TString& preRotatePath, const TString& postRotatePath, const ui64 maxSizeBytes); - TRotatingFileLogBackend(const TString& path, const ui64 maxSizeBytes, const ui32 rotatedFilesCount); + TRotatingFileLogBackend(const TString& path, const ui64 maxSizeBytes, const ui32 rotatedFilesCount); ~TRotatingFileLogBackend() override; void WriteData(const TLogRecord& rec) override; diff --git a/library/cpp/logger/rotating_file_ut.cpp b/library/cpp/logger/rotating_file_ut.cpp index 6cfa41da803..84966933d96 100644 --- a/library/cpp/logger/rotating_file_ut.cpp +++ b/library/cpp/logger/rotating_file_ut.cpp @@ -8,38 +8,38 @@ #include #include -Y_UNIT_TEST_SUITE(NewRotatingFileSuite) { - const TString PATH = GetWorkPath() + "/my.log"; - - Y_UNIT_TEST(TestFileWrite) { - TRotatingFileLogBackend log(PATH, 4000, 2); - TString data = "my data"; - log.WriteData(TLogRecord(ELogPriority::TLOG_INFO, data.data(), data.size())); - UNIT_ASSERT_C(TFileStat(PATH).Size > 0, "file " << PATH << " has zero size"); - } - - Y_UNIT_TEST(TestFileRotate) { - const ui64 maxSize = 40; - TRotatingFileLogBackend log(PATH, maxSize, 2); - TStringBuilder data; - for (size_t i = 0; i < 10; ++i) - data << "data\n"; - log.WriteData(TLogRecord(ELogPriority::TLOG_INFO, data.data(), data.size())); - UNIT_ASSERT_C(TFileStat(PATH).Size > 0, "file " << PATH << " has zero size"); - data.clear(); - data << "more data"; - log.WriteData(TLogRecord(ELogPriority::TLOG_INFO, data.data(), data.size())); - UNIT_ASSERT_C(TFileStat(PATH).Size > 0, "file " << PATH << " has zero size"); - UNIT_ASSERT_C(TFileStat(TStringBuilder{} << PATH << ".1").Size > 0, "file " << PATH << ".1 has zero size"); - UNIT_ASSERT_C(TFileStat(PATH).Size < maxSize, "size of file " << PATH << " is greater than the size limit of " << maxSize << " bytes"); - } - - Y_UNIT_TEST(TestDoubleFileRotate) { - const ui64 maxSize = 40; - TRotatingFileLogBackend log(PATH, maxSize, 2); - TStringBuilder data; - for (size_t i = 0; i < 10; ++i) - data << "data\n"; +Y_UNIT_TEST_SUITE(NewRotatingFileSuite) { + const TString PATH = GetWorkPath() + "/my.log"; + + Y_UNIT_TEST(TestFileWrite) { + TRotatingFileLogBackend log(PATH, 4000, 2); + TString data = "my data"; + log.WriteData(TLogRecord(ELogPriority::TLOG_INFO, data.data(), data.size())); + UNIT_ASSERT_C(TFileStat(PATH).Size > 0, "file " << PATH << " has zero size"); + } + + Y_UNIT_TEST(TestFileRotate) { + const ui64 maxSize = 40; + TRotatingFileLogBackend log(PATH, maxSize, 2); + TStringBuilder data; + for (size_t i = 0; i < 10; ++i) + data << "data\n"; + log.WriteData(TLogRecord(ELogPriority::TLOG_INFO, data.data(), data.size())); + UNIT_ASSERT_C(TFileStat(PATH).Size > 0, "file " << PATH << " has zero size"); + data.clear(); + data << "more data"; + log.WriteData(TLogRecord(ELogPriority::TLOG_INFO, data.data(), data.size())); + UNIT_ASSERT_C(TFileStat(PATH).Size > 0, "file " << PATH << " has zero size"); + UNIT_ASSERT_C(TFileStat(TStringBuilder{} << PATH << ".1").Size > 0, "file " << PATH << ".1 has zero size"); + UNIT_ASSERT_C(TFileStat(PATH).Size < maxSize, "size of file " << PATH << " is greater than the size limit of " << maxSize << " bytes"); + } + + Y_UNIT_TEST(TestDoubleFileRotate) { + const ui64 maxSize = 40; + TRotatingFileLogBackend log(PATH, maxSize, 2); + TStringBuilder data; + for (size_t i = 0; i < 10; ++i) + data << "data\n"; log.WriteData(TLogRecord(ELogPriority::TLOG_INFO, data.data(), data.size())); UNIT_ASSERT_C(TFileStat(PATH).Size > 0, "file " << PATH << " has zero size"); log.WriteData(TLogRecord(ELogPriority::TLOG_INFO, data.data(), data.size())); @@ -53,5 +53,5 @@ Y_UNIT_TEST_SUITE(NewRotatingFileSuite) { UNIT_ASSERT_C(TFileStat(TStringBuilder{} << PATH << ".1").Size > 0, "file " << PATH << ".1 has zero size"); UNIT_ASSERT_C(TFileStat(TStringBuilder{} << PATH << ".2").Size > 0, "file " << PATH << ".2 has zero size"); UNIT_ASSERT_C(TFileStat(PATH).Size < maxSize, "size of file " << PATH << " is greater than the size limit of " << maxSize << " bytes"); - } -} + } +} -- cgit v1.3