diff options
author | max42 <[email protected]> | 2023-07-29 00:02:16 +0300 |
---|---|---|
committer | max42 <[email protected]> | 2023-07-29 00:02:16 +0300 |
commit | 73b89de71748a21e102d27b9f3ed1bf658766cb5 (patch) | |
tree | 188bbd2d622fa91cdcbb1b6d6d77fbc84a0646f5 /library/cpp/yt/logging/backends | |
parent | 528e321bcc2a2b67b53aeba58c3bd88305a141ee (diff) |
YT-19210: expose YQL shared library for YT.
After this, a new target libyqlplugin.so appears. in open-source cmake build.
Diff in open-source YDB repo looks like the following: https://paste.yandex-team.ru/f302bdb4-7ef2-4362-91c7-6ca45f329264
Diffstat (limited to 'library/cpp/yt/logging/backends')
9 files changed, 297 insertions, 0 deletions
diff --git a/library/cpp/yt/logging/backends/arcadia/backend.cpp b/library/cpp/yt/logging/backends/arcadia/backend.cpp new file mode 100644 index 00000000000..3c6ff9f5f58 --- /dev/null +++ b/library/cpp/yt/logging/backends/arcadia/backend.cpp @@ -0,0 +1,86 @@ +#include "backend.h" + +#include <library/cpp/logger/backend.h> +#include <library/cpp/logger/record.h> + +#include <library/cpp/yt/assert/assert.h> + +#include <library/cpp/yt/logging/logger.h> + +namespace NYT::NLogging { +namespace { + +//////////////////////////////////////////////////////////////////////////////// + +ELogLevel ConvertToLogLevel(ELogPriority priority) +{ + switch (priority) { + case ELogPriority::TLOG_DEBUG: + return ELogLevel::Debug; + case ELogPriority::TLOG_INFO: + [[fallthrough]]; + case ELogPriority::TLOG_NOTICE: + return ELogLevel::Info; + case ELogPriority::TLOG_WARNING: + return ELogLevel::Warning; + case ELogPriority::TLOG_ERR: + return ELogLevel::Error; + case ELogPriority::TLOG_CRIT: + case ELogPriority::TLOG_ALERT: + return ELogLevel::Alert; + case ELogPriority::TLOG_EMERG: + return ELogLevel::Fatal; + case ELogPriority::TLOG_RESOURCES: + return ELogLevel::Maximum; + } + YT_ABORT(); +} + +class TLogBackendBridge + : public TLogBackend +{ +public: + TLogBackendBridge(const TLogger& logger) + : Logger_(logger) + { } + + void WriteData(const TLogRecord& rec) override + { + const auto logLevel = ConvertToLogLevel(rec.Priority); + if (!Logger_.IsLevelEnabled(logLevel)) { + return; + } + + // Remove trailing \n, because it will add it. + TStringBuf message(rec.Data, rec.Len); + message.ChopSuffix(TStringBuf("\n")); + // Use low-level api, because it is more convinient here. + auto loggingContext = GetLoggingContext(); + auto event = NDetail::CreateLogEvent(loggingContext, Logger_, logLevel); + event.MessageRef = NDetail::BuildLogMessage(loggingContext, Logger_, message).MessageRef; + event.Family = ELogFamily::PlainText; + Logger_.Write(std::move(event)); + } + + void ReopenLog() override + { } + + ELogPriority FiltrationLevel() const override + { + return LOG_MAX_PRIORITY; + } + +private: + const TLogger Logger_; +}; + +} // namespace + +THolder<TLogBackend> CreateArcadiaLogBackend(const TLogger& logger) +{ + return MakeHolder<TLogBackendBridge>(logger); +} + +//////////////////////////////////////////////////////////////////////////////// + +} // namespace NYT::NLogging diff --git a/library/cpp/yt/logging/backends/arcadia/backend.h b/library/cpp/yt/logging/backends/arcadia/backend.h new file mode 100644 index 00000000000..251918c9726 --- /dev/null +++ b/library/cpp/yt/logging/backends/arcadia/backend.h @@ -0,0 +1,18 @@ +#pragma once + +#include <library/cpp/yt/logging/public.h> + +#include <util/generic/ptr.h> + +class TLogBackend; + +namespace NYT::NLogging { + +//////////////////////////////////////////////////////////////////////////////// + +//! Create TLogBackend which redirects log messages to |logger|. +THolder<TLogBackend> CreateArcadiaLogBackend(const TLogger& logger); + +//////////////////////////////////////////////////////////////////////////////// + +} // namespace NYT::NLogging diff --git a/library/cpp/yt/logging/backends/arcadia/ya.make b/library/cpp/yt/logging/backends/arcadia/ya.make new file mode 100644 index 00000000000..ee90be8108f --- /dev/null +++ b/library/cpp/yt/logging/backends/arcadia/ya.make @@ -0,0 +1,16 @@ +LIBRARY() + +INCLUDE(${ARCADIA_ROOT}/library/cpp/yt/ya_cpp.make.inc) + +SRCS( + backend.cpp +) + +PEERDIR( + library/cpp/yt/assert + library/cpp/yt/logging + + library/cpp/logger +) + +END() diff --git a/library/cpp/yt/logging/backends/stream/stream_log_manager.cpp b/library/cpp/yt/logging/backends/stream/stream_log_manager.cpp new file mode 100644 index 00000000000..62269dc0c0d --- /dev/null +++ b/library/cpp/yt/logging/backends/stream/stream_log_manager.cpp @@ -0,0 +1,87 @@ +#include "stream_log_manager.h" + +#include <library/cpp/yt/logging/logger.h> + +#include <library/cpp/yt/logging/plain_text_formatter/formatter.h> + +#include <library/cpp/yt/string/raw_formatter.h> + +#include <library/cpp/yt/threading/fork_aware_spin_lock.h> + +namespace NYT::NLogging { + +//////////////////////////////////////////////////////////////////////////////// + +class TStreamLogManager + : public ILogManager +{ +public: + explicit TStreamLogManager(IOutputStream* output) + : Output_(output) + { } + + void RegisterStaticAnchor( + TLoggingAnchor* anchor, + ::TSourceLocation /*sourceLocation*/, + TStringBuf /*anchorMessage*/) override + { + anchor->Registered = true; + } + + virtual void UpdateAnchor(TLoggingAnchor* anchor) override + { + anchor->Enabled = true; + } + + virtual void Enqueue(TLogEvent&& event) override + { + Buffer_.Reset(); + EventFormatter_.Format(&Buffer_, event); + *Output_ << Buffer_.GetBuffer() << Endl; + } + + virtual const TLoggingCategory* GetCategory(TStringBuf categoryName) override + { + if (!categoryName) { + return nullptr; + } + + auto guard = Guard(SpinLock_); + auto it = NameToCategory_.find(categoryName); + if (it == NameToCategory_.end()) { + auto category = std::make_unique<TLoggingCategory>(); + category->Name = categoryName; + category->ActualVersion = &Version_; + category->CurrentVersion = Version_.load(); + it = NameToCategory_.emplace(categoryName, std::move(category)).first; + } + return it->second.get(); + } + + virtual void UpdateCategory(TLoggingCategory* /*category*/) override + { } + + virtual bool GetAbortOnAlert() const override + { + return false; + } + +private: + IOutputStream* const Output_; + + NThreading::TForkAwareSpinLock SpinLock_; + THashMap<TString, std::unique_ptr<TLoggingCategory>> NameToCategory_; + std::atomic<int> Version_ = 1; + + TPlainTextEventFormatter EventFormatter_{/*enableSourceLocation*/ false}; + TRawFormatter<MessageBufferSize> Buffer_; +}; + +std::unique_ptr<ILogManager> CreateStreamLogManager(IOutputStream* output) +{ + return std::make_unique<TStreamLogManager>(output); +} + +//////////////////////////////////////////////////////////////////////////////// + +} // namespace NYT::NLogging diff --git a/library/cpp/yt/logging/backends/stream/stream_log_manager.h b/library/cpp/yt/logging/backends/stream/stream_log_manager.h new file mode 100644 index 00000000000..2f6794e5878 --- /dev/null +++ b/library/cpp/yt/logging/backends/stream/stream_log_manager.h @@ -0,0 +1,15 @@ +#pragma once + +#include <library/cpp/yt/logging/public.h> + +namespace NYT::NLogging { + +//////////////////////////////////////////////////////////////////////////////// + +//! Creates a dead-simple implementation that synchronously logs +//! all events to #output. +std::unique_ptr<ILogManager> CreateStreamLogManager(IOutputStream* output); + +//////////////////////////////////////////////////////////////////////////////// + +} // namespace NYT::NLogging diff --git a/library/cpp/yt/logging/backends/stream/unittests/stream_log_manager_ut.cpp b/library/cpp/yt/logging/backends/stream/unittests/stream_log_manager_ut.cpp new file mode 100644 index 00000000000..cb3e244e3b9 --- /dev/null +++ b/library/cpp/yt/logging/backends/stream/unittests/stream_log_manager_ut.cpp @@ -0,0 +1,37 @@ +#include <library/cpp/testing/gtest/gtest.h> + +#include <library/cpp/yt/logging/logger.h> + +#include <library/cpp/yt/logging/backends/stream/stream_log_manager.h> + +#include <util/stream/str.h> + +#include <util/string/split.h> + +namespace NYT::NLogging { +namespace { + +//////////////////////////////////////////////////////////////////////////////// + +TEST(TStreamLogManagerTest, Simple) +{ + TString str; + { + TStringOutput output(str); + auto logManager = CreateStreamLogManager(&output); + TLogger Logger(logManager.get(), "Test"); + YT_LOG_INFO("Hello world"); + } + + TVector<TStringBuf> tokens; + Split(str, "\t", tokens); + EXPECT_GE(std::ssize(tokens), 4); + EXPECT_EQ(tokens[1], "I"); + EXPECT_EQ(tokens[2], "Test"); + EXPECT_EQ(tokens[3], "Hello world"); +} + +//////////////////////////////////////////////////////////////////////////////// + +} // namespace +} // namespace NYT::NLogging diff --git a/library/cpp/yt/logging/backends/stream/unittests/ya.make b/library/cpp/yt/logging/backends/stream/unittests/ya.make new file mode 100644 index 00000000000..29270459faa --- /dev/null +++ b/library/cpp/yt/logging/backends/stream/unittests/ya.make @@ -0,0 +1,14 @@ +GTEST(unittester-library-logging) + +INCLUDE(${ARCADIA_ROOT}/library/cpp/yt/ya_cpp.make.inc) + +SRCS( + stream_log_manager_ut.cpp +) + +PEERDIR( + library/cpp/testing/gtest + library/cpp/yt/logging/backends/stream +) + +END() diff --git a/library/cpp/yt/logging/backends/stream/ya.make b/library/cpp/yt/logging/backends/stream/ya.make new file mode 100644 index 00000000000..86e3aa046b9 --- /dev/null +++ b/library/cpp/yt/logging/backends/stream/ya.make @@ -0,0 +1,20 @@ +LIBRARY() + +INCLUDE(${ARCADIA_ROOT}/library/cpp/yt/ya_cpp.make.inc) + +SRCS( + stream_log_manager.cpp +) + +PEERDIR( + library/cpp/yt/logging + library/cpp/yt/logging/plain_text_formatter + library/cpp/yt/string + library/cpp/yt/threading +) + +END() + +RECURSE_FOR_TESTS( + unittests +) diff --git a/library/cpp/yt/logging/backends/ya.make b/library/cpp/yt/logging/backends/ya.make new file mode 100644 index 00000000000..6ee4b72bfe3 --- /dev/null +++ b/library/cpp/yt/logging/backends/ya.make @@ -0,0 +1,4 @@ +RECURSE( + arcadia + stream +) |