From 7e90dae3e4e72f8ba62859a25b4e773b182e4dce Mon Sep 17 00:00:00 2001 From: WenTao Ou Date: Sat, 4 Jun 2022 12:30:20 +0800 Subject: [PATCH] Fix global log handle symbols when using dlopen (#1420) --- .gitattributes | 4 ++ exporters/jaeger/BUILD | 1 + .../sdk/common/global_log_handler.h | 39 +++---------- sdk/src/common/BUILD | 12 ++++ sdk/src/common/CMakeLists.txt | 2 +- sdk/src/common/global_log_handler.cc | 57 +++++++++++++++++++ sdk/src/logs/BUILD | 1 + sdk/src/metrics/BUILD | 1 + sdk/src/trace/BUILD | 1 + sdk/test/common/BUILD | 1 + 10 files changed, 87 insertions(+), 32 deletions(-) diff --git a/.gitattributes b/.gitattributes index 03f42e5e1f..ca2467b474 100644 --- a/.gitattributes +++ b/.gitattributes @@ -41,3 +41,7 @@ LICENSE* text ## git files .gitignore text eol=lf .gitattributes text eol=lf + +## bazel files +WORKSPACE text eol=lf +BUILD text eol=lf diff --git a/exporters/jaeger/BUILD b/exporters/jaeger/BUILD index d373aa35b5..cc1babae06 100644 --- a/exporters/jaeger/BUILD +++ b/exporters/jaeger/BUILD @@ -180,6 +180,7 @@ cc_library( tags = ["jaeger"], deps = [ ":jaeger_exporter", + "//sdk/src/common:global_log_handler", ], ) diff --git a/sdk/include/opentelemetry/sdk/common/global_log_handler.h b/sdk/include/opentelemetry/sdk/common/global_log_handler.h index 612d21eca5..99cf48b820 100644 --- a/sdk/include/opentelemetry/sdk/common/global_log_handler.h +++ b/sdk/include/opentelemetry/sdk/common/global_log_handler.h @@ -55,7 +55,7 @@ inline std::string LevelToString(LogLevel level) class LogHandler { public: - virtual ~LogHandler() = default; + virtual ~LogHandler(); virtual void Handle(LogLevel level, const char *file, @@ -71,22 +71,7 @@ class DefaultLogHandler : public LogHandler const char *file, int line, const char *msg, - const sdk::common::AttributeMap &attributes) noexcept override - { - std::stringstream output_s; - output_s << "[" << LevelToString(level) << "] "; - if (file != nullptr) - { - output_s << "File: " << file << ":" << line; - } - if (msg != nullptr) - { - output_s << msg; - } - output_s << std::endl; - // TBD - print attributes - std::cout << output_s.str(); // thread safe. - } + const sdk::common::AttributeMap &attributes) noexcept override; }; class NoopLogHandler : public LogHandler @@ -96,10 +81,7 @@ class NoopLogHandler : public LogHandler const char *file, int line, const char *msg, - const sdk::common::AttributeMap &error_attributes) noexcept override - { - // ignore the log message - } + const sdk::common::AttributeMap &error_attributes) noexcept override; }; /** @@ -113,7 +95,7 @@ class GlobalLogHandler * * By default, a default LogHandler is returned. */ - static const nostd::shared_ptr &GetLogHandler() noexcept + static inline const nostd::shared_ptr &GetLogHandler() noexcept { return GetHandlerAndLevel().first; } @@ -123,7 +105,7 @@ class GlobalLogHandler * This should be called once at the start of application before creating any Provider * instance. */ - static void SetLogHandler(nostd::shared_ptr eh) noexcept + static inline void SetLogHandler(nostd::shared_ptr eh) noexcept { GetHandlerAndLevel().first = eh; } @@ -133,22 +115,17 @@ class GlobalLogHandler * * By default, a default log level is returned. */ - static LogLevel GetLogLevel() noexcept { return GetHandlerAndLevel().second; } + static inline LogLevel GetLogLevel() noexcept { return GetHandlerAndLevel().second; } /** * Changes the singleton Log level. * This should be called once at the start of application before creating any Provider * instance. */ - static void SetLogLevel(LogLevel level) noexcept { GetHandlerAndLevel().second = level; } + static inline void SetLogLevel(LogLevel level) noexcept { GetHandlerAndLevel().second = level; } private: - static std::pair, LogLevel> &GetHandlerAndLevel() noexcept - { - static std::pair, LogLevel> handler_and_level{ - nostd::shared_ptr(new DefaultLogHandler), LogLevel::Warning}; - return handler_and_level; - } + static std::pair, LogLevel> &GetHandlerAndLevel() noexcept; }; } // namespace internal_log diff --git a/sdk/src/common/BUILD b/sdk/src/common/BUILD index b0724c3810..b8369fc6f4 100644 --- a/sdk/src/common/BUILD +++ b/sdk/src/common/BUILD @@ -27,6 +27,18 @@ cc_library( include_prefix = "src/common", deps = [ "//api", + "//sdk:headers", "//sdk/src/common/platform:fork", ], ) + +cc_library( + name = "global_log_handler", + srcs = [ + "global_log_handler.cc", + ], + deps = [ + "//api", + "//sdk:headers", + ], +) diff --git a/sdk/src/common/CMakeLists.txt b/sdk/src/common/CMakeLists.txt index debffef495..d8674353b6 100644 --- a/sdk/src/common/CMakeLists.txt +++ b/sdk/src/common/CMakeLists.txt @@ -1,4 +1,4 @@ -set(COMMON_SRCS random.cc core.cc) +set(COMMON_SRCS random.cc core.cc global_log_handler.cc) if(WIN32) list(APPEND COMMON_SRCS platform/fork_windows.cc) else() diff --git a/sdk/src/common/global_log_handler.cc b/sdk/src/common/global_log_handler.cc index e69de29bb2..c86b652c70 100644 --- a/sdk/src/common/global_log_handler.cc +++ b/sdk/src/common/global_log_handler.cc @@ -0,0 +1,57 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +#include "opentelemetry/sdk/common/global_log_handler.h" + +#include +#include + +OPENTELEMETRY_BEGIN_NAMESPACE +namespace sdk +{ +namespace common +{ +namespace internal_log +{ + +LogHandler::~LogHandler() {} + +void DefaultLogHandler::Handle(LogLevel level, + const char *file, + int line, + const char *msg, + const sdk::common::AttributeMap &attributes) noexcept +{ + std::stringstream output_s; + output_s << "[" << LevelToString(level) << "] "; + if (file != nullptr) + { + output_s << "File: " << file << ":" << line; + } + if (msg != nullptr) + { + output_s << msg; + } + output_s << std::endl; + // TBD - print attributes + std::cout << output_s.str(); // thread safe. +} + +void NoopLogHandler::Handle(LogLevel, + const char *, + int, + const char *, + const sdk::common::AttributeMap &) noexcept +{} + +std::pair, LogLevel> &GlobalLogHandler::GetHandlerAndLevel() noexcept +{ + static std::pair, LogLevel> handler_and_level{ + nostd::shared_ptr(new DefaultLogHandler), LogLevel::Warning}; + return handler_and_level; +} + +} // namespace internal_log +} // namespace common +} // namespace sdk +OPENTELEMETRY_END_NAMESPACE diff --git a/sdk/src/logs/BUILD b/sdk/src/logs/BUILD index ac51799125..53465cb8be 100644 --- a/sdk/src/logs/BUILD +++ b/sdk/src/logs/BUILD @@ -22,6 +22,7 @@ cc_library( deps = [ "//api", "//sdk:headers", + "//sdk/src/common:global_log_handler", "//sdk/src/resource", ], ) diff --git a/sdk/src/metrics/BUILD b/sdk/src/metrics/BUILD index d21daa765c..e75ba6a1ac 100644 --- a/sdk/src/metrics/BUILD +++ b/sdk/src/metrics/BUILD @@ -22,6 +22,7 @@ cc_library( deps = [ "//api", "//sdk:headers", + "//sdk/src/common:global_log_handler", "//sdk/src/resource", ], ) diff --git a/sdk/src/trace/BUILD b/sdk/src/trace/BUILD index 34ec5108d2..362680b744 100644 --- a/sdk/src/trace/BUILD +++ b/sdk/src/trace/BUILD @@ -22,6 +22,7 @@ cc_library( deps = [ "//api", "//sdk:headers", + "//sdk/src/common:global_log_handler", "//sdk/src/common:random", "//sdk/src/resource", ], diff --git a/sdk/test/common/BUILD b/sdk/test/common/BUILD index 8a98e5617d..91d56996fb 100644 --- a/sdk/test/common/BUILD +++ b/sdk/test/common/BUILD @@ -124,6 +124,7 @@ cc_test( deps = [ "//api", "//sdk:headers", + "//sdk/src/common:global_log_handler", "@com_google_googletest//:gtest_main", ], )