Coverage Report

Created: 2025-10-08 19:34

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