diff options
author | dima-zakharov <dima-zakharov@yandex-team.ru> | 2022-02-10 16:46:02 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:46:02 +0300 |
commit | 41e8c41790ddd8d92722a09f7d4e1934aefff0c5 (patch) | |
tree | 2f175d77fb38a4093ccc33353129ccff1e7db7ce /library/cpp | |
parent | ed935c8a4c7e6ca561c19047402b0f7442175b51 (diff) | |
download | ydb-41e8c41790ddd8d92722a09f7d4e1934aefff0c5.tar.gz |
Restoring authorship annotation for <dima-zakharov@yandex-team.ru>. Commit 1 of 2.
Diffstat (limited to 'library/cpp')
-rw-r--r-- | library/cpp/logger/log_ut.cpp | 110 | ||||
-rw-r--r-- | library/cpp/logger/thread.cpp | 30 | ||||
-rw-r--r-- | library/cpp/logger/thread.h | 8 |
3 files changed, 74 insertions, 74 deletions
diff --git a/library/cpp/logger/log_ut.cpp b/library/cpp/logger/log_ut.cpp index 8de46f17f5..c5fd158c85 100644 --- a/library/cpp/logger/log_ut.cpp +++ b/library/cpp/logger/log_ut.cpp @@ -3,8 +3,8 @@ #include <library/cpp/testing/unittest/registar.h> #include <util/system/fs.h> -#include <util/system/rwlock.h> -#include <util/system/yield.h> +#include <util/system/rwlock.h> +#include <util/system/yield.h> #include <util/memory/blob.h> #include <util/stream/file.h> #include <util/generic/string.h> @@ -16,7 +16,7 @@ class TLogTest: public TTestBase { UNIT_TEST(TestFormat) UNIT_TEST(TestWrite) UNIT_TEST(TestThreaded) - UNIT_TEST(TestThreadedWithOverflow) + UNIT_TEST(TestThreadedWithOverflow) UNIT_TEST(TestNoFlush) UNIT_TEST_SUITE_END(); @@ -25,7 +25,7 @@ private: void TestFormat(); void TestWrite(); void TestThreaded(); - void TestThreadedWithOverflow(); + void TestThreadedWithOverflow(); void TestNoFlush(); void SetUp() override; void TearDown() override; @@ -78,59 +78,59 @@ void TLogTest::TestThreaded() { UNIT_ASSERT_EQUAL(TString((const char*)data.Begin(), data.Size()), "some useful data 12, 34, 3.000000, qwqwqw\n"); } -void TLogTest::TestThreadedWithOverflow() { - class TFakeLogBackend: public TLogBackend { - public: - TWriteGuard Guard() { - return TWriteGuard(Lock_); - } - - void WriteData(const TLogRecord&) override { - TReadGuard guard(Lock_); - } - - void ReopenLog() override { - TWriteGuard guard(Lock_); - } - - private: - TRWMutex Lock_; - }; - - auto waitForFreeQueue = [](const TLog& log) { - ThreadYield(); - while (log.BackEndQueueSize() > 0) { - Sleep(TDuration::MilliSeconds(1)); - } - }; - - TFakeLogBackend fb; - { +void TLogTest::TestThreadedWithOverflow() { + class TFakeLogBackend: public TLogBackend { + public: + TWriteGuard Guard() { + return TWriteGuard(Lock_); + } + + void WriteData(const TLogRecord&) override { + TReadGuard guard(Lock_); + } + + void ReopenLog() override { + TWriteGuard guard(Lock_); + } + + private: + TRWMutex Lock_; + }; + + auto waitForFreeQueue = [](const TLog& log) { + ThreadYield(); + while (log.BackEndQueueSize() > 0) { + Sleep(TDuration::MilliSeconds(1)); + } + }; + + TFakeLogBackend fb; + { TLog log(THolder(new TThreadedLogBackend(&fb, 2))); - - auto guard = fb.Guard(); - log.AddLog("first write"); - waitForFreeQueue(log); - log.AddLog("second write (first in queue)"); - log.AddLog("third write (second in queue)"); - UNIT_ASSERT_EXCEPTION(log.AddLog("fourth write (queue overflow)"), yexception); - } - - { - ui32 overflows = 0; + + auto guard = fb.Guard(); + log.AddLog("first write"); + waitForFreeQueue(log); + log.AddLog("second write (first in queue)"); + log.AddLog("third write (second in queue)"); + UNIT_ASSERT_EXCEPTION(log.AddLog("fourth write (queue overflow)"), yexception); + } + + { + ui32 overflows = 0; TLog log(THolder(new TThreadedLogBackend(&fb, 2, [&overflows] { ++overflows; }))); - - auto guard = fb.Guard(); - log.AddLog("first write"); - waitForFreeQueue(log); - log.AddLog("second write (first in queue)"); - log.AddLog("third write (second in queue)"); - UNIT_ASSERT_EQUAL(overflows, 0); - log.AddLog("fourth write (queue overflow)"); - UNIT_ASSERT_EQUAL(overflows, 1); - } -} - + + auto guard = fb.Guard(); + log.AddLog("first write"); + waitForFreeQueue(log); + log.AddLog("second write (first in queue)"); + log.AddLog("third write (second in queue)"); + UNIT_ASSERT_EQUAL(overflows, 0); + log.AddLog("fourth write (queue overflow)"); + UNIT_ASSERT_EQUAL(overflows, 1); + } +} + void TLogTest::TestNoFlush() { { TFileLogBackend fb(LOGFILE); diff --git a/library/cpp/logger/thread.cpp b/library/cpp/logger/thread.cpp index 0ccf9e374b..d12c5ee2fd 100644 --- a/library/cpp/logger/thread.cpp +++ b/library/cpp/logger/thread.cpp @@ -58,9 +58,9 @@ class TThreadedLogBackend::TImpl { }; public: - inline TImpl(TLogBackend* slave, size_t queuelen, std::function<void()> queueOverflowCallback = {}) + inline TImpl(TLogBackend* slave, size_t queuelen, std::function<void()> queueOverflowCallback = {}) : Slave_(slave) - , QueueOverflowCallback_(std::move(queueOverflowCallback)) + , QueueOverflowCallback_(std::move(queueOverflowCallback)) { Queue_.Start(1, queuelen); } @@ -72,14 +72,14 @@ public: inline void WriteData(const TLogRecord& rec) { THolder<TRec> obj(new (rec.Len) TRec(this, rec)); - if (Queue_.Add(obj.Get())) { - Y_UNUSED(obj.Release()); - return; - } - - if (QueueOverflowCallback_) { - QueueOverflowCallback_(); - } else { + if (Queue_.Add(obj.Get())) { + Y_UNUSED(obj.Release()); + return; + } + + if (QueueOverflowCallback_) { + QueueOverflowCallback_(); + } else { ythrow yexception() << "log queue exhausted"; } } @@ -113,7 +113,7 @@ public: private: TLogBackend* Slave_; TThreadPool Queue_{"ThreadedLogBack"}; - const std::function<void()> QueueOverflowCallback_; + const std::function<void()> QueueOverflowCallback_; }; TThreadedLogBackend::TThreadedLogBackend(TLogBackend* slave) @@ -121,8 +121,8 @@ TThreadedLogBackend::TThreadedLogBackend(TLogBackend* slave) { } -TThreadedLogBackend::TThreadedLogBackend(TLogBackend* slave, size_t queuelen, std::function<void()> queueOverflowCallback) - : Impl_(new TImpl(slave, queuelen, std::move(queueOverflowCallback))) +TThreadedLogBackend::TThreadedLogBackend(TLogBackend* slave, size_t queuelen, std::function<void()> queueOverflowCallback) + : Impl_(new TImpl(slave, queuelen, std::move(queueOverflowCallback))) { } @@ -155,9 +155,9 @@ TOwningThreadedLogBackend::TOwningThreadedLogBackend(TLogBackend* slave) { } -TOwningThreadedLogBackend::TOwningThreadedLogBackend(TLogBackend* slave, size_t queuelen, std::function<void()> queueOverflowCallback) +TOwningThreadedLogBackend::TOwningThreadedLogBackend(TLogBackend* slave, size_t queuelen, std::function<void()> queueOverflowCallback) : THolder<TLogBackend>(slave) - , TThreadedLogBackend(Get(), queuelen, std::move(queueOverflowCallback)) + , TThreadedLogBackend(Get(), queuelen, std::move(queueOverflowCallback)) { } diff --git a/library/cpp/logger/thread.h b/library/cpp/logger/thread.h index 65f7a88e87..ebb31e6218 100644 --- a/library/cpp/logger/thread.h +++ b/library/cpp/logger/thread.h @@ -4,12 +4,12 @@ #include <util/generic/ptr.h> -#include <functional> - +#include <functional> + class TThreadedLogBackend: public TLogBackend { public: TThreadedLogBackend(TLogBackend* slave); - TThreadedLogBackend(TLogBackend* slave, size_t queuelen, std::function<void()> queueOverflowCallback = {}); + TThreadedLogBackend(TLogBackend* slave, size_t queuelen, std::function<void()> queueOverflowCallback = {}); ~TThreadedLogBackend() override; void WriteData(const TLogRecord& rec) override; @@ -29,6 +29,6 @@ private: class TOwningThreadedLogBackend: private THolder<TLogBackend>, public TThreadedLogBackend { public: TOwningThreadedLogBackend(TLogBackend* slave); - TOwningThreadedLogBackend(TLogBackend* slave, size_t queuelen, std::function<void()> queueOverflowCallback = {}); + TOwningThreadedLogBackend(TLogBackend* slave, size_t queuelen, std::function<void()> queueOverflowCallback = {}); ~TOwningThreadedLogBackend() override; }; |