Debian Codegraph @

crt/aws-c-common/include/aws/common/logging.h

raw symbols SCIP
1 #ifndef AWS_COMMON_LOGGING_H
2 #define AWS_COMMON_LOGGING_H
3
4 /**
5 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
6 * SPDX-License-Identifier: Apache-2.0.
7 */
8
9 #include <aws/common/atomics.h>
10 #include <aws/common/common.h>
11 #include <aws/common/thread.h>
12
13 AWS_PUSH_SANE_WARNING_LEVEL
14
15 #define AWS_LOG_LEVEL_NONE 0
16 #define AWS_LOG_LEVEL_FATAL 1
17 #define AWS_LOG_LEVEL_ERROR 2
18 #define AWS_LOG_LEVEL_WARN 3
19 #define AWS_LOG_LEVEL_INFO 4
20 #define AWS_LOG_LEVEL_DEBUG 5
21 #define AWS_LOG_LEVEL_TRACE 6
22
23 /**
24 * Controls what log calls pass through the logger and what log calls get filtered out.
25 * If a log level has a value of X, then all log calls using a level <= X will appear, while
26 * those using a value > X will not occur.
27 *
28 * You can filter both dynamically (by setting the log level on the logger object) or statically
29 * (by defining AWS_STATIC_LOG_LEVEL to be an appropriate integer module-wide). Statically filtered
30 * log calls will be completely compiled out but require a rebuild if you want to get more detail
31 * about what's happening.
32 */
33 enum aws_log_level {
34 AWS_LL_NONE = AWS_LOG_LEVEL_NONE,
35 AWS_LL_FATAL = AWS_LOG_LEVEL_FATAL,
36 AWS_LL_ERROR = AWS_LOG_LEVEL_ERROR,
37 AWS_LL_WARN = AWS_LOG_LEVEL_WARN,
38 AWS_LL_INFO = AWS_LOG_LEVEL_INFO,
39 AWS_LL_DEBUG = AWS_LOG_LEVEL_DEBUG,
40 AWS_LL_TRACE = AWS_LOG_LEVEL_TRACE,
41
42 AWS_LL_COUNT
43 };
44
45 /**
46 * Log subject is a way of designating the topic of logging.
47 *
48 * The general idea is to support a finer-grained approach to log level control. The primary use case
49 * is for situations that require more detailed logging within a specific domain, where enabling that detail
50 * globally leads to an untenable flood of information.
51 *
52 * For example, enable TRACE logging for tls-related log statements (handshake binary payloads), but
53 * only WARN logging everywhere else (because http payloads would blow up the log files).
54 *
55 * Log subject is an enum similar to aws error: each library has its own value-space and someone is
56 * responsible for registering the value <-> string connections.
57 */
58 typedef uint32_t aws_log_subject_t;
59
60 /* Each library gets space for 2^^10 log subject entries */
61 enum {
62 AWS_LOG_SUBJECT_STRIDE_BITS = 10,
63 };
64 #define AWS_LOG_SUBJECT_STRIDE (1U << AWS_LOG_SUBJECT_STRIDE_BITS)
65 #define AWS_LOG_SUBJECT_BEGIN_RANGE(x) ((x) * AWS_LOG_SUBJECT_STRIDE)
66 #define AWS_LOG_SUBJECT_END_RANGE(x) (((x) + 1) * AWS_LOG_SUBJECT_STRIDE - 1)
67
68 struct aws_log_subject_info {
69 aws_log_subject_t subject_id;
70 const char *subject_name;
71 const char *subject_description;
72 };
73
74 #define DEFINE_LOG_SUBJECT_INFO(id, name, desc) \
75 {.subject_id = (id), .subject_name = (name), .subject_description = (desc)}
76
77 struct aws_log_subject_info_list {
78 struct aws_log_subject_info *subject_list;
79 size_t count;
80 };
81
82 enum aws_common_log_subject {
83 AWS_LS_COMMON_GENERAL = AWS_LOG_SUBJECT_BEGIN_RANGE(AWS_C_COMMON_PACKAGE_ID),
84 AWS_LS_COMMON_TASK_SCHEDULER,
85 AWS_LS_COMMON_THREAD,
86 AWS_LS_COMMON_MEMTRACE,
87 AWS_LS_COMMON_XML_PARSER,
88 AWS_LS_COMMON_IO,
89 AWS_LS_COMMON_BUS,
90 AWS_LS_COMMON_TEST,
91 AWS_LS_COMMON_JSON_PARSER,
92 AWS_LS_COMMON_CBOR,
93
94 AWS_LS_COMMON_LAST = AWS_LOG_SUBJECT_END_RANGE(AWS_C_COMMON_PACKAGE_ID)
95 };
96
97 struct aws_logger;
98 struct aws_log_formatter;
99 struct aws_log_channel;
100 struct aws_log_writer;
101
102 #ifdef _MSC_VER
103 # pragma warning(push)
104 # pragma warning(disable : 4623) /* default constructor was implicitly defined as deleted */
105 # pragma warning(disable : 4626) /* assignment operator was implicitly defined as deleted */
106 # pragma warning(disable : 5027) /* move assignment operator was implicitly defined as deleted */
107 #endif
108
109 /**
110 * We separate the log level function from the log call itself so that we can do the filter check in the macros (see
111 * below)
112 *
113 * By doing so, we make it so that the variadic format arguments are not even evaluated if the filter check does not
114 * succeed.
115 */
116 struct aws_logger_vtable {
117 int (*const log)(
118 struct aws_logger *logger,
119 enum aws_log_level log_level,
120 aws_log_subject_t subject,
121 const char *format,
122 ...)
123 #if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__)
124 __attribute__((format(printf, 4, 5)))
125 #endif /* non-ms compilers: TODO - find out what versions format support was added in */
126 ;
127 enum aws_log_level (*const get_log_level)(struct aws_logger *logger, aws_log_subject_t subject);
128 void (*const clean_up)(struct aws_logger *logger);
129 int (*set_log_level)(struct aws_logger *logger, enum aws_log_level);
130 };
131
132 #ifdef _MSC_VER
133 # pragma warning(pop)
134 #endif
135
136 struct aws_logger {
137 struct aws_logger_vtable *vtable;
138 struct aws_allocator *allocator;
139 void *p_impl;
140 };
141
142 /**
143 * The base formatted logging macro that all other formatted logging macros resolve to.
144 * Checks for a logger and filters based on log level.
145 */
146 #define AWS_LOGF(log_level, subject, ...) \
147 do { \
148 AWS_ASSERT(log_level > 0); \
149 struct aws_logger *logger = aws_logger_get(); \
150 if (logger != NULL && logger->vtable->get_log_level(logger, (subject)) >= (log_level)) { \
151 logger->vtable->log(logger, log_level, subject, __VA_ARGS__); \
152 } \
153 } while (0)
154 /**
155 * Unconditional logging macro that takes a logger and does not do a level check or a null check. Intended for
156 * situations when you need to log many things and do a single manual level check before beginning.
157 */
158 #define AWS_LOGUF(logger, log_level, subject, ...) \
159 { logger->vtable->log(logger, log_level, subject, __VA_ARGS__); }
160
161 /**
162 * LOGF_<level> variants for each level. These are what should be used directly to do all logging.
163 *
164 * i.e.
165 *
166 * LOGF_FATAL("Device \"%s\" not found", device->name);
167 *
168 *
169 * Later we will likely expose Subject-aware variants
170 */
171 #if !defined(AWS_STATIC_LOG_LEVEL) || (AWS_STATIC_LOG_LEVEL >= AWS_LOG_LEVEL_FATAL)
172 # define AWS_LOGF_FATAL(subject, ...) AWS_LOGF(AWS_LL_FATAL, subject, __VA_ARGS__)
173 #else
174 # define AWS_LOGF_FATAL(subject, ...)
175 #endif
176
177 #if !defined(AWS_STATIC_LOG_LEVEL) || (AWS_STATIC_LOG_LEVEL >= AWS_LOG_LEVEL_ERROR)
178 # define AWS_LOGF_ERROR(subject, ...) AWS_LOGF(AWS_LL_ERROR, subject, __VA_ARGS__)
179 #else
180 # define AWS_LOGF_ERROR(subject, ...)
181 #endif
182
183 #if !defined(AWS_STATIC_LOG_LEVEL) || (AWS_STATIC_LOG_LEVEL >= AWS_LOG_LEVEL_WARN)
184 # define AWS_LOGF_WARN(subject, ...) AWS_LOGF(AWS_LL_WARN, subject, __VA_ARGS__)
185 #else
186 # define AWS_LOGF_WARN(subject, ...)
187 #endif
188
189 #if !defined(AWS_STATIC_LOG_LEVEL) || (AWS_STATIC_LOG_LEVEL >= AWS_LOG_LEVEL_INFO)
190 # define AWS_LOGF_INFO(subject, ...) AWS_LOGF(AWS_LL_INFO, subject, __VA_ARGS__)
191 #else
192 # define AWS_LOGF_INFO(subject, ...)
193 #endif
194
195 #if !defined(AWS_STATIC_LOG_LEVEL) || (AWS_STATIC_LOG_LEVEL >= AWS_LOG_LEVEL_DEBUG)
196 # define AWS_LOGF_DEBUG(subject, ...) AWS_LOGF(AWS_LL_DEBUG, subject, __VA_ARGS__)
197 #else
198 # define AWS_LOGF_DEBUG(subject, ...)
199 #endif
200
201 #if !defined(AWS_STATIC_LOG_LEVEL) || (AWS_STATIC_LOG_LEVEL >= AWS_LOG_LEVEL_TRACE)
202 # define AWS_LOGF_TRACE(subject, ...) AWS_LOGF(AWS_LL_TRACE, subject, __VA_ARGS__)
203 #else
204 # define AWS_LOGF_TRACE(subject, ...)
205 #endif
206
207 /*
208 * Standard logger implementation composing three sub-components:
209 *
210 * The formatter takes var args input from the user and produces a formatted log line
211 * The writer takes a formatted log line and outputs it somewhere
212 * The channel is the transport between the two
213 */
214 struct aws_logger_pipeline {
215 struct aws_log_formatter *formatter;
216 struct aws_log_channel *channel;
217 struct aws_log_writer *writer;
218 struct aws_allocator *allocator;
219 struct aws_atomic_var level;
220 };
221
222 /**
223 * Options for aws_logger_init_standard().
224 * Set `filename` to open a file for logging and close it when the logger cleans up.
225 * Set `file` to use a file that is already open, such as `stderr` or `stdout`.
226 */
227 struct aws_logger_standard_options {
228 enum aws_log_level level;
229 const char *filename;
230 FILE *file;
231 };
232
233 AWS_EXTERN_C_BEGIN
234
235 /**
236 * Sets the aws logger used globally across the process. Not thread-safe. Must only be called once.
237 */
238 AWS_COMMON_API
239 void aws_logger_set(struct aws_logger *logger);
240
241 /**
242 * Gets the aws logger used globally across the process.
243 */
244 AWS_COMMON_API
245 struct aws_logger *aws_logger_get(void);
246
247 /**
248 * Gets the aws logger used globally across the process if the logging level is at least the inputted level.
249 *
250 * @param subject log subject to perform the level check versus, not currently used
251 * @param level logging level to check against in order to return the logger
252 * @return the current logger if the current logging level is at or more detailed then the supplied logging level
253 */
254 AWS_COMMON_API
255 struct aws_logger *aws_logger_get_conditional(aws_log_subject_t subject, enum aws_log_level level);
256
257 /**
258 * Cleans up all resources used by the logger; simply invokes the clean_up v-function
259 */
260 AWS_COMMON_API
261 void aws_logger_clean_up(struct aws_logger *logger);
262
263 /**
264 * Sets the current logging level for the logger. Loggers are not require to support this.
265 * @param logger logger to set the log level for
266 * @param level new log level for the logger
267 * @return AWS_OP_SUCCESS if the level was successfully set, AWS_OP_ERR otherwise
268 */
269 AWS_COMMON_API
270 int aws_logger_set_log_level(struct aws_logger *logger, enum aws_log_level level);
271
272 /**
273 * Converts a log level to a c-string constant. Intended primarily to support building log lines that
274 * include the level in them, i.e.
275 *
276 * [ERROR] 10:34:54.642 01-31-19 - Json parse error....
277 */
278 AWS_COMMON_API
279 int aws_log_level_to_string(enum aws_log_level log_level, const char **level_string);
280
281 /**
282 * Converts a c-string constant to a log level value. Uses case-insensitive comparison
283 * and simply iterates all possibilities until a match or nothing remains. If no match
284 * is found, AWS_OP_ERR is returned.
285 */
286 AWS_COMMON_API
287 int aws_string_to_log_level(const char *level_string, enum aws_log_level *log_level);
288
289 /**
290 * Converts an aws_thread_id_t to a c-string. For portability, aws_thread_id_t
291 * must not be printed directly. Intended primarily to support building log
292 * lines that include the thread id in them. The parameter `buffer` must
293 * point-to a char buffer of length `bufsz == AWS_THREAD_ID_T_REPR_BUFSZ`. The
294 * thread id representation is returned in `buffer`.
295 */
296 AWS_COMMON_API
297 int aws_thread_id_t_to_string(aws_thread_id_t thread_id, char *buffer, size_t bufsz);
298
299 /**
300 * Get subject name from log subject.
301 */
302 AWS_COMMON_API
303 const char *aws_log_subject_name(aws_log_subject_t subject);
304
305 /**
306 * Connects log subject strings with log subject integer values
307 */
308 AWS_COMMON_API
309 void aws_register_log_subject_info_list(struct aws_log_subject_info_list *log_subject_list);
310
311 /**
312 * Disconnects log subject strings with log subject integer values
313 */
314 AWS_COMMON_API
315 void aws_unregister_log_subject_info_list(struct aws_log_subject_info_list *log_subject_list);
316
317 /*
318 * Initializes a pipeline logger that is built from the default formatter, a background thread-based channel, and
319 * a file writer. The default logger in almost all circumstances.
320 */
321 AWS_COMMON_API
322 int aws_logger_init_standard(
323 struct aws_logger *logger,
324 struct aws_allocator *allocator,
325 struct aws_logger_standard_options *options);
326
327 /*
328 * Initializes a pipeline logger from components that have already been initialized. This is not an ownership transfer.
329 * After the pipeline logger is cleaned up, the components will have to manually be cleaned up by the user.
330 */
331 AWS_COMMON_API
332 int aws_logger_init_from_external(
333 struct aws_logger *logger,
334 struct aws_allocator *allocator,
335 struct aws_log_formatter *formatter,
336 struct aws_log_channel *channel,
337 struct aws_log_writer *writer,
338 enum aws_log_level level);
339
340 /*
341 * Pipeline logger vtable for custom configurations
342 */
343 AWS_COMMON_API
344 extern struct aws_logger_vtable g_pipeline_logger_owned_vtable;
345
346 /*
347 * Initializes a logger that does not perform any allocation during logging. Log lines larger than the internal
348 * constant are truncated. Formatting matches the standard logger. Used for memory tracing logging.
349 * If no file or filename is set in the aws_logger_standard_options, then it will use stderr.
350 */
351 AWS_COMMON_API
352 int aws_logger_init_noalloc(
353 struct aws_logger *logger,
354 struct aws_allocator *allocator,
355 struct aws_logger_standard_options *options);
356
357 AWS_EXTERN_C_END
358 AWS_POP_SANE_WARNING_LEVEL
359
360 #endif /* AWS_COMMON_LOGGING_H */