MeshX 0.4
This repository provides an implementation for Bluetooth Low Energy (BLE) Mesh network nodes. The project allows you to create BLE mesh nodes that can communicate with each other, enabling the development of smart home solutions or other IoT-based applications.
Loading...
Searching...
No Matches
meshx_log.h
Go to the documentation of this file.
1/**
2 * @file meshx_log.h
3 * @brief Logging interface for MeshX with color-coded output
4 */
5
6#ifndef MESHX_LOG_H
7#define MESHX_LOG_H
8
9#include <stdio.h>
10#include <stdarg.h>
11#include <stdint.h>
12#include <stdbool.h>
13#include "meshx_err.h"
14#include "module_id.h"
16
17#ifdef __cplusplus
18extern "C" {
19#endif
20
21#ifndef CONFIG_MESHX_LOG_THREADED
22#define CONFIG_MESHX_LOG_THREADED 0
23#endif
24
25/* Log levels matching esp_log.h levels for compatibility where needed */
26#define MESHX_LOG_NONE 0
27#define MESHX_LOG_ERROR 1
28#define MESHX_LOG_WARN 2
29#define MESHX_LOG_INFO 3
30#define MESHX_LOG_DEBUG 4
31#define MESHX_LOG_VERBOSE 5
32#define MESHX_LOG_MAX 6
33
34typedef uint8_t meshx_log_level_t;
35
36/* Logging control structure */
40
41/* Color codes for console output (used by host decoder or fallback) */
42#define MESHX_LOG_COLOR_BLACK "30"
43#define MESHX_LOG_COLOR_RED "31"
44#define MESHX_LOG_COLOR_GREEN "32"
45#define MESHX_LOG_COLOR_BROWN "33"
46#define MESHX_LOG_COLOR_BLUE "34"
47#define MESHX_LOG_COLOR_PURPLE "35"
48#define MESHX_LOG_COLOR_CYAN "36"
49#define MESHX_LOG_COLOR_WHITE "37"
50#define MESHX_LOG_COLOR_RESET "\033[0m"
51#define MESHX_LOG_COLOR(COLOR) "\033[0;" COLOR "m"
52
53#define MESHX_LOG_COLOR_E MESHX_LOG_COLOR(MESHX_LOG_COLOR_RED)
54#define MESHX_LOG_COLOR_W MESHX_LOG_COLOR(MESHX_LOG_COLOR_BROWN)
55#define MESHX_LOG_COLOR_I MESHX_LOG_COLOR(MESHX_LOG_COLOR_GREEN)
56#define MESHX_LOG_COLOR_D MESHX_LOG_COLOR(MESHX_LOG_COLOR_BLUE)
57#define MESHX_LOG_COLOR_V MESHX_LOG_COLOR(MESHX_LOG_COLOR_RESET)
58
59#define MESHX_LOG_GET_COLOR(level) ((level) == MESHX_LOG_ERROR ? MESHX_LOG_COLOR_E : \
60 (level) == MESHX_LOG_WARN ? MESHX_LOG_COLOR_W : \
61 (level) == MESHX_LOG_INFO ? MESHX_LOG_COLOR_I : \
62 (level) == MESHX_LOG_DEBUG ? MESHX_LOG_COLOR_BLUE : \
63 MESHX_LOG_COLOR_RESET)
64
65#ifndef CONFIG_MESHX_DEFAULT_LOG_LEVEL
66#define CONFIG_MESHX_DEFAULT_LOG_LEVEL MESHX_LOG_INFO
67#endif
68
69#define MESHX_LOG_STR_ATTR __attribute__((section(".meshx_log_str"), used))
70#define MESHX_LOG_SYNC_WORD 0xDEAD
71
72#define MESHX_LOG_CONCAT_INNER(a, b) a ## b
73#define MESHX_LOG_CONCAT(a, b) MESHX_LOG_CONCAT_INNER(a, b)
74
75#ifdef __COUNTER__
76#define MESHX_LOG_UNIQUE_ID __COUNTER__
77#else
78#define MESHX_LOG_UNIQUE_ID __LINE__
79#endif
80
81/**
82 * @brief TLV Log Packet structure
83 * @note Packed for transmission efficiency
84 */
85typedef struct {
86 uint16_t sync; /* 0xDEAD */
87 uint8_t len; /* Length of payload (everything after this field) */
88 uint8_t level; /* Log level */
89 uint8_t module_id; /* Module ID */
90 uint32_t timestamp; /* System time in ms */
91 uint32_t fmt_addr; /* Address of format string in ELF */
92 uint32_t file_addr; /* Address of file name in ELF */
93 uint16_t line_no; /* Line number */
94 uint32_t args[16]; /* 16 variadic arguments */
95 char inline_str[32]; /* Space for one dynamic string (up to 32 chars) */
96 uint8_t parity; /* XOR parity at the end */
97} __attribute__((packed)) meshx_log_packet_t;
98
99#define MESHX_LOG(module_id, level, format, ...) \
100 MESHX_LOG_INTERNAL(MESHX_LOG_UNIQUE_ID, module_id, level, format, ##__VA_ARGS__, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
101
102#define MESHX_STR_INNER(x) #x
103#define MESHX_STR(x) MESHX_STR_INNER(x)
104
105#define MESHX_LOG_INTERNAL(id, module_id, level, format, arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15, ...) \
106 do \
107 { \
108 static const char MESHX_LOG_CONCAT(_fmt, id)[] __attribute__((section(".meshx_log_str." MESHX_STR(id)), used)) = format; \
109 static const char MESHX_LOG_CONCAT(_file, id)[] __attribute__((section(".meshx_log_str." MESHX_STR(id)), used)) = __FILE__; \
110 meshx_log_packet(module_id, level, MESHX_LOG_CONCAT(_file, id), __LINE__, MESHX_LOG_CONCAT(_fmt, id), arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, arg10, arg11, arg12, arg13, arg14, arg15); \
111 } while (0)
112
113#if CONFIG_MESHX_DEFAULT_LOG_LEVEL < MESHX_LOG_MAX
114#define MESHX_LOGE(module_id, format, ...) \
115 do \
116 { \
117 MESHX_LOG(module_id, MESHX_LOG_ERROR, format, ##__VA_ARGS__); \
118 } while (0)
119
120#define MESHX_LOGW(module_id, format, ...) \
121 do \
122 { \
123 MESHX_LOG(module_id, MESHX_LOG_WARN, format, ##__VA_ARGS__); \
124 } while (0)
125
126#define MESHX_LOGI(module_id, format, ...) \
127 do \
128 { \
129 MESHX_LOG(module_id, MESHX_LOG_INFO, format, ##__VA_ARGS__); \
130 } while (0)
131
132#define MESHX_LOGD(module_id, format, ...) \
133 do \
134 { \
135 MESHX_LOG(module_id, MESHX_LOG_DEBUG, format, ##__VA_ARGS__); \
136 } while (0)
137
138#define MESHX_LOGV(module_id, format, ...) \
139 do \
140 { \
141 MESHX_LOG(module_id, MESHX_LOG_VERBOSE, format, ##__VA_ARGS__);\
142 } while (0)
143
144#else
145#define MESHX_LOGE(module_id, format, ...) do {} while (0)
146#define MESHX_LOGW(module_id, format, ...) do {} while (0)
147#define MESHX_LOGI(module_id, format, ...) do {} while (0)
148#define MESHX_LOGD(module_id, format, ...) do {} while (0)
149#define MESHX_LOGV(module_id, format, ...) do {} while (0)
150#endif
151
152/* Exported functions */
155
156/**
157 * @brief Logs a packetized binary log message.
158 * @note This is called by the MESHX_LOG macro.
159 */
160void meshx_log_packet(module_id_t module_id, meshx_log_level_t log_level,
161 const char *file, int line_no, const char *fmt, ...);
162
163/**
164 * @brief Legacy printf-style logging for external components.
165 */
166void meshx_log_printf(module_id_t module_id, meshx_log_level_t log_level,
167 const char *func, int line_no, const char *fmt, ...);
168
169#ifndef CONFIG_MESHX_LOG_PRINTF
170#define CONFIG_MESHX_LOG_PRINTF meshx_tiny_printf
171#endif
172
173#ifdef __cplusplus
174}
175#endif
176
177#endif /* MESHX_LOG_H */
MeshX Error Codes.
meshx_err_t
MeshX Error Codes.
Definition meshx_err.h:43
void meshx_module_set_log_level(module_id_t module_id, meshx_log_level_t log_level)
Definition meshx_log.c:124
meshx_err_t meshx_logging_init(const meshx_logging_t *config)
Definition meshx_log.c:88
void meshx_log_packet(module_id_t module_id, meshx_log_level_t log_level, const char *file, int line_no, const char *fmt,...)
Logs a packetized binary log message.
Definition meshx_log.c:221
uint8_t meshx_log_level_t
Definition meshx_log.h:34
void meshx_log_printf(module_id_t module_id, meshx_log_level_t log_level, const char *func, int line_no, const char *fmt,...)
Legacy printf-style logging for external components.
Definition meshx_log.c:230
Lightweight printf implementation to reduce flash footprint.
Defines module IDs for different elements in the BLE mesh node application.
module_id_t
Enumeration of module IDs.
Definition module_id.h:27
TLV Log Packet structure.
Definition meshx_log.h:85
uint8_t len
Definition meshx_log.h:87
uint32_t file_addr
Definition meshx_log.h:92
uint16_t sync
Definition meshx_log.h:86
uint32_t args[16]
Definition meshx_log.h:94
uint8_t parity
Definition meshx_log.h:96
uint32_t fmt_addr
Definition meshx_log.h:91
uint8_t module_id
Definition meshx_log.h:89
char inline_str[32]
Definition meshx_log.h:95
uint8_t level
Definition meshx_log.h:88
uint32_t timestamp
Definition meshx_log.h:90
uint16_t line_no
Definition meshx_log.h:93
Definition meshx_log.h:37
meshx_log_level_t def_log_level
Definition meshx_log.h:38