From 90a962768e55adcb6cb0d4307a240d4e52ceb454 Mon Sep 17 00:00:00 2001 From: Nikolay Shestakov Date: Thu, 7 Mar 2024 06:02:10 +0000 Subject: [PATCH] extrat method FormatMessage --- ydb/core/client/server/grpc_server.cpp | 32 ++-------------------- ydb/core/grpc_streaming/grpc_streaming.h | 35 ++---------------------- ydb/library/grpc/server/logger.h | 19 +++++++++++++ 3 files changed, 24 insertions(+), 62 deletions(-) diff --git a/ydb/core/client/server/grpc_server.cpp b/ydb/core/client/server/grpc_server.cpp index 6eeedc44d2f1..aab1569456ba 100644 --- a/ydb/core/client/server/grpc_server.cpp +++ b/ydb/core/client/server/grpc_server.cpp @@ -11,8 +11,6 @@ #include -#include - #include #include #include @@ -266,19 +264,8 @@ class TSimpleRequest } void Finish(const TOut& resp, ui32 status) { - auto makeResponseString = [&] { - TString x; - if (NYdbGrpc::LogBodyEnabled) { - google::protobuf::TextFormat::Printer printer; - printer.SetSingleLineMode(true); - printer.PrintToString(resp, &x); - } else { - x = ""; - } - return x; - }; LOG_DEBUG(ActorSystem, NKikimrServices::GRPC_SERVER, "[%p] issuing response Name# %s data# %s peer# %s", this, - Name, makeResponseString().data(), GetPeerName().c_str()); + Name, NYdbGrpc::FormatMessage(resp).data(), GetPeerName().c_str()); ResponseSize = resp.ByteSize(); ResponseStatus = status; StateFunc = &TSimpleRequest::FinishDone; @@ -304,23 +291,8 @@ class TSimpleRequest bool RequestDone(bool ok) { OnAfterCall(); - auto makeRequestString = [&] { - TString resp; - if (ok) { - if (NYdbGrpc::LogBodyEnabled) { - google::protobuf::TextFormat::Printer printer; - printer.SetSingleLineMode(true); - printer.PrintToString(Request, &resp); - } else { - resp = ""; - } - } else { - resp = ""; - } - return resp; - }; LOG_DEBUG(ActorSystem, NKikimrServices::GRPC_SERVER, "[%p] received request Name# %s ok# %s data# %s peer# %s current inflight# %li", this, - Name, ok ? "true" : "false", makeRequestString().data(), GetPeerName().c_str(), Server->GetCurrentInFlight()); + Name, ok ? "true" : "false", NYdbGrpc::FormatMessage(Request, ok).data(), GetPeerName().c_str(), Server->GetCurrentInFlight()); if (Context.c_call() == nullptr) { Y_ABORT_UNLESS(!ok); diff --git a/ydb/core/grpc_streaming/grpc_streaming.h b/ydb/core/grpc_streaming/grpc_streaming.h index 5e95413f7205..25b3a3131887 100644 --- a/ydb/core/grpc_streaming/grpc_streaming.h +++ b/ydb/core/grpc_streaming/grpc_streaming.h @@ -11,7 +11,6 @@ #include #include -#include #include @@ -347,26 +346,10 @@ class TGRpcStreamingRequest final } void OnReadDone(NYdbGrpc::EQueueEventStatus status) { - auto dumpResultText = [&] { - TString text; - if (status == NYdbGrpc::EQueueEventStatus::OK) { - if (NYdbGrpc::LogBodyEnabled) { - google::protobuf::TextFormat::Printer printer; - printer.SetSingleLineMode(true); - printer.PrintToString(ReadInProgress->Record, &text); - } else { - text = ""; - } - } else { - text = ""; - } - return text; - }; - LOG_DEBUG(ActorSystem, LoggerServiceId, "[%p] read finished Name# %s ok# %s data# %s peer# %s", this, Name, status == NYdbGrpc::EQueueEventStatus::OK ? "true" : "false", - dumpResultText().c_str(), + NYdbGrpc::FormatMessage(ReadInProgress->Record, status == NYdbGrpc::EQueueEventStatus::OK).c_str(), this->GetPeerName().c_str()); // Take current in-progress read first @@ -404,29 +387,17 @@ class TGRpcStreamingRequest final } bool Write(TOut&& message, const grpc::WriteOptions& options = { }, const grpc::Status* status = nullptr) { - auto dumpMessageText = [&] { - TString text; - if (NYdbGrpc::LogBodyEnabled) { - google::protobuf::TextFormat::Printer printer; - printer.SetSingleLineMode(true); - printer.PrintToString(message, &text); - } else { - text = ""; - } - return text; - }; - if (status) { LOG_DEBUG(ActorSystem, LoggerServiceId, "[%p] facade write Name# %s data# %s peer# %s grpc status# (%d) message# %s", this, Name, - dumpMessageText().c_str(), + NYdbGrpc::FormatMessage(message).c_str(), this->GetPeerName().c_str(), static_cast(status->error_code()), status->error_message().c_str()); } else { LOG_DEBUG(ActorSystem, LoggerServiceId, "[%p] facade write Name# %s data# %s peer# %s", this, Name, - dumpMessageText().c_str(), + NYdbGrpc::FormatMessage(message).c_str(), this->GetPeerName().c_str()); } diff --git a/ydb/library/grpc/server/logger.h b/ydb/library/grpc/server/logger.h index fb6afc8be808..d2ff80fc71fa 100644 --- a/ydb/library/grpc/server/logger.h +++ b/ydb/library/grpc/server/logger.h @@ -5,6 +5,8 @@ #include #include +#include + namespace NYdbGrpc { @@ -44,4 +46,21 @@ using TLoggerPtr = TIntrusivePtr; logger->Write(ELogPriority::TLOG_INFO, format, __VA_ARGS__); \ } else { } + +inline TString FormatMessage(const NProtoBuf::Message& message, bool ok = true) { + if (ok) { + if (LogBodyEnabled) { + TString text; + google::protobuf::TextFormat::Printer printer; + printer.SetSingleLineMode(true); + printer.PrintToString(message, &text); + return text; + } else { + return ""; + } + } else { + return ""; + } +} + } // namespace NYdbGrpc