aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp
diff options
context:
space:
mode:
authorbabenko <babenko@yandex-team.com>2024-05-14 09:53:40 +0300
committerbabenko <babenko@yandex-team.com>2024-05-14 10:04:28 +0300
commite7d6ad042a879874814b5b22ff0f6d3e2d983c9a (patch)
tree68e3498a24a0e172e755b2546368f78727ba5ab4 /library/cpp
parent9b8770da7c4d08d912e2a6281b3f073675d8f679 (diff)
downloadydb-e7d6ad042a879874814b5b22ff0f6d3e2d983c9a.tar.gz
Introduce YT_DEFINE_GLOBAL to help avoiding initialization order fiasco; apply to global loggers in yt/yt/core
787f98549edf6e8d46ac63cdb8db0609ccde42da
Diffstat (limited to 'library/cpp')
-rw-r--r--library/cpp/yt/logging/logger-inl.h5
-rw-r--r--library/cpp/yt/logging/logger.h6
-rw-r--r--library/cpp/yt/misc/global.h13
3 files changed, 23 insertions, 1 deletions
diff --git a/library/cpp/yt/logging/logger-inl.h b/library/cpp/yt/logging/logger-inl.h
index d076b3690e..dcf40d9c11 100644
--- a/library/cpp/yt/logging/logger-inl.h
+++ b/library/cpp/yt/logging/logger-inl.h
@@ -64,6 +64,11 @@ Y_FORCE_INLINE bool TLogger::IsLevelEnabled(ELogLevel level) const
return IsLevelEnabledHeavy(level);
}
+Y_FORCE_INLINE const TLogger& TLogger::operator()() const
+{
+ return *this;
+}
+
////////////////////////////////////////////////////////////////////////////////
namespace NDetail {
diff --git a/library/cpp/yt/logging/logger.h b/library/cpp/yt/logging/logger.h
index 0913b9ea08..693ea2b9cd 100644
--- a/library/cpp/yt/logging/logger.h
+++ b/library/cpp/yt/logging/logger.h
@@ -181,6 +181,10 @@ public:
explicit operator bool() const;
+ //! Enables using |Logger| in YT_LOG_* macros as both data members and functions
+ //! (e.g. those introduced by YT_DEFINE_GLOBAL).
+ const TLogger& operator()() const;
+
const TLoggingCategory* GetCategory() const;
//! Validate that level is admitted by logger's own min level
@@ -292,7 +296,7 @@ void LogStructuredEvent(
#define YT_LOG_EVENT_WITH_ANCHOR(logger, level, anchor, ...) \
do { \
- const auto& logger__ = (logger); \
+ const auto& logger__ = (logger)(); \
auto level__ = (level); \
\
if (!logger__.IsLevelEnabled(level__)) { \
diff --git a/library/cpp/yt/misc/global.h b/library/cpp/yt/misc/global.h
new file mode 100644
index 0000000000..df831b50c3
--- /dev/null
+++ b/library/cpp/yt/misc/global.h
@@ -0,0 +1,13 @@
+#pragma once
+
+//! Defines a global variable that is initialized on its first access.
+/*!
+ * In contrast to a usual variable with static storage duration, this one
+ * is not susceptible to initialization order fisco issues.
+ */
+#define YT_DEFINE_GLOBAL(type, name, ...) \
+ inline type& name() \
+ { \
+ static type result(__VA_ARGS__); \
+ return result; \
+ }