diff options
author | iddqd <iddqd@yandex-team.ru> | 2022-02-10 16:49:45 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:49:45 +0300 |
commit | 07fce9c5f7771600d0b3d70e1f88fd8a7e164d85 (patch) | |
tree | e4aa4750fbb864d70f8c06cf03d2750e979ea3bf | |
parent | af42068bf6cd93c976b80dd0388fa48cdf65da11 (diff) | |
download | ydb-07fce9c5f7771600d0b3d70e1f88fd8a7e164d85.tar.gz |
Restoring authorship annotation for <iddqd@yandex-team.ru>. Commit 1 of 2.
101 files changed, 1824 insertions, 1824 deletions
diff --git a/build/ymake.core.conf b/build/ymake.core.conf index 081833998b..976277cb2d 100644 --- a/build/ymake.core.conf +++ b/build/ymake.core.conf @@ -1478,9 +1478,9 @@ when ($COMMON_LINK_SETTINGS == "yes") { "BM" ? { PEERDIR+=library/cpp/balloc_market } - "C" ? { + "C" ? { PEERDIR+=library/cpp/malloc/calloc - } + } "LOCKLESS" ? { PEERDIR+=library/cpp/malloc/lockless } diff --git a/library/cpp/actors/core/executor_thread.cpp b/library/cpp/actors/core/executor_thread.cpp index 446b651efd..3f2742262c 100644 --- a/library/cpp/actors/core/executor_thread.cpp +++ b/library/cpp/actors/core/executor_thread.cpp @@ -10,9 +10,9 @@ #include <library/cpp/actors/util/datetime.h> #include <library/cpp/actors/util/thread.h> -#ifdef BALLOC +#ifdef BALLOC #include <library/cpp/balloc/optional/operators.h> -#endif +#endif #ifdef _linux_ #include <sys/syscall.h> @@ -293,9 +293,9 @@ namespace NActors { AtomicSet(ThreadId, (ui64)tid); #endif -#ifdef BALLOC - ThreadDisableBalloc(); -#endif +#ifdef BALLOC + ThreadDisableBalloc(); +#endif if (ThreadName) { ::SetCurrentThreadName(ThreadName); diff --git a/library/cpp/actors/core/scheduler_basic.cpp b/library/cpp/actors/core/scheduler_basic.cpp index fba200e16b..c9b9b836d8 100644 --- a/library/cpp/actors/core/scheduler_basic.cpp +++ b/library/cpp/actors/core/scheduler_basic.cpp @@ -4,9 +4,9 @@ #include <library/cpp/actors/util/datetime.h> #include <library/cpp/actors/util/thread.h> -#ifdef BALLOC +#ifdef BALLOC #include <library/cpp/balloc/optional/operators.h> -#endif +#endif namespace NActors { @@ -50,9 +50,9 @@ namespace NActors { } void TBasicSchedulerThread::CycleFunc() { -#ifdef BALLOC - ThreadDisableBalloc(); -#endif +#ifdef BALLOC + ThreadDisableBalloc(); +#endif ::SetCurrentThreadName("Scheduler"); ui64 currentMonotonic = RelaxedLoad(CurrentMonotonic); diff --git a/library/cpp/actors/core/ya.make b/library/cpp/actors/core/ya.make index 880a9d00db..afaf58f12f 100644 --- a/library/cpp/actors/core/ya.make +++ b/library/cpp/actors/core/ya.make @@ -16,8 +16,8 @@ IF (ALLOCATOR == "B" OR ALLOCATOR == "BS" OR ALLOCATOR == "C") PEERDIR( library/cpp/balloc/optional ) -ENDIF() - +ENDIF() + SRCS( actor_bootstrapped.h actor_coroutine.cpp diff --git a/library/cpp/balloc/balloc.cpp b/library/cpp/balloc/balloc.cpp index fab489db4c..5996f84529 100644 --- a/library/cpp/balloc/balloc.cpp +++ b/library/cpp/balloc/balloc.cpp @@ -24,7 +24,7 @@ namespace NBalloc { // ltls.Mode == Disabled const size_t extsize = size + sizeof(TAllocHeader); TAllocHeader* allocHeader = (TAllocHeader*)LibcMalloc(extsize); - allocHeader->Encode(allocHeader, size, DISABLED_SIGNATURE); + allocHeader->Encode(allocHeader, size, DISABLED_SIGNATURE); return allocHeader + 1; } } diff --git a/library/cpp/bucket_quoter/bucket_quoter.h b/library/cpp/bucket_quoter/bucket_quoter.h index 3d92ef8450..5f22b6d833 100644 --- a/library/cpp/bucket_quoter/bucket_quoter.h +++ b/library/cpp/bucket_quoter/bucket_quoter.h @@ -155,7 +155,7 @@ public: return Max<i64>(0, Bucket); } - void Use(ui64 tokens, bool sleep = false) { + void Use(ui64 tokens, bool sleep = false) { TGuard<Lock> g(BucketMutex); UseNoLock(tokens, sleep); } diff --git a/library/cpp/cache/thread_safe_cache.h b/library/cpp/cache/thread_safe_cache.h index 71e1442717..99659a5194 100644 --- a/library/cpp/cache/thread_safe_cache.h +++ b/library/cpp/cache/thread_safe_cache.h @@ -1,11 +1,11 @@ -#pragma once - -#include "cache.h" - -#include <util/generic/singleton.h> -#include <util/system/rwlock.h> - -namespace NPrivate { +#pragma once + +#include "cache.h" + +#include <util/generic/singleton.h> +#include <util/system/rwlock.h> + +namespace NPrivate { // We are interested in getters promotion policy _here_ because of Read-Write-Lock optimizations. enum class EGettersPromotionPolicy { Promoted, // LRU, TLRU, MRU, etc. @@ -13,29 +13,29 @@ namespace NPrivate { }; template <class Key, class Value, template <class, class> class List, EGettersPromotionPolicy GettersPromotionPolicy, class... TArgs> - class TThreadSafeCache { - public: + class TThreadSafeCache { + public: using TPtr = TAtomicSharedPtr<Value>; - class ICallbacks { - public: - using TKey = Key; - using TValue = Value; + class ICallbacks { + public: + using TKey = Key; + using TValue = Value; using TOwner = TThreadSafeCache<Key, Value, List, GettersPromotionPolicy, TArgs...>; - - public: + + public: virtual ~ICallbacks() = default; - virtual TKey GetKey(TArgs... args) const = 0; - virtual TValue* CreateObject(TArgs... args) const = 0; - }; - - public: + virtual TKey GetKey(TArgs... args) const = 0; + virtual TValue* CreateObject(TArgs... args) const = 0; + }; + + public: TThreadSafeCache(const ICallbacks& callbacks, size_t maxSize = Max<size_t>()) - : Callbacks(callbacks) + : Callbacks(callbacks) , Cache(maxSize) { } - + bool Insert(const Key& key, const TPtr& value) { if (!Contains(key)) { TWriteGuard w(Mutex); @@ -51,17 +51,17 @@ namespace NPrivate { const TPtr Get(TArgs... args) const { return GetValue<true>(args...); - } - + } + const TPtr GetUnsafe(TArgs... args) const { return GetValue<false>(args...); } - void Clear() { - TWriteGuard w(Mutex); - Cache.Clear(); - } - + void Clear() { + TWriteGuard w(Mutex); + Cache.Clear(); + } + void Erase(TArgs... args) { Key key = Callbacks.GetKey(args...); if (!Contains(key)) { @@ -83,19 +83,19 @@ namespace NPrivate { template <class TCallbacks> static const TPtr Get(TArgs... args) { - return TThreadSafeCacheSingleton<TCallbacks>::Get(args...); - } - + return TThreadSafeCacheSingleton<TCallbacks>::Get(args...); + } + template <class TCallbacks> static const TPtr Erase(TArgs... args) { return TThreadSafeCacheSingleton<TCallbacks>::Erase(args...); } template <class TCallbacks> - static void Clear() { - return TThreadSafeCacheSingleton<TCallbacks>::Clear(); - } - + static void Clear() { + return TThreadSafeCacheSingleton<TCallbacks>::Clear(); + } + size_t GetMaxSize() const { TReadGuard w(Mutex); return Cache.GetMaxSize(); @@ -106,7 +106,7 @@ namespace NPrivate { Cache.SetMaxSize(newSize); } - private: + private: template <bool AllowNullValues> const TPtr GetValue(TArgs... args) const { Key key = Callbacks.GetKey(args...); @@ -136,53 +136,53 @@ namespace NPrivate { private: using TInternalCache = TCache<Key, TPtr, List<Key, TPtr>, TNoopDelete>; - - template <class TCallbacks> - class TThreadSafeCacheSingleton { - public: + + template <class TCallbacks> + class TThreadSafeCacheSingleton { + public: static const TPtr Get(TArgs... args) { - return Singleton<TThreadSafeCacheSingleton>()->Cache.Get(args...); - } - + return Singleton<TThreadSafeCacheSingleton>()->Cache.Get(args...); + } + static const TPtr Erase(TArgs... args) { return Singleton<TThreadSafeCacheSingleton>()->Cache.Erase(args...); } - static void Clear() { - return Singleton<TThreadSafeCacheSingleton>()->Cache.Clear(); - } - - TThreadSafeCacheSingleton() - : Cache(Callbacks) + static void Clear() { + return Singleton<TThreadSafeCacheSingleton>()->Cache.Clear(); + } + + TThreadSafeCacheSingleton() + : Cache(Callbacks) { } - - private: - TCallbacks Callbacks; - typename TCallbacks::TOwner Cache; - }; - - private: - TRWMutex Mutex; - const ICallbacks& Callbacks; + + private: + TCallbacks Callbacks; + typename TCallbacks::TOwner Cache; + }; + + private: + TRWMutex Mutex; + const ICallbacks& Callbacks; mutable TInternalCache Cache; - }; - - struct TLWHelper { - template <class TValue> - struct TConstWeighter { - static int Weight(const TValue& /*value*/) { - return 0; - } - }; - - template <class TKey, class TValue> + }; + + struct TLWHelper { + template <class TValue> + struct TConstWeighter { + static int Weight(const TValue& /*value*/) { + return 0; + } + }; + + template <class TKey, class TValue> using TListType = TLWList<TKey, TValue, int, TConstWeighter<TValue>>; - - template <class TKey, class TValue, class... TArgs> + + template <class TKey, class TValue, class... TArgs> using TCache = TThreadSafeCache<TKey, TValue, TListType, EGettersPromotionPolicy::Unpromoted, TArgs...>; - }; - + }; + struct TLRUHelper { template <class TKey, class TValue> using TListType = TLRUList<TKey, TValue>; @@ -191,10 +191,10 @@ namespace NPrivate { using TCache = TThreadSafeCache<TKey, TValue, TListType, EGettersPromotionPolicy::Promoted, TArgs...>; }; -} - -template <class TKey, class TValue, class... TArgs> -using TThreadSafeCache = typename NPrivate::TLWHelper::template TCache<TKey, TValue, TArgs...>; +} + +template <class TKey, class TValue, class... TArgs> +using TThreadSafeCache = typename NPrivate::TLWHelper::template TCache<TKey, TValue, TArgs...>; template <class TKey, class TValue, class... TArgs> using TThreadSafeLRUCache = typename NPrivate::TLRUHelper::template TCache<TKey, TValue, TArgs...>; diff --git a/library/cpp/cache/ut/cache_ut.cpp b/library/cpp/cache/ut/cache_ut.cpp index 329872cfde..c3b984995e 100644 --- a/library/cpp/cache/ut/cache_ut.cpp +++ b/library/cpp/cache/ut/cache_ut.cpp @@ -356,30 +356,30 @@ Y_UNIT_TEST_SUITE(TCacheTest) { UNIT_ASSERT(s.Find(1) != s.End()); // Key 2 should have been deleted } } - + Y_UNIT_TEST_SUITE(TThreadSafeCacheTest) { typedef TThreadSafeCache<ui32, TString, ui32> TCache; - - const char* VALS[] = {"abcd", "defg", "hjkl"}; - + + const char* VALS[] = {"abcd", "defg", "hjkl"}; + class TCallbacks: public TCache::ICallbacks { - public: + public: TKey GetKey(ui32 i) const override { - return i; - } + return i; + } TValue* CreateObject(ui32 i) const override { Creations++; return new TString(VALS[i]); - } + } mutable i32 Creations = 0; - }; - + }; + Y_UNIT_TEST(SimpleTest) { for (ui32 i = 0; i < Y_ARRAY_SIZE(VALS); ++i) { const TString data = *TCache::Get<TCallbacks>(i); UNIT_ASSERT(data == VALS[i]); - } + } } Y_UNIT_TEST(InsertUpdateTest) { @@ -404,7 +404,7 @@ Y_UNIT_TEST_SUITE(TThreadSafeCacheTest) { UNIT_ASSERT(callbacks.Creations == 0); UNIT_ASSERT(*item == "hjk"); } -} +} Y_UNIT_TEST_SUITE(TThreadSafeCacheUnsafeTest) { typedef TThreadSafeCache<ui32, TString, ui32> TCache; diff --git a/library/cpp/digest/md5/md5.h b/library/cpp/digest/md5/md5.h index 2c17aa0518..49367658a3 100644 --- a/library/cpp/digest/md5/md5.h +++ b/library/cpp/digest/md5/md5.h @@ -13,7 +13,7 @@ public: void Init(); - inline MD5& Update(const void* data, size_t len) { + inline MD5& Update(const void* data, size_t len) { return Update(MakeArrayRef(static_cast<const ui8*>(data), len)); } @@ -23,7 +23,7 @@ public: inline MD5& Update(const TArrayRef<const ui8> data) { UpdatePart(data); - return *this; + return *this; } void Pad(); diff --git a/library/cpp/histogram/ya.make b/library/cpp/histogram/ya.make index 77cfd96902..d06b9f6838 100644 --- a/library/cpp/histogram/ya.make +++ b/library/cpp/histogram/ya.make @@ -4,6 +4,6 @@ RECURSE( hdr/ut simple simple/ut - rt - rt/ut + rt + rt/ut ) diff --git a/library/cpp/http/io/stream_ut.cpp b/library/cpp/http/io/stream_ut.cpp index 1ea35df675..286f73259d 100644 --- a/library/cpp/http/io/stream_ut.cpp +++ b/library/cpp/http/io/stream_ut.cpp @@ -266,27 +266,27 @@ Y_UNIT_TEST_SUITE(THttpStreamTest) { TString res = "qqqqqq\r\n\r\nsdasdsad\r\n"; TPortManager pm; const ui16 port = pm.GetPort(); - - TTestHttpServer serverImpl(res); - THttpServer server(&serverImpl, THttpServer::TOptions(port).EnableKeepAlive(true).EnableCompression(true)); - - UNIT_ASSERT(server.Start()); - - TNetworkAddress addr("localhost", port); - - TSocket s(addr); - + + TTestHttpServer serverImpl(res); + THttpServer server(&serverImpl, THttpServer::TOptions(port).EnableKeepAlive(true).EnableCompression(true)); + + UNIT_ASSERT(server.Start()); + + TNetworkAddress addr("localhost", port); + + TSocket s(addr); + SendMinimalHttpRequest(s, "www.yandex.ru", "/"); - - TSocketInput si(s); - THttpInput input(&si); - unsigned httpCode = ParseHttpRetCode(input.FirstLine()); - UNIT_ASSERT_VALUES_EQUAL(httpCode, 200u); + + TSocketInput si(s); + THttpInput input(&si); + unsigned httpCode = ParseHttpRetCode(input.FirstLine()); + UNIT_ASSERT_VALUES_EQUAL(httpCode, 200u); TString reply = input.ReadAll(); - UNIT_ASSERT_VALUES_EQUAL(reply, res); - server.Stop(); - } - + UNIT_ASSERT_VALUES_EQUAL(reply, res); + server.Stop(); + } + Y_UNIT_TEST(TestOutputFlush) { TString str; TStringOutput strOut(str); diff --git a/library/cpp/http/server/http.cpp b/library/cpp/http/server/http.cpp index 128583bdd7..bd6b13ca85 100644 --- a/library/cpp/http/server/http.cpp +++ b/library/cpp/http/server/http.cpp @@ -429,7 +429,7 @@ public: inline void DecreaseConnections() noexcept { AtomicDecrement(ConnectionCount); } - + inline void IncreaseConnections() noexcept { AtomicIncrement(ConnectionCount); } @@ -495,10 +495,10 @@ THttpServer::THttpServer(ICallBack* cb, TMtpQueueRef mainWorkers, TMtpQueueRef f THttpServer::~THttpServer() { } -i64 THttpServer::GetClientCount() const { - return Impl_->GetClientCount(); -} - +i64 THttpServer::GetClientCount() const { + return Impl_->GetClientCount(); +} + bool THttpServer::Start() { return Impl_->Start(); } diff --git a/library/cpp/lfalloc/lf_allocX64.cpp b/library/cpp/lfalloc/lf_allocX64.cpp index 2eb90761fe..e3dcac8e96 100644 --- a/library/cpp/lfalloc/lf_allocX64.cpp +++ b/library/cpp/lfalloc/lf_allocX64.cpp @@ -1,4 +1,4 @@ -#include "lf_allocX64.h" +#include "lf_allocX64.h" ////////////////////////////////////////////////////////////////////////// // hooks @@ -8,14 +8,14 @@ #define OP_THROWNOTHING #endif -#ifndef _darwin_ +#ifndef _darwin_ #if !defined(YMAKE) void* operator new(size_t size) { - return LFAlloc(size); + return LFAlloc(size); } void* operator new(size_t size, const std::nothrow_t&) OP_THROWNOTHING { - return LFAlloc(size); + return LFAlloc(size); } void operator delete(void* p)OP_THROWNOTHING { @@ -27,11 +27,11 @@ void operator delete(void* p, const std::nothrow_t&)OP_THROWNOTHING { } void* operator new[](size_t size) { - return LFAlloc(size); + return LFAlloc(size); } void* operator new[](size_t size, const std::nothrow_t&) OP_THROWNOTHING { - return LFAlloc(size); + return LFAlloc(size); } void operator delete[](void* p) OP_THROWNOTHING { @@ -46,20 +46,20 @@ void operator delete[](void* p, const std::nothrow_t&) OP_THROWNOTHING { //#ifndef _MSC_VER extern "C" void* malloc(size_t size) { - return LFAlloc(size); + return LFAlloc(size); } extern "C" void* valloc(size_t size) { - return LFVAlloc(size); + return LFVAlloc(size); } extern "C" int posix_memalign(void** memptr, size_t alignment, size_t size) { - return LFPosixMemalign(memptr, alignment, size); + return LFPosixMemalign(memptr, alignment, size); } extern "C" void* memalign(size_t alignment, size_t size) { void* ptr; - int res = LFPosixMemalign(&ptr, alignment, size); + int res = LFPosixMemalign(&ptr, alignment, size); return res ? nullptr : ptr; } @@ -84,7 +84,7 @@ extern "C" void* calloc(size_t n, size_t elem_size) { if (elem_size != 0 && size / elem_size != n) return nullptr; - void* result = LFAlloc(size); + void* result = LFAlloc(size); if (result != nullptr) { memset(result, 0, size); } @@ -97,7 +97,7 @@ extern "C" void cfree(void* ptr) { extern "C" void* realloc(void* old_ptr, size_t new_size) { if (old_ptr == nullptr) { - void* result = LFAlloc(new_size); + void* result = LFAlloc(new_size); return result; } if (new_size == 0) { @@ -105,7 +105,7 @@ extern "C" void* realloc(void* old_ptr, size_t new_size) { return nullptr; } - void* new_ptr = LFAlloc(new_size); + void* new_ptr = LFAlloc(new_size); if (new_ptr == nullptr) { return nullptr; } @@ -122,20 +122,20 @@ extern "C" size_t malloc_usable_size(void* ptr) { return LFGetSize(ptr); } -NMalloc::TMallocInfo NMalloc::MallocInfo() { - NMalloc::TMallocInfo r; -#if defined(LFALLOC_DBG) - r.Name = "lfalloc_dbg"; -#elif defined(LFALLOC_YT) - r.Name = "lfalloc_yt"; -#else - r.Name = "lfalloc"; -#endif - r.SetParam = &LFAlloc_SetParam; - r.GetParam = &LFAlloc_GetParam; - return r; -} +NMalloc::TMallocInfo NMalloc::MallocInfo() { + NMalloc::TMallocInfo r; +#if defined(LFALLOC_DBG) + r.Name = "lfalloc_dbg"; +#elif defined(LFALLOC_YT) + r.Name = "lfalloc_yt"; #else + r.Name = "lfalloc"; +#endif + r.SetParam = &LFAlloc_SetParam; + r.GetParam = &LFAlloc_GetParam; + return r; +} +#else NMalloc::TMallocInfo NMalloc::MallocInfo() { NMalloc::TMallocInfo r; r.Name = "system-darwin"; diff --git a/library/cpp/lfalloc/lf_allocX64.h b/library/cpp/lfalloc/lf_allocX64.h index fd2a906d6f..6b13d74102 100644 --- a/library/cpp/lfalloc/lf_allocX64.h +++ b/library/cpp/lfalloc/lf_allocX64.h @@ -1,5 +1,5 @@ -#pragma once - +#pragma once + #include <stdlib.h> #include <stdio.h> #include <stdarg.h> @@ -1504,7 +1504,7 @@ extern "C" void GetPerTagAllocInfo( #endif // LFALLOC_DBG ////////////////////////////////////////////////////////////////////////// -static Y_FORCE_INLINE void* LFAllocImpl(size_t _nSize) { +static Y_FORCE_INLINE void* LFAllocImpl(size_t _nSize) { #if defined(LFALLOC_DBG) size_t size = _nSize; _nSize += sizeof(TAllocHeader); @@ -1571,13 +1571,13 @@ static Y_FORCE_INLINE void* LFAllocImpl(size_t _nSize) { } } -static Y_FORCE_INLINE void* LFAlloc(size_t _nSize) { - void* res = LFAllocImpl(_nSize); +static Y_FORCE_INLINE void* LFAlloc(size_t _nSize) { + void* res = LFAllocImpl(_nSize); #ifdef DBG_FILL_MEMORY if (FillMemoryOnAllocation && res && (_nSize <= DBG_FILL_MAX_SIZE)) { memset(res, 0xcf, _nSize); } -#endif +#endif return res; } @@ -1868,7 +1868,7 @@ static const char* LFAlloc_GetParam(const char* param) { return nullptr; } -static Y_FORCE_INLINE int LFPosixMemalign(void** memptr, size_t alignment, size_t size) { +static Y_FORCE_INLINE int LFPosixMemalign(void** memptr, size_t alignment, size_t size) { if (Y_UNLIKELY(alignment > 4096)) { const char* error = "Larger alignment are not guaranteed with this implementation\n"; #ifdef _win_ @@ -1888,7 +1888,7 @@ static Y_FORCE_INLINE int LFPosixMemalign(void** memptr, size_t alignment, size_ } #endif - *memptr = LFAlloc(bigsize); + *memptr = LFAlloc(bigsize); #if defined(LFALLOC_DBG) if (alignment > sizeof(TAllocHeader)) { diff --git a/library/cpp/logger/backend_creator.cpp b/library/cpp/logger/backend_creator.cpp index ea430edb83..fdcdd02f7e 100644 --- a/library/cpp/logger/backend_creator.cpp +++ b/library/cpp/logger/backend_creator.cpp @@ -1,45 +1,45 @@ -#include "backend_creator.h" -#include "stream.h" -#include "uninitialized_creator.h" -#include <util/system/yassert.h> -#include <util/stream/debug.h> -#include <util/stream/output.h> - - -THolder<TLogBackend> ILogBackendCreator::CreateLogBackend() const { - try { - return DoCreateLogBackend(); - } catch(...) { - Cdbg << "Warning: " << CurrentExceptionMessage() << ". Use stderr instead." << Endl; - } - return MakeHolder<TStreamLogBackend>(&Cerr); -} - -bool ILogBackendCreator::Init(const IInitContext& /*ctx*/) { - return true; -} - - -NJson::TJsonValue ILogBackendCreator::AsJson() const { - NJson::TJsonValue json; - ToJson(json); - return json; -} - -THolder<ILogBackendCreator> ILogBackendCreator::Create(const IInitContext& ctx) { - auto res = MakeHolder<TLogBackendCreatorUninitialized>(); - if(!res->Init(ctx)) { - Cdbg << "Cannot init log backend creator"; - return nullptr; - } - return res; -} - -TLogBackendCreatorBase::TLogBackendCreatorBase(const TString& type) - : Type(type) -{} - -void TLogBackendCreatorBase::ToJson(NJson::TJsonValue& value) const { - value["LoggerType"] = Type; - DoToJson(value); -} +#include "backend_creator.h" +#include "stream.h" +#include "uninitialized_creator.h" +#include <util/system/yassert.h> +#include <util/stream/debug.h> +#include <util/stream/output.h> + + +THolder<TLogBackend> ILogBackendCreator::CreateLogBackend() const { + try { + return DoCreateLogBackend(); + } catch(...) { + Cdbg << "Warning: " << CurrentExceptionMessage() << ". Use stderr instead." << Endl; + } + return MakeHolder<TStreamLogBackend>(&Cerr); +} + +bool ILogBackendCreator::Init(const IInitContext& /*ctx*/) { + return true; +} + + +NJson::TJsonValue ILogBackendCreator::AsJson() const { + NJson::TJsonValue json; + ToJson(json); + return json; +} + +THolder<ILogBackendCreator> ILogBackendCreator::Create(const IInitContext& ctx) { + auto res = MakeHolder<TLogBackendCreatorUninitialized>(); + if(!res->Init(ctx)) { + Cdbg << "Cannot init log backend creator"; + return nullptr; + } + return res; +} + +TLogBackendCreatorBase::TLogBackendCreatorBase(const TString& type) + : Type(type) +{} + +void TLogBackendCreatorBase::ToJson(NJson::TJsonValue& value) const { + value["LoggerType"] = Type; + DoToJson(value); +} diff --git a/library/cpp/logger/backend_creator.h b/library/cpp/logger/backend_creator.h index 4f0ca24a4e..0394b9ccdb 100644 --- a/library/cpp/logger/backend_creator.h +++ b/library/cpp/logger/backend_creator.h @@ -1,59 +1,59 @@ -#pragma once - -#include "backend.h" -#include <library/cpp/object_factory/object_factory.h> -#include <library/cpp/json/json_value.h> -#include <util/generic/vector.h> -#include <util/generic/ptr.h> -#include <util/string/cast.h> - -class ILogBackendCreator { -public: - using TFactory = NObjectFactory::TObjectFactory<ILogBackendCreator, TString>; - - class IInitContext { - public: - template<class T> - bool GetValue(TStringBuf name, T& var) const { - TString tmp; - if (!GetValue(name, tmp)) { - return false; - } - var = FromString<T>(tmp); - return true; - } - - template<class T> - T GetOrElse(TStringBuf name, const T& def) const { - T res; - return GetValue(name, res) ? res : def; - } - - virtual ~IInitContext() = default; - virtual bool GetValue(TStringBuf name, TString& var) const = 0; - virtual TVector<THolder<IInitContext>> GetChildren(TStringBuf name) const = 0; - }; - -public: - virtual ~ILogBackendCreator() = default; - THolder<TLogBackend> CreateLogBackend() const; - virtual bool Init(const IInitContext& ctx); - - NJson::TJsonValue AsJson() const; - virtual void ToJson(NJson::TJsonValue& value) const = 0; - - static THolder<ILogBackendCreator> Create(const IInitContext& ctx); - -private: - virtual THolder<TLogBackend> DoCreateLogBackend() const = 0; -}; - -class TLogBackendCreatorBase: public ILogBackendCreator { -public: - TLogBackendCreatorBase(const TString& type); - virtual void ToJson(NJson::TJsonValue& value) const override final; - -protected: - virtual void DoToJson(NJson::TJsonValue& value) const = 0; - TString Type; -}; +#pragma once + +#include "backend.h" +#include <library/cpp/object_factory/object_factory.h> +#include <library/cpp/json/json_value.h> +#include <util/generic/vector.h> +#include <util/generic/ptr.h> +#include <util/string/cast.h> + +class ILogBackendCreator { +public: + using TFactory = NObjectFactory::TObjectFactory<ILogBackendCreator, TString>; + + class IInitContext { + public: + template<class T> + bool GetValue(TStringBuf name, T& var) const { + TString tmp; + if (!GetValue(name, tmp)) { + return false; + } + var = FromString<T>(tmp); + return true; + } + + template<class T> + T GetOrElse(TStringBuf name, const T& def) const { + T res; + return GetValue(name, res) ? res : def; + } + + virtual ~IInitContext() = default; + virtual bool GetValue(TStringBuf name, TString& var) const = 0; + virtual TVector<THolder<IInitContext>> GetChildren(TStringBuf name) const = 0; + }; + +public: + virtual ~ILogBackendCreator() = default; + THolder<TLogBackend> CreateLogBackend() const; + virtual bool Init(const IInitContext& ctx); + + NJson::TJsonValue AsJson() const; + virtual void ToJson(NJson::TJsonValue& value) const = 0; + + static THolder<ILogBackendCreator> Create(const IInitContext& ctx); + +private: + virtual THolder<TLogBackend> DoCreateLogBackend() const = 0; +}; + +class TLogBackendCreatorBase: public ILogBackendCreator { +public: + TLogBackendCreatorBase(const TString& type); + virtual void ToJson(NJson::TJsonValue& value) const override final; + +protected: + virtual void DoToJson(NJson::TJsonValue& value) const = 0; + TString Type; +}; diff --git a/library/cpp/logger/composite.cpp b/library/cpp/logger/composite.cpp index 3ce0154ced..669dc44525 100644 --- a/library/cpp/logger/composite.cpp +++ b/library/cpp/logger/composite.cpp @@ -1,18 +1,18 @@ -#include "composite.h" -#include "uninitialized_creator.h" - -void TCompositeLogBackend::WriteData(const TLogRecord& rec) { - for (auto& slave: Slaves) { - slave->WriteData(rec); - } -} - -void TCompositeLogBackend::ReopenLog() { - for (auto& slave : Slaves) { - slave->ReopenLog(); - } -} - -void TCompositeLogBackend::AddLogBackend(THolder<TLogBackend>&& backend) { - Slaves.emplace_back(std::move(backend)); -} +#include "composite.h" +#include "uninitialized_creator.h" + +void TCompositeLogBackend::WriteData(const TLogRecord& rec) { + for (auto& slave: Slaves) { + slave->WriteData(rec); + } +} + +void TCompositeLogBackend::ReopenLog() { + for (auto& slave : Slaves) { + slave->ReopenLog(); + } +} + +void TCompositeLogBackend::AddLogBackend(THolder<TLogBackend>&& backend) { + Slaves.emplace_back(std::move(backend)); +} diff --git a/library/cpp/logger/composite.h b/library/cpp/logger/composite.h index 142c61b8d5..7dd9bec5fa 100644 --- a/library/cpp/logger/composite.h +++ b/library/cpp/logger/composite.h @@ -1,14 +1,14 @@ -#pragma once - -#include "backend.h" -#include <util/generic/vector.h> - -class TCompositeLogBackend: public TLogBackend { -public: - virtual void WriteData(const TLogRecord& rec) override; - virtual void ReopenLog() override; - virtual void AddLogBackend(THolder<TLogBackend>&& backend); - -private: - TVector<THolder<TLogBackend>> Slaves; -}; +#pragma once + +#include "backend.h" +#include <util/generic/vector.h> + +class TCompositeLogBackend: public TLogBackend { +public: + virtual void WriteData(const TLogRecord& rec) override; + virtual void ReopenLog() override; + virtual void AddLogBackend(THolder<TLogBackend>&& backend); + +private: + TVector<THolder<TLogBackend>> Slaves; +}; diff --git a/library/cpp/logger/composite_creator.cpp b/library/cpp/logger/composite_creator.cpp index 2064265766..f85e9ebb69 100644 --- a/library/cpp/logger/composite_creator.cpp +++ b/library/cpp/logger/composite_creator.cpp @@ -1,34 +1,34 @@ -#include "composite_creator.h" -#include "composite.h" -#include "uninitialized_creator.h" - -THolder<TLogBackend> TCompositeBackendCreator::DoCreateLogBackend() const { - auto res = MakeHolder<TCompositeLogBackend>(); - for (const auto& child : Children) { - res->AddLogBackend(child->CreateLogBackend()); - } - return std::move(res); -} - - -TCompositeBackendCreator::TCompositeBackendCreator() - : TLogBackendCreatorBase("composite") -{} - -bool TCompositeBackendCreator::Init(const IInitContext& ctx) { - for (const auto& child : ctx.GetChildren("SubLogger")) { - Children.emplace_back(MakeHolder<TLogBackendCreatorUninitialized>()); - if (!Children.back()->Init(*child)) { - return false; - } - } - return true; -} - -ILogBackendCreator::TFactory::TRegistrator<TCompositeBackendCreator> TCompositeBackendCreator::Registrar("composite"); - -void TCompositeBackendCreator::DoToJson(NJson::TJsonValue& value) const { - for (const auto& child: Children) { - child->ToJson(value["SubLogger"].AppendValue(NJson::JSON_MAP)); - } -} +#include "composite_creator.h" +#include "composite.h" +#include "uninitialized_creator.h" + +THolder<TLogBackend> TCompositeBackendCreator::DoCreateLogBackend() const { + auto res = MakeHolder<TCompositeLogBackend>(); + for (const auto& child : Children) { + res->AddLogBackend(child->CreateLogBackend()); + } + return std::move(res); +} + + +TCompositeBackendCreator::TCompositeBackendCreator() + : TLogBackendCreatorBase("composite") +{} + +bool TCompositeBackendCreator::Init(const IInitContext& ctx) { + for (const auto& child : ctx.GetChildren("SubLogger")) { + Children.emplace_back(MakeHolder<TLogBackendCreatorUninitialized>()); + if (!Children.back()->Init(*child)) { + return false; + } + } + return true; +} + +ILogBackendCreator::TFactory::TRegistrator<TCompositeBackendCreator> TCompositeBackendCreator::Registrar("composite"); + +void TCompositeBackendCreator::DoToJson(NJson::TJsonValue& value) const { + for (const auto& child: Children) { + child->ToJson(value["SubLogger"].AppendValue(NJson::JSON_MAP)); + } +} diff --git a/library/cpp/logger/composite_creator.h b/library/cpp/logger/composite_creator.h index 877d3a2b33..7ec0cef67b 100644 --- a/library/cpp/logger/composite_creator.h +++ b/library/cpp/logger/composite_creator.h @@ -1,18 +1,18 @@ -#pragma once - -#include "backend_creator.h" -#include <util/generic/vector.h> - -class TCompositeBackendCreator : public TLogBackendCreatorBase { -public: - TCompositeBackendCreator(); - virtual bool Init(const IInitContext& ctx) override; - static TFactory::TRegistrator<TCompositeBackendCreator> Registrar; - -protected: - virtual void DoToJson(NJson::TJsonValue& value) const override; - -private: - virtual THolder<TLogBackend> DoCreateLogBackend() const override; - TVector<THolder<ILogBackendCreator>> Children; -}; +#pragma once + +#include "backend_creator.h" +#include <util/generic/vector.h> + +class TCompositeBackendCreator : public TLogBackendCreatorBase { +public: + TCompositeBackendCreator(); + virtual bool Init(const IInitContext& ctx) override; + static TFactory::TRegistrator<TCompositeBackendCreator> Registrar; + +protected: + virtual void DoToJson(NJson::TJsonValue& value) const override; + +private: + virtual THolder<TLogBackend> DoCreateLogBackend() const override; + TVector<THolder<ILogBackendCreator>> Children; +}; diff --git a/library/cpp/logger/composite_ut.cpp b/library/cpp/logger/composite_ut.cpp index f330419271..a75122eb57 100644 --- a/library/cpp/logger/composite_ut.cpp +++ b/library/cpp/logger/composite_ut.cpp @@ -1,98 +1,98 @@ -#include "log.h" -#include <library/cpp/logger/init_context/config.h> -#include <library/cpp/logger/init_context/yconf.h> -#include <library/cpp/testing/unittest/registar.h> -#include <library/cpp/yconf/patcher/unstrict_config.h> -#include <util/stream/file.h> -#include <util/system/fs.h> - -Y_UNIT_TEST_SUITE(TCompositeLogTest) -{ - TVector<TString> ReadLines(const TString & filename) { - TVector<TString> lines; - TIFStream fin(filename); - TString line; - while (fin.ReadLine(line)) { - lines.push_back(std::move(line)); - } - return lines; - } - - void Clear(const TString & filename) { - NFs::Remove(filename + "1"); - NFs::Remove(filename + "2"); - } - - void DoTestComposite(const ILogBackendCreator::IInitContext& ctx, const TString & filename) { - Clear(filename); - { - TLog log; - { - auto creator = ILogBackendCreator::Create(ctx); - log.ResetBackend(creator->CreateLogBackend()); - log.ReopenLog(); - } - log.Write(TLOG_ERR, "first\n"); - log.Write(TLOG_DEBUG, "second\n"); - } - auto data1 = ReadLines(filename + "1"); - auto data2 = ReadLines(filename + "2"); - UNIT_ASSERT_VALUES_EQUAL(data1.size(), 2); - UNIT_ASSERT(data1[0] == "first"); - UNIT_ASSERT(data1[1] == "second"); - - UNIT_ASSERT_VALUES_EQUAL(data2.size(), 1); - UNIT_ASSERT(data2[0] == "first"); - Clear(filename); - } - - Y_UNIT_TEST(TestCompositeConfig) { - TString s(R"( -{ - "LoggerType": "composite", - "SubLogger":[ - { - "LoggerType": "file", - "Path": "config_log_1" - }, { - "LoggerType": "config_log_2", - "LogLevel": "INFO" - } - ] -})"); - TStringInput si(s); - NConfig::TConfig cfg = NConfig::TConfig::FromJson(si); - //Прогоняем конфигурацию через серализацию и десериализацию - TLogBackendCreatorInitContextConfig ctx(cfg); - TString newCfg = ILogBackendCreator::Create(ctx)->AsJson().GetStringRobust(); - TStringInput si2(newCfg); - DoTestComposite(TLogBackendCreatorInitContextConfig(NConfig::TConfig::FromJson(si2)), "config_log_"); - - } - Y_UNIT_TEST(TestCompositeYConf) { - constexpr const char* CONFIG = R"( -<Logger> - LoggerType: composite - <SubLogger> - LoggerType: file - Path: yconf_log_1 - </SubLogger> - <SubLogger> - LoggerType: yconf_log_2 - LogLevel: INFO - </SubLogger> -</Logger> -)"; - TUnstrictConfig cfg; - if (!cfg.ParseMemory(CONFIG)) { - TString errors; - cfg.PrintErrors(errors); - UNIT_ASSERT_C(false, errors); - } - TLogBackendCreatorInitContextYConf ctx(*cfg.GetFirstChild("Logger")); - //Прогоняем конфигурацию через серализацию и десериализацию - TUnstrictConfig newCfg; - UNIT_ASSERT(newCfg.ParseJson(ILogBackendCreator::Create(ctx)->AsJson())); - DoTestComposite(TLogBackendCreatorInitContextYConf(*newCfg.GetRootSection()), "yconf_log_"); - } -}; +#include "log.h" +#include <library/cpp/logger/init_context/config.h> +#include <library/cpp/logger/init_context/yconf.h> +#include <library/cpp/testing/unittest/registar.h> +#include <library/cpp/yconf/patcher/unstrict_config.h> +#include <util/stream/file.h> +#include <util/system/fs.h> + +Y_UNIT_TEST_SUITE(TCompositeLogTest) +{ + TVector<TString> ReadLines(const TString & filename) { + TVector<TString> lines; + TIFStream fin(filename); + TString line; + while (fin.ReadLine(line)) { + lines.push_back(std::move(line)); + } + return lines; + } + + void Clear(const TString & filename) { + NFs::Remove(filename + "1"); + NFs::Remove(filename + "2"); + } + + void DoTestComposite(const ILogBackendCreator::IInitContext& ctx, const TString & filename) { + Clear(filename); + { + TLog log; + { + auto creator = ILogBackendCreator::Create(ctx); + log.ResetBackend(creator->CreateLogBackend()); + log.ReopenLog(); + } + log.Write(TLOG_ERR, "first\n"); + log.Write(TLOG_DEBUG, "second\n"); + } + auto data1 = ReadLines(filename + "1"); + auto data2 = ReadLines(filename + "2"); + UNIT_ASSERT_VALUES_EQUAL(data1.size(), 2); + UNIT_ASSERT(data1[0] == "first"); + UNIT_ASSERT(data1[1] == "second"); + + UNIT_ASSERT_VALUES_EQUAL(data2.size(), 1); + UNIT_ASSERT(data2[0] == "first"); + Clear(filename); + } + + Y_UNIT_TEST(TestCompositeConfig) { + TString s(R"( +{ + "LoggerType": "composite", + "SubLogger":[ + { + "LoggerType": "file", + "Path": "config_log_1" + }, { + "LoggerType": "config_log_2", + "LogLevel": "INFO" + } + ] +})"); + TStringInput si(s); + NConfig::TConfig cfg = NConfig::TConfig::FromJson(si); + //Прогоняем конфигурацию через серализацию и десериализацию + TLogBackendCreatorInitContextConfig ctx(cfg); + TString newCfg = ILogBackendCreator::Create(ctx)->AsJson().GetStringRobust(); + TStringInput si2(newCfg); + DoTestComposite(TLogBackendCreatorInitContextConfig(NConfig::TConfig::FromJson(si2)), "config_log_"); + + } + Y_UNIT_TEST(TestCompositeYConf) { + constexpr const char* CONFIG = R"( +<Logger> + LoggerType: composite + <SubLogger> + LoggerType: file + Path: yconf_log_1 + </SubLogger> + <SubLogger> + LoggerType: yconf_log_2 + LogLevel: INFO + </SubLogger> +</Logger> +)"; + TUnstrictConfig cfg; + if (!cfg.ParseMemory(CONFIG)) { + TString errors; + cfg.PrintErrors(errors); + UNIT_ASSERT_C(false, errors); + } + TLogBackendCreatorInitContextYConf ctx(*cfg.GetFirstChild("Logger")); + //Прогоняем конфигурацию через серализацию и десериализацию + TUnstrictConfig newCfg; + UNIT_ASSERT(newCfg.ParseJson(ILogBackendCreator::Create(ctx)->AsJson())); + DoTestComposite(TLogBackendCreatorInitContextYConf(*newCfg.GetRootSection()), "yconf_log_"); + } +}; diff --git a/library/cpp/logger/file_creator.cpp b/library/cpp/logger/file_creator.cpp index 0a84469e43..2807171017 100644 --- a/library/cpp/logger/file_creator.cpp +++ b/library/cpp/logger/file_creator.cpp @@ -1,22 +1,22 @@ -#include "file_creator.h" -#include "file.h" - -TFileLogBackendCreator::TFileLogBackendCreator(const TString& path /*= TString()*/, const TString& type /*= "file"*/) - : TLogBackendCreatorBase(type) - , Path(path) -{} - -THolder<TLogBackend> TFileLogBackendCreator::DoCreateLogBackend() const { - return MakeHolder<TFileLogBackend>(Path); -} - -bool TFileLogBackendCreator::Init(const IInitContext& ctx) { - ctx.GetValue("Path", Path); - return !!Path; -} - -ILogBackendCreator::TFactory::TRegistrator<TFileLogBackendCreator> TFileLogBackendCreator::Registrar("file"); - -void TFileLogBackendCreator::DoToJson(NJson::TJsonValue& value) const { - value["Path"] = Path; -} +#include "file_creator.h" +#include "file.h" + +TFileLogBackendCreator::TFileLogBackendCreator(const TString& path /*= TString()*/, const TString& type /*= "file"*/) + : TLogBackendCreatorBase(type) + , Path(path) +{} + +THolder<TLogBackend> TFileLogBackendCreator::DoCreateLogBackend() const { + return MakeHolder<TFileLogBackend>(Path); +} + +bool TFileLogBackendCreator::Init(const IInitContext& ctx) { + ctx.GetValue("Path", Path); + return !!Path; +} + +ILogBackendCreator::TFactory::TRegistrator<TFileLogBackendCreator> TFileLogBackendCreator::Registrar("file"); + +void TFileLogBackendCreator::DoToJson(NJson::TJsonValue& value) const { + value["Path"] = Path; +} diff --git a/library/cpp/logger/file_creator.h b/library/cpp/logger/file_creator.h index 73e55261ad..95543d2550 100644 --- a/library/cpp/logger/file_creator.h +++ b/library/cpp/logger/file_creator.h @@ -1,17 +1,17 @@ -#pragma once - -#include "backend_creator.h" - -class TFileLogBackendCreator : public TLogBackendCreatorBase { -public: - TFileLogBackendCreator(const TString& path = TString(), const TString& type = "file"); - virtual bool Init(const IInitContext& ctx) override; - static TFactory::TRegistrator<TFileLogBackendCreator> Registrar; - -protected: - virtual void DoToJson(NJson::TJsonValue& value) const override; - TString Path; - -private: - virtual THolder<TLogBackend> DoCreateLogBackend() const override; -}; +#pragma once + +#include "backend_creator.h" + +class TFileLogBackendCreator : public TLogBackendCreatorBase { +public: + TFileLogBackendCreator(const TString& path = TString(), const TString& type = "file"); + virtual bool Init(const IInitContext& ctx) override; + static TFactory::TRegistrator<TFileLogBackendCreator> Registrar; + +protected: + virtual void DoToJson(NJson::TJsonValue& value) const override; + TString Path; + +private: + virtual THolder<TLogBackend> DoCreateLogBackend() const override; +}; diff --git a/library/cpp/logger/filter.h b/library/cpp/logger/filter.h index 9ef83fb58c..7278a40ff9 100644 --- a/library/cpp/logger/filter.h +++ b/library/cpp/logger/filter.h @@ -6,12 +6,12 @@ #include <util/generic/ptr.h> class TFilteredLogBackend: public TLogBackend { - THolder<TLogBackend> Backend; + THolder<TLogBackend> Backend; ELogPriority Level; public: - TFilteredLogBackend(THolder<TLogBackend>&& t, ELogPriority level = LOG_MAX_PRIORITY) noexcept - : Backend(std::move(t)) + TFilteredLogBackend(THolder<TLogBackend>&& t, ELogPriority level = LOG_MAX_PRIORITY) noexcept + : Backend(std::move(t)) , Level(level) { } diff --git a/library/cpp/logger/filter_creator.cpp b/library/cpp/logger/filter_creator.cpp index fd5618087b..f9783f115a 100644 --- a/library/cpp/logger/filter_creator.cpp +++ b/library/cpp/logger/filter_creator.cpp @@ -1,20 +1,20 @@ -#include "filter_creator.h" -#include "filter.h" - -TFilteredBackendCreator::TFilteredBackendCreator(THolder<ILogBackendCreator> slave, ELogPriority priority) - : Slave(std::move(slave)) - , Priority(priority) -{} - -THolder<TLogBackend> TFilteredBackendCreator::DoCreateLogBackend() const { +#include "filter_creator.h" +#include "filter.h" + +TFilteredBackendCreator::TFilteredBackendCreator(THolder<ILogBackendCreator> slave, ELogPriority priority) + : Slave(std::move(slave)) + , Priority(priority) +{} + +THolder<TLogBackend> TFilteredBackendCreator::DoCreateLogBackend() const { return MakeHolder<TFilteredLogBackend>(Slave->CreateLogBackend(), Priority); -} - -bool TFilteredBackendCreator::Init(const IInitContext& ctx) { - return Slave->Init(ctx); -} - -void TFilteredBackendCreator::ToJson(NJson::TJsonValue& value) const { - value["LogLevel"] = ToString(Priority); - Slave->ToJson(value); -} +} + +bool TFilteredBackendCreator::Init(const IInitContext& ctx) { + return Slave->Init(ctx); +} + +void TFilteredBackendCreator::ToJson(NJson::TJsonValue& value) const { + value["LogLevel"] = ToString(Priority); + Slave->ToJson(value); +} diff --git a/library/cpp/logger/filter_creator.h b/library/cpp/logger/filter_creator.h index 0f301457a0..d4a44d32ff 100644 --- a/library/cpp/logger/filter_creator.h +++ b/library/cpp/logger/filter_creator.h @@ -1,16 +1,16 @@ -#pragma once - -#include "backend_creator.h" -#include "priority.h" - -class TFilteredBackendCreator : public ILogBackendCreator { -public: - TFilteredBackendCreator(THolder<ILogBackendCreator> slave, ELogPriority priority); - virtual bool Init(const IInitContext& ctx) override; - virtual void ToJson(NJson::TJsonValue& value) const override; - -private: - virtual THolder<TLogBackend> DoCreateLogBackend() const override; - THolder<ILogBackendCreator> Slave; - ELogPriority Priority; -}; +#pragma once + +#include "backend_creator.h" +#include "priority.h" + +class TFilteredBackendCreator : public ILogBackendCreator { +public: + TFilteredBackendCreator(THolder<ILogBackendCreator> slave, ELogPriority priority); + virtual bool Init(const IInitContext& ctx) override; + virtual void ToJson(NJson::TJsonValue& value) const override; + +private: + virtual THolder<TLogBackend> DoCreateLogBackend() const override; + THolder<ILogBackendCreator> Slave; + ELogPriority Priority; +}; diff --git a/library/cpp/logger/global/common.cpp b/library/cpp/logger/global/common.cpp index 4fb05c19b4..9cfd1b0386 100644 --- a/library/cpp/logger/global/common.cpp +++ b/library/cpp/logger/global/common.cpp @@ -1,12 +1,12 @@ #include "common.h" - + #include <util/generic/yexception.h> -namespace NLoggingImpl { +namespace NLoggingImpl { TString GetLocalTimeSSimple() { - struct tm tm; - return Strftime("%b%d_%H%M%S", Now().LocalTime(&tm)); - } + struct tm tm; + return Strftime("%b%d_%H%M%S", Now().LocalTime(&tm)); + } TString PrepareToOpenLog(TString logType, const int logLevel, const bool rotation, const bool startAsDaemon) { Y_ENSURE(logLevel >= 0 && logLevel <= (int)LOG_MAX_PRIORITY, "Incorrect log level"); @@ -21,16 +21,16 @@ namespace NLoggingImpl { return logType; } -} - +} + bool TLogFilter::CheckLoggingContext(TLog& log, const TLogRecordContext& context) { return context.Priority <= log.FiltrationLevel(); } -TSimpleSharedPtr<TLogElement> TLogFilter::StartRecord(TLog& logger, const TLogRecordContext& context, TSimpleSharedPtr<TLogElement> earlier) { - if (earlier) - return earlier; - TSimpleSharedPtr<TLogElement> result(new TLogElement(&logger)); - *result << context.Priority; - return result; -} +TSimpleSharedPtr<TLogElement> TLogFilter::StartRecord(TLog& logger, const TLogRecordContext& context, TSimpleSharedPtr<TLogElement> earlier) { + if (earlier) + return earlier; + TSimpleSharedPtr<TLogElement> result(new TLogElement(&logger)); + *result << context.Priority; + return result; +} diff --git a/library/cpp/logger/global/common.h b/library/cpp/logger/global/common.h index 7dcf650dec..a7a422c786 100644 --- a/library/cpp/logger/global/common.h +++ b/library/cpp/logger/global/common.h @@ -1,26 +1,26 @@ -#pragma once - -#include <util/datetime/base.h> - -#include <util/folder/path.h> -#include <util/generic/singleton.h> +#pragma once + +#include <util/datetime/base.h> + +#include <util/folder/path.h> +#include <util/generic/singleton.h> #include <util/generic/string.h> -#include <util/generic/ptr.h> -#include <util/generic/yexception.h> -#include <util/string/printf.h> -#include <util/system/src_location.h> - +#include <util/generic/ptr.h> +#include <util/generic/yexception.h> +#include <util/string/printf.h> +#include <util/system/src_location.h> + #include <library/cpp/logger/log.h> - + namespace NLoggingImpl { const size_t SingletonPriority = 500; } -template <class T> -T* CreateDefaultLogger() { - return nullptr; -} - +template <class T> +T* CreateDefaultLogger() { + return nullptr; +} + namespace NLoggingImpl { template<class T, class TTraits> class TOperatorBase { @@ -37,7 +37,7 @@ namespace NLoggingImpl { inline static bool Usage() { return SingletonWithPriority<TPtr, SingletonPriority>()->Instance.Get(); } - + inline static T* Get() { return SingletonWithPriority<TPtr, SingletonPriority>()->Instance.Get(); } @@ -45,8 +45,8 @@ namespace NLoggingImpl { inline static void Set(T* v) { SingletonWithPriority<TPtr, SingletonPriority>()->Instance.Reset(v); } - }; - + }; + template<class T> struct TLoggerTraits { static T* CreateDefault() { @@ -54,20 +54,20 @@ namespace NLoggingImpl { } }; } - + template <class T> class TLoggerOperator : public NLoggingImpl::TOperatorBase<T, NLoggingImpl::TLoggerTraits<T>> { public: - inline static TLog& Log() { + inline static TLog& Log() { Y_ASSERT(TLoggerOperator::Usage()); return *TLoggerOperator::Get(); - } + } }; -namespace NLoggingImpl { - +namespace NLoggingImpl { + TString GetLocalTimeSSimple(); - + // Returns correct log type to use TString PrepareToOpenLog(TString logType, int logLevel, bool rotation, bool startAsDaemon); @@ -75,64 +75,64 @@ namespace NLoggingImpl { void InitLogImpl(TString logType, const int logLevel, const bool rotation, const bool startAsDaemon) { TLoggerOperator<TLoggerType>::Set(new TLoggerType(PrepareToOpenLog(logType, logLevel, rotation, startAsDaemon), (ELogPriority)logLevel)); } -} - -struct TLogRecordContext { +} + +struct TLogRecordContext { constexpr TLogRecordContext(const TSourceLocation& sourceLocation, TStringBuf customMessage, ELogPriority priority) : SourceLocation(sourceLocation) , CustomMessage(customMessage) , Priority(priority) {} - - TSourceLocation SourceLocation; - TStringBuf CustomMessage; + + TSourceLocation SourceLocation; + TStringBuf CustomMessage; ELogPriority Priority; -}; - -template <class... R> -struct TLogRecordPreprocessor; - -template <> -struct TLogRecordPreprocessor<> { +}; + +template <class... R> +struct TLogRecordPreprocessor; + +template <> +struct TLogRecordPreprocessor<> { inline static bool CheckLoggingContext(TLog& /*log*/, const TLogRecordContext& /*context*/) { return true; } - inline static TSimpleSharedPtr<TLogElement> StartRecord(TLog& log, const TLogRecordContext& context, TSimpleSharedPtr<TLogElement> earlier) { + inline static TSimpleSharedPtr<TLogElement> StartRecord(TLog& log, const TLogRecordContext& context, TSimpleSharedPtr<TLogElement> earlier) { if (earlier) - return earlier; - TSimpleSharedPtr<TLogElement> result(new TLogElement(&log)); - *result << context.Priority; - return result; - } -}; - -template <class H, class... R> -struct TLogRecordPreprocessor<H, R...> { + return earlier; + TSimpleSharedPtr<TLogElement> result(new TLogElement(&log)); + *result << context.Priority; + return result; + } +}; + +template <class H, class... R> +struct TLogRecordPreprocessor<H, R...> { inline static bool CheckLoggingContext(TLog& log, const TLogRecordContext& context) { return H::CheckLoggingContext(log, context) && TLogRecordPreprocessor<R...>::CheckLoggingContext(log, context); } - inline static TSimpleSharedPtr<TLogElement> StartRecord(TLog& log, const TLogRecordContext& context, TSimpleSharedPtr<TLogElement> earlier) { - TSimpleSharedPtr<TLogElement> first = H::StartRecord(log, context, earlier); - return TLogRecordPreprocessor<R...>::StartRecord(log, context, first); - } -}; - -struct TLogFilter { + inline static TSimpleSharedPtr<TLogElement> StartRecord(TLog& log, const TLogRecordContext& context, TSimpleSharedPtr<TLogElement> earlier) { + TSimpleSharedPtr<TLogElement> first = H::StartRecord(log, context, earlier); + return TLogRecordPreprocessor<R...>::StartRecord(log, context, first); + } +}; + +struct TLogFilter { static bool CheckLoggingContext(TLog& log, const TLogRecordContext& context); static TSimpleSharedPtr<TLogElement> StartRecord(TLog& log, const TLogRecordContext& context, TSimpleSharedPtr<TLogElement> earlier); -}; - -class TNullLog; - -template <class TPreprocessor> -TSimpleSharedPtr<TLogElement> GetLoggerForce(TLog& log, const TLogRecordContext& context) { - if (TSimpleSharedPtr<TLogElement> result = TPreprocessor::StartRecord(log, context, nullptr)) - return result; - return new TLogElement(&TLoggerOperator<TNullLog>::Log()); -} - +}; + +class TNullLog; + +template <class TPreprocessor> +TSimpleSharedPtr<TLogElement> GetLoggerForce(TLog& log, const TLogRecordContext& context) { + if (TSimpleSharedPtr<TLogElement> result = TPreprocessor::StartRecord(log, context, nullptr)) + return result; + return new TLogElement(&TLoggerOperator<TNullLog>::Log()); +} + namespace NPrivateGlobalLogger { struct TEatStream { Y_FORCE_INLINE bool operator|(const IOutputStream&) const { @@ -141,9 +141,9 @@ namespace NPrivateGlobalLogger { }; } -#define LOGGER_GENERIC_LOG_CHECKED(logger, preprocessor, level, message) (*GetLoggerForce<preprocessor>(logger, TLogRecordContext(__LOCATION__, message, level))) +#define LOGGER_GENERIC_LOG_CHECKED(logger, preprocessor, level, message) (*GetLoggerForce<preprocessor>(logger, TLogRecordContext(__LOCATION__, message, level))) #define LOGGER_CHECKED_GENERIC_LOG(logger, preprocessor, level, message) \ (preprocessor::CheckLoggingContext(logger, TLogRecordContext(__LOCATION__, message, level))) && NPrivateGlobalLogger::TEatStream() | (*(preprocessor::StartRecord(logger, TLogRecordContext(__LOCATION__, message, level), nullptr))) - -#define SINGLETON_GENERIC_LOG_CHECKED(type, preprocessor, level, message) LOGGER_GENERIC_LOG_CHECKED(TLoggerOperator<type>::Log(), preprocessor, level, message) -#define SINGLETON_CHECKED_GENERIC_LOG(type, preprocessor, level, message) LOGGER_CHECKED_GENERIC_LOG(TLoggerOperator<type>::Log(), preprocessor, level, message) + +#define SINGLETON_GENERIC_LOG_CHECKED(type, preprocessor, level, message) LOGGER_GENERIC_LOG_CHECKED(TLoggerOperator<type>::Log(), preprocessor, level, message) +#define SINGLETON_CHECKED_GENERIC_LOG(type, preprocessor, level, message) LOGGER_CHECKED_GENERIC_LOG(TLoggerOperator<type>::Log(), preprocessor, level, message) diff --git a/library/cpp/logger/global/global.cpp b/library/cpp/logger/global/global.cpp index 9fbd10f666..72991b610b 100644 --- a/library/cpp/logger/global/global.cpp +++ b/library/cpp/logger/global/global.cpp @@ -25,16 +25,16 @@ void DoInitGlobalLog(THolder<TLogBackend> backend, THolder<ILoggerFormatter> for bool GlobalLogInitialized() { return TLoggerOperator<TGlobalLog>::Usage(); } - + template <> -TGlobalLog* CreateDefaultLogger<TGlobalLog>() { - return new TGlobalLog("console", TLOG_INFO); -} - +TGlobalLog* CreateDefaultLogger<TGlobalLog>() { + return new TGlobalLog("console", TLOG_INFO); +} + template <> -TNullLog* CreateDefaultLogger<TNullLog>() { - return new TNullLog("null"); -} +TNullLog* CreateDefaultLogger<TNullLog>() { + return new TNullLog("null"); +} NPrivateGlobalLogger::TVerifyEvent::~TVerifyEvent() { const TString info = Str(); diff --git a/library/cpp/logger/global/global.h b/library/cpp/logger/global/global.h index cbe71b16ea..762e31297e 100644 --- a/library/cpp/logger/global/global.h +++ b/library/cpp/logger/global/global.h @@ -1,7 +1,7 @@ #pragma once #include "common.h" -#include "rty_formater.h" +#include "rty_formater.h" // ATTENTION! MUST CALL DoInitGlobalLog BEFORE USAGE @@ -21,8 +21,8 @@ class TGlobalLog: public TLog { public: TGlobalLog(const TString& logType, ELogPriority priority = LOG_MAX_PRIORITY) : TLog(logType, priority) - { - } + { + } TGlobalLog(THolder<TLogBackend> backend) : TLog(std::move(backend)) @@ -31,14 +31,14 @@ public: }; template <> -TGlobalLog* CreateDefaultLogger<TGlobalLog>(); - +TGlobalLog* CreateDefaultLogger<TGlobalLog>(); + class TNullLog: public TLog { public: TNullLog(const TString& logType, ELogPriority priority = LOG_MAX_PRIORITY) : TLog(logType, priority) - { - } + { + } TNullLog(THolder<TLogBackend> backend) : TLog(std::move(backend)) @@ -47,43 +47,43 @@ public: }; template <> -TNullLog* CreateDefaultLogger<TNullLog>(); - -template <> -class TSingletonTraits<TLoggerOperator<TGlobalLog>::TPtr> { -public: - static const size_t Priority = NLoggingImpl::SingletonPriority; -}; - -template <> -class TSingletonTraits<TLoggerOperator<TNullLog>::TPtr> { -public: - static const size_t Priority = NLoggingImpl::SingletonPriority; -}; - -#define FATAL_LOG SINGLETON_CHECKED_GENERIC_LOG(TGlobalLog, TRTYLogPreprocessor, TLOG_CRIT, "CRITICAL_INFO") +TNullLog* CreateDefaultLogger<TNullLog>(); + +template <> +class TSingletonTraits<TLoggerOperator<TGlobalLog>::TPtr> { +public: + static const size_t Priority = NLoggingImpl::SingletonPriority; +}; + +template <> +class TSingletonTraits<TLoggerOperator<TNullLog>::TPtr> { +public: + static const size_t Priority = NLoggingImpl::SingletonPriority; +}; + +#define FATAL_LOG SINGLETON_CHECKED_GENERIC_LOG(TGlobalLog, TRTYLogPreprocessor, TLOG_CRIT, "CRITICAL_INFO") #define ALERT_LOG SINGLETON_CHECKED_GENERIC_LOG(TGlobalLog, TRTYLogPreprocessor, TLOG_ALERT, "ALERT") -#define ERROR_LOG SINGLETON_CHECKED_GENERIC_LOG(TGlobalLog, TRTYLogPreprocessor, TLOG_ERR, "ERROR") -#define WARNING_LOG SINGLETON_CHECKED_GENERIC_LOG(TGlobalLog, TRTYLogPreprocessor, TLOG_WARNING, "WARNING") -#define NOTICE_LOG SINGLETON_CHECKED_GENERIC_LOG(TGlobalLog, TRTYLogPreprocessor, TLOG_NOTICE, "NOTICE") -#define INFO_LOG SINGLETON_CHECKED_GENERIC_LOG(TGlobalLog, TRTYLogPreprocessor, TLOG_INFO, "INFO") -#define DEBUG_LOG SINGLETON_CHECKED_GENERIC_LOG(TGlobalLog, TRTYLogPreprocessor, TLOG_DEBUG, "DEBUG") +#define ERROR_LOG SINGLETON_CHECKED_GENERIC_LOG(TGlobalLog, TRTYLogPreprocessor, TLOG_ERR, "ERROR") +#define WARNING_LOG SINGLETON_CHECKED_GENERIC_LOG(TGlobalLog, TRTYLogPreprocessor, TLOG_WARNING, "WARNING") +#define NOTICE_LOG SINGLETON_CHECKED_GENERIC_LOG(TGlobalLog, TRTYLogPreprocessor, TLOG_NOTICE, "NOTICE") +#define INFO_LOG SINGLETON_CHECKED_GENERIC_LOG(TGlobalLog, TRTYLogPreprocessor, TLOG_INFO, "INFO") +#define DEBUG_LOG SINGLETON_CHECKED_GENERIC_LOG(TGlobalLog, TRTYLogPreprocessor, TLOG_DEBUG, "DEBUG") #define RESOURCES_LOG SINGLETON_CHECKED_GENERIC_LOG(TGlobalLog, TRTYLogPreprocessor, TLOG_RESOURCES, "RESOURCES") #define TEMPLATE_LOG(logLevel) SINGLETON_CHECKED_GENERIC_LOG(TGlobalLog, TRTYLogPreprocessor, logLevel, ToString(logLevel).data()) #define IS_LOG_ACTIVE(logLevel) (TLoggerOperator<TGlobalLog>::Log().FiltrationLevel() >= logLevel) -#define RTY_MEM_LOG(Action) \ - { NOTICE_LOG << "RESOURCES On " << Action << ": " << NLoggingImpl::GetSystemResources() << Endl; }; +#define RTY_MEM_LOG(Action) \ + { NOTICE_LOG << "RESOURCES On " << Action << ": " << NLoggingImpl::GetSystemResources() << Endl; }; -#define VERIFY_WITH_LOG(expr, msg, ...) \ - do { \ +#define VERIFY_WITH_LOG(expr, msg, ...) \ + do { \ if (Y_UNLIKELY(!(expr))) { \ - FATAL_LOG << Sprintf(msg, ##__VA_ARGS__) << Endl; \ + FATAL_LOG << Sprintf(msg, ##__VA_ARGS__) << Endl; \ Y_VERIFY(false, msg, ##__VA_ARGS__); \ - }; \ - } while (0); + }; \ + } while (0); namespace NPrivateGlobalLogger { class TVerifyEvent: public TStringStream { @@ -121,5 +121,5 @@ namespace NPrivateGlobalLogger { #define CHECK_EQ_WITH_LOG(a, b) CHECK_WITH_LOG((a) == (b)) << a << " != " << b; #define CHECK_LEQ_WITH_LOG(a, b) CHECK_WITH_LOG((a) <= (b)) << a << " > " << b; -#define FAIL_LOG(msg, ...) VERIFY_WITH_LOG(false, msg, ##__VA_ARGS__) +#define FAIL_LOG(msg, ...) VERIFY_WITH_LOG(false, msg, ##__VA_ARGS__) #define S_FAIL_LOG CHECK_WITH_LOG(false) diff --git a/library/cpp/logger/global/rty_formater.cpp b/library/cpp/logger/global/rty_formater.cpp index 305f8470c5..5f171cafde 100644 --- a/library/cpp/logger/global/rty_formater.cpp +++ b/library/cpp/logger/global/rty_formater.cpp @@ -1,20 +1,20 @@ -#include "rty_formater.h" -#include <util/datetime/base.h> +#include "rty_formater.h" +#include <util/datetime/base.h> #include <util/datetime/systime.h> -#include <util/stream/str.h> -#include <util/stream/printf.h> -#include <util/system/mem_info.h> +#include <util/stream/str.h> +#include <util/stream/printf.h> +#include <util/system/mem_info.h> #include <util/system/yassert.h> #include <inttypes.h> #include <cstdio> - + namespace { constexpr size_t LocalTimeSBufferSize = sizeof("2017-07-24 12:20:34.313 +0300"); size_t PrintLocalTimeS(const TInstant instant, char* const begin, const char* const end) { Y_VERIFY(static_cast<size_t>(end - begin) >= LocalTimeSBufferSize); - struct tm tm; + struct tm tm; instant.LocalTime(&tm); // both stftime and sprintf exclude the terminating null byte from the return value @@ -24,9 +24,9 @@ namespace { pos += strftime(pos, end - pos, " %z", &tm); Y_VERIFY(LocalTimeSBufferSize - 1 == pos - begin); // together with Y_VERIFY above this also implies pos<=end return (pos - begin); - } + } } - + namespace NLoggingImpl { IOutputStream& operator<<(IOutputStream& out, TLocalTimeS localTimeS) { char buffer[LocalTimeSBufferSize]; @@ -48,20 +48,20 @@ namespace NLoggingImpl { return res; } - TStringBuf StripFileName(TStringBuf string) { - return string.RNextTok(LOCSLASH_C); - } - + TStringBuf StripFileName(TStringBuf string) { + return string.RNextTok(LOCSLASH_C); + } + TString GetSystemResources() { - NMemInfo::TMemInfo mi = NMemInfo::GetMemInfo(); + NMemInfo::TMemInfo mi = NMemInfo::GetMemInfo(); return PrintSystemResources(mi); } TString PrintSystemResources(const NMemInfo::TMemInfo& mi) { - return Sprintf(" rss=%0.3fMb, vms=%0.3fMb", mi.RSS * 1.0 / (1024 * 1024), mi.VMS * 1.0 / (1024 * 1024)); - } -} - + return Sprintf(" rss=%0.3fMb, vms=%0.3fMb", mi.RSS * 1.0 / (1024 * 1024), mi.VMS * 1.0 / (1024 * 1024)); + } +} + namespace { class TRtyLoggerFormatter : public ILoggerFormatter { public: @@ -84,11 +84,11 @@ bool TRTYMessageFormater::CheckLoggingContext(TLog& /*logger*/, const TLogRecord return true; } -TSimpleSharedPtr<TLogElement> TRTYMessageFormater::StartRecord(TLog& logger, const TLogRecordContext& context, TSimpleSharedPtr<TLogElement> earlier) { +TSimpleSharedPtr<TLogElement> TRTYMessageFormater::StartRecord(TLog& logger, const TLogRecordContext& context, TSimpleSharedPtr<TLogElement> earlier) { if (!earlier) { - earlier.Reset(new TLogElement(&logger)); + earlier.Reset(new TLogElement(&logger)); } TLoggerFormatterOperator::Get()->Format(context, *earlier); - return earlier; -} + return earlier; +} diff --git a/library/cpp/logger/global/rty_formater.h b/library/cpp/logger/global/rty_formater.h index 6532e1d769..57bd86ac3f 100644 --- a/library/cpp/logger/global/rty_formater.h +++ b/library/cpp/logger/global/rty_formater.h @@ -1,7 +1,7 @@ -#pragma once - -#include "common.h" +#pragma once +#include "common.h" + namespace NMemInfo { struct TMemInfo; } @@ -15,7 +15,7 @@ public: ILoggerFormatter* CreateRtyLoggerFormatter(); -namespace NLoggingImpl { +namespace NLoggingImpl { class TLocalTimeS { public: TLocalTimeS(TInstant instant = TInstant::Now()) @@ -48,14 +48,14 @@ namespace NLoggingImpl { return CreateRtyLoggerFormatter(); } }; -} - +} + class TLoggerFormatterOperator : public NLoggingImpl::TOperatorBase<ILoggerFormatter, NLoggingImpl::TLoggerFormatterTraits> { }; -struct TRTYMessageFormater { +struct TRTYMessageFormater { static bool CheckLoggingContext(TLog& logger, const TLogRecordContext& context); - static TSimpleSharedPtr<TLogElement> StartRecord(TLog& logger, const TLogRecordContext& context, TSimpleSharedPtr<TLogElement> earlier); -}; - -using TRTYLogPreprocessor = TLogRecordPreprocessor<TLogFilter, TRTYMessageFormater>; + static TSimpleSharedPtr<TLogElement> StartRecord(TLog& logger, const TLogRecordContext& context, TSimpleSharedPtr<TLogElement> earlier); +}; + +using TRTYLogPreprocessor = TLogRecordPreprocessor<TLogFilter, TRTYMessageFormater>; diff --git a/library/cpp/logger/global/ya.make b/library/cpp/logger/global/ya.make index 20eb361e72..9563c8801c 100644 --- a/library/cpp/logger/global/ya.make +++ b/library/cpp/logger/global/ya.make @@ -1,19 +1,19 @@ -LIBRARY() - +LIBRARY() + OWNER(g:geosaas) - -PEERDIR( + +PEERDIR( library/cpp/logger -) - +) + IF (OS_WINDOWS) NO_WERROR() ENDIF() -SRCS( - common.cpp - global.cpp - rty_formater.cpp -) - -END() +SRCS( + common.cpp + global.cpp + rty_formater.cpp +) + +END() diff --git a/library/cpp/logger/init_context/README.md b/library/cpp/logger/init_context/README.md index 93564e4890..bc0103a2f6 100644 --- a/library/cpp/logger/init_context/README.md +++ b/library/cpp/logger/init_context/README.md @@ -1,5 +1,5 @@ -Эта библиотека содержит две раплизации InitContext для TLogBackendCreator. - -TLogBackendCreatorInitContextYConf работает с YandexConfig (library/cpp/yconf). - -TLogBackendCreatorInitContextConfig работает с NConfig::TConfig (library/cpp/config). +Эта библиотека содержит две раплизации InitContext для TLogBackendCreator. + +TLogBackendCreatorInitContextYConf работает с YandexConfig (library/cpp/yconf). + +TLogBackendCreatorInitContextConfig работает с NConfig::TConfig (library/cpp/config). diff --git a/library/cpp/logger/init_context/config.cpp b/library/cpp/logger/init_context/config.cpp index 30efa13333..2fe503fbc8 100644 --- a/library/cpp/logger/init_context/config.cpp +++ b/library/cpp/logger/init_context/config.cpp @@ -1,26 +1,26 @@ -#include "config.h" - -TLogBackendCreatorInitContextConfig::TLogBackendCreatorInitContextConfig(const NConfig::TConfig& config) - : Config(config) -{} - -bool TLogBackendCreatorInitContextConfig::GetValue(TStringBuf name, TString& var) const { - if (Config.Has(name)) { - var = Config[name].Get<TString>(); - return true; - } - return false; -} - -TVector<THolder<ILogBackendCreator::IInitContext>> TLogBackendCreatorInitContextConfig::GetChildren(TStringBuf name) const { - TVector<THolder<IInitContext>> result; - const NConfig::TConfig& child = Config[name]; - if (child.IsA<NConfig::TArray>()) { - for (const auto& i: child.Get<NConfig::TArray>()) { - result.emplace_back(MakeHolder<TLogBackendCreatorInitContextConfig>(i)); - } - } else if (!child.IsNull()) { - result.emplace_back(MakeHolder<TLogBackendCreatorInitContextConfig>(child)); - } - return result; -} +#include "config.h" + +TLogBackendCreatorInitContextConfig::TLogBackendCreatorInitContextConfig(const NConfig::TConfig& config) + : Config(config) +{} + +bool TLogBackendCreatorInitContextConfig::GetValue(TStringBuf name, TString& var) const { + if (Config.Has(name)) { + var = Config[name].Get<TString>(); + return true; + } + return false; +} + +TVector<THolder<ILogBackendCreator::IInitContext>> TLogBackendCreatorInitContextConfig::GetChildren(TStringBuf name) const { + TVector<THolder<IInitContext>> result; + const NConfig::TConfig& child = Config[name]; + if (child.IsA<NConfig::TArray>()) { + for (const auto& i: child.Get<NConfig::TArray>()) { + result.emplace_back(MakeHolder<TLogBackendCreatorInitContextConfig>(i)); + } + } else if (!child.IsNull()) { + result.emplace_back(MakeHolder<TLogBackendCreatorInitContextConfig>(child)); + } + return result; +} diff --git a/library/cpp/logger/init_context/config.h b/library/cpp/logger/init_context/config.h index 8227d13176..579deeb9e4 100644 --- a/library/cpp/logger/init_context/config.h +++ b/library/cpp/logger/init_context/config.h @@ -1,14 +1,14 @@ -#pragma once - -#include <library/cpp/logger/backend_creator.h> -#include <library/cpp/config/config.h> - -class TLogBackendCreatorInitContextConfig : public ILogBackendCreator::IInitContext { -public: - TLogBackendCreatorInitContextConfig(const NConfig::TConfig& config); - virtual bool GetValue(TStringBuf name, TString& var) const override; - virtual TVector<THolder<IInitContext>> GetChildren(TStringBuf name) const override; - -private: - const NConfig::TConfig& Config; -}; +#pragma once + +#include <library/cpp/logger/backend_creator.h> +#include <library/cpp/config/config.h> + +class TLogBackendCreatorInitContextConfig : public ILogBackendCreator::IInitContext { +public: + TLogBackendCreatorInitContextConfig(const NConfig::TConfig& config); + virtual bool GetValue(TStringBuf name, TString& var) const override; + virtual TVector<THolder<IInitContext>> GetChildren(TStringBuf name) const override; + +private: + const NConfig::TConfig& Config; +}; diff --git a/library/cpp/logger/init_context/ya.make b/library/cpp/logger/init_context/ya.make index 9572a34c60..450a3e4fe7 100644 --- a/library/cpp/logger/init_context/ya.make +++ b/library/cpp/logger/init_context/ya.make @@ -1,20 +1,20 @@ -OWNER( - pg - mvel - g:util - g:base -) - -LIBRARY() - -PEERDIR( - library/cpp/logger - library/cpp/config - library/cpp/yconf -) -SRCS( - config.cpp - yconf.cpp -) - -END() +OWNER( + pg + mvel + g:util + g:base +) + +LIBRARY() + +PEERDIR( + library/cpp/logger + library/cpp/config + library/cpp/yconf +) +SRCS( + config.cpp + yconf.cpp +) + +END() diff --git a/library/cpp/logger/init_context/yconf.cpp b/library/cpp/logger/init_context/yconf.cpp index c7da1d607c..5a7a7d2500 100644 --- a/library/cpp/logger/init_context/yconf.cpp +++ b/library/cpp/logger/init_context/yconf.cpp @@ -1,18 +1,18 @@ -#include "yconf.h" - -TLogBackendCreatorInitContextYConf::TLogBackendCreatorInitContextYConf(const TYandexConfig::Section& section) - : Section(section) -{} - -bool TLogBackendCreatorInitContextYConf::GetValue(TStringBuf name, TString& var) const { - return Section.GetDirectives().GetValue(name, var); -} - -TVector<THolder<ILogBackendCreator::IInitContext>> TLogBackendCreatorInitContextYConf::GetChildren(TStringBuf name) const { - TVector<THolder<IInitContext>> result; - auto children = Section.GetAllChildren(); - for (auto range = children.equal_range(TCiString(name)); range.first != range.second; ++range.first) { - result.emplace_back(MakeHolder<TLogBackendCreatorInitContextYConf>(*range.first->second)); - } - return result; -} +#include "yconf.h" + +TLogBackendCreatorInitContextYConf::TLogBackendCreatorInitContextYConf(const TYandexConfig::Section& section) + : Section(section) +{} + +bool TLogBackendCreatorInitContextYConf::GetValue(TStringBuf name, TString& var) const { + return Section.GetDirectives().GetValue(name, var); +} + +TVector<THolder<ILogBackendCreator::IInitContext>> TLogBackendCreatorInitContextYConf::GetChildren(TStringBuf name) const { + TVector<THolder<IInitContext>> result; + auto children = Section.GetAllChildren(); + for (auto range = children.equal_range(TCiString(name)); range.first != range.second; ++range.first) { + result.emplace_back(MakeHolder<TLogBackendCreatorInitContextYConf>(*range.first->second)); + } + return result; +} diff --git a/library/cpp/logger/init_context/yconf.h b/library/cpp/logger/init_context/yconf.h index b1867d271d..574cb617ef 100644 --- a/library/cpp/logger/init_context/yconf.h +++ b/library/cpp/logger/init_context/yconf.h @@ -1,13 +1,13 @@ -#pragma once - -#include <library/cpp/logger/backend_creator.h> -#include <library/cpp/yconf/conf.h> - -class TLogBackendCreatorInitContextYConf: public ILogBackendCreator::IInitContext { -public: - TLogBackendCreatorInitContextYConf(const TYandexConfig::Section& section); - virtual bool GetValue(TStringBuf name, TString& var) const override; - virtual TVector<THolder<IInitContext>> GetChildren(TStringBuf name) const override; -private: - const TYandexConfig::Section& Section; -}; +#pragma once + +#include <library/cpp/logger/backend_creator.h> +#include <library/cpp/yconf/conf.h> + +class TLogBackendCreatorInitContextYConf: public ILogBackendCreator::IInitContext { +public: + TLogBackendCreatorInitContextYConf(const TYandexConfig::Section& section); + virtual bool GetValue(TStringBuf name, TString& var) const override; + virtual TVector<THolder<IInitContext>> GetChildren(TStringBuf name) const override; +private: + const TYandexConfig::Section& Section; +}; diff --git a/library/cpp/logger/log.cpp b/library/cpp/logger/log.cpp index e1d70cc3d2..8d83a61196 100644 --- a/library/cpp/logger/log.cpp +++ b/library/cpp/logger/log.cpp @@ -1,5 +1,5 @@ -#include "log.h" -#include "uninitialized_creator.h" +#include "log.h" +#include "uninitialized_creator.h" #include "filter.h" #include "null.h" #include "stream.h" @@ -13,9 +13,9 @@ #include <util/generic/yexception.h> THolder<TLogBackend> CreateLogBackend(const TString& fname, ELogPriority priority, bool threaded) { - TLogBackendCreatorUninitialized creator; - creator.InitCustom(fname, priority, threaded); - return creator.CreateLogBackend(); + TLogBackendCreatorUninitialized creator; + creator.InitCustom(fname, priority, threaded); + return creator.CreateLogBackend(); } THolder<TLogBackend> CreateFilteredOwningThreadedLogBackend(const TString& fname, ELogPriority priority, size_t queueLen) { @@ -23,7 +23,7 @@ THolder<TLogBackend> CreateFilteredOwningThreadedLogBackend(const TString& fname } THolder<TOwningThreadedLogBackend> CreateOwningThreadedLogBackend(const TString& fname, size_t queueLen) { - return MakeHolder<TOwningThreadedLogBackend>(CreateLogBackend(fname, LOG_MAX_PRIORITY, false).Release(), queueLen); + return MakeHolder<TOwningThreadedLogBackend>(CreateLogBackend(fname, LOG_MAX_PRIORITY, false).Release(), queueLen); } class TLog::TImpl: public TAtomicRefCount<TImpl> { @@ -131,7 +131,7 @@ TLog::TLog() } TLog::TLog(const TString& fname, ELogPriority priority) - : TLog(CreateLogBackend(fname, priority, false)) + : TLog(CreateLogBackend(fname, priority, false)) { } @@ -206,7 +206,7 @@ ELogPriority TLog::DefaultPriority() const noexcept { bool TLog::OpenLog(const char* path, ELogPriority lp) { if (path) { - ResetBackend(CreateLogBackend(path, lp)); + ResetBackend(CreateLogBackend(path, lp)); } else { ResetBackend(MakeHolder<TStreamLogBackend>(&Cerr)); } diff --git a/library/cpp/logger/null_creator.cpp b/library/cpp/logger/null_creator.cpp index 9f63d5c739..4258fb25fd 100644 --- a/library/cpp/logger/null_creator.cpp +++ b/library/cpp/logger/null_creator.cpp @@ -1,17 +1,17 @@ -#include "null_creator.h" -#include "null.h" - -THolder<TLogBackend> TNullLogBackendCreator::DoCreateLogBackend() const { - return MakeHolder<TNullLogBackend>(); -} - -ILogBackendCreator::TFactory::TRegistrator<TNullLogBackendCreator> TNullLogBackendCreator::RegistrarDevNull("/dev/null"); -ILogBackendCreator::TFactory::TRegistrator<TNullLogBackendCreator> TNullLogBackendCreator::RegistrarNull("null"); - - -void TNullLogBackendCreator::DoToJson(NJson::TJsonValue& /*value*/) const { -} - -TNullLogBackendCreator::TNullLogBackendCreator() - : TLogBackendCreatorBase("null") -{} +#include "null_creator.h" +#include "null.h" + +THolder<TLogBackend> TNullLogBackendCreator::DoCreateLogBackend() const { + return MakeHolder<TNullLogBackend>(); +} + +ILogBackendCreator::TFactory::TRegistrator<TNullLogBackendCreator> TNullLogBackendCreator::RegistrarDevNull("/dev/null"); +ILogBackendCreator::TFactory::TRegistrator<TNullLogBackendCreator> TNullLogBackendCreator::RegistrarNull("null"); + + +void TNullLogBackendCreator::DoToJson(NJson::TJsonValue& /*value*/) const { +} + +TNullLogBackendCreator::TNullLogBackendCreator() + : TLogBackendCreatorBase("null") +{} diff --git a/library/cpp/logger/null_creator.h b/library/cpp/logger/null_creator.h index a5bcab63f9..a262b3701f 100644 --- a/library/cpp/logger/null_creator.h +++ b/library/cpp/logger/null_creator.h @@ -1,15 +1,15 @@ -#pragma once - -#include "backend_creator.h" - -class TNullLogBackendCreator : public TLogBackendCreatorBase { -public: - TNullLogBackendCreator(); - static TFactory::TRegistrator<TNullLogBackendCreator> RegistrarNull; - static TFactory::TRegistrator<TNullLogBackendCreator> RegistrarDevNull; -protected: - virtual void DoToJson(NJson::TJsonValue& value) const override; - -private: - virtual THolder<TLogBackend> DoCreateLogBackend() const override; -}; +#pragma once + +#include "backend_creator.h" + +class TNullLogBackendCreator : public TLogBackendCreatorBase { +public: + TNullLogBackendCreator(); + static TFactory::TRegistrator<TNullLogBackendCreator> RegistrarNull; + static TFactory::TRegistrator<TNullLogBackendCreator> RegistrarDevNull; +protected: + virtual void DoToJson(NJson::TJsonValue& value) const override; + +private: + virtual THolder<TLogBackend> DoCreateLogBackend() const override; +}; diff --git a/library/cpp/logger/priority.h b/library/cpp/logger/priority.h index d2a9fa0a07..4e6fbf1365 100644 --- a/library/cpp/logger/priority.h +++ b/library/cpp/logger/priority.h @@ -11,5 +11,5 @@ enum ELogPriority { TLOG_DEBUG = 7 /* "DEBUG" */, TLOG_RESOURCES = 8 /* "RESOURCES" */ }; -#define LOG_MAX_PRIORITY TLOG_RESOURCES +#define LOG_MAX_PRIORITY TLOG_RESOURCES #define LOG_DEF_PRIORITY TLOG_INFO diff --git a/library/cpp/logger/rotating_file_creator.cpp b/library/cpp/logger/rotating_file_creator.cpp index 6f71b68573..8b4f981b5c 100644 --- a/library/cpp/logger/rotating_file_creator.cpp +++ b/library/cpp/logger/rotating_file_creator.cpp @@ -1,28 +1,28 @@ -#include "rotating_file_creator.h" -#include "rotating_file.h" - -THolder<TLogBackend> TRotatingFileLogBackendCreator::DoCreateLogBackend() const { - return MakeHolder<TRotatingFileLogBackend>(Path, MaxSizeBytes, RotatedFilesCount); -} - - -TRotatingFileLogBackendCreator::TRotatingFileLogBackendCreator() - : TFileLogBackendCreator("", "rotating") -{} - -bool TRotatingFileLogBackendCreator::Init(const IInitContext& ctx) { - if (!TFileLogBackendCreator::Init(ctx)) { - return false; - } - ctx.GetValue("MaxSizeBytes", MaxSizeBytes); - ctx.GetValue("RotatedFilesCount", RotatedFilesCount); - return true; -} - -ILogBackendCreator::TFactory::TRegistrator<TRotatingFileLogBackendCreator> TRotatingFileLogBackendCreator::Registrar("rotating"); - -void TRotatingFileLogBackendCreator::DoToJson(NJson::TJsonValue& value) const { - TFileLogBackendCreator::DoToJson(value); - value["MaxSizeBytes"] = MaxSizeBytes; - value["RotatedFilesCount"] = RotatedFilesCount; -} +#include "rotating_file_creator.h" +#include "rotating_file.h" + +THolder<TLogBackend> TRotatingFileLogBackendCreator::DoCreateLogBackend() const { + return MakeHolder<TRotatingFileLogBackend>(Path, MaxSizeBytes, RotatedFilesCount); +} + + +TRotatingFileLogBackendCreator::TRotatingFileLogBackendCreator() + : TFileLogBackendCreator("", "rotating") +{} + +bool TRotatingFileLogBackendCreator::Init(const IInitContext& ctx) { + if (!TFileLogBackendCreator::Init(ctx)) { + return false; + } + ctx.GetValue("MaxSizeBytes", MaxSizeBytes); + ctx.GetValue("RotatedFilesCount", RotatedFilesCount); + return true; +} + +ILogBackendCreator::TFactory::TRegistrator<TRotatingFileLogBackendCreator> TRotatingFileLogBackendCreator::Registrar("rotating"); + +void TRotatingFileLogBackendCreator::DoToJson(NJson::TJsonValue& value) const { + TFileLogBackendCreator::DoToJson(value); + value["MaxSizeBytes"] = MaxSizeBytes; + value["RotatedFilesCount"] = RotatedFilesCount; +} diff --git a/library/cpp/logger/rotating_file_creator.h b/library/cpp/logger/rotating_file_creator.h index b2e94584da..ee2e7e6a6c 100644 --- a/library/cpp/logger/rotating_file_creator.h +++ b/library/cpp/logger/rotating_file_creator.h @@ -1,18 +1,18 @@ -#pragma once - -#include "file_creator.h" - -class TRotatingFileLogBackendCreator : public TFileLogBackendCreator { -public: - TRotatingFileLogBackendCreator(); - virtual bool Init(const IInitContext& ctx) override; - static TFactory::TRegistrator<TRotatingFileLogBackendCreator> Registrar; - -protected: - virtual void DoToJson(NJson::TJsonValue& value) const override; - -private: - virtual THolder<TLogBackend> DoCreateLogBackend() const override; - ui64 MaxSizeBytes = Max<ui64>(); - ui64 RotatedFilesCount = Max<ui64>(); -}; +#pragma once + +#include "file_creator.h" + +class TRotatingFileLogBackendCreator : public TFileLogBackendCreator { +public: + TRotatingFileLogBackendCreator(); + virtual bool Init(const IInitContext& ctx) override; + static TFactory::TRegistrator<TRotatingFileLogBackendCreator> Registrar; + +protected: + virtual void DoToJson(NJson::TJsonValue& value) const override; + +private: + virtual THolder<TLogBackend> DoCreateLogBackend() const override; + ui64 MaxSizeBytes = Max<ui64>(); + ui64 RotatedFilesCount = Max<ui64>(); +}; diff --git a/library/cpp/logger/stream_creator.cpp b/library/cpp/logger/stream_creator.cpp index 6246f9bf9c..fafdcfbfb3 100644 --- a/library/cpp/logger/stream_creator.cpp +++ b/library/cpp/logger/stream_creator.cpp @@ -1,32 +1,32 @@ -#include "stream_creator.h" -#include "stream.h" - -THolder<TLogBackend> TCerrLogBackendCreator::DoCreateLogBackend() const { - return MakeHolder<TStreamLogBackend>(&Cerr); -} - - -TCerrLogBackendCreator::TCerrLogBackendCreator() - : TLogBackendCreatorBase("cerr") -{} - -void TCerrLogBackendCreator::DoToJson(NJson::TJsonValue& /*value*/) const { -} - -ILogBackendCreator::TFactory::TRegistrator<TCerrLogBackendCreator> TCerrLogBackendCreator::RegistrarCerr("cerr"); -ILogBackendCreator::TFactory::TRegistrator<TCerrLogBackendCreator> TCerrLogBackendCreator::RegistrarConsole("console"); - - -THolder<TLogBackend> TCoutLogBackendCreator::DoCreateLogBackend() const { - return MakeHolder<TStreamLogBackend>(&Cout); -} - - -TCoutLogBackendCreator::TCoutLogBackendCreator() - : TLogBackendCreatorBase("cout") -{} - -ILogBackendCreator::TFactory::TRegistrator<TCoutLogBackendCreator> TCoutLogBackendCreator::Registrar("cout"); - -void TCoutLogBackendCreator::DoToJson(NJson::TJsonValue& /*value*/) const { -} +#include "stream_creator.h" +#include "stream.h" + +THolder<TLogBackend> TCerrLogBackendCreator::DoCreateLogBackend() const { + return MakeHolder<TStreamLogBackend>(&Cerr); +} + + +TCerrLogBackendCreator::TCerrLogBackendCreator() + : TLogBackendCreatorBase("cerr") +{} + +void TCerrLogBackendCreator::DoToJson(NJson::TJsonValue& /*value*/) const { +} + +ILogBackendCreator::TFactory::TRegistrator<TCerrLogBackendCreator> TCerrLogBackendCreator::RegistrarCerr("cerr"); +ILogBackendCreator::TFactory::TRegistrator<TCerrLogBackendCreator> TCerrLogBackendCreator::RegistrarConsole("console"); + + +THolder<TLogBackend> TCoutLogBackendCreator::DoCreateLogBackend() const { + return MakeHolder<TStreamLogBackend>(&Cout); +} + + +TCoutLogBackendCreator::TCoutLogBackendCreator() + : TLogBackendCreatorBase("cout") +{} + +ILogBackendCreator::TFactory::TRegistrator<TCoutLogBackendCreator> TCoutLogBackendCreator::Registrar("cout"); + +void TCoutLogBackendCreator::DoToJson(NJson::TJsonValue& /*value*/) const { +} diff --git a/library/cpp/logger/stream_creator.h b/library/cpp/logger/stream_creator.h index cd66e986c0..73c43b4955 100644 --- a/library/cpp/logger/stream_creator.h +++ b/library/cpp/logger/stream_creator.h @@ -1,28 +1,28 @@ -#pragma once - -#include "backend_creator.h" - -class TCerrLogBackendCreator : public TLogBackendCreatorBase { -public: - TCerrLogBackendCreator(); - static TFactory::TRegistrator<TCerrLogBackendCreator> RegistrarCerr; - static TFactory::TRegistrator<TCerrLogBackendCreator> RegistrarConsole; - -protected: - virtual void DoToJson(NJson::TJsonValue& value) const override; - -private: - virtual THolder<TLogBackend> DoCreateLogBackend() const override; -}; - -class TCoutLogBackendCreator : public TLogBackendCreatorBase { -public: - TCoutLogBackendCreator(); - static TFactory::TRegistrator<TCoutLogBackendCreator> Registrar; - -protected: - virtual void DoToJson(NJson::TJsonValue& value) const override; - -private: - virtual THolder<TLogBackend> DoCreateLogBackend() const override; -}; +#pragma once + +#include "backend_creator.h" + +class TCerrLogBackendCreator : public TLogBackendCreatorBase { +public: + TCerrLogBackendCreator(); + static TFactory::TRegistrator<TCerrLogBackendCreator> RegistrarCerr; + static TFactory::TRegistrator<TCerrLogBackendCreator> RegistrarConsole; + +protected: + virtual void DoToJson(NJson::TJsonValue& value) const override; + +private: + virtual THolder<TLogBackend> DoCreateLogBackend() const override; +}; + +class TCoutLogBackendCreator : public TLogBackendCreatorBase { +public: + TCoutLogBackendCreator(); + static TFactory::TRegistrator<TCoutLogBackendCreator> Registrar; + +protected: + virtual void DoToJson(NJson::TJsonValue& value) const override; + +private: + virtual THolder<TLogBackend> DoCreateLogBackend() const override; +}; diff --git a/library/cpp/logger/sync_page_cache_file_creator.cpp b/library/cpp/logger/sync_page_cache_file_creator.cpp index bf91d20c23..9820e440bc 100644 --- a/library/cpp/logger/sync_page_cache_file_creator.cpp +++ b/library/cpp/logger/sync_page_cache_file_creator.cpp @@ -1,28 +1,28 @@ -#include "sync_page_cache_file_creator.h" -#include "sync_page_cache_file.h" - -THolder<TLogBackend> TSyncPageCacheFileLogBackendCreator::DoCreateLogBackend() const { - return MakeHolder<TSyncPageCacheFileLogBackend>(Path, MaxBufferSize, MaxPendingCacheSize); -} - - -TSyncPageCacheFileLogBackendCreator::TSyncPageCacheFileLogBackendCreator() - : TFileLogBackendCreator("", "sync_page") -{} - -bool TSyncPageCacheFileLogBackendCreator::Init(const IInitContext& ctx) { - if (!TFileLogBackendCreator::Init(ctx)) { - return false; - } - ctx.GetValue("MaxBufferSize", MaxBufferSize); - ctx.GetValue("MaxPendingCacheSize", MaxPendingCacheSize); - return true; -} - -ILogBackendCreator::TFactory::TRegistrator<TSyncPageCacheFileLogBackendCreator> TSyncPageCacheFileLogBackendCreator::Registrar("sync_page"); - -void TSyncPageCacheFileLogBackendCreator::DoToJson(NJson::TJsonValue& value) const { - TFileLogBackendCreator::DoToJson(value); - value["MaxBufferSize"] = MaxBufferSize; - value["MaxPendingCacheSize"] = MaxPendingCacheSize; -} +#include "sync_page_cache_file_creator.h" +#include "sync_page_cache_file.h" + +THolder<TLogBackend> TSyncPageCacheFileLogBackendCreator::DoCreateLogBackend() const { + return MakeHolder<TSyncPageCacheFileLogBackend>(Path, MaxBufferSize, MaxPendingCacheSize); +} + + +TSyncPageCacheFileLogBackendCreator::TSyncPageCacheFileLogBackendCreator() + : TFileLogBackendCreator("", "sync_page") +{} + +bool TSyncPageCacheFileLogBackendCreator::Init(const IInitContext& ctx) { + if (!TFileLogBackendCreator::Init(ctx)) { + return false; + } + ctx.GetValue("MaxBufferSize", MaxBufferSize); + ctx.GetValue("MaxPendingCacheSize", MaxPendingCacheSize); + return true; +} + +ILogBackendCreator::TFactory::TRegistrator<TSyncPageCacheFileLogBackendCreator> TSyncPageCacheFileLogBackendCreator::Registrar("sync_page"); + +void TSyncPageCacheFileLogBackendCreator::DoToJson(NJson::TJsonValue& value) const { + TFileLogBackendCreator::DoToJson(value); + value["MaxBufferSize"] = MaxBufferSize; + value["MaxPendingCacheSize"] = MaxPendingCacheSize; +} diff --git a/library/cpp/logger/sync_page_cache_file_creator.h b/library/cpp/logger/sync_page_cache_file_creator.h index 7148150222..3962c54abf 100644 --- a/library/cpp/logger/sync_page_cache_file_creator.h +++ b/library/cpp/logger/sync_page_cache_file_creator.h @@ -1,18 +1,18 @@ -#pragma once - -#include "file_creator.h" - -class TSyncPageCacheFileLogBackendCreator : public TFileLogBackendCreator { -public: - TSyncPageCacheFileLogBackendCreator(); - virtual bool Init(const IInitContext& ctx) override; - static TFactory::TRegistrator<TSyncPageCacheFileLogBackendCreator> Registrar; - -protected: - virtual void DoToJson(NJson::TJsonValue& value) const override; - -private: - virtual THolder<TLogBackend> DoCreateLogBackend() const override; - size_t MaxBufferSize = Max<size_t>(); - size_t MaxPendingCacheSize = Max<size_t>(); -}; +#pragma once + +#include "file_creator.h" + +class TSyncPageCacheFileLogBackendCreator : public TFileLogBackendCreator { +public: + TSyncPageCacheFileLogBackendCreator(); + virtual bool Init(const IInitContext& ctx) override; + static TFactory::TRegistrator<TSyncPageCacheFileLogBackendCreator> Registrar; + +protected: + virtual void DoToJson(NJson::TJsonValue& value) const override; + +private: + virtual THolder<TLogBackend> DoCreateLogBackend() const override; + size_t MaxBufferSize = Max<size_t>(); + size_t MaxPendingCacheSize = Max<size_t>(); +}; diff --git a/library/cpp/logger/system_creator.cpp b/library/cpp/logger/system_creator.cpp index e1cd02d422..efcc2c2acb 100644 --- a/library/cpp/logger/system_creator.cpp +++ b/library/cpp/logger/system_creator.cpp @@ -1,25 +1,25 @@ -#include "system_creator.h" - -THolder<TLogBackend> TSysLogBackendCreator::DoCreateLogBackend() const { - return MakeHolder<TSysLogBackend>(Ident.c_str(), Facility, Flags); -} - - -TSysLogBackendCreator::TSysLogBackendCreator() - : TLogBackendCreatorBase("system") -{} - -bool TSysLogBackendCreator::Init(const IInitContext& ctx) { - ctx.GetValue("Ident", Ident); - ctx.GetValue("Facility", (int&)Facility); - ctx.GetValue("Flags", Flags); - return true; -} - -ILogBackendCreator::TFactory::TRegistrator<TSysLogBackendCreator> TSysLogBackendCreator::Registrar("system"); - -void TSysLogBackendCreator::DoToJson(NJson::TJsonValue& value) const { - value["Ident"] = Ident; - value["Facility"] = (int&)Facility; - value["Flags"] = Flags; -} +#include "system_creator.h" + +THolder<TLogBackend> TSysLogBackendCreator::DoCreateLogBackend() const { + return MakeHolder<TSysLogBackend>(Ident.c_str(), Facility, Flags); +} + + +TSysLogBackendCreator::TSysLogBackendCreator() + : TLogBackendCreatorBase("system") +{} + +bool TSysLogBackendCreator::Init(const IInitContext& ctx) { + ctx.GetValue("Ident", Ident); + ctx.GetValue("Facility", (int&)Facility); + ctx.GetValue("Flags", Flags); + return true; +} + +ILogBackendCreator::TFactory::TRegistrator<TSysLogBackendCreator> TSysLogBackendCreator::Registrar("system"); + +void TSysLogBackendCreator::DoToJson(NJson::TJsonValue& value) const { + value["Ident"] = Ident; + value["Facility"] = (int&)Facility; + value["Flags"] = Flags; +} diff --git a/library/cpp/logger/system_creator.h b/library/cpp/logger/system_creator.h index e5321267e9..3f2677bad6 100644 --- a/library/cpp/logger/system_creator.h +++ b/library/cpp/logger/system_creator.h @@ -1,20 +1,20 @@ -#pragma once - -#include "backend_creator.h" -#include "system.h" - -class TSysLogBackendCreator : public TLogBackendCreatorBase { -public: - TSysLogBackendCreator(); - virtual bool Init(const IInitContext& ctx) override; - static TFactory::TRegistrator<TSysLogBackendCreator> Registrar; - -protected: - virtual void DoToJson(NJson::TJsonValue& value) const override; - -private: - virtual THolder<TLogBackend> DoCreateLogBackend() const override; - TString Ident; - TSysLogBackend::EFacility Facility = TSysLogBackend::TSYSLOG_LOCAL0; - int Flags = 0; -}; +#pragma once + +#include "backend_creator.h" +#include "system.h" + +class TSysLogBackendCreator : public TLogBackendCreatorBase { +public: + TSysLogBackendCreator(); + virtual bool Init(const IInitContext& ctx) override; + static TFactory::TRegistrator<TSysLogBackendCreator> Registrar; + +protected: + virtual void DoToJson(NJson::TJsonValue& value) const override; + +private: + virtual THolder<TLogBackend> DoCreateLogBackend() const override; + TString Ident; + TSysLogBackend::EFacility Facility = TSysLogBackend::TSYSLOG_LOCAL0; + int Flags = 0; +}; diff --git a/library/cpp/logger/thread_creator.cpp b/library/cpp/logger/thread_creator.cpp index 8f5cfe2782..7e2eaba109 100644 --- a/library/cpp/logger/thread_creator.cpp +++ b/library/cpp/logger/thread_creator.cpp @@ -1,30 +1,30 @@ -#include "thread_creator.h" -#include "thread.h" - -TOwningThreadedLogBackendCreator::TOwningThreadedLogBackendCreator(THolder<ILogBackendCreator>&& slave) - : Slave(std::move(slave)) -{} - -THolder<TLogBackend> TOwningThreadedLogBackendCreator::DoCreateLogBackend() const { - return MakeHolder<TOwningThreadedLogBackend>(Slave->CreateLogBackend().Release(), QueueLen, QueueOverflowCallback); -} - -bool TOwningThreadedLogBackendCreator::Init(const IInitContext& ctx) { - ctx.GetValue("QueueLen", QueueLen); - return Slave->Init(ctx); -} - - -void TOwningThreadedLogBackendCreator::ToJson(NJson::TJsonValue& value) const { - value["QueueLen"] = QueueLen; - value["Threaded"] = true; - Slave->ToJson(value); -} - -void TOwningThreadedLogBackendCreator::SetQueueOverflowCallback(std::function<void()> callback) { - QueueOverflowCallback = std::move(callback); -} - -void TOwningThreadedLogBackendCreator::SetQueueLen(size_t len) { - QueueLen = len; -} +#include "thread_creator.h" +#include "thread.h" + +TOwningThreadedLogBackendCreator::TOwningThreadedLogBackendCreator(THolder<ILogBackendCreator>&& slave) + : Slave(std::move(slave)) +{} + +THolder<TLogBackend> TOwningThreadedLogBackendCreator::DoCreateLogBackend() const { + return MakeHolder<TOwningThreadedLogBackend>(Slave->CreateLogBackend().Release(), QueueLen, QueueOverflowCallback); +} + +bool TOwningThreadedLogBackendCreator::Init(const IInitContext& ctx) { + ctx.GetValue("QueueLen", QueueLen); + return Slave->Init(ctx); +} + + +void TOwningThreadedLogBackendCreator::ToJson(NJson::TJsonValue& value) const { + value["QueueLen"] = QueueLen; + value["Threaded"] = true; + Slave->ToJson(value); +} + +void TOwningThreadedLogBackendCreator::SetQueueOverflowCallback(std::function<void()> callback) { + QueueOverflowCallback = std::move(callback); +} + +void TOwningThreadedLogBackendCreator::SetQueueLen(size_t len) { + QueueLen = len; +} diff --git a/library/cpp/logger/thread_creator.h b/library/cpp/logger/thread_creator.h index 9e5ee9571d..61fd001a92 100644 --- a/library/cpp/logger/thread_creator.h +++ b/library/cpp/logger/thread_creator.h @@ -1,20 +1,20 @@ -#pragma once - -#include "backend_creator.h" - -#include <functional> - -class TOwningThreadedLogBackendCreator: public ILogBackendCreator { -public: - TOwningThreadedLogBackendCreator(THolder<ILogBackendCreator>&& slave); - virtual bool Init(const IInitContext& ctx) override; - virtual void ToJson(NJson::TJsonValue& value) const override; - void SetQueueOverflowCallback(std::function<void()> callback); - void SetQueueLen(size_t len); - -private: - virtual THolder<TLogBackend> DoCreateLogBackend() const override; - THolder<ILogBackendCreator> Slave; - std::function<void()> QueueOverflowCallback = {}; - size_t QueueLen = 0; -}; +#pragma once + +#include "backend_creator.h" + +#include <functional> + +class TOwningThreadedLogBackendCreator: public ILogBackendCreator { +public: + TOwningThreadedLogBackendCreator(THolder<ILogBackendCreator>&& slave); + virtual bool Init(const IInitContext& ctx) override; + virtual void ToJson(NJson::TJsonValue& value) const override; + void SetQueueOverflowCallback(std::function<void()> callback); + void SetQueueLen(size_t len); + +private: + virtual THolder<TLogBackend> DoCreateLogBackend() const override; + THolder<ILogBackendCreator> Slave; + std::function<void()> QueueOverflowCallback = {}; + size_t QueueLen = 0; +}; diff --git a/library/cpp/logger/uninitialized_creator.cpp b/library/cpp/logger/uninitialized_creator.cpp index 26dd168529..5d7528ae23 100644 --- a/library/cpp/logger/uninitialized_creator.cpp +++ b/library/cpp/logger/uninitialized_creator.cpp @@ -1,50 +1,50 @@ -#include "uninitialized_creator.h" -#include "filter_creator.h" -#include "thread_creator.h" -#include "file_creator.h" -#include "null_creator.h" -#include <util/string/cast.h> - -THolder<TLogBackend> TLogBackendCreatorUninitialized::DoCreateLogBackend() const { - return Slave->CreateLogBackend(); -} - -void TLogBackendCreatorUninitialized::InitCustom(const TString& type, ELogPriority priority, bool threaded) { - if (!type) { - Slave = MakeHolder<TNullLogBackendCreator>(); - } else if (TFactory::Has(type)) { - Slave = TFactory::MakeHolder(type); - } else { - Slave = MakeHolder<TFileLogBackendCreator>(type); - } - - if (threaded) { - Slave = MakeHolder<TOwningThreadedLogBackendCreator>(std::move(Slave)); - } - - if (priority != LOG_MAX_PRIORITY) { - Slave = MakeHolder<TFilteredBackendCreator>(std::move(Slave), priority); - } -} - -bool TLogBackendCreatorUninitialized::Init(const IInitContext& ctx) { - auto type = ctx.GetOrElse("LoggerType", TString()); - bool threaded = ctx.GetOrElse("Threaded", false); - ELogPriority priority = LOG_MAX_PRIORITY; - TString prStr; - if (ctx.GetValue("LogLevel", prStr)) { - if (!TryFromString(prStr, priority)) { - priority = (ELogPriority)FromString<int>(prStr); - } - } - InitCustom(type, priority, threaded); - return Slave->Init(ctx); -} - - -void TLogBackendCreatorUninitialized::ToJson(NJson::TJsonValue& value) const { - Y_VERIFY(Slave, "Serialization off uninitialized LogBackendCreator"); - Slave->ToJson(value); -} - -ILogBackendCreator::TFactory::TRegistrator<TLogBackendCreatorUninitialized> TLogBackendCreatorUninitialized::Registrar(""); +#include "uninitialized_creator.h" +#include "filter_creator.h" +#include "thread_creator.h" +#include "file_creator.h" +#include "null_creator.h" +#include <util/string/cast.h> + +THolder<TLogBackend> TLogBackendCreatorUninitialized::DoCreateLogBackend() const { + return Slave->CreateLogBackend(); +} + +void TLogBackendCreatorUninitialized::InitCustom(const TString& type, ELogPriority priority, bool threaded) { + if (!type) { + Slave = MakeHolder<TNullLogBackendCreator>(); + } else if (TFactory::Has(type)) { + Slave = TFactory::MakeHolder(type); + } else { + Slave = MakeHolder<TFileLogBackendCreator>(type); + } + + if (threaded) { + Slave = MakeHolder<TOwningThreadedLogBackendCreator>(std::move(Slave)); + } + + if (priority != LOG_MAX_PRIORITY) { + Slave = MakeHolder<TFilteredBackendCreator>(std::move(Slave), priority); + } +} + +bool TLogBackendCreatorUninitialized::Init(const IInitContext& ctx) { + auto type = ctx.GetOrElse("LoggerType", TString()); + bool threaded = ctx.GetOrElse("Threaded", false); + ELogPriority priority = LOG_MAX_PRIORITY; + TString prStr; + if (ctx.GetValue("LogLevel", prStr)) { + if (!TryFromString(prStr, priority)) { + priority = (ELogPriority)FromString<int>(prStr); + } + } + InitCustom(type, priority, threaded); + return Slave->Init(ctx); +} + + +void TLogBackendCreatorUninitialized::ToJson(NJson::TJsonValue& value) const { + Y_VERIFY(Slave, "Serialization off uninitialized LogBackendCreator"); + Slave->ToJson(value); +} + +ILogBackendCreator::TFactory::TRegistrator<TLogBackendCreatorUninitialized> TLogBackendCreatorUninitialized::Registrar(""); diff --git a/library/cpp/logger/uninitialized_creator.h b/library/cpp/logger/uninitialized_creator.h index 16ee0b92f6..0340995338 100644 --- a/library/cpp/logger/uninitialized_creator.h +++ b/library/cpp/logger/uninitialized_creator.h @@ -1,16 +1,16 @@ -#pragma once - -#include "backend_creator.h" - -class TLogBackendCreatorUninitialized : public ILogBackendCreator { -public: - void InitCustom(const TString& type, ELogPriority priority, bool threaded); - virtual bool Init(const IInitContext& ctx) override; - virtual void ToJson(NJson::TJsonValue& value) const override; - - static TFactory::TRegistrator<TLogBackendCreatorUninitialized> Registrar; - -private: - virtual THolder<TLogBackend> DoCreateLogBackend() const override; - THolder<ILogBackendCreator> Slave; -}; +#pragma once + +#include "backend_creator.h" + +class TLogBackendCreatorUninitialized : public ILogBackendCreator { +public: + void InitCustom(const TString& type, ELogPriority priority, bool threaded); + virtual bool Init(const IInitContext& ctx) override; + virtual void ToJson(NJson::TJsonValue& value) const override; + + static TFactory::TRegistrator<TLogBackendCreatorUninitialized> Registrar; + +private: + virtual THolder<TLogBackend> DoCreateLogBackend() const override; + THolder<ILogBackendCreator> Slave; +}; diff --git a/library/cpp/logger/ut/ya.make b/library/cpp/logger/ut/ya.make index 2a461c1353..211c97c8a9 100644 --- a/library/cpp/logger/ut/ya.make +++ b/library/cpp/logger/ut/ya.make @@ -4,8 +4,8 @@ OWNER(pg) PEERDIR( ADDINCL library/cpp/logger - library/cpp/logger/init_context - library/cpp/yconf/patcher + library/cpp/logger/init_context + library/cpp/yconf/patcher ) SRCDIR(library/cpp/logger) @@ -14,7 +14,7 @@ SRCS( log_ut.cpp element_ut.cpp rotating_file_ut.cpp - composite_ut.cpp + composite_ut.cpp ) END() diff --git a/library/cpp/logger/ya.make b/library/cpp/logger/ya.make index 00a5263cba..ba98035b10 100644 --- a/library/cpp/logger/ya.make +++ b/library/cpp/logger/ya.make @@ -9,37 +9,37 @@ LIBRARY() GENERATE_ENUM_SERIALIZATION(priority.h) -PEERDIR ( - library/cpp/json -) - +PEERDIR ( + library/cpp/json +) + SRCS( - all.h - backend.cpp - backend_creator.cpp - composite.cpp - GLOBAL composite_creator.cpp - element.cpp - file.cpp - GLOBAL file_creator.cpp - filter.cpp - filter_creator.cpp + all.h + backend.cpp + backend_creator.cpp + composite.cpp + GLOBAL composite_creator.cpp + element.cpp + file.cpp + GLOBAL file_creator.cpp + filter.cpp + filter_creator.cpp log.cpp - null.cpp - GLOBAL null_creator.cpp - priority.h - record.h + null.cpp + GLOBAL null_creator.cpp + priority.h + record.h rotating_file.cpp - GLOBAL rotating_file_creator.cpp + GLOBAL rotating_file_creator.cpp stream.cpp - GLOBAL stream_creator.cpp + GLOBAL stream_creator.cpp sync_page_cache_file.cpp - GLOBAL sync_page_cache_file_creator.cpp - system.cpp - GLOBAL system_creator.cpp - thread.cpp - thread_creator.cpp - GLOBAL uninitialized_creator.cpp + GLOBAL sync_page_cache_file_creator.cpp + system.cpp + GLOBAL system_creator.cpp + thread.cpp + thread_creator.cpp + GLOBAL uninitialized_creator.cpp ) END() diff --git a/library/cpp/malloc/api/malloc.h b/library/cpp/malloc/api/malloc.h index ebd545d6dd..bd9300aede 100644 --- a/library/cpp/malloc/api/malloc.h +++ b/library/cpp/malloc/api/malloc.h @@ -1,7 +1,7 @@ #pragma once #include <string.h> -#include <util/system/compiler.h> +#include <util/system/compiler.h> namespace NMalloc { struct TMallocInfo { @@ -20,13 +20,13 @@ namespace NMalloc { // this function should be implemented by malloc implementations TMallocInfo MallocInfo(); - - struct TAllocHeader { - void* Block; - size_t AllocSize; - void Y_FORCE_INLINE Encode(void* block, size_t size, size_t signature) { - Block = block; - AllocSize = size | signature; - } - }; + + struct TAllocHeader { + void* Block; + size_t AllocSize; + void Y_FORCE_INLINE Encode(void* block, size_t size, size_t signature) { + Block = block; + AllocSize = size | signature; + } + }; } diff --git a/library/cpp/monlib/encode/prometheus/prometheus_encoder_ut.cpp b/library/cpp/monlib/encode/prometheus/prometheus_encoder_ut.cpp index fd9debb060..4a0b9c36f9 100644 --- a/library/cpp/monlib/encode/prometheus/prometheus_encoder_ut.cpp +++ b/library/cpp/monlib/encode/prometheus/prometheus_encoder_ut.cpp @@ -367,48 +367,48 @@ Y_UNIT_TEST_SUITE(TPrometheusEncoderTest) { "bytesRx_count{host=\"solomon-sas-01\", dc=\"sas\", } 17 1512216010000\n" "\n"); } - - Y_UNIT_TEST(CommonLables) { - auto result = EncodeToString([](IMetricEncoder* e) { - e->OnStreamBegin(); - { // common time - e->OnCommonTime(TInstant::Seconds(1500000000)); - } - { // common labels - e->OnLabelsBegin(); - e->OnLabel("project", "solomon"); - e->OnLabelsEnd(); - } - { // metric #1 - e->OnMetricBegin(EMetricType::COUNTER); - { - e->OnLabelsBegin(); - e->OnLabel("sensor", "single"); - e->OnLabel("labels", "l1"); - e->OnLabelsEnd(); - } - e->OnUint64(TInstant::ParseIso8601Deprecated("2017-12-02T12:00:10Z"), 17); - e->OnMetricEnd(); - } - { // metric #2 - e->OnMetricBegin(EMetricType::COUNTER); - { - e->OnLabelsBegin(); - e->OnLabel("sensor", "two"); - e->OnLabel("labels", "l2"); - e->OnLabelsEnd(); - } - e->OnUint64(TInstant::Zero(), 42); - e->OnMetricEnd(); - } - e->OnStreamEnd(); - }); - UNIT_ASSERT_STRINGS_EQUAL(result, -R"(# TYPE single counter -single{labels="l1", project="solomon", } 17 1512216010000 -# TYPE two counter -two{labels="l2", project="solomon", } 42 1500000000000 - -)"); - } + + Y_UNIT_TEST(CommonLables) { + auto result = EncodeToString([](IMetricEncoder* e) { + e->OnStreamBegin(); + { // common time + e->OnCommonTime(TInstant::Seconds(1500000000)); + } + { // common labels + e->OnLabelsBegin(); + e->OnLabel("project", "solomon"); + e->OnLabelsEnd(); + } + { // metric #1 + e->OnMetricBegin(EMetricType::COUNTER); + { + e->OnLabelsBegin(); + e->OnLabel("sensor", "single"); + e->OnLabel("labels", "l1"); + e->OnLabelsEnd(); + } + e->OnUint64(TInstant::ParseIso8601Deprecated("2017-12-02T12:00:10Z"), 17); + e->OnMetricEnd(); + } + { // metric #2 + e->OnMetricBegin(EMetricType::COUNTER); + { + e->OnLabelsBegin(); + e->OnLabel("sensor", "two"); + e->OnLabel("labels", "l2"); + e->OnLabelsEnd(); + } + e->OnUint64(TInstant::Zero(), 42); + e->OnMetricEnd(); + } + e->OnStreamEnd(); + }); + UNIT_ASSERT_STRINGS_EQUAL(result, +R"(# TYPE single counter +single{labels="l1", project="solomon", } 17 1512216010000 +# TYPE two counter +two{labels="l2", project="solomon", } 42 1500000000000 + +)"); + } } diff --git a/library/cpp/monlib/service/pages/registry_mon_page.cpp b/library/cpp/monlib/service/pages/registry_mon_page.cpp index c59e50f622..35c499e0dc 100644 --- a/library/cpp/monlib/service/pages/registry_mon_page.cpp +++ b/library/cpp/monlib/service/pages/registry_mon_page.cpp @@ -2,7 +2,7 @@ #include <library/cpp/monlib/encode/text/text.h> #include <library/cpp/monlib/encode/json/json.h> -#include <library/cpp/monlib/encode/prometheus/prometheus.h> +#include <library/cpp/monlib/encode/prometheus/prometheus.h> #include <library/cpp/monlib/encode/spack/spack_v1.h> #include <library/cpp/monlib/service/format.h> @@ -22,9 +22,9 @@ namespace NMonitoring { resp = HTTPOKSPACK; const auto compression = ParseCompression(request); encoder = NMonitoring::EncoderSpackV1(&out, ETimePrecision::SECONDS, compression); - } else if (formatStr == TStringBuf("prometheus")) { - resp = HTTPOKPROMETHEUS; - encoder = NMonitoring::EncoderPrometheus(&out); + } else if (formatStr == TStringBuf("prometheus")) { + resp = HTTPOKPROMETHEUS; + encoder = NMonitoring::EncoderPrometheus(&out); } else { ythrow yexception() << "unsupported metric encoding format: " << formatStr; } diff --git a/library/cpp/monlib/service/pages/registry_mon_page.h b/library/cpp/monlib/service/pages/registry_mon_page.h index 2d26d3319c..f76017c3a7 100644 --- a/library/cpp/monlib/service/pages/registry_mon_page.h +++ b/library/cpp/monlib/service/pages/registry_mon_page.h @@ -5,7 +5,7 @@ #include <library/cpp/monlib/metrics/metric_registry.h> namespace NMonitoring { - // For now this class can only enumerate all metrics without any grouping or serve JSON/Spack/Prometheus + // For now this class can only enumerate all metrics without any grouping or serve JSON/Spack/Prometheus class TMetricRegistryPage: public TPreMonPage { public: TMetricRegistryPage(const TString& path, const TString& title, TAtomicSharedPtr<IMetricSupplier> registry) diff --git a/library/cpp/monlib/service/pages/ya.make b/library/cpp/monlib/service/pages/ya.make index 48d44a0838..6af26f73f4 100644 --- a/library/cpp/monlib/service/pages/ya.make +++ b/library/cpp/monlib/service/pages/ya.make @@ -25,7 +25,7 @@ PEERDIR( library/cpp/monlib/encode/json library/cpp/monlib/encode/text library/cpp/monlib/encode/spack - library/cpp/monlib/encode/prometheus + library/cpp/monlib/encode/prometheus ) END() diff --git a/library/cpp/monlib/service/service.cpp b/library/cpp/monlib/service/service.cpp index 929efbf816..8571549b14 100644 --- a/library/cpp/monlib/service/service.cpp +++ b/library/cpp/monlib/service/service.cpp @@ -47,10 +47,10 @@ namespace NMonitoring { Handler(out, *this); out.Finish(); } catch (...) { - auto msg = CurrentExceptionMessage(); - out << "HTTP/1.1 500 Internal server error\r\nConnection: Close\r\n\r\n" << msg; - out.Finish(); - YSYSLOG(TLOG_ERR, "THttpClient: error while serving monitoring request: %s", msg.data()); + auto msg = CurrentExceptionMessage(); + out << "HTTP/1.1 500 Internal server error\r\nConnection: Close\r\n\r\n" << msg; + out.Finish(); + YSYSLOG(TLOG_ERR, "THttpClient: error while serving monitoring request: %s", msg.data()); } } diff --git a/library/cpp/object_factory/object_factory.h b/library/cpp/object_factory/object_factory.h index 96cc11bcfd..a6183dab48 100644 --- a/library/cpp/object_factory/object_factory.h +++ b/library/cpp/object_factory/object_factory.h @@ -1,12 +1,12 @@ -#pragma once - -#include <util/system/guard.h> +#pragma once + +#include <util/system/guard.h> #include <util/system/rwlock.h> -#include <util/generic/map.h> -#include <util/generic/set.h> +#include <util/generic/map.h> +#include <util/generic/set.h> #include <util/generic/singleton.h> #include <util/generic/yexception.h> - + namespace NObjectFactory { template <class TProduct, class... TArgs> class IFactoryObjectCreator { @@ -62,7 +62,7 @@ namespace NObjectFactory { if (!Creators.insert(typename ICreators::value_type(key, creator)).second) ythrow yexception() << "Product with key " << key << " already registered"; } - + template <class TDerivedProduct> void Register(const TKey& key) { Register<TDerivedProduct>(key, new TFactoryObjectCreator<TProduct, TDerivedProduct, TArgs...>); @@ -74,8 +74,8 @@ namespace NObjectFactory { for (typename ICreators::const_iterator i = Creators.begin(), e = Creators.end(); i != e; ++i) { keys.insert(i->first); } - } - + } + protected: IFactoryObjectCreator<TProduct, TArgs...>* GetCreator(const TKey& key) const { TReadGuard guard(CreatorsLock); @@ -94,7 +94,7 @@ namespace NObjectFactory { ICreators Creators; TRWMutex CreatorsLock; }; - + template <class TProduct, class TKey> class TObjectFactory: public IObjectFactory<TProduct, TKey, void> { public: @@ -132,11 +132,11 @@ namespace NObjectFactory { return result; } - template<class... Args> - static THolder<TProduct> MakeHolder(Args&&... args) { + template<class... Args> + static THolder<TProduct> MakeHolder(Args&&... args) { return THolder<TProduct>(Construct(std::forward<Args>(args)...)); - } - + } + static bool Has(const TKey& key) { return Singleton<TObjectFactory<TProduct, TKey>>()->HasImpl(key); } @@ -144,7 +144,7 @@ namespace NObjectFactory { static void GetRegisteredKeys(TSet<TKey>& keys) { return Singleton<TObjectFactory<TProduct, TKey>>()->GetKeys(keys); } - + static TSet<TKey> GetRegisteredKeys() { TSet<TKey> keys; Singleton<TObjectFactory<TProduct, TKey>>()->GetKeys(keys); @@ -182,16 +182,16 @@ namespace NObjectFactory { template <class TProduct, class TKey, class... TArgs> class TParametrizedObjectFactory: public IObjectFactory<TProduct, TKey, TArgs...> { - public: + public: TProduct* Create(const TKey& key, TArgs... args) const { IFactoryObjectCreator<TProduct, TArgs...>* creator = IObjectFactory<TProduct, TKey, TArgs...>::GetCreator(key); return creator == nullptr ? nullptr : creator->Create(std::forward<TArgs>(args)...); - } + } static bool Has(const TKey& key) { return Singleton<TParametrizedObjectFactory<TProduct, TKey, TArgs...>>()->HasImpl(key); } - + static TProduct* Construct(const TKey& key, TArgs... args) { return Singleton<TParametrizedObjectFactory<TProduct, TKey, TArgs...>>()->Create(key, std::forward<TArgs>(args)...); } @@ -203,11 +203,11 @@ namespace NObjectFactory { return result; } - template<class... Args> - static THolder<TProduct> MakeHolder(Args&&... args) { - return THolder<TProduct>(Construct(std::forward<Args>(args)...)); - } - + template<class... Args> + static THolder<TProduct> MakeHolder(Args&&... args) { + return THolder<TProduct>(Construct(std::forward<Args>(args)...)); + } + static void GetRegisteredKeys(TSet<TKey>& keys) { return Singleton<TParametrizedObjectFactory<TProduct, TKey, TArgs...>>()->GetKeys(keys); } @@ -238,6 +238,6 @@ namespace NObjectFactory { return Product::GetTypeName(); } }; - }; + }; } diff --git a/library/cpp/protobuf/json/proto2json.h b/library/cpp/protobuf/json/proto2json.h index 89a1781a40..71daeeb4ab 100644 --- a/library/cpp/protobuf/json/proto2json.h +++ b/library/cpp/protobuf/json/proto2json.h @@ -9,7 +9,7 @@ #include <util/generic/fwd.h> #include <util/generic/vector.h> -#include <util/generic/yexception.h> +#include <util/generic/yexception.h> #include <util/stream/str.h> #include <functional> diff --git a/library/cpp/protobuf/json/ut/proto2json_ut.cpp b/library/cpp/protobuf/json/ut/proto2json_ut.cpp index 07e52d7f2f..0a2f5652c0 100644 --- a/library/cpp/protobuf/json/ut/proto2json_ut.cpp +++ b/library/cpp/protobuf/json/ut/proto2json_ut.cpp @@ -27,12 +27,12 @@ using namespace NProtobufJsonTest; Y_UNIT_TEST_SUITE(TProto2JsonFlatTest) { Y_UNIT_TEST(TestFlatDefault) { - using namespace ::google::protobuf; - TFlatDefault proto; - NJson::TJsonValue json; - TProto2JsonConfig cfg; - cfg.SetMissingSingleKeyMode(TProto2JsonConfig::MissingKeyDefault); - UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, json, cfg)); + using namespace ::google::protobuf; + TFlatDefault proto; + NJson::TJsonValue json; + TProto2JsonConfig cfg; + cfg.SetMissingSingleKeyMode(TProto2JsonConfig::MissingKeyDefault); + UNIT_ASSERT_NO_EXCEPTION(Proto2Json(proto, json, cfg)); #define DEFINE_FIELD(name, value) \ { \ auto descr = proto.GetMetadata().descriptor->FindFieldByName(#name); \ @@ -71,9 +71,9 @@ Y_UNIT_TEST_SUITE(TProto2JsonFlatTest) { } \ } #include <library/cpp/protobuf/json/ut/fields.incl> -#undef DEFINE_FIELD - } - +#undef DEFINE_FIELD + } + Y_UNIT_TEST(TestNameGenerator) { TNameGeneratorType proto; proto.SetField(42); diff --git a/library/cpp/protobuf/json/ut/test.proto b/library/cpp/protobuf/json/ut/test.proto index 0fa996fd41..0a207232c6 100644 --- a/library/cpp/protobuf/json/ut/test.proto +++ b/library/cpp/protobuf/json/ut/test.proto @@ -91,34 +91,34 @@ message TFlatRepeated { repeated string UserID = 20; }; -message TFlatDefault { - optional int32 I32 = 1 [default = 132]; - optional int64 I64 = 2 [default = 164]; - optional uint32 UI32 = 3 [default = 232]; - optional uint64 UI64 = 4 [default = 264]; - optional sint32 SI32 = 5 [default = 332]; - optional sint64 SI64 = 6 [default = 364]; - optional fixed32 FI32 = 7 [default = 432]; - optional fixed64 FI64 = 8 [default = 464]; - optional sfixed32 SFI32 = 9 [default = 532]; - optional sfixed64 SFI64 = 10 [default = 564]; - - optional bool Bool = 11 [default = true]; - - optional string String = 12 [default = "string"]; - optional bytes Bytes = 13 [default = "bytes"]; - - optional EEnum Enum = 14 [default = E_2]; - - optional float Float = 15 [default = 0.123]; - optional double Double = 16 [default = 0.456]; +message TFlatDefault { + optional int32 I32 = 1 [default = 132]; + optional int64 I64 = 2 [default = 164]; + optional uint32 UI32 = 3 [default = 232]; + optional uint64 UI64 = 4 [default = 264]; + optional sint32 SI32 = 5 [default = 332]; + optional sint64 SI64 = 6 [default = 364]; + optional fixed32 FI32 = 7 [default = 432]; + optional fixed64 FI64 = 8 [default = 464]; + optional sfixed32 SFI32 = 9 [default = 532]; + optional sfixed64 SFI64 = 10 [default = 564]; + + optional bool Bool = 11 [default = true]; + + optional string String = 12 [default = "string"]; + optional bytes Bytes = 13 [default = "bytes"]; + + optional EEnum Enum = 14 [default = E_2]; + + optional float Float = 15 [default = 0.123]; + optional double Double = 16 [default = 0.456]; optional string OneString = 17 [default = "string"]; optional string OneTwoString = 18 [default = "string"]; optional string ABC = 19 [default = "abc"]; optional string UserID = 20 [default = "some_id"]; -}; - +}; + message TCompositeOptional { optional TFlatOptional Part = 1; }; diff --git a/library/cpp/testing/unittest/gtest.cpp b/library/cpp/testing/unittest/gtest.cpp index ebad1ea4d6..e064938f13 100644 --- a/library/cpp/testing/unittest/gtest.cpp +++ b/library/cpp/testing/unittest/gtest.cpp @@ -46,7 +46,7 @@ namespace { return new TCurrentTest(Test); } - void AddTest(const char* name, void (*body)(TTestContext&), bool forceFork) override { + void AddTest(const char* name, void (*body)(TTestContext&), bool forceFork) override { Test.Tests.push_back(TBaseTestCase(name, body, forceFork)); } diff --git a/library/cpp/testing/unittest/gtest.h b/library/cpp/testing/unittest/gtest.h index b6768b1bf0..7dc5eac4a2 100644 --- a/library/cpp/testing/unittest/gtest.h +++ b/library/cpp/testing/unittest/gtest.h @@ -15,7 +15,7 @@ namespace NUnitTest { struct IGTestFactory: public ITestBaseFactory { ~IGTestFactory() override; - virtual void AddTest(const char* name, void (*body)(TTestContext&), bool forceFork) = 0; + virtual void AddTest(const char* name, void (*body)(TTestContext&), bool forceFork) = 0; }; IGTestFactory* ByName(const char* name); @@ -42,7 +42,7 @@ namespace testing { } #define TEST_IMPL(N, NN, FF) \ - void Test##N##NN(NUnitTest::TTestContext&); \ + void Test##N##NN(NUnitTest::TTestContext&); \ namespace NTestSuite##N##NN { \ struct TReg { \ inline TReg() { \ @@ -51,7 +51,7 @@ namespace testing { }; \ static TReg reg; \ } \ - void Test##N##NN(NUnitTest::TTestContext&) + void Test##N##NN(NUnitTest::TTestContext&) #define TEST_F_IMPL(N, NN, FF) \ namespace NTestSuite##N##NN { \ diff --git a/library/cpp/testing/unittest/registar.h b/library/cpp/testing/unittest/registar.h index 44517a0092..dd62a5110b 100644 --- a/library/cpp/testing/unittest/registar.h +++ b/library/cpp/testing/unittest/registar.h @@ -62,7 +62,7 @@ namespace NUnitTest { class ITestSuiteProcessor; - struct TTestContext { + struct TTestContext { TTestContext() : Processor(nullptr) { @@ -74,11 +74,11 @@ namespace NUnitTest { } using TMetrics = THashMap<TString, double>; - TMetrics Metrics; + TMetrics Metrics; ITestSuiteProcessor* Processor; - }; - + }; + class ITestSuiteProcessor { public: struct TUnit { @@ -285,8 +285,8 @@ private: \ #define CATCH_REACTION(FN, e, context) this->AddError(("(" + TypeName(e) + ") " + e.what()).data(), context) #define CATCH_REACTION_BT(FN, e, context) this->AddError(("(" + TypeName(e) + ") " + e.what()).data(), (e.BackTrace() ? e.BackTrace()->PrintToString() : TString()), context) #else -#define CATCH_REACTION(FN, e, context) throw -#define CATCH_REACTION_BT(FN, e, context) throw +#define CATCH_REACTION(FN, e, context) throw +#define CATCH_REACTION_BT(FN, e, context) throw #endif #define UNIT_TEST_CHECK_TEST_IS_DECLARED_ONLY_ONCE(F) \ @@ -334,7 +334,7 @@ private: \ if (this->GetForkTests() && !this->GetIsForked()) { \ UNIT_TEST_IMPL(F, false); \ /* forked process (or main without "--fork-tests") treats some exceptions as success - it's exception test! */ \ - } else { \ + } else { \ NUnitTest::TTestContext context(this->TTestBase::Processor()); \ if (this->CheckAccessTest((#F))) { \ try { \ @@ -350,9 +350,9 @@ private: \ } catch (...) { \ this->AddError("non-std exception!", &context); \ } \ - this->Finish((#F), &context); \ - } \ - } + this->Finish((#F), &context); \ + } \ + } #define UNIT_TEST_SUITE_END() \ this->AtEnd(); \ @@ -753,7 +753,7 @@ public: \ #define UNIT_ASSERT_TEST_FAILS(A) UNIT_ASSERT_TEST_FAILS_C(A, "") #define UNIT_ADD_METRIC(name, value) ut_context.Metrics[name] = value - + class TTestFactory { friend class TTestBase; friend class ITestBaseFactory; @@ -1010,7 +1010,7 @@ public: \ #define Y_UNIT_TEST_IMPL(N, FF, F) \ Y_UNIT_TEST_IMPL_REGISTER(N, FF, F) \ void TTestCase##N::Execute_(NUnitTest::TTestContext& ut_context Y_DECLARE_UNUSED) - + #define Y_UNIT_TEST(N) Y_UNIT_TEST_IMPL(N, false, TCurrentTestCase) #define Y_UNIT_TEST_F(N, F) Y_UNIT_TEST_IMPL(N, false, F) #define SIMPLE_UNIT_FORKED_TEST(N) Y_UNIT_TEST_IMPL(N, true, TCurrentTestCase) diff --git a/library/cpp/testing/unittest/utmain.cpp b/library/cpp/testing/unittest/utmain.cpp index 305bc6b40f..2c1dcc4208 100644 --- a/library/cpp/testing/unittest/utmain.cpp +++ b/library/cpp/testing/unittest/utmain.cpp @@ -89,11 +89,11 @@ private: event.InsertValue("status", status); event.InsertValue("comment", comment.data()); event.InsertValue("time", (now - PrevTime).SecondsFloat()); - if (context) { - for (const auto& metric : context->Metrics) { - event["metrics"].InsertValue(metric.first, metric.second); - } - } + if (context) { + for (const auto& metric : context->Metrics) { + event["metrics"].InsertValue(metric.first, metric.second); + } + } Trace("subtest-finished", event); PrevTime = now; diff --git a/util/folder/dirut.cpp b/util/folder/dirut.cpp index ffc9b09f96..8922884136 100644 --- a/util/folder/dirut.cpp +++ b/util/folder/dirut.cpp @@ -222,22 +222,22 @@ int correctpath(char* cpath, const char* path) { } char* ptr = (char*)path; char* cptr = cpath; - int counter = 0; - while (*ptr) { - char c = *ptr; - if (c == '/') - c = '\\'; - if (c == '\\') + int counter = 0; + while (*ptr) { + char c = *ptr; + if (c == '/') + c = '\\'; + if (c == '\\') ++counter; - else - counter = 0; - if (counter <= 1) { - *cptr = c; + else + counter = 0; + if (counter <= 1) { + *cptr = c; ++cptr; - } + } ++ptr; - } - *cptr = 0; + } + *cptr = 0; // replace '/' by '\' int dk = skip_disk(cpath); diff --git a/util/folder/fts.cpp b/util/folder/fts.cpp index 0e6a6f86eb..906f987084 100644 --- a/util/folder/fts.cpp +++ b/util/folder/fts.cpp @@ -37,7 +37,7 @@ #include <util/system/compat.h> #include <util/system/compiler.h> #include <util/system/defaults.h> -#include <util/system/error.h> +#include <util/system/error.h> #include <stdlib.h> #ifndef _win_ @@ -465,7 +465,7 @@ yfts_read(FTS* sp) { char* t; int saved_errno; - ClearLastSystemError(); + ClearLastSystemError(); /* If finished or unrecoverable error, return NULL. */ if (sp->fts_cur == nullptr || ISSET(FTS_STOP)) { diff --git a/util/generic/guid.cpp b/util/generic/guid.cpp index 8b907457bc..11434ed80b 100644 --- a/util/generic/guid.cpp +++ b/util/generic/guid.cpp @@ -3,44 +3,44 @@ #include "string.h" #include <util/string/ascii.h> -#include <util/string/builder.h> +#include <util/string/builder.h> #include <util/stream/format.h> #include <util/system/unaligned_mem.h> #include <util/random/easy.h> -namespace { - inline void LowerCaseHex(TString& s) { - for (auto&& c : s) { - c = AsciiToLower(c); - } - } -} - -TString TGUID::AsGuidString() const { - TStringBuilder s; - s.reserve(50); - s << Hex(dw[0], 0) << '-' << Hex(dw[1], 0) << '-' << Hex(dw[2], 0) << '-' << Hex(dw[3], 0); - LowerCaseHex(s); - return std::move(s); -} - -TString TGUID::AsUuidString() const { - TStringBuilder s; - s.reserve(50); - s << Hex(dw[0], HF_FULL) << '-'; - s << Hex(static_cast<ui16>(dw[1] >> 16), HF_FULL) << '-' << Hex(static_cast<ui16>(dw[1]), HF_FULL) << '-'; - s << Hex(static_cast<ui16>(dw[2] >> 16), HF_FULL) << '-' << Hex(static_cast<ui16>(dw[2]), HF_FULL); - s << Hex(dw[3], HF_FULL); - LowerCaseHex(s); - return std::move(s); -} - -TGUID TGUID::Create() { - TGUID result; - CreateGuid(&result); - return result; -} - +namespace { + inline void LowerCaseHex(TString& s) { + for (auto&& c : s) { + c = AsciiToLower(c); + } + } +} + +TString TGUID::AsGuidString() const { + TStringBuilder s; + s.reserve(50); + s << Hex(dw[0], 0) << '-' << Hex(dw[1], 0) << '-' << Hex(dw[2], 0) << '-' << Hex(dw[3], 0); + LowerCaseHex(s); + return std::move(s); +} + +TString TGUID::AsUuidString() const { + TStringBuilder s; + s.reserve(50); + s << Hex(dw[0], HF_FULL) << '-'; + s << Hex(static_cast<ui16>(dw[1] >> 16), HF_FULL) << '-' << Hex(static_cast<ui16>(dw[1]), HF_FULL) << '-'; + s << Hex(static_cast<ui16>(dw[2] >> 16), HF_FULL) << '-' << Hex(static_cast<ui16>(dw[2]), HF_FULL); + s << Hex(dw[3], HF_FULL); + LowerCaseHex(s); + return std::move(s); +} + +TGUID TGUID::Create() { + TGUID result; + CreateGuid(&result); + return result; +} + void CreateGuid(TGUID* res) { ui64* dw = reinterpret_cast<ui64*>(res->dw); @@ -66,11 +66,11 @@ TGUID TGUID::CreateTimebased() { } TString GetGuidAsString(const TGUID& g) { - return g.AsGuidString(); + return g.AsGuidString(); } TString CreateGuidAsString() { - return TGUID::Create().AsGuidString(); + return TGUID::Create().AsGuidString(); } static bool GetDigit(const char c, ui32& digit) { diff --git a/util/generic/guid.h b/util/generic/guid.h index 2bf6c8ad99..d6259a8942 100644 --- a/util/generic/guid.h +++ b/util/generic/guid.h @@ -24,18 +24,18 @@ struct TGUID { constexpr explicit operator bool() const noexcept { return !IsEmpty(); } - - // xxxx-xxxx-xxxx-xxxx - TString AsGuidString() const; - - /** - * RFC4122 GUID, which described in - * https://en.wikipedia.org/wiki/Universally_unique_identifier - * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx - **/ - TString AsUuidString() const; - - static TGUID Create(); + + // xxxx-xxxx-xxxx-xxxx + TString AsGuidString() const; + + /** + * RFC4122 GUID, which described in + * https://en.wikipedia.org/wiki/Universally_unique_identifier + * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx + **/ + TString AsUuidString() const; + + static TGUID Create(); /** * Generate time based UUID version 1 RFC4122 GUID diff --git a/util/generic/guid_ut.cpp b/util/generic/guid_ut.cpp index 048354ff39..fdcb723ee8 100644 --- a/util/generic/guid_ut.cpp +++ b/util/generic/guid_ut.cpp @@ -94,31 +94,31 @@ Y_UNIT_TEST_SUITE(TGuidTest) { UNIT_ASSERT_EQUAL(t.G, GetUuid(t.S)); } } - - Y_UNIT_TEST(DoubleConvert) { - /** - * test print and parsing RFC4122 GUID, which described in - * https://en.wikipedia.org/wiki/Universally_unique_identifier - * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx - **/ - auto guid = TGUID::Create(); - auto printed = guid.AsUuidString(); - - TGUID read; - UNIT_ASSERT(GetUuid(printed, read)); - - UNIT_ASSERT_VALUES_EQUAL(guid.dw[0], read.dw[0]); - UNIT_ASSERT_VALUES_EQUAL(guid.dw[1], read.dw[1]); - UNIT_ASSERT_VALUES_EQUAL(guid.dw[2], read.dw[2]); - UNIT_ASSERT_VALUES_EQUAL(guid.dw[3], read.dw[3]); - } - - Y_UNIT_TEST(OutputFormat) { - TGUID guid = Construct(0x00005612U, 0x12000000U, 0x00000123U, 0x00000000U); - - UNIT_ASSERT_VALUES_EQUAL(guid.AsGuidString(), "5612-12000000-123-0"); - UNIT_ASSERT_VALUES_EQUAL(guid.AsUuidString(), "00005612-1200-0000-0000-012300000000"); - } + + Y_UNIT_TEST(DoubleConvert) { + /** + * test print and parsing RFC4122 GUID, which described in + * https://en.wikipedia.org/wiki/Universally_unique_identifier + * xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx + **/ + auto guid = TGUID::Create(); + auto printed = guid.AsUuidString(); + + TGUID read; + UNIT_ASSERT(GetUuid(printed, read)); + + UNIT_ASSERT_VALUES_EQUAL(guid.dw[0], read.dw[0]); + UNIT_ASSERT_VALUES_EQUAL(guid.dw[1], read.dw[1]); + UNIT_ASSERT_VALUES_EQUAL(guid.dw[2], read.dw[2]); + UNIT_ASSERT_VALUES_EQUAL(guid.dw[3], read.dw[3]); + } + + Y_UNIT_TEST(OutputFormat) { + TGUID guid = Construct(0x00005612U, 0x12000000U, 0x00000123U, 0x00000000U); + + UNIT_ASSERT_VALUES_EQUAL(guid.AsGuidString(), "5612-12000000-123-0"); + UNIT_ASSERT_VALUES_EQUAL(guid.AsUuidString(), "00005612-1200-0000-0000-012300000000"); + } Y_UNIT_TEST(TimeBased) { TString guid = TGUID::CreateTimebased().AsUuidString(); diff --git a/util/stream/buffer.cpp b/util/stream/buffer.cpp index 2facece4ea..bc2159dff8 100644 --- a/util/stream/buffer.cpp +++ b/util/stream/buffer.cpp @@ -113,8 +113,8 @@ size_t TBufferInput::DoNext(const void** ptr, size_t len) { Readed_ += len; return len; } - -void TBufferInput::DoUndo(size_t len) { - Y_VERIFY(len <= Readed_); - Readed_ -= len; -} + +void TBufferInput::DoUndo(size_t len) { + Y_VERIFY(len <= Readed_); + Readed_ -= len; +} diff --git a/util/stream/buffer.h b/util/stream/buffer.h index 9dc99dbe49..c77960bcb6 100644 --- a/util/stream/buffer.h +++ b/util/stream/buffer.h @@ -77,7 +77,7 @@ public: protected: size_t DoNext(const void** ptr, size_t len) override; - void DoUndo(size_t len) override; + void DoUndo(size_t len) override; private: const TBuffer& Buf_; diff --git a/util/stream/buffered.cpp b/util/stream/buffered.cpp index a00e592e1c..de4ec8c756 100644 --- a/util/stream/buffered.cpp +++ b/util/stream/buffered.cpp @@ -35,7 +35,7 @@ public: return MemInput_.Read(buf, len); } - inline size_t Skip(size_t len) { + inline size_t Skip(size_t len) { size_t totalSkipped = 0; while (len) { const size_t skipped = DoSkip(len); @@ -45,11 +45,11 @@ public: totalSkipped += skipped; len -= skipped; - } + } return totalSkipped; - } - + } + inline size_t DoSkip(size_t len) { if (MemInput_.Exhausted()) { if (len > BufLen() / 2) { @@ -131,10 +131,10 @@ size_t TBufferedInput::DoRead(void* buf, size_t len) { return Impl_->Read(buf, len); } -size_t TBufferedInput::DoSkip(size_t len) { - return Impl_->Skip(len); -} - +size_t TBufferedInput::DoSkip(size_t len) { + return Impl_->Skip(len); +} + size_t TBufferedInput::DoNext(const void** ptr, size_t len) { return Impl_->Next(ptr, len); } diff --git a/util/stream/buffered_ut.cpp b/util/stream/buffered_ut.cpp index 41d2fc3030..62a32776de 100644 --- a/util/stream/buffered_ut.cpp +++ b/util/stream/buffered_ut.cpp @@ -59,7 +59,7 @@ Y_UNIT_TEST_SUITE(TestBufferedIO) { UNIT_ASSERT_VALUES_EQUAL(s, "112"); } - + Y_UNIT_TEST(Test4) { TString s; @@ -109,24 +109,24 @@ Y_UNIT_TEST_SUITE(TestBufferedIO) { Y_UNIT_TEST(TestInput) { TString s("0123456789abcdefghijklmn"); - TBuffered<TStringInput> in(5, s); - char c; + TBuffered<TStringInput> in(5, s); + char c; UNIT_ASSERT_VALUES_EQUAL(in.Read(&c, 1), 1); //1 - UNIT_ASSERT_VALUES_EQUAL(c, '0'); + UNIT_ASSERT_VALUES_EQUAL(c, '0'); UNIT_ASSERT_VALUES_EQUAL(in.Skip(4), 4); //5 end of buffer UNIT_ASSERT_VALUES_EQUAL(in.Read(&c, 1), 1); //6 - UNIT_ASSERT_VALUES_EQUAL(c, '5'); + UNIT_ASSERT_VALUES_EQUAL(c, '5'); UNIT_ASSERT_VALUES_EQUAL(in.Skip(3), 3); //9 UNIT_ASSERT_VALUES_EQUAL(in.Read(&c, 1), 1); //10 end of buffer - UNIT_ASSERT_VALUES_EQUAL(c, '9'); + UNIT_ASSERT_VALUES_EQUAL(c, '9'); UNIT_ASSERT_VALUES_EQUAL(in.Skip(3), 3); //13 UNIT_ASSERT_VALUES_EQUAL(in.Read(&c, 1), 1); //14 start new buffer - UNIT_ASSERT_VALUES_EQUAL(c, 'd'); + UNIT_ASSERT_VALUES_EQUAL(c, 'd'); UNIT_ASSERT_VALUES_EQUAL(in.Skip(6), 6); //20 UNIT_ASSERT_VALUES_EQUAL(in.Read(&c, 1), 1); //21 start new buffer - UNIT_ASSERT_VALUES_EQUAL(c, 'k'); + UNIT_ASSERT_VALUES_EQUAL(c, 'k'); UNIT_ASSERT_VALUES_EQUAL(in.Skip(6), 3); //24 eof - } + } Y_UNIT_TEST(TestReadTo) { TString s("0123456789abc"); diff --git a/util/stream/direct_io.cpp b/util/stream/direct_io.cpp index 649033af34..5aab0830e1 100644 --- a/util/stream/direct_io.cpp +++ b/util/stream/direct_io.cpp @@ -1,47 +1,47 @@ -#include "direct_io.h" - +#include "direct_io.h" + #include <util/generic/utility.h> -size_t TRandomAccessFileInput::DoRead(void* buf, size_t len) { - const size_t result = File.Pread(buf, len, Position); - Position += result; - return result; -} - -TRandomAccessFileInput::TRandomAccessFileInput(TDirectIOBufferedFile& file, ui64 position) - : File(file) - , Position(position) -{ -} - -size_t TRandomAccessFileInput::DoSkip(size_t len) { +size_t TRandomAccessFileInput::DoRead(void* buf, size_t len) { + const size_t result = File.Pread(buf, len, Position); + Position += result; + return result; +} + +TRandomAccessFileInput::TRandomAccessFileInput(TDirectIOBufferedFile& file, ui64 position) + : File(file) + , Position(position) +{ +} + +size_t TRandomAccessFileInput::DoSkip(size_t len) { size_t skiped = Min(len, (size_t)Min((ui64)Max<size_t>(), File.GetLength() - Position)); - Position += skiped; - return skiped; -} - -TRandomAccessFileOutput::TRandomAccessFileOutput(TDirectIOBufferedFile& file) + Position += skiped; + return skiped; +} + +TRandomAccessFileOutput::TRandomAccessFileOutput(TDirectIOBufferedFile& file) : File(&file) -{ -} - -void TRandomAccessFileOutput::DoWrite(const void* buf, size_t len) { +{ +} + +void TRandomAccessFileOutput::DoWrite(const void* buf, size_t len) { File->Write(buf, len); -} - -void TRandomAccessFileOutput::DoFlush() { +} + +void TRandomAccessFileOutput::DoFlush() { File->FlushData(); -} - +} + TBufferedFileOutputEx::TBufferedFileOutputEx(const TString& path, EOpenMode oMode, size_t buflen) - : TRandomAccessFileOutput(*(new TDirectIOBufferedFile(path, oMode, buflen))) + : TRandomAccessFileOutput(*(new TDirectIOBufferedFile(path, oMode, buflen))) , FileHolder(File) -{ -} - -void TBufferedFileOutputEx::DoFinish() { - FileHolder->Finish(); -} - -void TBufferedFileOutputEx::DoFlush() { -} +{ +} + +void TBufferedFileOutputEx::DoFinish() { + FileHolder->Finish(); +} + +void TBufferedFileOutputEx::DoFlush() { +} diff --git a/util/stream/direct_io.h b/util/stream/direct_io.h index 2e1f2e07dd..0c55f15a95 100644 --- a/util/stream/direct_io.h +++ b/util/stream/direct_io.h @@ -1,43 +1,43 @@ -#pragma once - -#include "input.h" -#include "output.h" -#include <util/system/direct_io.h> - +#pragma once + +#include "input.h" +#include "output.h" +#include <util/system/direct_io.h> + class TRandomAccessFileInput: public IInputStream { -public: - TRandomAccessFileInput(TDirectIOBufferedFile& file, ui64 position); - -protected: +public: + TRandomAccessFileInput(TDirectIOBufferedFile& file, ui64 position); + +protected: size_t DoRead(void* buf, size_t len) override; size_t DoSkip(size_t len) override; - -private: - TDirectIOBufferedFile& File; - ui64 Position; -}; - + +private: + TDirectIOBufferedFile& File; + ui64 Position; +}; + class TRandomAccessFileOutput: public IOutputStream { -public: - TRandomAccessFileOutput(TDirectIOBufferedFile& file); - +public: + TRandomAccessFileOutput(TDirectIOBufferedFile& file); + TRandomAccessFileOutput(TRandomAccessFileOutput&&) noexcept = default; TRandomAccessFileOutput& operator=(TRandomAccessFileOutput&&) noexcept = default; -protected: +protected: TDirectIOBufferedFile* File; - -private: + +private: void DoWrite(const void* buf, size_t len) override; void DoFlush() override; -}; - -class TBufferedFileOutputEx: public TRandomAccessFileOutput { -public: +}; + +class TBufferedFileOutputEx: public TRandomAccessFileOutput { +public: TBufferedFileOutputEx(const TString& path, EOpenMode oMode, size_t buflen = 1 << 17); - -private: + +private: void DoFlush() override; void DoFinish() override; - THolder<TDirectIOBufferedFile> FileHolder; -}; + THolder<TDirectIOBufferedFile> FileHolder; +}; diff --git a/util/stream/direct_io_ut.cpp b/util/stream/direct_io_ut.cpp index 01d09db232..0dbc2454f2 100644 --- a/util/stream/direct_io_ut.cpp +++ b/util/stream/direct_io_ut.cpp @@ -1,12 +1,12 @@ #include <library/cpp/testing/unittest/registar.h> - + #include <util/generic/string.h> #include <util/generic/array_size.h> #include <util/system/env.h> - + #include "buffered.h" #include "direct_io.h" - + Y_UNIT_TEST_SUITE(TDirectIOTests) { // Decrease numBufToWrite further if tests continue to time out static void Y_NO_INLINE Test(EOpenMode mode, size_t numBufToWrite) { @@ -55,8 +55,8 @@ Y_UNIT_TEST_SUITE(TDirectIOTests) { } UNIT_ASSERT_VALUES_EQUAL(bytesRead, numBufToWrite * BUFFER_SIZE); - } - + } + Y_UNIT_TEST(ReadWriteTest) { Test(0, 100 * 32); } @@ -67,5 +67,5 @@ Y_UNIT_TEST_SUITE(TDirectIOTests) { Y_UNIT_TEST(ReadWriteDirectSeqTest) { Test(Direct | Seq, 100 * 4); - } -} + } +} diff --git a/util/stream/ios_ut.cpp b/util/stream/ios_ut.cpp index 139f4296e5..2094295103 100644 --- a/util/stream/ios_ut.cpp +++ b/util/stream/ios_ut.cpp @@ -2,7 +2,7 @@ #include "tokenizer.h" #include "buffer.h" #include "buffered.h" -#include "walk.h" +#include "walk.h" #include <library/cpp/testing/unittest/registar.h> @@ -23,8 +23,8 @@ class TStreamsTest: public TTestBase { UNIT_TEST(TestBufferStream); UNIT_TEST(TestStringStream); UNIT_TEST(TestWtrokaInput); - UNIT_TEST(TestStrokaInput); - UNIT_TEST(TestReadTo); + UNIT_TEST(TestStrokaInput); + UNIT_TEST(TestReadTo); UNIT_TEST(TestWtrokaOutput); UNIT_TEST(TestIStreamOperators); UNIT_TEST(TestWchar16Output); @@ -41,10 +41,10 @@ public: void TestBufferStream(); void TestStringStream(); void TestWtrokaInput(); - void TestStrokaInput(); + void TestStrokaInput(); void TestWtrokaOutput(); void TestIStreamOperators(); - void TestReadTo(); + void TestReadTo(); void TestWchar16Output(); void TestWchar32Output(); void TestUtf16StingOutputByChars(); @@ -280,30 +280,30 @@ void TStreamsTest::TestBufferStream() { namespace { class TStringListInput: public IWalkInput { - public: + public: TStringListInput(const TVector<TString>& data) - : Data_(data) - , Index_(0) - { - } - - protected: - size_t DoUnboundedNext(const void** ptr) override { - if (Index_ >= Data_.size()) { - return 0; - } - + : Data_(data) + , Index_(0) + { + } + + protected: + size_t DoUnboundedNext(const void** ptr) override { + if (Index_ >= Data_.size()) { + return 0; + } + const TString& string = Data_[Index_++]; - - *ptr = string.data(); - return string.size(); - } - - private: + + *ptr = string.data(); + return string.size(); + } + + private: const TVector<TString>& Data_; - size_t Index_; - }; - + size_t Index_; + }; + const char Text[] = // UTF8 encoded "one \ntwo\r\nthree\n\tfour\nfive\n" in russian and ... "один \n" @@ -335,18 +335,18 @@ namespace { TString tmp; input.ReadTo(tmp, 'c'); UNIT_ASSERT_VALUES_EQUAL_C(tmp, "111a222b333", comment); - + char tmp2; input.Read(&tmp2, 1); UNIT_ASSERT_VALUES_EQUAL_C(tmp2, '4', comment); - + input.ReadTo(tmp, '6'); UNIT_ASSERT_VALUES_EQUAL_C(tmp, "44d555e", comment); - + tmp = input.ReadAll(); UNIT_ASSERT_VALUES_EQUAL_C(tmp, "66f", comment); } - + void TestStreamReadTo2(IInputStream& input, const char* comment) { TString s; size_t i = 0; @@ -354,30 +354,30 @@ namespace { UNIT_ASSERT_C(i < Y_ARRAY_SIZE(Expected), comment); UNIT_ASSERT_VALUES_EQUAL_C(s, Expected[i], comment); ++i; - } + } } - + void TestStreamReadTo3(IInputStream& input, const char* comment) { UNIT_ASSERT_VALUES_EQUAL_C(input.ReadLine(), "111a222b333c444d555e666f", comment); } - + void TestStreamReadTo4(IInputStream& input, const char* comment) { UNIT_ASSERT_VALUES_EQUAL_C(input.ReadTo('\0'), "one", comment); UNIT_ASSERT_VALUES_EQUAL_C(input.ReadTo('\0'), "two", comment); UNIT_ASSERT_VALUES_EQUAL_C(input.ReadTo('\0'), "three", comment); } - + void TestStrokaInput(IInputStream& input, const char* comment) { TString line; ui32 i = 0; TInstant start = Now(); while (input.ReadLine(line)) { ++i; - } + } Cout << comment << ":" << (Now() - start).SecondsFloat() << Endl; UNIT_ASSERT_VALUES_EQUAL(i, 100000); } - + template <class T> void TestStreamReadTo(const TString& text, T test) { TStringInput is(text); @@ -396,25 +396,25 @@ namespace { } } -void TStreamsTest::TestReadTo() { - TestStreamReadTo("111a222b333c444d555e666f", TestStreamReadTo1); - TestStreamReadTo(Text, TestStreamReadTo2); - TestStreamReadTo("111a222b333c444d555e666f", TestStreamReadTo3); +void TStreamsTest::TestReadTo() { + TestStreamReadTo("111a222b333c444d555e666f", TestStreamReadTo1); + TestStreamReadTo(Text, TestStreamReadTo2); + TestStreamReadTo("111a222b333c444d555e666f", TestStreamReadTo3); TString withZero = "one"; - withZero.append('\0').append("two").append('\0').append("three"); - TestStreamReadTo(withZero, TestStreamReadTo4); -} - -void TStreamsTest::TestStrokaInput() { + withZero.append('\0').append("two").append('\0').append("three"); + TestStreamReadTo(withZero, TestStreamReadTo4); +} + +void TStreamsTest::TestStrokaInput() { TString s; - for (ui32 i = 0; i < 100000; ++i) { + for (ui32 i = 0; i < 100000; ++i) { TVector<char> d(i % 1000, 'a'); s.append(d.data(), d.size()); - s.append('\n'); - } + s.append('\n'); + } TestStreamReadTo(s, ::TestStrokaInput); -} - +} + void TStreamsTest::TestWtrokaInput() { const TString s(Text); TStringInput is(s); diff --git a/util/stream/mem.cpp b/util/stream/mem.cpp index 22a3339e27..68778a86f2 100644 --- a/util/stream/mem.cpp +++ b/util/stream/mem.cpp @@ -31,7 +31,7 @@ size_t TMemoryInput::DoNext(const void** ptr, size_t len) { return len; } -void TMemoryInput::DoUndo(size_t len) { +void TMemoryInput::DoUndo(size_t len) { Len_ += len; Buf_ -= len; } diff --git a/util/stream/mem.h b/util/stream/mem.h index 18a5d46772..383e49dfd5 100644 --- a/util/stream/mem.h +++ b/util/stream/mem.h @@ -98,7 +98,7 @@ public: private: size_t DoNext(const void** ptr, size_t len) override; - void DoUndo(size_t len) override; + void DoUndo(size_t len) override; private: const char* Buf_; diff --git a/util/stream/str.cpp b/util/stream/str.cpp index 13f0e8ef28..e1b6e72d67 100644 --- a/util/stream/str.cpp +++ b/util/stream/str.cpp @@ -11,11 +11,11 @@ size_t TStringInput::DoNext(const void** ptr, size_t len) { return len; } -void TStringInput::DoUndo(size_t len) { - Y_VERIFY(len <= Pos_); - Pos_ -= len; -} - +void TStringInput::DoUndo(size_t len) { + Y_VERIFY(len <= Pos_); + Pos_ -= len; +} + TStringOutput::~TStringOutput() = default; size_t TStringOutput::DoNext(void** ptr) { diff --git a/util/stream/str.h b/util/stream/str.h index 028bd572c0..89f42c1fd7 100644 --- a/util/stream/str.h +++ b/util/stream/str.h @@ -49,7 +49,7 @@ public: protected: size_t DoNext(const void** ptr, size_t len) override; - void DoUndo(size_t len) override; + void DoUndo(size_t len) override; private: const TString* S_; diff --git a/util/stream/walk.cpp b/util/stream/walk.cpp index 57dc9ab036..47a6cae5f8 100644 --- a/util/stream/walk.cpp +++ b/util/stream/walk.cpp @@ -3,8 +3,8 @@ #include <util/generic/string.h> void IWalkInput::DoUndo(size_t len) { - Len_ += len; - Buf_ = static_cast<const char*>(Buf_) - len; + Len_ += len; + Buf_ = static_cast<const char*>(Buf_) - len; } size_t IWalkInput::DoNext(const void** ptr, size_t len) { diff --git a/util/stream/walk.h b/util/stream/walk.h index 7e62cb44dc..9683943331 100644 --- a/util/stream/walk.h +++ b/util/stream/walk.h @@ -16,7 +16,7 @@ public: } protected: - void DoUndo(size_t len) override; + void DoUndo(size_t len) override; size_t DoNext(const void** ptr, size_t len) override; /** diff --git a/util/stream/zerocopy.cpp b/util/stream/zerocopy.cpp index dc2982ad55..b5f8e58b70 100644 --- a/util/stream/zerocopy.cpp +++ b/util/stream/zerocopy.cpp @@ -31,30 +31,30 @@ size_t IZeroCopyInput::DoSkip(size_t len) { return DoNext(&ptr, len); } - + IZeroCopyInputFastReadTo::~IZeroCopyInputFastReadTo() = default; - + size_t IZeroCopyInputFastReadTo::DoReadTo(TString& st, char ch) { - const char* ptr; - size_t len = Next(&ptr); - if (!len) { - return 0; - } - size_t result = 0; - st.clear(); - do { - if (const char* pos = (const char*)memchr(ptr, ch, len)) { + const char* ptr; + size_t len = Next(&ptr); + if (!len) { + return 0; + } + size_t result = 0; + st.clear(); + do { + if (const char* pos = (const char*)memchr(ptr, ch, len)) { size_t bytesRead = (pos - ptr) + 1; if (bytesRead > 1) { - st.append(ptr, pos); - } + st.append(ptr, pos); + } Undo(len - bytesRead); result += bytesRead; - return result; - } else { - result += len; - st.append(ptr, len); - } - } while (len = Next(&ptr)); - return result; -} + return result; + } else { + result += len; + st.append(ptr, len); + } + } while (len = Next(&ptr)); + return result; +} diff --git a/util/stream/zerocopy.h b/util/stream/zerocopy.h index 3315aa3a51..d44d5e0d77 100644 --- a/util/stream/zerocopy.h +++ b/util/stream/zerocopy.h @@ -56,36 +56,36 @@ protected: virtual size_t DoNext(const void** ptr, size_t len) = 0; }; -/** -* Input stream with direct access to the input buffer and ability to undo read -* -* Derived classes must implement `DoUndo` method. -*/ +/** +* Input stream with direct access to the input buffer and ability to undo read +* +* Derived classes must implement `DoUndo` method. +*/ class IZeroCopyInputFastReadTo: public IZeroCopyInput { -public: +public: IZeroCopyInputFastReadTo() noexcept = default; ~IZeroCopyInputFastReadTo() override; - + IZeroCopyInputFastReadTo(IZeroCopyInputFastReadTo&&) noexcept = default; IZeroCopyInputFastReadTo& operator=(IZeroCopyInputFastReadTo&&) noexcept = default; -protected: +protected: size_t DoReadTo(TString& st, char ch) override; - -private: - /** - * Undo read. - * - * Note that this function not check if you try undo more that read. In fact Undo used for undo read in last chunk. - * - * @param len[in] Bytes to undo. - */ - inline void Undo(size_t len) { - if (len) { - DoUndo(len); - } - } - virtual void DoUndo(size_t len) = 0; -}; - + +private: + /** + * Undo read. + * + * Note that this function not check if you try undo more that read. In fact Undo used for undo read in last chunk. + * + * @param len[in] Bytes to undo. + */ + inline void Undo(size_t len) { + if (len) { + DoUndo(len); + } + } + virtual void DoUndo(size_t len) = 0; +}; + /** @} */ diff --git a/util/system/atexit.cpp b/util/system/atexit.cpp index 74fb10b6b1..c816bc5fe0 100644 --- a/util/system/atexit.cpp +++ b/util/system/atexit.cpp @@ -35,7 +35,7 @@ namespace { } inline void Finish() noexcept { - AtomicSet(FinishStarted_, 1); + AtomicSet(FinishStarted_, 1); auto guard = Guard(Lock_); @@ -65,13 +65,13 @@ namespace { } } - inline bool FinishStarted() const { - return AtomicGet(FinishStarted_); - } + inline bool FinishStarted() const { + return AtomicGet(FinishStarted_); + } private: TAdaptiveLock Lock_; - TAtomic FinishStarted_; + TAtomic FinishStarted_; TDeque<TFunc> Store_; TPriorityQueue<TFunc*, TVector<TFunc*>, TCmp> Items_; }; @@ -108,13 +108,13 @@ void ManualRunAtExitFinalizers() { OnExit(); } -bool ExitStarted() { +bool ExitStarted() { if (TAtExit* const atExit = AtomicGet(atExitPtr)) { return atExit->FinishStarted(); - } + } return false; -} - +} + void AtExit(TAtExitFunc func, void* ctx, size_t priority) { Instance()->Register(func, ctx, priority); } diff --git a/util/system/atexit.h b/util/system/atexit.h index eb3188615c..f70c2fcd8c 100644 --- a/util/system/atexit.h +++ b/util/system/atexit.h @@ -10,8 +10,8 @@ void AtExit(TAtExitFunc func, void* ctx, size_t priority); void AtExit(TTraditionalAtExitFunc func); void AtExit(TTraditionalAtExitFunc func, size_t priority); - -bool ExitStarted(); + +bool ExitStarted(); /** * Generally it's a bad idea to call this method except for some rare cases, diff --git a/util/system/direct_io.cpp b/util/system/direct_io.cpp index f59c54b0cb..3ace0bfc57 100644 --- a/util/system/direct_io.cpp +++ b/util/system/direct_io.cpp @@ -1,36 +1,36 @@ -#include "direct_io.h" - -#include <util/generic/singleton.h> -#include <util/generic/yexception.h> +#include "direct_io.h" + +#include <util/generic/singleton.h> +#include <util/generic/yexception.h> #include <util/system/info.h> #include "align.h" - -#ifdef _linux_ + +#ifdef _linux_ #include <util/string/cast.h> #include <linux/version.h> #include <sys/utsname.h> -#endif +#endif -namespace { +namespace { struct TAlignmentCalcer { inline TAlignmentCalcer() - : Alignment(0) - { -#ifdef _linux_ - utsname sysInfo; + : Alignment(0) + { +#ifdef _linux_ + utsname sysInfo; Y_VERIFY(!uname(&sysInfo), "Error while call uname: %s", LastSystemErrorText()); - TStringBuf release(sysInfo.release); - release = release.substr(0, release.find_first_not_of(".0123456789")); + TStringBuf release(sysInfo.release); + release = release.substr(0, release.find_first_not_of(".0123456789")); - int v1 = FromString<int>(release.NextTok('.')); - int v2 = FromString<int>(release.NextTok('.')); - int v3 = FromString<int>(release.NextTok('.')); - int linuxVersionCode = KERNEL_VERSION(v1, v2, v3); + int v1 = FromString<int>(release.NextTok('.')); + int v2 = FromString<int>(release.NextTok('.')); + int v3 = FromString<int>(release.NextTok('.')); + int linuxVersionCode = KERNEL_VERSION(v1, v2, v3); if (linuxVersionCode < KERNEL_VERSION(2, 4, 10)) { - Alignment = 0; + Alignment = 0; } else if (linuxVersionCode < KERNEL_VERSION(2, 6, 0)) { Alignment = NSystemInfo::GetPageSize(); } else { @@ -39,44 +39,44 @@ namespace { // See IGNIETFERRO-946. Alignment = 4096; } -#endif - } - - size_t Alignment; - }; -} - +#endif + } + + size_t Alignment; + }; +} + TDirectIOBufferedFile::TDirectIOBufferedFile(const TString& path, EOpenMode oMode, size_t buflen /*= 1 << 17*/) - : File(path, oMode) - , Alignment(0) - , DataLen(0) - , ReadPosition(0) - , WritePosition(0) - , DirectIO(false) -{ + : File(path, oMode) + , Alignment(0) + , DataLen(0) + , ReadPosition(0) + , WritePosition(0) + , DirectIO(false) +{ if (buflen == 0) { ythrow TFileError() << "unbuffered usage is not supported"; - } - - if (oMode & Direct) { + } + + if (oMode & Direct) { Alignment = Singleton<TAlignmentCalcer>()->Alignment; - SetDirectIO(true); - } - - WritePosition = File.GetLength(); - FlushedBytes = WritePosition; - FlushedToDisk = FlushedBytes; - BufLen = (!!Alignment) ? AlignUp(buflen, Alignment) : buflen; - BufferStorage.Resize(BufLen + Alignment); - Buffer = (!!Alignment) ? AlignUp(BufferStorage.Data(), Alignment) : BufferStorage.Data(); -} - -#define DIRECT_IO_FLAGS (O_DIRECT | O_SYNC) - -void TDirectIOBufferedFile::SetDirectIO(bool value) { -#ifdef _linux_ + SetDirectIO(true); + } + + WritePosition = File.GetLength(); + FlushedBytes = WritePosition; + FlushedToDisk = FlushedBytes; + BufLen = (!!Alignment) ? AlignUp(buflen, Alignment) : buflen; + BufferStorage.Resize(BufLen + Alignment); + Buffer = (!!Alignment) ? AlignUp(BufferStorage.Data(), Alignment) : BufferStorage.Data(); +} + +#define DIRECT_IO_FLAGS (O_DIRECT | O_SYNC) + +void TDirectIOBufferedFile::SetDirectIO(bool value) { +#ifdef _linux_ if (DirectIO == value) { - return; + return; } if (!!Alignment && value) { @@ -85,73 +85,73 @@ void TDirectIOBufferedFile::SetDirectIO(bool value) { (void)fcntl(File.GetHandle(), F_SETFL, fcntl(File.GetHandle(), F_GETFL) & ~DIRECT_IO_FLAGS); } + DirectIO = value; +#else DirectIO = value; -#else - DirectIO = value; -#endif -} - -TDirectIOBufferedFile::~TDirectIOBufferedFile() { +#endif +} + +TDirectIOBufferedFile::~TDirectIOBufferedFile() { try { Finish(); } catch (...) { } -} - -void TDirectIOBufferedFile::FlushData() { +} + +void TDirectIOBufferedFile::FlushData() { WriteToFile(Buffer, DataLen, FlushedBytes); DataLen = 0; File.FlushData(); -} +} -void TDirectIOBufferedFile::Finish() { +void TDirectIOBufferedFile::Finish() { FlushData(); File.Flush(); File.Close(); -} - +} + void TDirectIOBufferedFile::Write(const void* buffer, size_t byteCount) { WriteToBuffer(buffer, byteCount, DataLen); WritePosition += byteCount; -} - -void TDirectIOBufferedFile::WriteToBuffer(const void* buf, size_t len, ui64 position) { - while (len > 0) { - size_t writeLen = Min<size_t>(BufLen - position, len); - - if (writeLen > 0) { - memcpy((char*)Buffer + position, buf, writeLen); - buf = (char*)buf + writeLen; - len -= writeLen; +} + +void TDirectIOBufferedFile::WriteToBuffer(const void* buf, size_t len, ui64 position) { + while (len > 0) { + size_t writeLen = Min<size_t>(BufLen - position, len); + + if (writeLen > 0) { + memcpy((char*)Buffer + position, buf, writeLen); + buf = (char*)buf + writeLen; + len -= writeLen; DataLen = (size_t)Max(position + writeLen, (ui64)DataLen); - position += writeLen; - } - - if (DataLen == BufLen) { - WriteToFile(Buffer, DataLen, FlushedBytes); - DataLen = 0; - position = 0; - } - } -} - -void TDirectIOBufferedFile::WriteToFile(const void* buf, size_t len, ui64 position) { - if (!!len) { - SetDirectIO(IsAligned(buf) && IsAligned(len) && IsAligned(position)); + position += writeLen; + } + + if (DataLen == BufLen) { + WriteToFile(Buffer, DataLen, FlushedBytes); + DataLen = 0; + position = 0; + } + } +} + +void TDirectIOBufferedFile::WriteToFile(const void* buf, size_t len, ui64 position) { + if (!!len) { + SetDirectIO(IsAligned(buf) && IsAligned(len) && IsAligned(position)); File.Pwrite(buf, len, position); - FlushedBytes = Max(FlushedBytes, position + len); - FlushedToDisk = Min(FlushedToDisk, position); - } -} - + FlushedBytes = Max(FlushedBytes, position + len); + FlushedToDisk = Min(FlushedToDisk, position); + } +} + size_t TDirectIOBufferedFile::PreadSafe(void* buffer, size_t byteCount, ui64 offset) { - if (FlushedToDisk < offset + byteCount) { - File.FlushData(); - FlushedToDisk = FlushedBytes; - } - + if (FlushedToDisk < offset + byteCount) { + File.FlushData(); + FlushedToDisk = FlushedBytes; + } + #ifdef _linux_ ssize_t bytesRead = 0; do { @@ -159,15 +159,15 @@ size_t TDirectIOBufferedFile::PreadSafe(void* buffer, size_t byteCount, ui64 off } while (bytesRead == -1 && errno == EINTR); if (bytesRead < 0) { - ythrow yexception() << "error while pread file: " << LastSystemError() << "(" << LastSystemErrorText() << ")"; + ythrow yexception() << "error while pread file: " << LastSystemError() << "(" << LastSystemErrorText() << ")"; } return bytesRead; #else return File.Pread(buffer, byteCount, offset); #endif -} - +} + size_t TDirectIOBufferedFile::ReadFromFile(void* buffer, size_t byteCount, ui64 offset) { SetDirectIO(true); @@ -187,64 +187,64 @@ size_t TDirectIOBufferedFile::ReadFromFile(void* buffer, size_t byteCount, ui64 break; } } - + if (!byteCount) { return bytesRead; } - ui64 bufSize = AlignUp(Min<size_t>(BufferStorage.Size(), byteCount + (Alignment << 1)), Alignment); - TBuffer readBufferStorage(bufSize + Alignment); - char* readBuffer = AlignUp((char*)readBufferStorage.Data(), Alignment); + ui64 bufSize = AlignUp(Min<size_t>(BufferStorage.Size(), byteCount + (Alignment << 1)), Alignment); + TBuffer readBufferStorage(bufSize + Alignment); + char* readBuffer = AlignUp((char*)readBufferStorage.Data(), Alignment); - while (byteCount) { + while (byteCount) { ui64 begin = AlignDown(offset, (ui64)Alignment); ui64 end = AlignUp(offset + byteCount, (ui64)Alignment); ui64 toRead = Min(end - begin, bufSize); ui64 fromFile = PreadSafe(readBuffer, toRead, begin); if (!fromFile) { - break; + break; } ui64 delta = offset - begin; ui64 count = Min<ui64>(fromFile - delta, byteCount); - memcpy(buffer, readBuffer + delta, count); - buffer = (char*)buffer + count; - byteCount -= count; - offset += count; + memcpy(buffer, readBuffer + delta, count); + buffer = (char*)buffer + count; + byteCount -= count; + offset += count; bytesRead += count; - } + } return bytesRead; -} - +} + size_t TDirectIOBufferedFile::Read(void* buffer, size_t byteCount) { size_t bytesRead = Pread(buffer, byteCount, ReadPosition); ReadPosition += bytesRead; return bytesRead; -} - +} + size_t TDirectIOBufferedFile::Pread(void* buffer, size_t byteCount, ui64 offset) { if (!byteCount) { - return 0; + return 0; } size_t readFromFile = 0; - if (offset < FlushedBytes) { - readFromFile = Min<ui64>(byteCount, FlushedBytes - offset); + if (offset < FlushedBytes) { + readFromFile = Min<ui64>(byteCount, FlushedBytes - offset); size_t bytesRead = ReadFromFile(buffer, readFromFile, offset); if (bytesRead != readFromFile || readFromFile == byteCount) { return bytesRead; } - } - ui64 start = offset > FlushedBytes ? offset - FlushedBytes : 0; + } + ui64 start = offset > FlushedBytes ? offset - FlushedBytes : 0; ui64 count = Min<ui64>(DataLen - start, byteCount - readFromFile); if (count) { - memcpy((char*)buffer + readFromFile, (const char*)Buffer + start, count); + memcpy((char*)buffer + readFromFile, (const char*)Buffer + start, count); } - return count + readFromFile; -} - + return count + readFromFile; +} + void TDirectIOBufferedFile::Pwrite(const void* buffer, size_t byteCount, ui64 offset) { if (offset > WritePosition) { ythrow yexception() << "cannot frite to position" << offset; @@ -262,5 +262,5 @@ void TDirectIOBufferedFile::Pwrite(const void* buffer, size_t byteCount, ui64 of if (writeToBufer > 0) { ui64 bufferOffset = offset + writeToFile - FlushedBytes; WriteToBuffer((const char*)buffer + writeToFile, writeToBufer, bufferOffset); - } -} + } +} diff --git a/util/system/direct_io.h b/util/system/direct_io.h index 6a3325a960..351b647501 100644 --- a/util/system/direct_io.h +++ b/util/system/direct_io.h @@ -1,42 +1,42 @@ -#pragma once - +#pragma once + #include "align.h" #include "file.h" -#include <util/generic/buffer.h> - -// Supports Linux Direct-IO: -// - Simple buffering logic. -// - Default buffer size of 128KB matches VM page writeback granularity, to maximize IO throughput. -// - Supports writing odd sized files by turning off direct IO for the last chunk. -class TDirectIOBufferedFile { -public: +#include <util/generic/buffer.h> + +// Supports Linux Direct-IO: +// - Simple buffering logic. +// - Default buffer size of 128KB matches VM page writeback granularity, to maximize IO throughput. +// - Supports writing odd sized files by turning off direct IO for the last chunk. +class TDirectIOBufferedFile { +public: TDirectIOBufferedFile(const TString& path, EOpenMode oMode, size_t buflen = 1 << 17); - ~TDirectIOBufferedFile(); + ~TDirectIOBufferedFile(); - void FlushData(); - void Finish(); + void FlushData(); + void Finish(); size_t Read(void* buffer, size_t byteCount); void Write(const void* buffer, size_t byteCount); size_t Pread(void* buffer, size_t byteCount, ui64 offset); void Pwrite(const void* buffer, size_t byteCount, ui64 offset); - - inline bool IsOpen() const { + + inline bool IsOpen() const { return true; - } - - inline ui64 GetWritePosition() const { - return WritePosition; - } - - inline ui64 GetLength() const { - return FlushedBytes + DataLen; - } - + } + + inline ui64 GetWritePosition() const { + return WritePosition; + } + + inline ui64 GetLength() const { + return FlushedBytes + DataLen; + } + inline FHANDLE GetHandle() { return File.GetHandle(); - } - + } + inline void FallocateNoResize(ui64 length) { File.FallocateNoResize(length); } @@ -45,31 +45,31 @@ public: File.ShrinkToFit(); } -private: - inline bool IsAligned(i64 value) { +private: + inline bool IsAligned(i64 value) { return Alignment ? value == AlignDown<i64>(value, Alignment) : true; - } + } - inline bool IsAligned(const void* value) { + inline bool IsAligned(const void* value) { return Alignment ? value == AlignDown(value, Alignment) : true; - } - + } + size_t PreadSafe(void* buffer, size_t byteCount, ui64 offset); size_t ReadFromFile(void* buffer, size_t byteCount, ui64 offset); - void WriteToFile(const void* buf, size_t len, ui64 position); - void WriteToBuffer(const void* buf, size_t len, ui64 position); - void SetDirectIO(bool value); - + void WriteToFile(const void* buf, size_t len, ui64 position); + void WriteToBuffer(const void* buf, size_t len, ui64 position); + void SetDirectIO(bool value); + private: TFile File; - size_t Alignment; - size_t BufLen; - size_t DataLen; - void* Buffer; - TBuffer BufferStorage; - ui64 ReadPosition; - ui64 WritePosition; - ui64 FlushedBytes; - ui64 FlushedToDisk; - bool DirectIO; -}; + size_t Alignment; + size_t BufLen; + size_t DataLen; + void* Buffer; + TBuffer BufferStorage; + ui64 ReadPosition; + ui64 WritePosition; + ui64 FlushedBytes; + ui64 FlushedToDisk; + bool DirectIO; +}; diff --git a/util/system/direct_io_ut.cpp b/util/system/direct_io_ut.cpp index 839c3de7ca..f3872df228 100644 --- a/util/system/direct_io_ut.cpp +++ b/util/system/direct_io_ut.cpp @@ -1,12 +1,12 @@ #include <library/cpp/testing/unittest/registar.h> - + #include <util/generic/yexception.h> #include <util/system/fs.h> #include <util/system/tempfile.h> -#include <util/random/random.h> +#include <util/random/random.h> #include "direct_io.h" - + static const char* FileName_("./test.file"); Y_UNIT_TEST_SUITE(TDirectIoTestSuite) { @@ -86,7 +86,7 @@ Y_UNIT_TEST_SUITE(TDirectIoTestSuite) { TestHugeFile(5 * 1024 * 1024 * 1024ULL + 1111); } } -} +} Y_UNIT_TEST_SUITE(TDirectIoErrorHandling) { Y_UNIT_TEST(Constructor) { diff --git a/util/system/error.cpp b/util/system/error.cpp index f778ec42cb..3396f21d0c 100644 --- a/util/system/error.cpp +++ b/util/system/error.cpp @@ -58,7 +58,7 @@ namespace { const char* LastSystemErrorText(int code) { #if defined(_win_) - TErrString& text(*Singleton<TErrString>()); + TErrString& text(*Singleton<TErrString>()); LastSystemErrorText(text.data, sizeof(text.data), code); return text.data; diff --git a/util/system/info.cpp b/util/system/info.cpp index cf6681e89a..6cb5ac8b9d 100644 --- a/util/system/info.cpp +++ b/util/system/info.cpp @@ -187,7 +187,7 @@ size_t NSystemInfo::GetPageSize() noexcept { #endif } -size_t NSystemInfo::TotalMemorySize() { +size_t NSystemInfo::TotalMemorySize() { #if defined(_linux_) && defined(_64_) try { auto q = FromString<size_t>(StripString(TFileInput("/sys/fs/cgroup/memory/memory.limit_in_bytes").ReadAll())); @@ -200,9 +200,9 @@ size_t NSystemInfo::TotalMemorySize() { #endif #if defined(_linux_) || defined(_cygwin_) - struct sysinfo info; - sysinfo(&info); - return info.totalram; + struct sysinfo info; + sysinfo(&info); + return info.totalram; #elif defined(_darwin_) int mib[2]; int64_t memSize; @@ -223,10 +223,10 @@ size_t NSystemInfo::TotalMemorySize() { ythrow yexception() << "GlobalMemoryStatusEx failed: " << LastSystemErrorText(); } return (size_t)memoryStatusEx.ullTotalPhys; -#else - return 0; -#endif -} +#else + return 0; +#endif +} size_t NSystemInfo::MaxOpenFiles() { #if defined(ANDROID) || defined(__ANDROID__) diff --git a/util/system/info.h b/util/system/info.h index 73ebe48a9a..cdf401cdfb 100644 --- a/util/system/info.h +++ b/util/system/info.h @@ -7,6 +7,6 @@ namespace NSystemInfo { size_t CachedNumberOfCpus(); size_t LoadAverage(double* la, size_t len); size_t GetPageSize() noexcept; - size_t TotalMemorySize(); + size_t TotalMemorySize(); size_t MaxOpenFiles(); } diff --git a/util/system/mlock.cpp b/util/system/mlock.cpp index 435338c98f..df69fed797 100644 --- a/util/system/mlock.cpp +++ b/util/system/mlock.cpp @@ -26,7 +26,7 @@ void LockMemory(const void* addr, size_t len) { #elif defined(_win_) HANDLE hndl = GetCurrentProcess(); SIZE_T min, max; - if (!GetProcessWorkingSetSize(hndl, &min, &max)) + if (!GetProcessWorkingSetSize(hndl, &min, &max)) ythrow yexception() << LastSystemErrorText(); if (!SetProcessWorkingSetSize(hndl, min + len, max + len)) ythrow yexception() << LastSystemErrorText(); @@ -43,7 +43,7 @@ void UnlockMemory(const void* addr, size_t len) { #elif defined(_win_) HANDLE hndl = GetCurrentProcess(); SIZE_T min, max; - if (!GetProcessWorkingSetSize(hndl, &min, &max)) + if (!GetProcessWorkingSetSize(hndl, &min, &max)) ythrow yexception() << LastSystemErrorText(); if (!SetProcessWorkingSetSize(hndl, min - len, max - len)) ythrow yexception() << LastSystemErrorText(); |