Use Abseil logging for Protobuf v22 and above (fix #16).

(cherry picked from commit 93dc2b1878)
This commit is contained in:
Dmitry Plotnikov 2024-02-29 10:06:53 -08:00 committed by Sergey A. Osokin
parent ed031aafe8
commit b845bf4172

View file

@ -3,6 +3,10 @@
#include "grpc_log.hpp"
#include <grpc/support/log.h>
#include <google/protobuf/stubs/common.h>
#if GOOGLE_PROTOBUF_VERSION < 4022000
#include <google/protobuf/stubs/logging.h>
class ProtobufLog {
@ -26,6 +30,43 @@ private:
}
};
#else
#include <absl/log/globals.h>
#include <absl/log/initialize.h>
#include <absl/log/log_sink_registry.h>
class ProtobufLog : absl::LogSink {
public:
ProtobufLog()
{
absl::InitializeLog();
absl::AddLogSink(this);
// Disable logging to stderr
absl::SetStderrThreshold(static_cast<absl::LogSeverity>(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); }