diff options
author | Anton Samokhvalov <pg83@yandex.ru> | 2022-02-10 16:45:17 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:45:17 +0300 |
commit | d3a398281c6fd1d3672036cb2d63f842d2cb28c5 (patch) | |
tree | dd4bd3ca0f36b817e96812825ffaf10d645803f2 /library/cpp/logger | |
parent | 72cb13b4aff9bc9cf22e49251bc8fd143f82538f (diff) | |
download | ydb-d3a398281c6fd1d3672036cb2d63f842d2cb28c5.tar.gz |
Restoring authorship annotation for Anton Samokhvalov <pg83@yandex.ru>. Commit 2 of 2.
Diffstat (limited to 'library/cpp/logger')
28 files changed, 705 insertions, 705 deletions
diff --git a/library/cpp/logger/all.h b/library/cpp/logger/all.h index ec4593eda8..ee1666844e 100644 --- a/library/cpp/logger/all.h +++ b/library/cpp/logger/all.h @@ -1,9 +1,9 @@ #pragma once - -#include "log.h" -#include "null.h" -#include "file.h" -#include "stream.h" -#include "thread.h" -#include "system.h" + +#include "log.h" +#include "null.h" +#include "file.h" +#include "stream.h" +#include "thread.h" +#include "system.h" #include "sync_page_cache_file.h" diff --git a/library/cpp/logger/backend.cpp b/library/cpp/logger/backend.cpp index 8804c990f2..b26bf5e88c 100644 --- a/library/cpp/logger/backend.cpp +++ b/library/cpp/logger/backend.cpp @@ -1,15 +1,15 @@ -#include "backend.h" +#include "backend.h" #include <util/generic/vector.h> #include <util/system/mutex.h> #include <util/generic/singleton.h> #include <util/generic/yexception.h> - + namespace { class TGlobalLogsStorage { private: TVector<TLogBackend*> Backends; TMutex Mutex; - + public: void Register(TLogBackend* backend) { TGuard<TMutex> g(Mutex); @@ -40,7 +40,7 @@ namespace { }; } -template <> +template <> class TSingletonTraits<TGlobalLogsStorage> { public: static const size_t Priority = 50; @@ -52,11 +52,11 @@ ELogPriority TLogBackend::FiltrationLevel() const { TLogBackend::TLogBackend() noexcept { Singleton<TGlobalLogsStorage>()->Register(this); -} - +} + TLogBackend::~TLogBackend() { Singleton<TGlobalLogsStorage>()->UnRegister(this); -} +} void TLogBackend::ReopenLogNoFlush() { ReopenLog(); diff --git a/library/cpp/logger/backend.h b/library/cpp/logger/backend.h index 20e01eabd1..d088726d6d 100644 --- a/library/cpp/logger/backend.h +++ b/library/cpp/logger/backend.h @@ -5,17 +5,17 @@ #include <util/generic/noncopyable.h> #include <cstddef> - -struct TLogRecord; + +struct TLogRecord; // NOTE: be aware that all `TLogBackend`s are registred in singleton. -class TLogBackend: public TNonCopyable { -public: - TLogBackend() noexcept; - virtual ~TLogBackend(); - - virtual void WriteData(const TLogRecord& rec) = 0; - virtual void ReopenLog() = 0; +class TLogBackend: public TNonCopyable { +public: + TLogBackend() noexcept; + virtual ~TLogBackend(); + + virtual void WriteData(const TLogRecord& rec) = 0; + virtual void ReopenLog() = 0; // Does not guarantee consistency with previous WriteData() calls: // log entries could be written to the new (reopened) log file due to @@ -27,4 +27,4 @@ public: static void ReopenAllBackends(bool flush = true); virtual size_t QueueSize() const; -}; +}; diff --git a/library/cpp/logger/element.cpp b/library/cpp/logger/element.cpp index 80a9d74419..b510fe16e1 100644 --- a/library/cpp/logger/element.cpp +++ b/library/cpp/logger/element.cpp @@ -1,15 +1,15 @@ -#include "log.h" -#include "element.h" - +#include "log.h" +#include "element.h" + #include <utility> TLogElement::TLogElement(const TLog* parent) - : Parent_(parent) - , Priority_(Parent_->DefaultPriority()) -{ - Reset(); -} - + : Parent_(parent) + , Priority_(Parent_->DefaultPriority()) +{ + Reset(); +} + TLogElement::TLogElement(const TLog* parent, ELogPriority priority) : Parent_(parent) , Priority_(priority) @@ -18,21 +18,21 @@ TLogElement::TLogElement(const TLog* parent, ELogPriority priority) } TLogElement::~TLogElement() { - try { - Finish(); - } catch (...) { - } -} - -void TLogElement::DoFlush() { + try { + Finish(); + } catch (...) { + } +} + +void TLogElement::DoFlush() { if (IsNull()) { return; } - const size_t filled = Filled(); - - if (filled) { - Parent_->Write(Priority_, Data(), filled); - Reset(); - } -} + const size_t filled = Filled(); + + if (filled) { + Parent_->Write(Priority_, Data(), filled); + Reset(); + } +} diff --git a/library/cpp/logger/element.h b/library/cpp/logger/element.h index 73ce81bf2f..fc9bff851f 100644 --- a/library/cpp/logger/element.h +++ b/library/cpp/logger/element.h @@ -1,48 +1,48 @@ #pragma once - -#include "priority.h" - -#include <util/stream/tempbuf.h> - -class TLog; - -/* - * better do not use directly - */ + +#include "priority.h" + +#include <util/stream/tempbuf.h> + +class TLog; + +/* + * better do not use directly + */ class TLogElement: public TTempBufOutput { -public: - TLogElement(const TLog* parent); +public: + TLogElement(const TLog* parent); TLogElement(const TLog* parent, ELogPriority priority); - TLogElement(TLogElement&&) noexcept = default; - TLogElement& operator=(TLogElement&&) noexcept = default; + TLogElement(TLogElement&&) noexcept = default; + TLogElement& operator=(TLogElement&&) noexcept = default; + + ~TLogElement() override; - ~TLogElement() override; - - template <class T> - inline TLogElement& operator<<(const T& t) { + template <class T> + inline TLogElement& operator<<(const T& t) { static_cast<IOutputStream&>(*this) << t; - - return *this; - } - - /* - * for pretty usage: logger << TLOG_ERROR << "Error description"; - */ + + return *this; + } + + /* + * for pretty usage: logger << TLOG_ERROR << "Error description"; + */ inline TLogElement& operator<<(ELogPriority priority) { - Flush(); - Priority_ = priority; - return *this; - } - + Flush(); + Priority_ = priority; + return *this; + } + ELogPriority Priority() const noexcept { - return Priority_; - } - -protected: - void DoFlush() override; - -protected: - const TLog* Parent_; + return Priority_; + } + +protected: + void DoFlush() override; + +protected: + const TLog* Parent_; ELogPriority Priority_; -}; +}; diff --git a/library/cpp/logger/file.cpp b/library/cpp/logger/file.cpp index c5ecd5550c..15a4946eda 100644 --- a/library/cpp/logger/file.cpp +++ b/library/cpp/logger/file.cpp @@ -1,59 +1,59 @@ -#include "file.h" -#include "record.h" - -#include <util/system/file.h> -#include <util/system/rwlock.h> +#include "file.h" +#include "record.h" + +#include <util/system/file.h> +#include <util/system/rwlock.h> #include <util/generic/string.h> - -/* - * file log - */ + +/* + * file log + */ class TFileLogBackend::TImpl { -public: +public: inline TImpl(const TString& path) - : File_(OpenFile(path)) - { - } - - inline void WriteData(const TLogRecord& rec) { - //many writes are thread-safe - TReadGuard guard(Lock_); - - File_.Write(rec.Data, rec.Len); - } - - inline void ReopenLog() { - //but log rotate not thread-safe - TWriteGuard guard(Lock_); - - File_.LinkTo(OpenFile(File_.GetName())); - } - -private: + : File_(OpenFile(path)) + { + } + + inline void WriteData(const TLogRecord& rec) { + //many writes are thread-safe + TReadGuard guard(Lock_); + + File_.Write(rec.Data, rec.Len); + } + + inline void ReopenLog() { + //but log rotate not thread-safe + TWriteGuard guard(Lock_); + + File_.LinkTo(OpenFile(File_.GetName())); + } + +private: static inline TFile OpenFile(const TString& path) { - return TFile(path, OpenAlways | WrOnly | ForAppend | Seq | NoReuse); - } - -private: - TRWMutex Lock_; - TFile File_; -}; - + return TFile(path, OpenAlways | WrOnly | ForAppend | Seq | NoReuse); + } + +private: + TRWMutex Lock_; + TFile File_; +}; + TFileLogBackend::TFileLogBackend(const TString& path) - : Impl_(new TImpl(path)) -{ -} - + : Impl_(new TImpl(path)) +{ +} + TFileLogBackend::~TFileLogBackend() { -} - -void TFileLogBackend::WriteData(const TLogRecord& rec) { - Impl_->WriteData(rec); -} - -void TFileLogBackend::ReopenLog() { +} + +void TFileLogBackend::WriteData(const TLogRecord& rec) { + Impl_->WriteData(rec); +} + +void TFileLogBackend::ReopenLog() { TAtomicSharedPtr<TImpl> copy = Impl_; if (copy) { copy->ReopenLog(); } -} +} diff --git a/library/cpp/logger/file.h b/library/cpp/logger/file.h index fb8d3879c4..10b4cd0c20 100644 --- a/library/cpp/logger/file.h +++ b/library/cpp/logger/file.h @@ -1,19 +1,19 @@ #pragma once -#include "backend.h" - +#include "backend.h" + #include <util/generic/fwd.h> #include <util/generic/ptr.h> - -class TFileLogBackend: public TLogBackend { -public: + +class TFileLogBackend: public TLogBackend { +public: TFileLogBackend(const TString& path); - ~TFileLogBackend() override; - - void WriteData(const TLogRecord& rec) override; - void ReopenLog() override; - -private: - class TImpl; + ~TFileLogBackend() override; + + void WriteData(const TLogRecord& rec) override; + void ReopenLog() override; + +private: + class TImpl; TAtomicSharedPtr<TImpl> Impl_; }; diff --git a/library/cpp/logger/filter.h b/library/cpp/logger/filter.h index 5355fa4493..9ef83fb58c 100644 --- a/library/cpp/logger/filter.h +++ b/library/cpp/logger/filter.h @@ -1,5 +1,5 @@ #pragma once - + #include "priority.h" #include "record.h" #include "backend.h" @@ -8,7 +8,7 @@ class TFilteredLogBackend: public TLogBackend { THolder<TLogBackend> Backend; ELogPriority Level; - + public: TFilteredLogBackend(THolder<TLogBackend>&& t, ELogPriority level = LOG_MAX_PRIORITY) noexcept : Backend(std::move(t)) diff --git a/library/cpp/logger/global/common.h b/library/cpp/logger/global/common.h index 0f5b9893c6..7dcf650dec 100644 --- a/library/cpp/logger/global/common.h +++ b/library/cpp/logger/global/common.h @@ -36,7 +36,7 @@ namespace NLoggingImpl { public: inline static bool Usage() { return SingletonWithPriority<TPtr, SingletonPriority>()->Instance.Get(); - } + } inline static T* Get() { return SingletonWithPriority<TPtr, SingletonPriority>()->Instance.Get(); @@ -139,11 +139,11 @@ namespace NPrivateGlobalLogger { return true; } }; -} +} #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 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) diff --git a/library/cpp/logger/global/global.cpp b/library/cpp/logger/global/global.cpp index d871fcd350..9fbd10f666 100644 --- a/library/cpp/logger/global/global.cpp +++ b/library/cpp/logger/global/global.cpp @@ -26,12 +26,12 @@ bool GlobalLogInitialized() { return TLoggerOperator<TGlobalLog>::Usage(); } -template <> +template <> TGlobalLog* CreateDefaultLogger<TGlobalLog>() { return new TGlobalLog("console", TLOG_INFO); } -template <> +template <> TNullLog* CreateDefaultLogger<TNullLog>() { return new TNullLog("null"); } diff --git a/library/cpp/logger/global/global.h b/library/cpp/logger/global/global.h index 0613709ff0..cbe71b16ea 100644 --- a/library/cpp/logger/global/global.h +++ b/library/cpp/logger/global/global.h @@ -30,7 +30,7 @@ public: } }; -template <> +template <> TGlobalLog* CreateDefaultLogger<TGlobalLog>(); class TNullLog: public TLog { @@ -46,7 +46,7 @@ public: } }; -template <> +template <> TNullLog* CreateDefaultLogger<TNullLog>(); template <> @@ -79,9 +79,9 @@ public: #define VERIFY_WITH_LOG(expr, msg, ...) \ do { \ - if (Y_UNLIKELY(!(expr))) { \ + if (Y_UNLIKELY(!(expr))) { \ FATAL_LOG << Sprintf(msg, ##__VA_ARGS__) << Endl; \ - Y_VERIFY(false, msg, ##__VA_ARGS__); \ + Y_VERIFY(false, msg, ##__VA_ARGS__); \ }; \ } while (0); @@ -90,7 +90,7 @@ namespace NPrivateGlobalLogger { public: ~TVerifyEvent(); template <class T> - inline TVerifyEvent& operator<<(const T& t) { + inline TVerifyEvent& operator<<(const T& t) { static_cast<IOutputStream&>(*this) << t; return *this; diff --git a/library/cpp/logger/global/rty_formater.cpp b/library/cpp/logger/global/rty_formater.cpp index c7799280bd..305f8470c5 100644 --- a/library/cpp/logger/global/rty_formater.cpp +++ b/library/cpp/logger/global/rty_formater.cpp @@ -18,7 +18,7 @@ namespace { instant.LocalTime(&tm); // both stftime and sprintf exclude the terminating null byte from the return value - char* pos = begin; + char* pos = begin; pos += strftime(pos, end - pos, "%Y-%m-%d %H:%M:%S.", &tm); pos += sprintf(pos, "%03" PRIu32, instant.MilliSecondsOfSecond()); pos += strftime(pos, end - pos, " %z", &tm); @@ -28,7 +28,7 @@ namespace { } namespace NLoggingImpl { - IOutputStream& operator<<(IOutputStream& out, TLocalTimeS localTimeS) { + IOutputStream& operator<<(IOutputStream& out, TLocalTimeS localTimeS) { char buffer[LocalTimeSBufferSize]; size_t len = PrintLocalTimeS(localTimeS.GetInstant(), buffer, buffer + sizeof(buffer)); out.Write(buffer, len); diff --git a/library/cpp/logger/global/rty_formater.h b/library/cpp/logger/global/rty_formater.h index cf58bb7949..6532e1d769 100644 --- a/library/cpp/logger/global/rty_formater.h +++ b/library/cpp/logger/global/rty_formater.h @@ -1,5 +1,5 @@ #pragma once - + #include "common.h" namespace NMemInfo { diff --git a/library/cpp/logger/global/rty_formater_ut.cpp b/library/cpp/logger/global/rty_formater_ut.cpp index c5d4461ae7..551a97c5bf 100644 --- a/library/cpp/logger/global/rty_formater_ut.cpp +++ b/library/cpp/logger/global/rty_formater_ut.cpp @@ -26,6 +26,6 @@ Y_UNIT_TEST_SUITE(NLoggingImplTest) { Y_UNIT_TEST(TestTLocalTimeSAddRight) { NLoggingImpl::TLocalTimeS lt(TInstant::ParseIso8601Deprecated(SampleISO8601)); TString prefix("prefix"); - UNIT_ASSERT_EQUAL(prefix + lt, prefix + SampleRtyLog); + UNIT_ASSERT_EQUAL(prefix + lt, prefix + SampleRtyLog); } } diff --git a/library/cpp/logger/log.cpp b/library/cpp/logger/log.cpp index 33cfff57c0..e1d70cc3d2 100644 --- a/library/cpp/logger/log.cpp +++ b/library/cpp/logger/log.cpp @@ -2,16 +2,16 @@ #include "uninitialized_creator.h" #include "filter.h" #include "null.h" -#include "stream.h" +#include "stream.h" #include "thread.h" - -#include <util/string/cast.h> -#include <util/stream/printf.h> -#include <util/system/yassert.h> + +#include <util/string/cast.h> +#include <util/stream/printf.h> +#include <util/system/yassert.h> #include <util/generic/string.h> -#include <util/generic/scope.h> -#include <util/generic/yexception.h> - +#include <util/generic/scope.h> +#include <util/generic/yexception.h> + THolder<TLogBackend> CreateLogBackend(const TString& fname, ELogPriority priority, bool threaded) { TLogBackendCreatorUninitialized creator; creator.InitCustom(fname, priority, threaded); @@ -26,38 +26,38 @@ THolder<TOwningThreadedLogBackend> CreateOwningThreadedLogBackend(const TString& return MakeHolder<TOwningThreadedLogBackend>(CreateLogBackend(fname, LOG_MAX_PRIORITY, false).Release(), queueLen); } -class TLog::TImpl: public TAtomicRefCount<TImpl> { +class TLog::TImpl: public TAtomicRefCount<TImpl> { class TPriorityLogStream final: public IOutputStream { - public: + public: inline TPriorityLogStream(ELogPriority p, const TImpl* parent) - : Priority_(p) - , Parent_(parent) - { - } - - void DoWrite(const void* buf, size_t len) override { - Parent_->WriteData(Priority_, (const char*)buf, len); - } - - private: + : Priority_(p) + , Parent_(parent) + { + } + + void DoWrite(const void* buf, size_t len) override { + Parent_->WriteData(Priority_, (const char*)buf, len); + } + + private: ELogPriority Priority_ = LOG_DEF_PRIORITY; const TImpl* Parent_ = nullptr; - }; - -public: + }; + +public: inline TImpl(THolder<TLogBackend> backend) : Backend_(std::move(backend)) - { - } - - inline void ReopenLog() { - if (!IsOpen()) { - return; - } - + { + } + + inline void ReopenLog() { + if (!IsOpen()) { + return; + } + Backend_->ReopenLog(); - } - + } + inline void ReopenLogNoFlush() { if (!IsOpen()) { return; @@ -67,133 +67,133 @@ public: } inline void AddLog(ELogPriority priority, const char* format, va_list args) const { - if (!IsOpen()) { - return; + if (!IsOpen()) { + return; } - TPriorityLogStream ls(priority, this); + TPriorityLogStream ls(priority, this); + + Printf(ls, format, args); + } - Printf(ls, format, args); - } - inline void ResetBackend(THolder<TLogBackend> backend) noexcept { Backend_ = std::move(backend); - } - + } + inline THolder<TLogBackend> ReleaseBackend() noexcept { return std::move(Backend_); - } - - inline bool IsNullLog() const noexcept { + } + + inline bool IsNullLog() const noexcept { return !IsOpen() || (dynamic_cast<TNullLogBackend*>(Backend_.Get()) != nullptr); - } - - inline bool IsOpen() const noexcept { + } + + inline bool IsOpen() const noexcept { return nullptr != Backend_.Get(); - } + } - inline void CloseLog() noexcept { + inline void CloseLog() noexcept { Backend_.Destroy(); - Y_ASSERT(!IsOpen()); - } - + Y_ASSERT(!IsOpen()); + } + inline void WriteData(ELogPriority priority, const char* data, size_t len) const { - if (IsOpen()) { + if (IsOpen()) { Backend_->WriteData(TLogRecord(priority, data, len)); } - } + } inline ELogPriority DefaultPriority() noexcept { - return DefaultPriority_; - } + return DefaultPriority_; + } inline void SetDefaultPriority(ELogPriority priority) noexcept { - DefaultPriority_ = priority; - } - + DefaultPriority_ = priority; + } + inline ELogPriority FiltrationLevel() const noexcept { return Backend_->FiltrationLevel(); - } - + } + inline size_t BackEndQueueSize() const { return Backend_->QueueSize(); } -private: +private: THolder<TLogBackend> Backend_; ELogPriority DefaultPriority_ = LOG_DEF_PRIORITY; -}; - -TLog::TLog() +}; + +TLog::TLog() : Impl_(MakeIntrusive<TImpl>(nullptr)) -{ -} - +{ +} + TLog::TLog(const TString& fname, ELogPriority priority) : TLog(CreateLogBackend(fname, priority, false)) { -} - +} + TLog::TLog(THolder<TLogBackend> backend) : Impl_(MakeIntrusive<TImpl>(std::move(backend))) -{ -} - +{ +} + TLog::TLog(const TLog&) = default; TLog::TLog(TLog&&) = default; TLog::~TLog() = default; TLog& TLog::operator=(const TLog&) = default; TLog& TLog::operator=(TLog&&) = default; - + bool TLog::IsOpen() const noexcept { - return Impl_->IsOpen(); -} - + return Impl_->IsOpen(); +} + void TLog::AddLog(const char* format, ...) const { - va_list args; - va_start(args, format); - - Y_DEFER { - va_end(args); - }; - - Impl_->AddLog(Impl_->DefaultPriority(), format, args); -} - + va_list args; + va_start(args, format); + + Y_DEFER { + va_end(args); + }; + + Impl_->AddLog(Impl_->DefaultPriority(), format, args); +} + void TLog::AddLog(ELogPriority priority, const char* format, ...) const { va_list args; va_start(args, format); - Y_DEFER { + Y_DEFER { va_end(args); - }; + }; - Impl_->AddLog(priority, format, args); + Impl_->AddLog(priority, format, args); } void TLog::AddLogVAList(const char* format, va_list lst) { - Impl_->AddLog(Impl_->DefaultPriority(), format, lst); -} - -void TLog::ReopenLog() { + Impl_->AddLog(Impl_->DefaultPriority(), format, lst); +} + +void TLog::ReopenLog() { if (const auto copy = Impl_) { copy->ReopenLog(); } -} - +} + void TLog::ReopenLogNoFlush() { if (const auto copy = Impl_) { copy->ReopenLogNoFlush(); } } -void TLog::CloseLog() { - Impl_->CloseLog(); -} - +void TLog::CloseLog() { + Impl_->CloseLog(); +} + void TLog::SetDefaultPriority(ELogPriority priority) noexcept { - Impl_->SetDefaultPriority(priority); + Impl_->SetDefaultPriority(priority); } ELogPriority TLog::FiltrationLevel() const noexcept { @@ -201,23 +201,23 @@ ELogPriority TLog::FiltrationLevel() const noexcept { } ELogPriority TLog::DefaultPriority() const noexcept { - return Impl_->DefaultPriority(); -} - + return Impl_->DefaultPriority(); +} + bool TLog::OpenLog(const char* path, ELogPriority lp) { - if (path) { + if (path) { ResetBackend(CreateLogBackend(path, lp)); - } else { + } else { ResetBackend(MakeHolder<TStreamLogBackend>(&Cerr)); - } - - return true; -} - + } + + return true; +} + void TLog::ResetBackend(THolder<TLogBackend> backend) noexcept { Impl_->ResetBackend(std::move(backend)); -} - +} + bool TLog::IsNullLog() const noexcept { return Impl_->IsNullLog(); } @@ -241,7 +241,7 @@ void TLog::Write(ELogPriority priority, const TStringBuf data) const { void TLog::Write(const char* data, size_t len) const { Write(Impl_->DefaultPriority(), data, len); -} +} void TLog::SetFormatter(TLogFormatter formatter) noexcept { Formatter_ = std::move(formatter); diff --git a/library/cpp/logger/log.h b/library/cpp/logger/log.h index 7cb2a471f1..8be984ccc8 100644 --- a/library/cpp/logger/log.h +++ b/library/cpp/logger/log.h @@ -1,17 +1,17 @@ #pragma once #include "backend.h" -#include "element.h" -#include "priority.h" +#include "element.h" +#include "priority.h" #include "record.h" #include "thread.h" - + #include <util/generic/fwd.h> -#include <util/generic/ptr.h> +#include <util/generic/ptr.h> #include <functional> -#include <cstdarg> - +#include <cstdarg> + using TLogFormatter = std::function<TString(ELogPriority priority, TStringBuf)>; // Logging facilities interface. @@ -30,21 +30,21 @@ using TLogFormatter = std::function<TString(ELogPriority priority, TStringBuf)>; // ``` // // Users are encouraged to copy `TLog` instance. -class TLog { -public: +class TLog { +public: // Construct empty logger all writes will be spilled. - TLog(); + TLog(); // Construct file logger. TLog(const TString& fname, ELogPriority priority = LOG_MAX_PRIORITY); // Construct any type of logger TLog(THolder<TLogBackend> backend); - + TLog(const TLog&); TLog(TLog&&); - ~TLog(); + ~TLog(); TLog& operator=(const TLog&); TLog& operator=(TLog&&); - + // Change underlying backend. // NOTE: not thread safe. void ResetBackend(THolder<TLogBackend> backend) noexcept; @@ -53,30 +53,30 @@ public: THolder<TLogBackend> ReleaseBackend() noexcept; // Check if underlying backend is defined and is not null. // NOTE: not thread safe with respect to `ResetBackend` and `ReleaseBackend`. - bool IsNullLog() const noexcept; - + bool IsNullLog() const noexcept; + // Write message to the log. // // @param[in] priority Message priority to use. // @param[in] message Message to write. void Write(ELogPriority priority, TStringBuf message) const; // Write message to the log using `DefaultPriority()`. - void Write(const char* data, size_t len) const; + void Write(const char* data, size_t len) const; // Write message to the log, but pass the message in a c-style. void Write(ELogPriority priority, const char* data, size_t len) const; // Write message to the log in a c-like printf style. void Y_PRINTF_FORMAT(3, 4) AddLog(ELogPriority priority, const char* format, ...) const; // Write message to the log in a c-like printf style with `DefaultPriority()` priority. - void Y_PRINTF_FORMAT(2, 3) AddLog(const char* format, ...) const; + void Y_PRINTF_FORMAT(2, 3) AddLog(const char* format, ...) const; // Call `ReopenLog()` of the underlying backend. - void ReopenLog(); + void ReopenLog(); // Call `ReopenLogNoFlush()` of the underlying backend. void ReopenLogNoFlush(); // Call `QueueSize()` of the underlying backend. size_t BackEndQueueSize() const; - + // Set log default priority. // NOTE: not thread safe. void SetDefaultPriority(ELogPriority priority) noexcept; @@ -89,13 +89,13 @@ public: // Set current log formatter. void SetFormatter(TLogFormatter formatter) noexcept; - template <class T> + template <class T> inline TLogElement operator<<(const T& t) const { - TLogElement ret(this); - ret << t; - return ret; - } - + TLogElement ret(this); + ret << t; + return ret; + } + public: // These methods are deprecated and present here only for compatibility reasons (for 13 years // already ...). Do not use them. @@ -104,11 +104,11 @@ public: void AddLogVAList(const char* format, va_list lst); void CloseLog(); -private: - class TImpl; +private: + class TImpl; TIntrusivePtr<TImpl> Impl_; TLogFormatter Formatter_; -}; +}; THolder<TLogBackend> CreateLogBackend(const TString& fname, ELogPriority priority = LOG_MAX_PRIORITY, bool threaded = false); THolder<TLogBackend> CreateFilteredOwningThreadedLogBackend(const TString& fname, ELogPriority priority = LOG_MAX_PRIORITY, size_t queueLen = 0); diff --git a/library/cpp/logger/log_ut.cpp b/library/cpp/logger/log_ut.cpp index 498a667756..8de46f17f5 100644 --- a/library/cpp/logger/log_ut.cpp +++ b/library/cpp/logger/log_ut.cpp @@ -1,83 +1,83 @@ -#include "all.h" - +#include "all.h" + #include <library/cpp/testing/unittest/registar.h> - -#include <util/system/fs.h> + +#include <util/system/fs.h> #include <util/system/rwlock.h> #include <util/system/yield.h> #include <util/memory/blob.h> -#include <util/stream/file.h> +#include <util/stream/file.h> #include <util/generic/string.h> #include <util/generic/vector.h> - -class TLogTest: public TTestBase { - UNIT_TEST_SUITE(TLogTest); - UNIT_TEST(TestFile) - UNIT_TEST(TestFormat) - UNIT_TEST(TestWrite) - UNIT_TEST(TestThreaded) + +class TLogTest: public TTestBase { + UNIT_TEST_SUITE(TLogTest); + UNIT_TEST(TestFile) + UNIT_TEST(TestFormat) + UNIT_TEST(TestWrite) + UNIT_TEST(TestThreaded) UNIT_TEST(TestThreadedWithOverflow) UNIT_TEST(TestNoFlush) - UNIT_TEST_SUITE_END(); - -private: - void TestFile(); - void TestFormat(); - void TestWrite(); - void TestThreaded(); + UNIT_TEST_SUITE_END(); + +private: + void TestFile(); + void TestFormat(); + void TestWrite(); + void TestThreaded(); void TestThreadedWithOverflow(); void TestNoFlush(); - void SetUp() override; - void TearDown() override; -}; - -UNIT_TEST_SUITE_REGISTRATION(TLogTest); - -#define LOGFILE "tmplogfile" - -void TLogTest::TestFile() { - { - TLog log; - - { - TLog filelog(LOGFILE); - - log = filelog; - } - - int v1 = 12; - unsigned v2 = 34; - double v3 = 3.0; - const char* v4 = "qwqwqw"; - - log.ReopenLog(); - log.AddLog("some useful data %d, %u, %lf, %s\n", v1, v2, v3, v4); - } - + void SetUp() override; + void TearDown() override; +}; + +UNIT_TEST_SUITE_REGISTRATION(TLogTest); + +#define LOGFILE "tmplogfile" + +void TLogTest::TestFile() { + { + TLog log; + + { + TLog filelog(LOGFILE); + + log = filelog; + } + + int v1 = 12; + unsigned v2 = 34; + double v3 = 3.0; + const char* v4 = "qwqwqw"; + + log.ReopenLog(); + log.AddLog("some useful data %d, %u, %lf, %s\n", v1, v2, v3, v4); + } + TBlob data = TBlob::FromFileSingleThreaded(LOGFILE); - + UNIT_ASSERT_EQUAL(TString((const char*)data.Begin(), data.Size()), "some useful data 12, 34, 3.000000, qwqwqw\n"); -} - -void TLogTest::TestThreaded() { - { - TFileLogBackend fb(LOGFILE); +} + +void TLogTest::TestThreaded() { + { + TFileLogBackend fb(LOGFILE); TLog log(THolder(new TThreadedLogBackend(&fb))); - - int v1 = 12; - unsigned v2 = 34; - double v3 = 3.0; - const char* v4 = "qwqwqw"; - - log.ReopenLog(); - log.AddLog("some useful data %d, %u, %lf, %s\n", v1, v2, v3, v4); - } - + + int v1 = 12; + unsigned v2 = 34; + double v3 = 3.0; + const char* v4 = "qwqwqw"; + + log.ReopenLog(); + log.AddLog("some useful data %d, %u, %lf, %s\n", v1, v2, v3, v4); + } + TBlob data = TBlob::FromFileSingleThreaded(LOGFILE); - + UNIT_ASSERT_EQUAL(TString((const char*)data.Begin(), data.Size()), "some useful data 12, 34, 3.000000, qwqwqw\n"); -} - +} + void TLogTest::TestThreadedWithOverflow() { class TFakeLogBackend: public TLogBackend { public: @@ -150,42 +150,42 @@ void TLogTest::TestNoFlush() { UNIT_ASSERT_EQUAL(TString((const char*)data.Begin(), data.Size()), "some useful data 12, 34, 3.000000, qwqwqw\n"); } -void TLogTest::TestFormat() { - TStringStream data; - - { +void TLogTest::TestFormat() { + TStringStream data; + + { TLog log(THolder(new TStreamLogBackend(&data))); - - log << "qw" - << " " - << "1234" << 1234 << " " << 12.3 << 'q' << Endl; - } - - UNIT_ASSERT_EQUAL(data.Str(), "qw 12341234 12.3q\n"); -} - -void TLogTest::TestWrite() { - TStringStream data; + + log << "qw" + << " " + << "1234" << 1234 << " " << 12.3 << 'q' << Endl; + } + + UNIT_ASSERT_EQUAL(data.Str(), "qw 12341234 12.3q\n"); +} + +void TLogTest::TestWrite() { + TStringStream data; TString test; - - { + + { TLog log(THolder(new TStreamLogBackend(&data))); - - for (size_t i = 0; i < 1000; ++i) { + + for (size_t i = 0; i < 1000; ++i) { TVector<char> buf(i, (char)i); - + test.append(buf.data(), buf.size()); log.Write(buf.data(), buf.size()); - } - } - - UNIT_ASSERT_EQUAL(data.Str(), test); -} - -void TLogTest::SetUp() { - TearDown(); -} - -void TLogTest::TearDown() { - NFs::Remove(LOGFILE); -} + } + } + + UNIT_ASSERT_EQUAL(data.Str(), test); +} + +void TLogTest::SetUp() { + TearDown(); +} + +void TLogTest::TearDown() { + NFs::Remove(LOGFILE); +} diff --git a/library/cpp/logger/null.cpp b/library/cpp/logger/null.cpp index 9ed26c35ae..debb22f794 100644 --- a/library/cpp/logger/null.cpp +++ b/library/cpp/logger/null.cpp @@ -1,13 +1,13 @@ -#include "null.h" - -TNullLogBackend::TNullLogBackend() { -} - +#include "null.h" + +TNullLogBackend::TNullLogBackend() { +} + TNullLogBackend::~TNullLogBackend() { -} - -void TNullLogBackend::WriteData(const TLogRecord&) { -} - -void TNullLogBackend::ReopenLog() { -} +} + +void TNullLogBackend::WriteData(const TLogRecord&) { +} + +void TNullLogBackend::ReopenLog() { +} diff --git a/library/cpp/logger/null.h b/library/cpp/logger/null.h index 277d4b583f..a02f250b00 100644 --- a/library/cpp/logger/null.h +++ b/library/cpp/logger/null.h @@ -1,12 +1,12 @@ #pragma once -#include "backend.h" +#include "backend.h" -class TNullLogBackend: public TLogBackend { -public: - TNullLogBackend(); - ~TNullLogBackend() override; - - void WriteData(const TLogRecord& rec) override; - void ReopenLog() override; -}; +class TNullLogBackend: public TLogBackend { +public: + TNullLogBackend(); + ~TNullLogBackend() override; + + void WriteData(const TLogRecord& rec) override; + void ReopenLog() override; +}; diff --git a/library/cpp/logger/priority.h b/library/cpp/logger/priority.h index db7607ef22..d2a9fa0a07 100644 --- a/library/cpp/logger/priority.h +++ b/library/cpp/logger/priority.h @@ -1,15 +1,15 @@ #pragma once enum ELogPriority { - TLOG_EMERG = 0 /* "EMERG" */, - TLOG_ALERT = 1 /* "ALERT" */, - TLOG_CRIT = 2 /* "CRITICAL_INFO" */, - TLOG_ERR = 3 /* "ERROR" */, - TLOG_WARNING = 4 /* "WARNING" */, - TLOG_NOTICE = 5 /* "NOTICE" */, - TLOG_INFO = 6 /* "INFO" */, - TLOG_DEBUG = 7 /* "DEBUG" */, - TLOG_RESOURCES = 8 /* "RESOURCES" */ + TLOG_EMERG = 0 /* "EMERG" */, + TLOG_ALERT = 1 /* "ALERT" */, + TLOG_CRIT = 2 /* "CRITICAL_INFO" */, + TLOG_ERR = 3 /* "ERROR" */, + TLOG_WARNING = 4 /* "WARNING" */, + TLOG_NOTICE = 5 /* "NOTICE" */, + TLOG_INFO = 6 /* "INFO" */, + TLOG_DEBUG = 7 /* "DEBUG" */, + TLOG_RESOURCES = 8 /* "RESOURCES" */ }; #define LOG_MAX_PRIORITY TLOG_RESOURCES -#define LOG_DEF_PRIORITY TLOG_INFO +#define LOG_DEF_PRIORITY TLOG_INFO diff --git a/library/cpp/logger/record.h b/library/cpp/logger/record.h index 1b0f0a9774..c28a7785fd 100644 --- a/library/cpp/logger/record.h +++ b/library/cpp/logger/record.h @@ -1,18 +1,18 @@ #pragma once -#include "priority.h" - -#include <util/system/defaults.h> - -struct TLogRecord { - const char* Data; - size_t Len; +#include "priority.h" + +#include <util/system/defaults.h> + +struct TLogRecord { + const char* Data; + size_t Len; ELogPriority Priority; inline TLogRecord(ELogPriority priority, const char* data, size_t len) noexcept - : Data(data) - , Len(len) - , Priority(priority) - { - } -}; + : Data(data) + , Len(len) + , Priority(priority) + { + } +}; diff --git a/library/cpp/logger/stream.cpp b/library/cpp/logger/stream.cpp index 5cd114bc83..96787ad94b 100644 --- a/library/cpp/logger/stream.cpp +++ b/library/cpp/logger/stream.cpp @@ -1,19 +1,19 @@ -#include "stream.h" -#include "record.h" - -#include <util/stream/output.h> - +#include "stream.h" +#include "record.h" + +#include <util/stream/output.h> + TStreamLogBackend::TStreamLogBackend(IOutputStream* slave) - : Slave_(slave) -{ -} - + : Slave_(slave) +{ +} + TStreamLogBackend::~TStreamLogBackend() { -} - -void TStreamLogBackend::WriteData(const TLogRecord& rec) { - Slave_->Write(rec.Data, rec.Len); -} - -void TStreamLogBackend::ReopenLog() { -} +} + +void TStreamLogBackend::WriteData(const TLogRecord& rec) { + Slave_->Write(rec.Data, rec.Len); +} + +void TStreamLogBackend::ReopenLog() { +} diff --git a/library/cpp/logger/stream.h b/library/cpp/logger/stream.h index bb89011919..feb240afcb 100644 --- a/library/cpp/logger/stream.h +++ b/library/cpp/logger/stream.h @@ -1,17 +1,17 @@ #pragma once -#include "backend.h" +#include "backend.h" class IOutputStream; - -class TStreamLogBackend: public TLogBackend { -public: + +class TStreamLogBackend: public TLogBackend { +public: TStreamLogBackend(IOutputStream* slave); - ~TStreamLogBackend() override; - - void WriteData(const TLogRecord& rec) override; - void ReopenLog() override; - -private: + ~TStreamLogBackend() override; + + void WriteData(const TLogRecord& rec) override; + void ReopenLog() override; + +private: IOutputStream* Slave_; -}; +}; diff --git a/library/cpp/logger/system.cpp b/library/cpp/logger/system.cpp index ad4ebef1c8..42233f63d2 100644 --- a/library/cpp/logger/system.cpp +++ b/library/cpp/logger/system.cpp @@ -1,59 +1,59 @@ #include <util/stream/output.h> #include <util/stream/null.h> -#include <util/system/compat.h> -#include <util/system/yassert.h> -#include <util/system/defaults.h> -#include <util/generic/singleton.h> +#include <util/system/compat.h> +#include <util/system/yassert.h> +#include <util/system/defaults.h> +#include <util/generic/singleton.h> #include <util/generic/utility.h> - -#if defined(_unix_) -#include <syslog.h> -#endif - -#include "system.h" -#include "record.h" -#include "stream.h" - + +#if defined(_unix_) +#include <syslog.h> +#endif + +#include "system.h" +#include "record.h" +#include "stream.h" + TSysLogBackend::TSysLogBackend(const char* ident, EFacility facility, int flags) : Ident(ident) , Facility(facility) , Flags(flags) { -#if defined(_unix_) +#if defined(_unix_) Y_ASSERT(TSYSLOG_LOCAL0 <= facility && facility <= TSYSLOG_LOCAL7); - + static const int f2sf[] = { - LOG_LOCAL0, - LOG_LOCAL1, - LOG_LOCAL2, - LOG_LOCAL3, - LOG_LOCAL4, - LOG_LOCAL5, - LOG_LOCAL6, - LOG_LOCAL7}; - + LOG_LOCAL0, + LOG_LOCAL1, + LOG_LOCAL2, + LOG_LOCAL3, + LOG_LOCAL4, + LOG_LOCAL5, + LOG_LOCAL6, + LOG_LOCAL7}; + int sysflags = LOG_NDELAY | LOG_PID; - - if (flags & LogPerror) { + + if (flags & LogPerror) { sysflags |= LOG_PERROR; - } - - if (flags & LogCons) { + } + + if (flags & LogCons) { sysflags |= LOG_CONS; - } - + } + openlog(Ident.data(), sysflags, f2sf[(size_t)facility]); #endif } TSysLogBackend::~TSysLogBackend() { -#if defined(_unix_) +#if defined(_unix_) closelog(); #endif } -void TSysLogBackend::WriteData(const TLogRecord& rec) { -#if defined(_unix_) +void TSysLogBackend::WriteData(const TLogRecord& rec) { +#if defined(_unix_) syslog(ELogPriority2SyslogPriority(rec.Priority), "%.*s", (int)rec.Len, rec.Data); #else Y_UNUSED(rec); @@ -71,17 +71,17 @@ int TSysLogBackend::ELogPriority2SyslogPriority(ELogPriority priority) { return int(priority); #endif } - -namespace { - class TSysLogInstance: public TLog { - public: - inline TSysLogInstance() + +namespace { + class TSysLogInstance: public TLog { + public: + inline TSysLogInstance() : TLog(MakeHolder<TStreamLogBackend>(&Cnull)) - { - } - }; -} - -TLog& SysLogInstance() { - return *Singleton<TSysLogInstance>(); -} + { + } + }; +} + +TLog& SysLogInstance() { + return *Singleton<TSysLogInstance>(); +} diff --git a/library/cpp/logger/system.h b/library/cpp/logger/system.h index a4e38dece8..b8c60b3023 100644 --- a/library/cpp/logger/system.h +++ b/library/cpp/logger/system.h @@ -1,66 +1,66 @@ #pragma once -#include "log.h" -#include "backend.h" -#include "priority.h" - -#define YSYSLOG(priority, ...) SysLogInstance().AddLog((priority), __VA_ARGS__) -#define YSYSLOGINIT_FLAGS(ident, facility, flags) \ - struct TLogIniter { \ - TLogIniter() { \ +#include "log.h" +#include "backend.h" +#include "priority.h" + +#define YSYSLOG(priority, ...) SysLogInstance().AddLog((priority), __VA_ARGS__) +#define YSYSLOGINIT_FLAGS(ident, facility, flags) \ + struct TLogIniter { \ + TLogIniter() { \ SysLogInstance().ResetBackend(THolder<TLogBackend>( \ (ident) ? (TLogBackend*)(new TSysLogBackend((ident), (facility), (flags))) : (TLogBackend*)(new TNullLogBackend())));\ - } \ + } \ } Y_CAT(loginit, __LINE__); -#define YSYSLOGINIT(ident, facility) YSYSLOGINIT_FLAGS((ident), (facility), 0) +#define YSYSLOGINIT(ident, facility) YSYSLOGINIT_FLAGS((ident), (facility), 0) class TSysLogBackend: public TLogBackend { -public: - enum EFacility { - TSYSLOG_LOCAL0 = 0, - TSYSLOG_LOCAL1 = 1, - TSYSLOG_LOCAL2 = 2, - TSYSLOG_LOCAL3 = 3, - TSYSLOG_LOCAL4 = 4, - TSYSLOG_LOCAL5 = 5, - TSYSLOG_LOCAL6 = 6, - TSYSLOG_LOCAL7 = 7 - }; +public: + enum EFacility { + TSYSLOG_LOCAL0 = 0, + TSYSLOG_LOCAL1 = 1, + TSYSLOG_LOCAL2 = 2, + TSYSLOG_LOCAL3 = 3, + TSYSLOG_LOCAL4 = 4, + TSYSLOG_LOCAL5 = 5, + TSYSLOG_LOCAL6 = 6, + TSYSLOG_LOCAL7 = 7 + }; - enum EFlags { - LogPerror = 1, - LogCons = 2 - }; + enum EFlags { + LogPerror = 1, + LogCons = 2 + }; - TSysLogBackend(const char* ident, EFacility facility, int flags = 0); - ~TSysLogBackend() override; + TSysLogBackend(const char* ident, EFacility facility, int flags = 0); + ~TSysLogBackend() override; - void WriteData(const TLogRecord& rec) override; - void ReopenLog() override; + void WriteData(const TLogRecord& rec) override; + void ReopenLog() override; virtual TString GetIdent() const { - return Ident; - } + return Ident; + } - virtual EFacility GetFacility() const { - return Facility; - } + virtual EFacility GetFacility() const { + return Facility; + } - virtual int GetFlags() const { - return Flags; - } + virtual int GetFlags() const { + return Flags; + } -protected: +protected: int ELogPriority2SyslogPriority(ELogPriority priority); TString Ident; - EFacility Facility; - int Flags; + EFacility Facility; + int Flags; }; -/* - * return system-wide logger instance - * better do not use in real programs(instead of robot, of course) - */ -TLog& SysLogInstance(); +/* + * return system-wide logger instance + * better do not use in real programs(instead of robot, of course) + */ +TLog& SysLogInstance(); diff --git a/library/cpp/logger/thread.cpp b/library/cpp/logger/thread.cpp index a2650f195a..0ccf9e374b 100644 --- a/library/cpp/logger/thread.cpp +++ b/library/cpp/logger/thread.cpp @@ -1,77 +1,77 @@ -#include "thread.h" -#include "record.h" - +#include "thread.h" +#include "record.h" + #include <util/thread/pool.h> -#include <util/system/event.h> -#include <util/memory/addstorage.h> -#include <util/generic/ptr.h> -#include <util/generic/yexception.h> - -class TThreadedLogBackend::TImpl { - class TRec: public IObjectInQueue, public TAdditionalStorage<TRec>, public TLogRecord { - public: - inline TRec(TImpl* parent, const TLogRecord& rec) - : TLogRecord(rec.Priority, (const char*)AdditionalData(), rec.Len) - , Parent_(parent) - { - memcpy(AdditionalData(), rec.Data, rec.Len); - } - - inline ~TRec() override { - } - - private: - void Process(void* /*tsr*/) override { - THolder<TRec> This(this); - - Parent_->Slave_->WriteData(*this); - } - - private: - TImpl* Parent_; - }; - +#include <util/system/event.h> +#include <util/memory/addstorage.h> +#include <util/generic/ptr.h> +#include <util/generic/yexception.h> + +class TThreadedLogBackend::TImpl { + class TRec: public IObjectInQueue, public TAdditionalStorage<TRec>, public TLogRecord { + public: + inline TRec(TImpl* parent, const TLogRecord& rec) + : TLogRecord(rec.Priority, (const char*)AdditionalData(), rec.Len) + , Parent_(parent) + { + memcpy(AdditionalData(), rec.Data, rec.Len); + } + + inline ~TRec() override { + } + + private: + void Process(void* /*tsr*/) override { + THolder<TRec> This(this); + + Parent_->Slave_->WriteData(*this); + } + + private: + TImpl* Parent_; + }; + class TReopener: public IObjectInQueue, public TSystemEvent, public TAtomicRefCount<TReopener> { - public: - inline TReopener(TImpl* parent) - : Parent_(parent) - { - Ref(); - } - - inline ~TReopener() override { - } - - private: - void Process(void* /*tsr*/) override { - try { - Parent_->Slave_->ReopenLog(); - } catch (...) { - } - - Signal(); - UnRef(); - } - - private: - TImpl* Parent_; - }; - -public: + public: + inline TReopener(TImpl* parent) + : Parent_(parent) + { + Ref(); + } + + inline ~TReopener() override { + } + + private: + void Process(void* /*tsr*/) override { + try { + Parent_->Slave_->ReopenLog(); + } catch (...) { + } + + Signal(); + UnRef(); + } + + private: + TImpl* Parent_; + }; + +public: inline TImpl(TLogBackend* slave, size_t queuelen, std::function<void()> queueOverflowCallback = {}) - : Slave_(slave) + : Slave_(slave) , QueueOverflowCallback_(std::move(queueOverflowCallback)) - { - Queue_.Start(1, queuelen); - } - - inline ~TImpl() { - Queue_.Stop(); - } - - inline void WriteData(const TLogRecord& rec) { - THolder<TRec> obj(new (rec.Len) TRec(this, rec)); - + { + Queue_.Start(1, queuelen); + } + + inline ~TImpl() { + Queue_.Stop(); + } + + inline void WriteData(const TLogRecord& rec) { + THolder<TRec> obj(new (rec.Len) TRec(this, rec)); + if (Queue_.Add(obj.Get())) { Y_UNUSED(obj.Release()); return; @@ -80,28 +80,28 @@ public: if (QueueOverflowCallback_) { QueueOverflowCallback_(); } else { - ythrow yexception() << "log queue exhausted"; + ythrow yexception() << "log queue exhausted"; } - } - - // Write an emergency message when the memory allocator is corrupted. - // The TThreadedLogBackend object can't be used after this method is called. - inline void WriteEmergencyData(const TLogRecord& rec) noexcept { - Queue_.Stop(); - Slave_->WriteData(rec); - } - - inline void ReopenLog() { - TIntrusivePtr<TReopener> reopener(new TReopener(this)); - - if (!Queue_.Add(reopener.Get())) { - reopener->UnRef(); // Ref() was called in constructor - ythrow yexception() << "log queue exhausted"; - } - - reopener->Wait(); - } - + } + + // Write an emergency message when the memory allocator is corrupted. + // The TThreadedLogBackend object can't be used after this method is called. + inline void WriteEmergencyData(const TLogRecord& rec) noexcept { + Queue_.Stop(); + Slave_->WriteData(rec); + } + + inline void ReopenLog() { + TIntrusivePtr<TReopener> reopener(new TReopener(this)); + + if (!Queue_.Add(reopener.Get())) { + reopener->UnRef(); // Ref() was called in constructor + ythrow yexception() << "log queue exhausted"; + } + + reopener->Wait(); + } + inline void ReopenLogNoFlush() { Slave_->ReopenLogNoFlush(); } @@ -110,32 +110,32 @@ public: return Queue_.Size(); } -private: - TLogBackend* Slave_; +private: + TLogBackend* Slave_; TThreadPool Queue_{"ThreadedLogBack"}; const std::function<void()> QueueOverflowCallback_; -}; - -TThreadedLogBackend::TThreadedLogBackend(TLogBackend* slave) - : Impl_(new TImpl(slave, 0)) -{ -} - +}; + +TThreadedLogBackend::TThreadedLogBackend(TLogBackend* slave) + : Impl_(new TImpl(slave, 0)) +{ +} + TThreadedLogBackend::TThreadedLogBackend(TLogBackend* slave, size_t queuelen, std::function<void()> queueOverflowCallback) : Impl_(new TImpl(slave, queuelen, std::move(queueOverflowCallback))) -{ -} - +{ +} + TThreadedLogBackend::~TThreadedLogBackend() { -} - -void TThreadedLogBackend::WriteData(const TLogRecord& rec) { - Impl_->WriteData(rec); -} - -void TThreadedLogBackend::ReopenLog() { - Impl_->ReopenLog(); -} +} + +void TThreadedLogBackend::WriteData(const TLogRecord& rec) { + Impl_->WriteData(rec); +} + +void TThreadedLogBackend::ReopenLog() { + Impl_->ReopenLog(); +} void TThreadedLogBackend::ReopenLogNoFlush() { Impl_->ReopenLogNoFlush(); diff --git a/library/cpp/logger/thread.h b/library/cpp/logger/thread.h index 48cfe1349e..65f7a88e87 100644 --- a/library/cpp/logger/thread.h +++ b/library/cpp/logger/thread.h @@ -1,30 +1,30 @@ #pragma once -#include "backend.h" - +#include "backend.h" + #include <util/generic/ptr.h> - + #include <functional> -class TThreadedLogBackend: public TLogBackend { -public: - TThreadedLogBackend(TLogBackend* slave); +class TThreadedLogBackend: public TLogBackend { +public: + TThreadedLogBackend(TLogBackend* slave); TThreadedLogBackend(TLogBackend* slave, size_t queuelen, std::function<void()> queueOverflowCallback = {}); - ~TThreadedLogBackend() override; - - void WriteData(const TLogRecord& rec) override; - void ReopenLog() override; + ~TThreadedLogBackend() override; + + void WriteData(const TLogRecord& rec) override; + void ReopenLog() override; void ReopenLogNoFlush() override; size_t QueueSize() const override; - - // Write an emergency message when the memory allocator is corrupted. - // The TThreadedLogBackend object can't be used after this method is called. - void WriteEmergencyData(const TLogRecord& rec); - -private: - class TImpl; - THolder<TImpl> Impl_; -}; + + // Write an emergency message when the memory allocator is corrupted. + // The TThreadedLogBackend object can't be used after this method is called. + void WriteEmergencyData(const TLogRecord& rec); + +private: + class TImpl; + THolder<TImpl> Impl_; +}; class TOwningThreadedLogBackend: private THolder<TLogBackend>, public TThreadedLogBackend { public: diff --git a/library/cpp/logger/ya.make b/library/cpp/logger/ya.make index af93cf7412..00a5263cba 100644 --- a/library/cpp/logger/ya.make +++ b/library/cpp/logger/ya.make @@ -4,7 +4,7 @@ OWNER( g:util g:base ) - + LIBRARY() GENERATE_ENUM_SERIALIZATION(priority.h) @@ -13,7 +13,7 @@ PEERDIR ( library/cpp/json ) -SRCS( +SRCS( all.h backend.cpp backend_creator.cpp @@ -24,14 +24,14 @@ SRCS( GLOBAL file_creator.cpp filter.cpp filter_creator.cpp - log.cpp + log.cpp null.cpp GLOBAL null_creator.cpp priority.h record.h rotating_file.cpp GLOBAL rotating_file_creator.cpp - stream.cpp + stream.cpp GLOBAL stream_creator.cpp sync_page_cache_file.cpp GLOBAL sync_page_cache_file_creator.cpp @@ -40,8 +40,8 @@ SRCS( thread.cpp thread_creator.cpp GLOBAL uninitialized_creator.cpp -) - -END() +) + +END() RECURSE_FOR_TESTS(ut) |