Merge remote-tracking branch 'upstream/master' into abo_RTH_sanity_fix
[inav.git] / src / main / common / log.h
blob1622063d353d044cf1f0eddebab602937b58dc7c
1 #pragma once
3 #include <stddef.h>
4 #include <stdbool.h>
5 #include <stdint.h>
7 #include "platform.h"
9 #include "config/parameter_group.h"
11 #include "common/utils.h"
13 // Log levels. Defined as preprocessor constants instead of
14 // a number to allow compile-time comparisons.
15 #define LOG_LEVEL_ERROR 0
16 #define LOG_LEVEL_WARNING 1
17 #define LOG_LEVEL_INFO 2
18 #define LOG_LEVEL_VERBOSE 3
19 #define LOG_LEVEL_DEBUG 4
21 typedef enum {
22 LOG_TOPIC_SYSTEM, // 0, mask = 1
23 LOG_TOPIC_GYRO, // 1, mask = 2
24 LOG_TOPIC_BARO, // 2, mask = 4
25 LOG_TOPIC_PITOT, // 3, mask = 8
26 LOG_TOPIC_PWM, // 4, mask = 16
27 LOG_TOPIC_TIMER, // 5, mask = 32
28 LOG_TOPIC_IMU, // 6, mask = 64
29 LOG_TOPIC_TEMPERATURE, // 7, mask = 128
30 LOG_TOPIC_POS_ESTIMATOR, // 8, mask = 256
31 LOG_TOPIC_VTX, // 9, mask = 512
32 LOG_TOPIC_OSD, // 10, mask = 1024
34 LOG_TOPIC_COUNT,
35 } logTopic_e;
37 STATIC_ASSERT(LOG_TOPIC_COUNT < 32, too_many_log_topics);
39 typedef struct logConfig_s {
40 uint8_t level; // from LOG_LEVEL_ constants. All messages equal or below this verbosity level are printed.
41 uint32_t topics; // All messages with topics in this bitmask (1 << topic) will be printed regardless of their level.
42 } logConfig_t;
44 PG_DECLARE(logConfig_t, logConfig);
46 void logInit(void);
47 void _logf(logTopic_e topic, unsigned level, const char *fmt, ...) __attribute__ ((format (printf, 3, 4)));
48 void _logBufferHex(logTopic_e topic, unsigned level, const void *buffer, size_t size);
50 // LOG_* macro definitions
52 #if !defined(LOG_LEVEL_MAXIMUM)
53 #define LOG_LEVEL_MAXIMUM LOG_LEVEL_DEBUG
54 #endif
56 #if defined(USE_LOG) && LOG_LEVEL_MAXIMUM >= LOG_LEVEL_ERROR
57 #define LOG_E(topic, fmt, ...) _logf(LOG_TOPIC_ ## topic, LOG_LEVEL_ERROR, fmt, ##__VA_ARGS__)
58 #define LOG_BUFFER_E(topic, buf, size) _logBufferHex(LOG_TOPIC_ ## topic, LOG_LEVEL_ERROR, buf, size)
59 #else
60 #define LOG_E(...)
61 #define LOG_BUFFER_E(...)
62 #endif
64 #if defined(USE_LOG) && LOG_LEVEL_MAXIMUM >= LOG_LEVEL_WARNING
65 #define LOG_W(topic, fmt, ...) _logf(LOG_TOPIC_ ## topic, LOG_LEVEL_WARNING, fmt, ##__VA_ARGS__)
66 #define LOG_BUF_W(topic, buf, size) _logBufferHex(LOG_TOPIC_ ## topic, LOG_LEVEL_WARNING, buf, size)
67 #else
68 #define LOG_W(...)
69 #define LOG_BUF_W(...)
70 #endif
72 #if defined(USE_LOG) && LOG_LEVEL_MAXIMUM >= LOG_LEVEL_INFO
73 #define LOG_I(topic, fmt, ...) _logf(LOG_TOPIC_ ## topic, LOG_LEVEL_INFO, fmt, ##__VA_ARGS__)
74 #define LOG_BUF_I(topic, buf, size) _logBufferHex(LOG_TOPIC_ ## topic, LOG_LEVEL_INFO, buf, size)
75 #else
76 #define LOG_I(...)
77 #define LOG_BUF_I(...)
78 #endif
80 #if defined(USE_LOG) && LOG_LEVEL_MAXIMUM >= LOG_LEVEL_VERBOSE
81 #define LOG_V(topic, fmt, ...) _logf(LOG_TOPIC_ ## topic, LOG_LEVEL_VERBOSE, fmt, ##__VA_ARGS__)
82 #define LOG_BUF_V(topic, buf, size) _logBufferHex(LOG_TOPIC_ ## topic, LOG_LEVEL_VERBOSE, buf, size)
83 #else
84 #define LOG_V(...)
85 #define LOG_BUF_V(...)
86 #endif
88 #if defined(USE_LOG) && LOG_LEVEL_MAXIMUM >= LOG_LEVEL_DEBUG
89 #define LOG_D(topic, fmt, ...) _logf(LOG_TOPIC_ ## topic, LOG_LEVEL_DEBUG, fmt, ##__VA_ARGS__)
90 #define LOG_BUF_D(topic, buf, size) _logBufferHex(LOG_TOPIC_ ## topic, LOG_LEVEL_DEBUG, buf, size)
91 #else
92 #define LOG_D(...)
93 #define LOG_BUF_D(...)
94 #endif