Skip to content

Commit 1dba952

Browse files
committed
Ignore global handle when singlethon is destroyed
1 parent efa496c commit 1dba952

File tree

2 files changed

+43
-9
lines changed

2 files changed

+43
-9
lines changed

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

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,36 @@ class NoopLogHandler : public LogHandler
9393
*/
9494
class GlobalLogHandler
9595
{
96+
private:
97+
struct GlobalLogHandlerData
98+
{
99+
nostd::shared_ptr<LogHandler> handler;
100+
LogLevel log_level;
101+
bool destroyed;
102+
103+
~GlobalLogHandlerData();
104+
105+
GlobalLogHandlerData(const GlobalLogHandlerData &) = delete;
106+
GlobalLogHandlerData(GlobalLogHandlerData &&) = delete;
107+
108+
GlobalLogHandlerData &operator=(const GlobalLogHandlerData &) = delete;
109+
GlobalLogHandlerData &operator=(GlobalLogHandlerData &&) = delete;
110+
};
111+
96112
public:
97113
/**
98114
* Returns the singleton LogHandler.
99115
*
100116
* By default, a default LogHandler is returned.
101117
*/
102-
static inline const nostd::shared_ptr<LogHandler> &GetLogHandler() noexcept
118+
static inline nostd::shared_ptr<LogHandler> GetLogHandler() noexcept
103119
{
104-
return GetHandlerAndLevel().first;
120+
if OPENTELEMETRY_UNLIKELY_CONDITION (GetHandlerAndLevel().destroyed)
121+
{
122+
return nostd::shared_ptr<LogHandler>();
123+
}
124+
125+
return GetHandlerAndLevel().handler;
105126
}
106127

107128
/**
@@ -111,25 +132,33 @@ class GlobalLogHandler
111132
*/
112133
static inline void SetLogHandler(const nostd::shared_ptr<LogHandler> &eh) noexcept
113134
{
114-
GetHandlerAndLevel().first = eh;
135+
if OPENTELEMETRY_UNLIKELY_CONDITION (GetHandlerAndLevel().destroyed)
136+
{
137+
return;
138+
}
139+
140+
GetHandlerAndLevel().handler = eh;
115141
}
116142

117143
/**
118144
* Returns the singleton log level.
119145
*
120146
* By default, a default log level is returned.
121147
*/
122-
static inline LogLevel GetLogLevel() noexcept { return GetHandlerAndLevel().second; }
148+
static inline LogLevel GetLogLevel() noexcept { return GetHandlerAndLevel().log_level; }
123149

124150
/**
125151
* Changes the singleton Log level.
126152
* This should be called once at the start of application before creating any Provider
127153
* instance.
128154
*/
129-
static inline void SetLogLevel(LogLevel level) noexcept { GetHandlerAndLevel().second = level; }
155+
static inline void SetLogLevel(LogLevel level) noexcept
156+
{
157+
GetHandlerAndLevel().log_level = level;
158+
}
130159

131160
private:
132-
static std::pair<nostd::shared_ptr<LogHandler>, LogLevel> &GetHandlerAndLevel() noexcept;
161+
static GlobalLogHandlerData &GetHandlerAndLevel() noexcept;
133162
};
134163

135164
} // namespace internal_log

sdk/src/common/global_log_handler.cc

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,15 @@ void NoopLogHandler::Handle(LogLevel,
5757
const sdk::common::AttributeMap &) noexcept
5858
{}
5959

60-
std::pair<nostd::shared_ptr<LogHandler>, LogLevel> &GlobalLogHandler::GetHandlerAndLevel() noexcept
60+
GlobalLogHandler::GlobalLogHandlerData::~GlobalLogHandlerData()
6161
{
62-
static std::pair<nostd::shared_ptr<LogHandler>, LogLevel> handler_and_level{
63-
nostd::shared_ptr<LogHandler>(new DefaultLogHandler), LogLevel::Warning};
62+
destroyed = true;
63+
}
64+
65+
GlobalLogHandler::GlobalLogHandlerData &GlobalLogHandler::GetHandlerAndLevel() noexcept
66+
{
67+
static GlobalLogHandlerData handler_and_level{
68+
nostd::shared_ptr<LogHandler>(new DefaultLogHandler), LogLevel::Warning, false};
6469
return handler_and_level;
6570
}
6671

0 commit comments

Comments
 (0)