Skip to content

Commit

Permalink
Remove GlobalLogHandlerData and move functions of `GlobalLogHandler…
Browse files Browse the repository at this point in the history
…` into .cc
  • Loading branch information
owent committed Jan 15, 2025
1 parent 5ac5e4f commit f7d1993
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 57 deletions.
47 changes: 4 additions & 43 deletions sdk/include/opentelemetry/sdk/common/global_log_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,73 +93,34 @@ class NoopLogHandler : public LogHandler
*/
class GlobalLogHandler
{
private:
struct GlobalLogHandlerData
{
nostd::shared_ptr<LogHandler> handler;
LogLevel log_level;

GlobalLogHandlerData();
~GlobalLogHandlerData();

GlobalLogHandlerData(const GlobalLogHandlerData &) = delete;
GlobalLogHandlerData(GlobalLogHandlerData &&) = delete;

GlobalLogHandlerData &operator=(const GlobalLogHandlerData &) = delete;
GlobalLogHandlerData &operator=(GlobalLogHandlerData &&) = delete;
};

public:
/**
* Returns the singleton LogHandler.
*
* By default, a default LogHandler is returned.
*/
static inline nostd::shared_ptr<LogHandler> GetLogHandler() noexcept
{
if OPENTELEMETRY_UNLIKELY_CONDITION (IsHandlerAndLevelDestroyed())
{
return nostd::shared_ptr<LogHandler>();
}

return GetHandlerAndLevel().handler;
}
static nostd::shared_ptr<LogHandler> GetLogHandler() noexcept;

/**
* Changes the singleton LogHandler.
* This should be called once at the start of application before creating any Provider
* instance.
*/
static inline void SetLogHandler(const nostd::shared_ptr<LogHandler> &eh) noexcept
{
if OPENTELEMETRY_UNLIKELY_CONDITION (IsHandlerAndLevelDestroyed())
{
return;
}

GetHandlerAndLevel().handler = eh;
}
static void SetLogHandler(const nostd::shared_ptr<LogHandler> &eh) noexcept;

/**
* Returns the singleton log level.
*
* By default, a default log level is returned.
*/
static inline LogLevel GetLogLevel() noexcept { return GetHandlerAndLevel().log_level; }
static LogLevel GetLogLevel() noexcept;

/**
* Changes the singleton Log level.
* This should be called once at the start of application before creating any Provider
* instance.
*/
static inline void SetLogLevel(LogLevel level) noexcept
{
GetHandlerAndLevel().log_level = level;
}

private:
static GlobalLogHandlerData &GetHandlerAndLevel() noexcept;
static bool IsHandlerAndLevelDestroyed() noexcept;
static void SetLogLevel(LogLevel level) noexcept;
};

} // namespace internal_log
Expand Down
68 changes: 55 additions & 13 deletions sdk/src/common/global_log_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,34 @@ namespace internal_log

namespace
{
bool &InternalHandlerAndLevelDestroyedFlag() noexcept
struct GlobalLogHandlerData
{
static bool destroyed = false;
return destroyed;
nostd::shared_ptr<LogHandler> handler;
LogLevel log_level;

GlobalLogHandlerData()
: handler(nostd::shared_ptr<LogHandler>(new DefaultLogHandler)), log_level(LogLevel::Warning)
{}
~GlobalLogHandlerData() { is_singleton_destroyed = true; }

GlobalLogHandlerData(const GlobalLogHandlerData &) = delete;
GlobalLogHandlerData(GlobalLogHandlerData &&) = delete;

GlobalLogHandlerData &operator=(const GlobalLogHandlerData &) = delete;
GlobalLogHandlerData &operator=(GlobalLogHandlerData &&) = delete;

static GlobalLogHandlerData &Instance() noexcept;
static bool is_singleton_destroyed;
};

bool GlobalLogHandlerData::is_singleton_destroyed = false;

GlobalLogHandlerData &GlobalLogHandlerData::Instance() noexcept
{
static GlobalLogHandlerData instance;
return instance;
}

} // namespace

LogHandler::~LogHandler() {}
Expand Down Expand Up @@ -66,24 +89,43 @@ void NoopLogHandler::Handle(LogLevel,
const sdk::common::AttributeMap &) noexcept
{}

GlobalLogHandler::GlobalLogHandlerData::GlobalLogHandlerData()
: handler(nostd::shared_ptr<LogHandler>(new DefaultLogHandler)), log_level(LogLevel::Warning)
{}
nostd::shared_ptr<LogHandler> GlobalLogHandler::GetLogHandler() noexcept
{
if OPENTELEMETRY_UNLIKELY_CONDITION (GlobalLogHandlerData::is_singleton_destroyed)
{
return nostd::shared_ptr<LogHandler>();
}

return GlobalLogHandlerData::Instance().handler;
}

GlobalLogHandler::GlobalLogHandlerData::~GlobalLogHandlerData()
void GlobalLogHandler::SetLogHandler(const nostd::shared_ptr<LogHandler> &eh) noexcept
{
InternalHandlerAndLevelDestroyedFlag() = true;
if OPENTELEMETRY_UNLIKELY_CONDITION (GlobalLogHandlerData::is_singleton_destroyed)
{
return;
}

GlobalLogHandlerData::Instance().handler = eh;
}

GlobalLogHandler::GlobalLogHandlerData &GlobalLogHandler::GetHandlerAndLevel() noexcept
LogLevel GlobalLogHandler::GetLogLevel() noexcept
{
static GlobalLogHandlerData handler_and_level;
return handler_and_level;
if OPENTELEMETRY_UNLIKELY_CONDITION (GlobalLogHandlerData::is_singleton_destroyed)
{
return LogLevel::None;
}

return GlobalLogHandlerData::Instance().log_level;
}

bool GlobalLogHandler::IsHandlerAndLevelDestroyed() noexcept
void GlobalLogHandler::SetLogLevel(LogLevel level) noexcept
{
return InternalHandlerAndLevelDestroyedFlag();
if OPENTELEMETRY_UNLIKELY_CONDITION (GlobalLogHandlerData::is_singleton_destroyed)
{
return;
}
GlobalLogHandlerData::Instance().log_level = level;
}

} // namespace internal_log
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ static GlobalLogHandlerChecker &ConstructChecker()
class CustomLogHandler : public opentelemetry::sdk::common::internal_log::LogHandler
{
public:
~CustomLogHandler()
~CustomLogHandler() override
{
GlobalLogHandlerChecker::custom_handler_destroyed = true;
std::cout << "Custom Gobal Log Handle destroyed\n";
Expand Down

0 comments on commit f7d1993

Please sign in to comment.