diff options
Diffstat (limited to 'library/cpp/yt/error')
| -rw-r--r-- | library/cpp/yt/error/error-inl.h | 20 | ||||
| -rw-r--r-- | library/cpp/yt/error/error.cpp | 42 | ||||
| -rw-r--r-- | library/cpp/yt/error/error.h | 16 |
3 files changed, 58 insertions, 20 deletions
diff --git a/library/cpp/yt/error/error-inl.h b/library/cpp/yt/error/error-inl.h index 1542d008f58..85751e8a3cf 100644 --- a/library/cpp/yt/error/error-inl.h +++ b/library/cpp/yt/error/error-inl.h @@ -604,4 +604,24 @@ void FormatValue(TStringBuilderBase* builder, const TErrorOr<T>& error, TStringB //////////////////////////////////////////////////////////////////////////////// +template <class F, class... As> +auto RunNoExcept(F&& functor, As&&... args) noexcept -> decltype(functor(std::forward<As>(args)...)) +{ + return functor(std::forward<As>(args)...); +} + +//////////////////////////////////////////////////////////////////////////////// + +inline TStringBuf GetWellKnownLoggingTag(const std::exception&) +{ + return "Error"_sb; +} + +inline TStringBuf GetWellKnownLoggingTag(const TError&) +{ + return "Error"_sb; +} + +//////////////////////////////////////////////////////////////////////////////// + } // namespace NYT diff --git a/library/cpp/yt/error/error.cpp b/library/cpp/yt/error/error.cpp index d0a5489ab22..dc5aca7ccd2 100644 --- a/library/cpp/yt/error/error.cpp +++ b/library/cpp/yt/error/error.cpp @@ -5,6 +5,8 @@ #include <library/cpp/yt/error/error_attributes.h> #include <library/cpp/yt/error/origin_attributes.h> +#include <library/cpp/yt/memory/leaky_singleton.h> + #include <library/cpp/yt/string/string.h> #include <library/cpp/yt/system/proc.h> @@ -29,8 +31,20 @@ void FormatValue(TStringBuilderBase* builder, TErrorCode code, TStringBuf spec) constexpr TStringBuf ErrorMessageTruncatedSuffix = "...<message truncated>"; -TError::TEnricher TError::Enricher_; -TError::TFromExceptionEnricher TError::FromExceptionEnricher_; +namespace { + +struct TEnricherStorage +{ + static TEnricherStorage* Get() + { + return LeakySingleton<TEnricherStorage>(); + } + + TError::TEnricher Enricher; + TError::TFromExceptionEnricher FromExceptionEnricher; +}; + +} // namespace //////////////////////////////////////////////////////////////////////////////// @@ -636,11 +650,12 @@ void TError::RegisterEnricher(TEnricher enricher) { // NB: This daisy-chaining strategy is optimal when there's O(1) callbacks. Convert to a vector // if the number grows. - if (!Enricher_) { - Enricher_ = std::move(enricher); + auto* storage = TEnricherStorage::Get(); + if (!storage->Enricher) { + storage->Enricher = std::move(enricher); return; } - Enricher_ = [first = std::move(Enricher_), second = std::move(enricher)] (TError* error) { + storage->Enricher = [first = std::move(storage->Enricher), second = std::move(enricher)] (TError* error) { first(error); second(error); }; @@ -650,12 +665,13 @@ void TError::RegisterFromExceptionEnricher(TFromExceptionEnricher enricher) { // NB: This daisy-chaining strategy is optimal when there's O(1) callbacks. Convert to a vector // if the number grows. - if (!FromExceptionEnricher_) { - FromExceptionEnricher_ = std::move(enricher); + auto* storage = TEnricherStorage::Get(); + if (!storage->FromExceptionEnricher) { + storage->FromExceptionEnricher = std::move(enricher); return; } - FromExceptionEnricher_ = [ - first = std::move(FromExceptionEnricher_), + storage->FromExceptionEnricher = [ + first = std::move(storage->FromExceptionEnricher), second = std::move(enricher) ] (TError* error, const std::exception& exception) { first(error, exception); @@ -676,15 +692,15 @@ void TError::MakeMutable() void TError::Enrich() { - if (Enricher_) { - Enricher_(this); + if (const auto& enricher = TEnricherStorage::Get()->Enricher) { + enricher(this); } } void TError::EnrichFromException(const std::exception& exception) { - if (FromExceptionEnricher_) { - FromExceptionEnricher_(this, exception); + if (const auto& enricher = TEnricherStorage::Get()->FromExceptionEnricher) { + enricher(this, exception); } } diff --git a/library/cpp/yt/error/error.h b/library/cpp/yt/error/error.h index b2d0abacfe8..1e5c2147d82 100644 --- a/library/cpp/yt/error/error.h +++ b/library/cpp/yt/error/error.h @@ -253,9 +253,6 @@ private: void EnrichFromException(const std::exception& exception); friend class TErrorAttributes; - - static TEnricher Enricher_; - static TFromExceptionEnricher FromExceptionEnricher_; }; //////////////////////////////////////////////////////////////////////////////// @@ -478,10 +475,15 @@ void FormatValue(TStringBuilderBase* builder, const TErrorOr<T>& error, TStringB //////////////////////////////////////////////////////////////////////////////// template <class F, class... As> -auto RunNoExcept(F&& functor, As&&... args) noexcept -> decltype(functor(std::forward<As>(args)...)) -{ - return functor(std::forward<As>(args)...); -} +auto RunNoExcept(F&& functor, As&&... args) noexcept -> decltype(functor(std::forward<As>(args)...)); + +//////////////////////////////////////////////////////////////////////////////// + +//! Registers errors as a well-known logging tag (ADL customization point for +//! library/cpp/yt/logging), so that |YT_TLOG_*(...).With(error)| attaches them under +//! the "Error" key, rendered after the message in plain text. +TStringBuf GetWellKnownLoggingTag(const std::exception&); +TStringBuf GetWellKnownLoggingTag(const TError&); //////////////////////////////////////////////////////////////////////////////// |
