diff options
author | iddqd <iddqd@yandex-team.ru> | 2022-02-10 16:49:45 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:49:45 +0300 |
commit | 07fce9c5f7771600d0b3d70e1f88fd8a7e164d85 (patch) | |
tree | e4aa4750fbb864d70f8c06cf03d2750e979ea3bf /library/cpp/logger/global | |
parent | af42068bf6cd93c976b80dd0388fa48cdf65da11 (diff) | |
download | ydb-07fce9c5f7771600d0b3d70e1f88fd8a7e164d85.tar.gz |
Restoring authorship annotation for <iddqd@yandex-team.ru>. Commit 1 of 2.
Diffstat (limited to 'library/cpp/logger/global')
-rw-r--r-- | library/cpp/logger/global/common.cpp | 28 | ||||
-rw-r--r-- | library/cpp/logger/global/common.h | 142 | ||||
-rw-r--r-- | library/cpp/logger/global/global.cpp | 16 | ||||
-rw-r--r-- | library/cpp/logger/global/global.h | 70 | ||||
-rw-r--r-- | library/cpp/logger/global/rty_formater.cpp | 44 | ||||
-rw-r--r-- | library/cpp/logger/global/rty_formater.h | 22 | ||||
-rw-r--r-- | library/cpp/logger/global/ya.make | 26 |
7 files changed, 174 insertions, 174 deletions
diff --git a/library/cpp/logger/global/common.cpp b/library/cpp/logger/global/common.cpp index 4fb05c19b4b..9cfd1b03866 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 7dcf650decb..a7a422c7869 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 9fbd10f6661..72991b610b0 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 cbe71b16ea5..762e31297ea 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 305f8470c55..5f171cafded 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 6532e1d7695..57bd86ac3f8 100644 --- a/library/cpp/logger/global/rty_formater.h +++ b/library/cpp/logger/global/rty_formater.h @@ -1,7 +1,7 @@ -#pragma once - -#include "common.h" +#pragma once +#include "common.h" + namespace NMemInfo { struct TMemInfo; } @@ -15,7 +15,7 @@ public: ILoggerFormatter* CreateRtyLoggerFormatter(); -namespace NLoggingImpl { +namespace NLoggingImpl { class TLocalTimeS { public: TLocalTimeS(TInstant instant = TInstant::Now()) @@ -48,14 +48,14 @@ namespace NLoggingImpl { return CreateRtyLoggerFormatter(); } }; -} - +} + class TLoggerFormatterOperator : public NLoggingImpl::TOperatorBase<ILoggerFormatter, NLoggingImpl::TLoggerFormatterTraits> { }; -struct TRTYMessageFormater { +struct TRTYMessageFormater { static bool CheckLoggingContext(TLog& logger, const TLogRecordContext& context); - static TSimpleSharedPtr<TLogElement> StartRecord(TLog& logger, const TLogRecordContext& context, TSimpleSharedPtr<TLogElement> earlier); -}; - -using TRTYLogPreprocessor = TLogRecordPreprocessor<TLogFilter, TRTYMessageFormater>; + static TSimpleSharedPtr<TLogElement> StartRecord(TLog& logger, const TLogRecordContext& context, TSimpleSharedPtr<TLogElement> earlier); +}; + +using TRTYLogPreprocessor = TLogRecordPreprocessor<TLogFilter, TRTYMessageFormater>; diff --git a/library/cpp/logger/global/ya.make b/library/cpp/logger/global/ya.make index 20eb361e728..9563c8801c8 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() |