]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/opentelemetry-cpp/sdk/include/opentelemetry/sdk/common/global_log_handler.h
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / jaegertracing / opentelemetry-cpp / sdk / include / opentelemetry / sdk / common / global_log_handler.h
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 #pragma once
5
6 #include <iostream>
7 #include <sstream>
8 #include <utility>
9
10 #include "opentelemetry/nostd/shared_ptr.h"
11 #include "opentelemetry/sdk/common/attribute_utils.h"
12 #include "opentelemetry/version.h"
13
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
21 #endif
22
23 OPENTELEMETRY_BEGIN_NAMESPACE
24 namespace sdk
25 {
26 namespace common
27 {
28 namespace internal_log
29 {
30
31 enum class LogLevel
32 {
33 Error = 0,
34 Warning,
35 Info,
36 Debug
37 };
38
39 inline std::string LevelToString(LogLevel level)
40 {
41 switch (level)
42 {
43 case LogLevel::Error:
44 return "Error";
45 case LogLevel::Warning:
46 return "Warning";
47 case LogLevel::Info:
48 return "Info";
49 case LogLevel::Debug:
50 return "Debug";
51 }
52 return {};
53 }
54
55 class LogHandler
56 {
57 public:
58 virtual ~LogHandler();
59
60 virtual void Handle(LogLevel level,
61 const char *file,
62 int line,
63 const char *msg,
64 const sdk::common::AttributeMap &attributes) noexcept = 0;
65 };
66
67 class DefaultLogHandler : public LogHandler
68 {
69 public:
70 void Handle(LogLevel level,
71 const char *file,
72 int line,
73 const char *msg,
74 const sdk::common::AttributeMap &attributes) noexcept override;
75 };
76
77 class NoopLogHandler : public LogHandler
78 {
79 public:
80 void Handle(LogLevel level,
81 const char *file,
82 int line,
83 const char *msg,
84 const sdk::common::AttributeMap &error_attributes) noexcept override;
85 };
86
87 /**
88 * Stores the singleton global LogHandler.
89 */
90 class GlobalLogHandler
91 {
92 public:
93 /**
94 * Returns the singleton LogHandler.
95 *
96 * By default, a default LogHandler is returned.
97 */
98 static inline const nostd::shared_ptr<LogHandler> &GetLogHandler() noexcept
99 {
100 return GetHandlerAndLevel().first;
101 }
102
103 /**
104 * Changes the singleton LogHandler.
105 * This should be called once at the start of application before creating any Provider
106 * instance.
107 */
108 static inline void SetLogHandler(nostd::shared_ptr<LogHandler> eh) noexcept
109 {
110 GetHandlerAndLevel().first = eh;
111 }
112
113 /**
114 * Returns the singleton log level.
115 *
116 * By default, a default log level is returned.
117 */
118 static inline LogLevel GetLogLevel() noexcept { return GetHandlerAndLevel().second; }
119
120 /**
121 * Changes the singleton Log level.
122 * This should be called once at the start of application before creating any Provider
123 * instance.
124 */
125 static inline void SetLogLevel(LogLevel level) noexcept { GetHandlerAndLevel().second = level; }
126
127 private:
128 static std::pair<nostd::shared_ptr<LogHandler>, LogLevel> &GetHandlerAndLevel() noexcept;
129 };
130
131 } // namespace internal_log
132 } // namespace common
133 } // namespace sdk
134 OPENTELEMETRY_END_NAMESPACE
135
136 /**
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.
141 */
142 #define OTEL_INTERNAL_LOG_DISPATCH(level, message, attributes) \
143 do \
144 { \
145 using opentelemetry::sdk::common::internal_log::GlobalLogHandler; \
146 using opentelemetry::sdk::common::internal_log::LogHandler; \
147 if (level > GlobalLogHandler::GetLogLevel()) \
148 { \
149 break; \
150 } \
151 const opentelemetry::nostd::shared_ptr<LogHandler> &log_handler = \
152 GlobalLogHandler::GetLogHandler(); \
153 if (!log_handler) \
154 { \
155 break; \
156 } \
157 std::stringstream tmp_stream; \
158 tmp_stream << message; \
159 log_handler->Handle(level, __FILE__, __LINE__, tmp_stream.str().c_str(), attributes); \
160 } while (false);
161
162 #define OTEL_INTERNAL_LOG_GET_3RD_ARG(arg1, arg2, arg3, ...) arg3
163
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, \
167 {})
168 # define OTEL_INTERNAL_LOG_ERROR_2_ARGS(message, attributes) \
169 OTEL_INTERNAL_LOG_DISPATCH(opentelemetry::sdk::common::internal_log::LogLevel::Error, message, \
170 attributes)
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__)
175 #else
176 # define OTEL_INTERNAL_LOG_ERROR(...)
177 #endif
178
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, \
182 message, {})
183 # define OTEL_INTERNAL_LOG_WARN_2_ARGS(message, attributes) \
184 OTEL_INTERNAL_LOG_DISPATCH(opentelemetry::sdk::common::internal_log::LogLevel::Warning, \
185 message, attributes)
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__)
190 #else
191 # define OTEL_INTERNAL_LOG_ERROR(...)
192 #endif
193
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, \
197 {})
198 # define OTEL_INTERNAL_LOG_DEBUG_2_ARGS(message, attributes) \
199 OTEL_INTERNAL_LOG_DISPATCH(opentelemetry::sdk::common::internal_log::LogLevel::Debug, message, \
200 attributes)
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__)
205 #else
206 # define OTEL_INTERNAL_LOG_DEBUG(...)
207 #endif
208
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, \
212 {})
213 # define OTEL_INTERNAL_LOG_INFO_2_ARGS(message, attributes) \
214 OTEL_INTERNAL_LOG_DISPATCH(opentelemetry::sdk::common::internal_log::LogLevel::Info, message, \
215 attributes)
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__)
220 #else
221 # define OTEL_INTERNAL_LOG_INFO(...)
222 #endif