remove test code
[inav.git] / src / main / common / log.h
blobbc6795e5dd9f3cd5e0e2daee8e8efaee0087b107
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 #if defined(USE_LOG)
51 #define LOG_ERROR(topic, fmt, ...) _logf(LOG_TOPIC_ ## topic, LOG_LEVEL_ERROR, fmt, ##__VA_ARGS__)
52 #define LOG_BUFFER_ERROR(topic, buf, size) _logBufferHex(LOG_TOPIC_ ## topic, LOG_LEVEL_ERROR, buf, size)
53 #else
54 #define LOG_ERROR(...)
55 #define LOG_BUFFER_ERROR(...)
56 #endif
58 #if defined(USE_LOG)
59 #define LOG_WARNING(topic, fmt, ...) _logf(LOG_TOPIC_ ## topic, LOG_LEVEL_WARNING, fmt, ##__VA_ARGS__)
60 #define LOG_BUF_WARNING(topic, buf, size) _logBufferHex(LOG_TOPIC_ ## topic, LOG_LEVEL_WARNING, buf, size)
61 #else
62 #define LOG_WARNING(...)
63 #define LOG_BUF_WARNING(...)
64 #endif
66 #if defined(USE_LOG)
67 #define LOG_INFO(topic, fmt, ...) _logf(LOG_TOPIC_ ## topic, LOG_LEVEL_INFO, fmt, ##__VA_ARGS__)
68 #define LOG_BUF_INFO(topic, buf, size) _logBufferHex(LOG_TOPIC_ ## topic, LOG_LEVEL_INFO, buf, size)
69 #else
70 #define LOG_INFO(...)
71 #define LOG_BUF_INFO(...)
72 #endif
74 #if defined(USE_LOG)
75 #define LOG_VERBOSE(topic, fmt, ...) _logf(LOG_TOPIC_ ## topic, LOG_LEVEL_VERBOSE, fmt, ##__VA_ARGS__)
76 #define LOG_BUF_VERBOSE(topic, buf, size) _logBufferHex(LOG_TOPIC_ ## topic, LOG_LEVEL_VERBOSE, buf, size)
77 #else
78 #define LOG_VERBOSE(...)
79 #define LOG_BUF_VERBOSE(...)
80 #endif
82 #if defined(USE_LOG)
83 #define LOG_DEBUG(topic, fmt, ...) _logf(LOG_TOPIC_ ## topic, LOG_LEVEL_DEBUG, fmt, ##__VA_ARGS__)
84 #define LOG_BUF_DEBUG(topic, buf, size) _logBufferHex(LOG_TOPIC_ ## topic, LOG_LEVEL_DEBUG, buf, size)
85 #else
86 #define LOG_DEBUG(...)
87 #define LOG_BUF_DEBUG(...)
88 #endif