Line | Count | Source (jump to first uncovered line) |
1 | | /* SPDX-License-Identifier: GPL-3.0-or-later |
2 | | * Copyright © 2016-2018 The TokTok team. |
3 | | * Copyright © 2013 Tox project. |
4 | | */ |
5 | | |
6 | | /** |
7 | | * Logger abstraction backed by callbacks for writing. |
8 | | */ |
9 | | #ifndef C_TOXCORE_TOXCORE_LOGGER_H |
10 | | #define C_TOXCORE_TOXCORE_LOGGER_H |
11 | | |
12 | | #include <stdint.h> |
13 | | |
14 | | #include "attributes.h" |
15 | | |
16 | | #ifdef __cplusplus |
17 | | extern "C" { |
18 | | #endif |
19 | | |
20 | | #ifndef MIN_LOGGER_LEVEL |
21 | | #define MIN_LOGGER_LEVEL LOGGER_LEVEL_INFO |
22 | | #endif /* MIN_LOGGER_LEVEL */ |
23 | | |
24 | | // NOTE: Don't forget to update build system files after modifying the enum. |
25 | | typedef enum Logger_Level { |
26 | | LOGGER_LEVEL_TRACE, |
27 | | LOGGER_LEVEL_DEBUG, |
28 | | LOGGER_LEVEL_INFO, |
29 | | LOGGER_LEVEL_WARNING, |
30 | | LOGGER_LEVEL_ERROR, |
31 | | } Logger_Level; |
32 | | |
33 | | typedef struct Logger Logger; |
34 | | |
35 | | typedef void logger_cb(void *context, Logger_Level level, const char *file, int line, |
36 | | const char *func, const char *message, void *userdata); |
37 | | |
38 | | /** |
39 | | * Creates a new logger with logging disabled (callback is NULL) by default. |
40 | | */ |
41 | | Logger *logger_new(void); |
42 | | |
43 | | /** |
44 | | * Frees all resources associated with the logger. |
45 | | */ |
46 | | nullable(1) |
47 | | void logger_kill(Logger *log); |
48 | | |
49 | | /** |
50 | | * Sets the logger callback. Disables logging if set to NULL. |
51 | | * The context parameter is passed to the callback as first argument. |
52 | | */ |
53 | | non_null(1) nullable(2, 3, 4) |
54 | | void logger_callback_log(Logger *log, logger_cb *function, void *context, void *userdata); |
55 | | |
56 | | /** @brief Main write function. If logging is disabled, this does nothing. |
57 | | * |
58 | | * If the logger is NULL and `NDEBUG` is not defined, this writes to stderr. |
59 | | * This behaviour should not be used in production code, but can be useful for |
60 | | * temporarily debugging a function that does not have a logger available. It's |
61 | | * essentially `fprintf(stderr, ...)`, but with source location. |
62 | | * |
63 | | * If `NDEBUG` is defined, the NULL logger does nothing. |
64 | | */ |
65 | | non_null(3, 5, 6) nullable(1) GNU_PRINTF(6, 7) |
66 | | void logger_write( |
67 | | const Logger *log, Logger_Level level, const char *file, int line, const char *func, |
68 | | const char *format, ...); |
69 | | |
70 | | /* @brief Terminate the program with a signal. */ |
71 | | void logger_abort(void); |
72 | | |
73 | | |
74 | | #define LOGGER_WRITE(log, level, ...) \ |
75 | 3.01M | do { \ |
76 | 3.01M | if (level >= MIN_LOGGER_LEVEL) { \ |
77 | 4.43M | logger_write(log, level, __FILE__, __LINE__, __func__, __VA_ARGS__); \ |
78 | 3.01M | } \ |
79 | 3.01M | } while (0) |
80 | | |
81 | | /* To log with an logger */ |
82 | 2.91M | #define LOGGER_TRACE(log, ...) LOGGER_WRITE(log, LOGGER_LEVEL_TRACE, __VA_ARGS__) |
83 | 38.8k | #define LOGGER_DEBUG(log, ...) LOGGER_WRITE(log, LOGGER_LEVEL_DEBUG, __VA_ARGS__) |
84 | 225 | #define LOGGER_INFO(log, ...) LOGGER_WRITE(log, LOGGER_LEVEL_INFO, __VA_ARGS__) |
85 | 22.2k | #define LOGGER_WARNING(log, ...) LOGGER_WRITE(log, LOGGER_LEVEL_WARNING, __VA_ARGS__) |
86 | 41.2k | #define LOGGER_ERROR(log, ...) LOGGER_WRITE(log, LOGGER_LEVEL_ERROR, __VA_ARGS__) |
87 | | |
88 | | #define LOGGER_FATAL(log, ...) \ |
89 | 0 | do { \ |
90 | 0 | LOGGER_ERROR(log, __VA_ARGS__); \ |
91 | 0 | logger_abort(); \ |
92 | 0 | } while (0) |
93 | | |
94 | | #define LOGGER_ASSERT(log, cond, ...) \ |
95 | 3.02M | do { \ |
96 | 3.02M | if (!(cond)) { \ |
97 | 0 | LOGGER_ERROR(log, "Assertion failed"); \ |
98 | 0 | LOGGER_FATAL(log, __VA_ARGS__); \ |
99 | 0 | } \ |
100 | 3.02M | } while (0) |
101 | | |
102 | | #ifdef __cplusplus |
103 | | } /* extern "C" */ |
104 | | #endif |
105 | | |
106 | | #endif /* C_TOXCORE_TOXCORE_LOGGER_H */ |