MIP_SDK  v3.0.0-502-gc890c1c
MicroStrain Communications Library for embedded systems
logging.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <stdarg.h>
4 #include <stddef.h>
5 #include <stdint.h>
6 #include <stdbool.h>
7 
8 #ifdef __cplusplus
9 extern "C" {
10 #endif
11 
29 
36 
37 
42 {
51 
60 typedef void (*microstrain_log_callback)(void* user, const microstrain_log_level level, const char* fmt, va_list args);
61 
62 void microstrain_logging_init(const microstrain_log_callback callback, const microstrain_log_level level, void* user);
63 
65 
67 
69 
70 void microstrain_logging_log_v(const microstrain_log_level level, const char* fmt, va_list args);
71 
72 void microstrain_logging_log(const microstrain_log_level level, const char* fmt, ...);
73 
75 
76 void microstrain_log_bytes(const microstrain_log_level level, const char* msg, const uint8_t* data, size_t length);
77 
88 #ifdef MICROSTRAIN_ENABLE_LOGGING
89 #define MICROSTRAIN_LOG_INIT(callback, level, user) microstrain_logging_init(callback, level, user)
90 #else
91 #define MICROSTRAIN_LOG_INIT(callback, level, user) (void)0
92 #endif
93 
100 #ifdef MICROSTRAIN_ENABLE_LOGGING
101 #define MICROSTRAIN_LOG_LOG(level, ...) microstrain_logging_log(level, __VA_ARGS__)
102 #define MICROSTRAIN_LOG_LOG_V(level, fmt, args) microstrain_logging_log_v(level, fmt, args)
103 #else
104 #define MICROSTRAIN_LOG_LOG(level, ...) (void)0
105 #define MICROSTRAIN_LOG_LOG_V(level, fmt, args) (void)0
106 #endif
107 
108 #ifndef MICROSTRAIN_LOGGING_MAX_LEVEL
109 #define MICROSTRAIN_LOGGING_MAX_LEVEL MICROSTRAIN_LOG_LEVEL_WARN
110 #endif
111 
121 #if MICROSTRAIN_LOGGING_MAX_LEVEL >= MICROSTRAIN_LOG_LEVEL_FATAL
122 #define MICROSTRAIN_LOG_FATAL(...) MICROSTRAIN_LOG_LOG(MICROSTRAIN_LOG_LEVEL_FATAL, __VA_ARGS__)
123 #define MICROSTRAIN_LOG_FATAL_V(fmt, args) MICROSTRAIN_LOG_LOG_V(MICROSTRAIN_LOG_LEVEL_FATAL, fmt, args)
124 #else
125 #define MICROSTRAIN_LOG_FATAL(...) (void)0
126 #define MICROSTRAIN_LOG_FATAL_V(fmt, args) (void)0
127 #endif
128 
134 #if MICROSTRAIN_LOGGING_MAX_LEVEL >= MICROSTRAIN_LOG_LEVEL_ERROR
135 #define MICROSTRAIN_LOG_ERROR(...) MICROSTRAIN_LOG_LOG(MICROSTRAIN_LOG_LEVEL_ERROR, __VA_ARGS__)
136 #define MICROSTRAIN_LOG_ERROR_V(fmt, args) MICROSTRAIN_LOG_LOG_V(MICROSTRAIN_LOG_LEVEL_ERROR, fmt, args)
137 #else
138 #define MICROSTRAIN_LOG_ERROR(...) (void)0
139 #define MICROSTRAIN_LOG_ERROR_V(fmt, args) (void)0
140 #endif
141 
146 #if MICROSTRAIN_LOGGING_MAX_LEVEL >= MICROSTRAIN_LOG_LEVEL_WARN
147 #define MICROSTRAIN_LOG_WARN(...) MICROSTRAIN_LOG_LOG(MICROSTRAIN_LOG_LEVEL_WARN, __VA_ARGS__)
148 #define MICROSTRAIN_LOG_WARN_V(fmt, args) MICROSTRAIN_LOG_LOG_V(MICROSTRAIN_LOG_LEVEL_WARN, fmt, args)
149 #else
150 #define MICROSTRAIN_LOG_WARN(...) (void)0
151 #define MICROSTRAIN_LOG_WARN_V(fmt, args) (void)0
152 #endif
153 
158 #if MICROSTRAIN_LOGGING_MAX_LEVEL >= MICROSTRAIN_LOG_LEVEL_INFO
159 #define MICROSTRAIN_LOG_INFO(...) MICROSTRAIN_LOG_LOG(MICROSTRAIN_LOG_LEVEL_INFO, __VA_ARGS__)
160 #define MICROSTRAIN_LOG_INFO_V(fmt, args) MICROSTRAIN_LOG_LOG_V(MICROSTRAIN_LOG_LEVEL_INFO, fmt, args)
161 #else
162 #define MICROSTRAIN_LOG_INFO(...) (void)0
163 #define MICROSTRAIN_LOG_INFO_V(fmt, args) (void)0
164 #endif
165 
171 #if MICROSTRAIN_LOGGING_MAX_LEVEL >= MICROSTRAIN_LOG_LEVEL_DEBUG
172 #define MICROSTRAIN_LOG_DEBUG(...) MICROSTRAIN_LOG_LOG(MICROSTRAIN_LOG_LEVEL_DEBUG, __VA_ARGS__)
173 #define MICROSTRAIN_LOG_DEBUG_V(fmt, args) MICROSTRAIN_LOG_LOG_V(MICROSTRAIN_LOG_LEVEL_DEBUG, fmt, args)
174 #else
175 #define MICROSTRAIN_LOG_DEBUG(...) (void)0
176 #define MICROSTRAIN_LOG_DEBUG_V(fmt, args) (void)0
177 #endif
178 
184 #if MICROSTRAIN_LOGGING_MAX_LEVEL >= MICROSTRAIN_LOG_LEVEL_TRACE
185 #define MICROSTRAIN_LOG_TRACE(...) MICROSTRAIN_LOG_LOG(MICROSTRAIN_LOG_LEVEL_TRACE, __VA_ARGS__)
186 #define MICROSTRAIN_LOG_TRACE_V(fmt, args) MICROSTRAIN_LOG_LOG_V(MICROSTRAIN_LOG_LEVEL_TRACE, fmt, args)
187 #else
188 #define MICROSTRAIN_LOG_TRACE(...) (void)0
189 #define MICROSTRAIN_LOG_TRACE_V(fmt, args) (void)0
190 #endif
191 
195 #if MICROSTRAIN_ENABLE_LOGGING
196 #define MICROSTRAIN_LOG_BYTES(level, msg, data, length) microstrain_log_bytes(level, msg, data, length)
197 #else
198 #define MICROSTRAIN_LOG_BYTES(level, msg, data, length) (void)0
199 #endif
200 
207 #if MICROSTRAIN_LOGGING_MAX_LEVEL >= MICROSTRAIN_LOG_LEVEL_TRACE
208 #define MICROSTRAIN_LOG_BYTES_TRACE(msg, data, length) microstrain_log_bytes(MICROSTRAIN_LOG_LEVEL_TRACE, msg, data, length)
209 #else
210 #define MICROSTRAIN_LOG_BYTES_TRACE(msg, data, length) (void)0
211 #endif
212 
213 
220 #define MICROSTRAIN_LOG_ERROR_WITH_ERRNO(msg) MICROSTRAIN_LOG_ERROR(msg ": %d %s\n", errno, strerror(errno))
221 
230 #define MICROSTRAIN_LOG_ERROR_WITH_ERRNO_EX(msg, ...) MICROSTRAIN_LOG_ERROR(msg ": %d %s\n", __VA_ARGS__, errno, strerror(errno))
231 
235 
236 #ifdef __cplusplus
237 } // extern "C"
238 #endif
MICROSTRAIN_LOG_LEVEL_WARN
@ MICROSTRAIN_LOG_LEVEL_WARN
Warning logs are logged when something concerning happens that may or not be a mistake.
Definition: logging.h:46
microstrain_log_bytes
void microstrain_log_bytes(const microstrain_log_level level, const char *msg, const uint8_t *data, size_t length)
Print bytes in hex to the log.
Definition: logging.c:136
MICROSTRAIN_LOG_LEVEL_INFO
@ MICROSTRAIN_LOG_LEVEL_INFO
Info logs are logged when some general info needs to be conveyed to the user.
Definition: logging.h:47
microstrain_logging_log
void microstrain_logging_log(const microstrain_log_level level, const char *fmt,...)
Internal log function called by logging macros. Call MICROSTRAIN_LOG_* macros instead of using this f...
Definition: logging.c:88
microstrain_logging_callback
microstrain_log_callback microstrain_logging_callback(void)
Gets the currently active logging callback.
Definition: logging.c:43
microstrain_log_level
microstrain_log_level
Logging level enum.
Definition: logging.h:41
MICROSTRAIN_LOG_LEVEL_ERROR
@ MICROSTRAIN_LOG_LEVEL_ERROR
Error logs are logged when an error occurs.
Definition: logging.h:45
microstrain_log_callback
void(* microstrain_log_callback)(void *user, const microstrain_log_level level, const char *fmt, va_list args)
Callback function typedef for custom logging behavior.
Definition: logging.h:60
microstrain_logging_level_name
const char * microstrain_logging_level_name(const microstrain_log_level level)
Returns a string representing the given log level.
Definition: logging.c:104
microstrain_logging_user_data
void * microstrain_logging_user_data(void)
Gets the currently active logging user data.
Definition: logging.c:63
microstrain_logging_level
microstrain_log_level microstrain_logging_level(void)
Gets the currently active logging level.
Definition: logging.c:53
MICROSTRAIN_LOG_LEVEL_OFF
@ MICROSTRAIN_LOG_LEVEL_OFF
Signifies that the log is turned off.
Definition: logging.h:43
MICROSTRAIN_LOG_LEVEL_FATAL
@ MICROSTRAIN_LOG_LEVEL_FATAL
Fatal logs are logged when an unrecoverable error occurs.
Definition: logging.h:44
MICROSTRAIN_LOG_LEVEL_TRACE
@ MICROSTRAIN_LOG_LEVEL_TRACE
Trace logs are logged in similar cases to debug logs but can be logged in tight loops.
Definition: logging.h:49
microstrain_logging_init
void microstrain_logging_init(const microstrain_log_callback callback, const microstrain_log_level level, void *user)
Initializes the logger with a callback and user data. Call MICROSTRAIN_LOG_INIT instead of using this...
Definition: logging.c:31
MICROSTRAIN_LOG_LEVEL_DEBUG
@ MICROSTRAIN_LOG_LEVEL_DEBUG
Debug logs are logged for debug purposes.
Definition: logging.h:48
microstrain_logging_log_v
void microstrain_logging_log_v(const microstrain_log_level level, const char *fmt, va_list args)
Internal log function called by variadic logging macros. Call MICROSTRAIN_LOG_*_V macros instead of u...
Definition: logging.c:74