From 93dc2b1878aa72d7c92dcb8fc8a08a198067b22d Mon Sep 17 00:00:00 2001 From: Dmitry Plotnikov Date: Thu, 29 Feb 2024 10:06:53 -0800 Subject: [PATCH] Use Abseil logging for Protobuf v22 and above (fix #16). --- src/grpc_log.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/src/grpc_log.cpp b/src/grpc_log.cpp index d6e1150..84d796c 100644 --- a/src/grpc_log.cpp +++ b/src/grpc_log.cpp @@ -3,6 +3,10 @@ #include "grpc_log.hpp" #include +#include + +#if GOOGLE_PROTOBUF_VERSION < 4022000 + #include class ProtobufLog { @@ -26,6 +30,43 @@ private: } }; +#else + +#include +#include +#include + +class ProtobufLog : absl::LogSink { +public: + ProtobufLog() + { + absl::InitializeLog(); + absl::AddLogSink(this); + // Disable logging to stderr + absl::SetStderrThreshold(static_cast(100)); + } + + ~ProtobufLog() override { absl::RemoveLogSink(this); } + + void Send(const absl::LogEntry& entry) override + { + auto severity = entry.log_severity(); + + ngx_uint_t level = + severity == absl::LogSeverity::kFatal ? NGX_LOG_EMERG : + severity == absl::LogSeverity::kError ? NGX_LOG_ERR : + severity == absl::LogSeverity::kWarning ? NGX_LOG_WARN : + /*absl::LogSeverity::kInfo*/ NGX_LOG_INFO; + + ngx_str_t message { entry.text_message().size(), + (u_char*)entry.text_message().data() }; + + ngx_log_error(level, ngx_cycle->log, 0, "OTel/protobuf: %V", &message); + } +}; + +#endif + class GrpcLog { public: GrpcLog() { gpr_set_log_function(grpcLogHandler); }