diff options
author | Daniil Cherednik <dan.cherednik@gmail.com> | 2023-03-31 10:54:08 +0300 |
---|---|---|
committer | Daniil Cherednik <dan.cherednik@gmail.com> | 2023-03-31 12:28:07 +0300 |
commit | fc1cffcfa7f0497a1f97b384a24bcbf23362f3be (patch) | |
tree | c15f7ab5b9e9b20fd0ef8fc07d598d28e8b32004 /library/cpp/unified_agent_client/logger.cpp | |
parent | 8a749596d40e91c896a1907afcd108d9221fbde1 (diff) | |
download | ydb-fc1cffcfa7f0497a1f97b384a24bcbf23362f3be.tar.gz |
Ydb stable 23-1-1923.1.19
x-stable-origin-commit: c5d5a396e89d0a72e0267a55e93d8404d4fb54fe
Diffstat (limited to 'library/cpp/unified_agent_client/logger.cpp')
-rw-r--r-- | library/cpp/unified_agent_client/logger.cpp | 130 |
1 files changed, 130 insertions, 0 deletions
diff --git a/library/cpp/unified_agent_client/logger.cpp b/library/cpp/unified_agent_client/logger.cpp new file mode 100644 index 0000000000..e9c713f0d0 --- /dev/null +++ b/library/cpp/unified_agent_client/logger.cpp @@ -0,0 +1,130 @@ +#include "logger.h" + +#include <library/cpp/unified_agent_client/clock.h> + +#include <library/cpp/logger/log.h> + +#include <util/datetime/base.h> +#include <util/stream/str.h> +#include <util/system/getpid.h> +#include <util/system/thread.h> + +namespace NUnifiedAgent { + namespace { + TString FormatLogLine(ELogPriority logLevel, const TStringBuf message, const TString& scope) { + TString result; + { + TStringOutput output(result); + output << FormatIsoLocal(TClock::Now()) + << " " << GetPID() + << " " << TThread::CurrentThreadId() + << " " << logLevel; + if (!scope.Empty()) { + output << " " << scope; + } + output << " " << message << "\n"; + } + return result; + } + } + + TLogger::TThrottlerWithLock::TThrottlerWithLock(size_t rateLimitBytes) + : Throttler(rateLimitBytes, rateLimitBytes / 2) + , Lock() + { + } + + bool TLogger::TThrottlerWithLock::TryConsume(double tokens) { + with_lock(Lock) { + return Throttler.TryConsume(tokens); + } + } + + TLogger::TLogger(TLog& log, TFMaybe<size_t> rateLimitBytes) + : DefaultLogContext{log, log.IsNullLog() ? ELogPriority::TLOG_EMERG : log.FiltrationLevel()} + , TracingLogContexts() + , CurrentLogContext_() + , Errors(nullptr) + , DroppedBytes(nullptr) + , Throttler(rateLimitBytes.Defined() ? MakeHolder<TThrottlerWithLock>(*rateLimitBytes) : nullptr) + , Lock() + { + SetCurrentLogContext(DefaultLogContext); + } + + void TLogger::SetCurrentLogContext(TLogContext& logContext) { + CurrentLogContext_.store(logContext.Log.IsNullLog() ? nullptr : &logContext, std::memory_order_release); + } + + void TLogger::Log(TLog& log, ELogPriority logPriority, const TStringBuf message, const TString& scope) const { + try { + const auto logLine = FormatLogLine(logPriority, message, scope); + if (Throttler && &log == &DefaultLogContext.Log && !Throttler->TryConsume(logLine.size())) { + if (DroppedBytes) { + DroppedBytes->Add(logLine.size()); + } + return; + } + log.Write(logPriority, logLine); + } catch (...) { + } + } + + void TLogger::StartTracing(ELogPriority logPriority) noexcept { + with_lock(Lock) { + auto& logContext = GetOrCreateTracingLogContext(logPriority); + SetTracing(logContext, "started"); + } + } + + void TLogger::FinishTracing() noexcept { + with_lock(Lock) { + SetTracing(DefaultLogContext, "finished"); + } + } + + void TLogger::SetTracing(TLogContext& logContext, const char* action) { + // Lock must be held + + SetCurrentLogContext(logContext); + + Log(logContext.Log, + TLOG_INFO, + Sprintf("tracing %s, log priority is set to [%s]", + action, ToString(logContext.Priority).c_str()), + ""); + } + + auto TLogger::GetOrCreateTracingLogContext(ELogPriority logPriority) -> TLogContext& { + // Lock must be held + + for (const auto& c: TracingLogContexts) { + if (c->Priority == logPriority) { + return *c; + } + } + + auto newLogContext = MakeHolder<TLogContext>(); + newLogContext->Log = TLog("cerr", logPriority); + newLogContext->Priority = logPriority; + auto* result = newLogContext.Get(); + TracingLogContexts.push_back(std::move(newLogContext)); + return *result; + } + + TScopeLogger::TScopeLogger() + : Logger(nullptr) + , Scope() + , Errors(nullptr) + { + } + + TScopeLogger::TScopeLogger(TLogger* logger, + const TString& scope, + NMonitoring::TDeprecatedCounter* errors) + : Logger(logger) + , Scope(scope) + , Errors(errors) + { + } +} |