diff options
author | iddqd <iddqd@yandex-team.ru> | 2022-02-10 16:49:46 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:49:46 +0300 |
commit | ff241e1daf76d79ed38015cdb76d55eb3a33ac27 (patch) | |
tree | 5d5cb817648f650d76cf1076100726fd9b8448e8 /library | |
parent | 07fce9c5f7771600d0b3d70e1f88fd8a7e164d85 (diff) | |
download | ydb-ff241e1daf76d79ed38015cdb76d55eb3a33ac27.tar.gz |
Restoring authorship annotation for <iddqd@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'library')
70 files changed, 1325 insertions, 1325 deletions
diff --git a/library/cpp/actors/core/executor_thread.cpp b/library/cpp/actors/core/executor_thread.cpp index 3f2742262c..446b651efd 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 c9b9b836d8..fba200e16b 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 afaf58f12f..880a9d00db 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 5996f84529..fab489db4c 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 5f22b6d833..3d92ef8450 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 99659a5194..71e1442717 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 c3b984995e..329872cfde 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 49367658a3..2c17aa0518 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 d06b9f6838..77cfd96902 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 286f73259d..1ea35df675 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 bd6b13ca85..128583bdd7 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 e3dcac8e96..2eb90761fe 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"; +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 - 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 6b13d74102..fd2a906d6f 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 fdcdd02f7e..ea430edb83 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 0394b9ccdb..4f0ca24a4e 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 669dc44525..3ce0154ced 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 7dd9bec5fa..142c61b8d5 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 f85e9ebb69..2064265766 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 7ec0cef67b..877d3a2b33 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 a75122eb57..f330419271 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 2807171017..0a84469e43 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 95543d2550..73e55261ad 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 7278a40ff9..9ef83fb58c 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 f9783f115a..fd5618087b 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 d4a44d32ff..0f301457a0 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 9cfd1b0386..4fb05c19b4 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 a7a422c786..7dcf650dec 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 72991b610b..9fbd10f666 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 762e31297e..cbe71b16ea 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 5f171cafde..305f8470c5 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 57bd86ac3f..6532e1d769 100644 --- a/library/cpp/logger/global/rty_formater.h +++ b/library/cpp/logger/global/rty_formater.h @@ -1,7 +1,7 @@ -#pragma once +#pragma once + +#include "common.h" -#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 9563c8801c..20eb361e72 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 bc0103a2f6..93564e4890 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 2fe503fbc8..30efa13333 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 579deeb9e4..8227d13176 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 450a3e4fe7..9572a34c60 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 5a7a7d2500..c7da1d607c 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 574cb617ef..b1867d271d 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 8d83a61196..e1d70cc3d2 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 4258fb25fd..9f63d5c739 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 a262b3701f..a5bcab63f9 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 4e6fbf1365..d2a9fa0a07 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 8b4f981b5c..6f71b68573 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 ee2e7e6a6c..b2e94584da 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 fafdcfbfb3..6246f9bf9c 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 73c43b4955..cd66e986c0 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 9820e440bc..bf91d20c23 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 3962c54abf..7148150222 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 efcc2c2acb..e1cd02d422 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 3f2677bad6..e5321267e9 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 7e2eaba109..8f5cfe2782 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 61fd001a92..9e5ee9571d 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 5d7528ae23..26dd168529 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 0340995338..16ee0b92f6 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 211c97c8a9..2a461c1353 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 ba98035b10..00a5263cba 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 bd9300aede..ebd545d6dd 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 4a0b9c36f9..fd9debb060 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 35c499e0dc..c59e50f622 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 f76017c3a7..2d26d3319c 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 6af26f73f4..48d44a0838 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 8571549b14..929efbf816 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 a6183dab48..96cc11bcfd 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 71daeeb4ab..89a1781a40 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 0a2f5652c0..07e52d7f2f 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 0a207232c6..0fa996fd41 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 e064938f13..ebad1ea4d6 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 7dc5eac4a2..b6768b1bf0 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 dd62a5110b..44517a0092 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 2c1dcc4208..305bc6b40f 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; |