1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
10 #include "opentelemetry/nostd/shared_ptr.h"
11 #include "opentelemetry/sdk/common/attribute_utils.h"
12 #include "opentelemetry/version.h"
14 #define OTEL_INTERNAL_LOG_LEVEL_ERROR 0
15 #define OTEL_INTERNAL_LOG_LEVEL_WARN 1
16 #define OTEL_INTERNAL_LOG_LEVEL_INFO 2
17 #define OTEL_INTERNAL_LOG_LEVEL_DEBUG 3
18 #ifndef OTEL_INTERNAL_LOG_LEVEL
19 // DEBUG by default, we can change log level on runtime
20 # define OTEL_INTERNAL_LOG_LEVEL OTEL_INTERNAL_LOG_LEVEL_DEBUG
23 OPENTELEMETRY_BEGIN_NAMESPACE
28 namespace internal_log
39 inline std::string
LevelToString(LogLevel level
)
45 case LogLevel::Warning
:
58 virtual ~LogHandler();
60 virtual void Handle(LogLevel level
,
64 const sdk::common::AttributeMap
&attributes
) noexcept
= 0;
67 class DefaultLogHandler
: public LogHandler
70 void Handle(LogLevel level
,
74 const sdk::common::AttributeMap
&attributes
) noexcept override
;
77 class NoopLogHandler
: public LogHandler
80 void Handle(LogLevel level
,
84 const sdk::common::AttributeMap
&error_attributes
) noexcept override
;
88 * Stores the singleton global LogHandler.
90 class GlobalLogHandler
94 * Returns the singleton LogHandler.
96 * By default, a default LogHandler is returned.
98 static inline const nostd::shared_ptr
<LogHandler
> &GetLogHandler() noexcept
100 return GetHandlerAndLevel().first
;
104 * Changes the singleton LogHandler.
105 * This should be called once at the start of application before creating any Provider
108 static inline void SetLogHandler(nostd::shared_ptr
<LogHandler
> eh
) noexcept
110 GetHandlerAndLevel().first
= eh
;
114 * Returns the singleton log level.
116 * By default, a default log level is returned.
118 static inline LogLevel
GetLogLevel() noexcept
{ return GetHandlerAndLevel().second
; }
121 * Changes the singleton Log level.
122 * This should be called once at the start of application before creating any Provider
125 static inline void SetLogLevel(LogLevel level
) noexcept
{ GetHandlerAndLevel().second
= level
; }
128 static std::pair
<nostd::shared_ptr
<LogHandler
>, LogLevel
> &GetHandlerAndLevel() noexcept
;
131 } // namespace internal_log
132 } // namespace common
134 OPENTELEMETRY_END_NAMESPACE
137 * We can not decide the destroying order of signaltons.
138 * Which means, the destructors of other singletons (GlobalLogHandler,TracerProvider and etc.)
139 * may be called after destroying of global LogHandler and use OTEL_INTERNAL_LOG_* in it.We can do
140 * nothing but ignore the log in this situation.
142 #define OTEL_INTERNAL_LOG_DISPATCH(level, message, attributes) \
145 using opentelemetry::sdk::common::internal_log::GlobalLogHandler; \
146 using opentelemetry::sdk::common::internal_log::LogHandler; \
147 if (level > GlobalLogHandler::GetLogLevel()) \
151 const opentelemetry::nostd::shared_ptr<LogHandler> &log_handler = \
152 GlobalLogHandler::GetLogHandler(); \
157 std::stringstream tmp_stream; \
158 tmp_stream << message; \
159 log_handler->Handle(level, __FILE__, __LINE__, tmp_stream.str().c_str(), attributes); \
162 #define OTEL_INTERNAL_LOG_GET_3RD_ARG(arg1, arg2, arg3, ...) arg3
164 #if OTEL_INTERNAL_LOG_LEVEL >= OTEL_INTERNAL_LOG_LEVEL_ERROR
165 # define OTEL_INTERNAL_LOG_ERROR_1_ARGS(message) \
166 OTEL_INTERNAL_LOG_DISPATCH(opentelemetry::sdk::common::internal_log::LogLevel::Error, message, \
168 # define OTEL_INTERNAL_LOG_ERROR_2_ARGS(message, attributes) \
169 OTEL_INTERNAL_LOG_DISPATCH(opentelemetry::sdk::common::internal_log::LogLevel::Error, message, \
171 # define OTEL_INTERNAL_LOG_ERROR_MACRO(...) \
172 OTEL_INTERNAL_LOG_GET_3RD_ARG(__VA_ARGS__, OTEL_INTERNAL_LOG_ERROR_2_ARGS, \
173 OTEL_INTERNAL_LOG_ERROR_1_ARGS)
174 # define OTEL_INTERNAL_LOG_ERROR(...) OTEL_INTERNAL_LOG_ERROR_MACRO(__VA_ARGS__)(__VA_ARGS__)
176 # define OTEL_INTERNAL_LOG_ERROR(...)
179 #if OTEL_INTERNAL_LOG_LEVEL >= OTEL_INTERNAL_LOG_LEVEL_WARN
180 # define OTEL_INTERNAL_LOG_WARN_1_ARGS(message) \
181 OTEL_INTERNAL_LOG_DISPATCH(opentelemetry::sdk::common::internal_log::LogLevel::Warning, \
183 # define OTEL_INTERNAL_LOG_WARN_2_ARGS(message, attributes) \
184 OTEL_INTERNAL_LOG_DISPATCH(opentelemetry::sdk::common::internal_log::LogLevel::Warning, \
186 # define OTEL_INTERNAL_LOG_WARN_MACRO(...) \
187 OTEL_INTERNAL_LOG_GET_3RD_ARG(__VA_ARGS__, OTEL_INTERNAL_LOG_WARN_2_ARGS, \
188 OTEL_INTERNAL_LOG_WARN_1_ARGS)
189 # define OTEL_INTERNAL_LOG_WARN(...) OTEL_INTERNAL_LOG_WARN_MACRO(__VA_ARGS__)(__VA_ARGS__)
191 # define OTEL_INTERNAL_LOG_ERROR(...)
194 #if OTEL_INTERNAL_LOG_LEVEL >= OTEL_INTERNAL_LOG_LEVEL_DEBUG
195 # define OTEL_INTERNAL_LOG_DEBUG_1_ARGS(message) \
196 OTEL_INTERNAL_LOG_DISPATCH(opentelemetry::sdk::common::internal_log::LogLevel::Debug, message, \
198 # define OTEL_INTERNAL_LOG_DEBUG_2_ARGS(message, attributes) \
199 OTEL_INTERNAL_LOG_DISPATCH(opentelemetry::sdk::common::internal_log::LogLevel::Debug, message, \
201 # define OTEL_INTERNAL_LOG_DEBUG_MACRO(...) \
202 OTEL_INTERNAL_LOG_GET_3RD_ARG(__VA_ARGS__, OTEL_INTERNAL_LOG_DEBUG_2_ARGS, \
203 OTEL_INTERNAL_LOG_DEBUG_1_ARGS)
204 # define OTEL_INTERNAL_LOG_DEBUG(...) OTEL_INTERNAL_LOG_DEBUG_MACRO(__VA_ARGS__)(__VA_ARGS__)
206 # define OTEL_INTERNAL_LOG_DEBUG(...)
209 #if OTEL_INTERNAL_LOG_LEVEL >= OTEL_INTERNAL_LOG_LEVEL_INFO
210 # define OTEL_INTERNAL_LOG_INFO_1_ARGS(message) \
211 OTEL_INTERNAL_LOG_DISPATCH(opentelemetry::sdk::common::internal_log::LogLevel::Info, message, \
213 # define OTEL_INTERNAL_LOG_INFO_2_ARGS(message, attributes) \
214 OTEL_INTERNAL_LOG_DISPATCH(opentelemetry::sdk::common::internal_log::LogLevel::Info, message, \
216 # define OTEL_INTERNAL_LOG_INFO_MACRO(...) \
217 OTEL_INTERNAL_LOG_GET_3RD_ARG(__VA_ARGS__, OTEL_INTERNAL_LOG_INFO_2_ARGS, \
218 OTEL_INTERNAL_LOG_INFO_1_ARGS)
219 # define OTEL_INTERNAL_LOG_INFO(...) OTEL_INTERNAL_LOG_INFO_MACRO(__VA_ARGS__)(__VA_ARGS__)
221 # define OTEL_INTERNAL_LOG_INFO(...)