Skip to content

Commit

Permalink
Fix global log handle symbols when using dlopen (open-telemetry#1420)
Browse files Browse the repository at this point in the history
  • Loading branch information
owent committed Jun 4, 2022
1 parent 62b65fa commit 7e90dae
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 32 deletions.
4 changes: 4 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions exporters/jaeger/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ cc_library(
tags = ["jaeger"],
deps = [
":jaeger_exporter",
"//sdk/src/common:global_log_handler",
],
)

Expand Down
39 changes: 8 additions & 31 deletions sdk/include/opentelemetry/sdk/common/global_log_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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;
};

/**
Expand All @@ -113,7 +95,7 @@ class GlobalLogHandler
*
* By default, a default LogHandler is returned.
*/
static const nostd::shared_ptr<LogHandler> &GetLogHandler() noexcept
static inline const nostd::shared_ptr<LogHandler> &GetLogHandler() noexcept
{
return GetHandlerAndLevel().first;
}
Expand All @@ -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<LogHandler> eh) noexcept
static inline void SetLogHandler(nostd::shared_ptr<LogHandler> eh) noexcept
{
GetHandlerAndLevel().first = eh;
}
Expand All @@ -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<nostd::shared_ptr<LogHandler>, LogLevel> &GetHandlerAndLevel() noexcept
{
static std::pair<nostd::shared_ptr<LogHandler>, LogLevel> handler_and_level{
nostd::shared_ptr<LogHandler>(new DefaultLogHandler), LogLevel::Warning};
return handler_and_level;
}
static std::pair<nostd::shared_ptr<LogHandler>, LogLevel> &GetHandlerAndLevel() noexcept;
};

} // namespace internal_log
Expand Down
12 changes: 12 additions & 0 deletions sdk/src/common/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
)
2 changes: 1 addition & 1 deletion sdk/src/common/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
57 changes: 57 additions & 0 deletions sdk/src/common/global_log_handler.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#include "opentelemetry/sdk/common/global_log_handler.h"

#include <cstring>
#include <random>

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<nostd::shared_ptr<LogHandler>, LogLevel> &GlobalLogHandler::GetHandlerAndLevel() noexcept
{
static std::pair<nostd::shared_ptr<LogHandler>, LogLevel> handler_and_level{
nostd::shared_ptr<LogHandler>(new DefaultLogHandler), LogLevel::Warning};
return handler_and_level;
}

} // namespace internal_log
} // namespace common
} // namespace sdk
OPENTELEMETRY_END_NAMESPACE
1 change: 1 addition & 0 deletions sdk/src/logs/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ cc_library(
deps = [
"//api",
"//sdk:headers",
"//sdk/src/common:global_log_handler",
"//sdk/src/resource",
],
)
1 change: 1 addition & 0 deletions sdk/src/metrics/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ cc_library(
deps = [
"//api",
"//sdk:headers",
"//sdk/src/common:global_log_handler",
"//sdk/src/resource",
],
)
1 change: 1 addition & 0 deletions sdk/src/trace/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ cc_library(
deps = [
"//api",
"//sdk:headers",
"//sdk/src/common:global_log_handler",
"//sdk/src/common:random",
"//sdk/src/resource",
],
Expand Down
1 change: 1 addition & 0 deletions sdk/test/common/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ cc_test(
deps = [
"//api",
"//sdk:headers",
"//sdk/src/common:global_log_handler",
"@com_google_googletest//:gtest_main",
],
)
Expand Down

0 comments on commit 7e90dae

Please sign in to comment.