MIP_SDK  v3.0.0-187-g93c7302
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 <stdint.h>
5 
6 #ifdef __cplusplus
7 extern "C" {
8 #endif
9 
23 
27 typedef uint8_t microstrain_log_level;
28 #define MICROSTRAIN_LOG_LEVEL_OFF 0
29 #define MICROSTRAIN_LOG_LEVEL_FATAL 1
30 #define MICROSTRAIN_LOG_LEVEL_ERROR 2
31 #define MICROSTRAIN_LOG_LEVEL_WARN 3
32 #define MICROSTRAIN_LOG_LEVEL_INFO 4
33 #define MICROSTRAIN_LOG_LEVEL_DEBUG 5
34 #define MICROSTRAIN_LOG_LEVEL_TRACE 6
35 
36 typedef void (*microstrain_log_callback)(void* user, const microstrain_log_level level, const char* fmt, va_list args);
45 
46 void microstrain_logging_init(const microstrain_log_callback callback, const microstrain_log_level level, void* user);
47 
51 
52 void microstrain_logging_log_v(const microstrain_log_level level, const char* fmt, va_list args);
53 void microstrain_logging_log (const microstrain_log_level level, const char* fmt, ...);
54 
56 
67 #ifdef MICROSTRAIN_ENABLE_LOGGING
68 #define MICROSTRAIN_LOG_INIT(callback, level, user) microstrain_logging_init(callback, level, user)
69 #else
70 #define MICROSTRAIN_LOG_INIT(callback, level, user) (void)0
71 #endif
72 
79 #ifdef MICROSTRAIN_ENABLE_LOGGING
80 #define MICROSTRAIN_LOG_LOG(level, ...) microstrain_logging_log(level, __VA_ARGS__)
81 #define MICROSTRAIN_LOG_LOG_V(level, fmt, args) microstrain_logging_log_v(level, fmt, args)
82 #else
83 #define MICROSTRAIN_LOG_LOG(level, ...) (void)0
84 #define MICROSTRAIN_LOG_LOG_V(level, fmt, args) (void)0
85 #endif
86 
87 #ifndef MICROSTRAIN_LOGGING_MAX_LEVEL
88 #define MICROSTRAIN_LOGGING_MAX_LEVEL MICROSTRAIN_LOG_LEVEL_WARN
89 #endif
90 
100 #if MICROSTRAIN_LOGGING_MAX_LEVEL >= MICROSTRAIN_LOG_LEVEL_FATAL
101 #define MICROSTRAIN_LOG_FATAL(...) MICROSTRAIN_LOG_LOG(MICROSTRAIN_LOG_LEVEL_FATAL, __VA_ARGS__)
102 #define MICROSTRAIN_LOG_FATAL_V(fmt, args) MICROSTRAIN_LOG_LOG_V(MICROSTRAIN_LOG_LEVEL_FATAL, fmt, args)
103 #else
104 #define MICROSTRAIN_LOG_FATAL(...) (void)0
105 #define MICROSTRAIN_LOG_FATAL_V(fmt, args) (void)0
106 #endif
107 
113 #if MICROSTRAIN_LOGGING_MAX_LEVEL >= MICROSTRAIN_LOG_LEVEL_ERROR
114 #define MICROSTRAIN_LOG_ERROR(...) MICROSTRAIN_LOG_LOG(MICROSTRAIN_LOG_LEVEL_ERROR, __VA_ARGS__)
115 #define MICROSTRAIN_LOG_ERROR_V(fmt, args) MICROSTRAIN_LOG_LOG_V(MICROSTRAIN_LOG_LEVEL_ERROR, fmt, args)
116 #else
117 #define MICROSTRAIN_LOG_ERROR(...) (void)0
118 #define MICROSTRAIN_LOG_ERROR_V(fmt, args) (void)0
119 #endif
120 
125 #if MICROSTRAIN_LOGGING_MAX_LEVEL >= MICROSTRAIN_LOG_LEVEL_WARN
126 #define MICROSTRAIN_LOG_WARN(...) MICROSTRAIN_LOG_LOG(MICROSTRAIN_LOG_LEVEL_WARN, __VA_ARGS__)
127 #define MICROSTRAIN_LOG_WARN_V(fmt, args) MICROSTRAIN_LOG_LOG_V(MICROSTRAIN_LOG_LEVEL_WARN, fmt, args)
128 #else
129 #define MICROSTRAIN_LOG_WARN(...) (void)0
130 #define MICROSTRAIN_LOG_WARN_V(fmt, args) (void)0
131 #endif
132 
137 #if MICROSTRAIN_LOGGING_MAX_LEVEL >= MICROSTRAIN_LOG_LEVEL_INFO
138 #define MICROSTRAIN_LOG_INFO(...) MICROSTRAIN_LOG_LOG(MICROSTRAIN_LOG_LEVEL_INFO, __VA_ARGS__)
139 #define MICROSTRAIN_LOG_INFO_V(fmt, args) MICROSTRAIN_LOG_LOG_V(MICROSTRAIN_LOG_LEVEL_INFO, fmt, args)
140 #else
141 #define MICROSTRAIN_LOG_INFO(...) (void)0
142 #define MICROSTRAIN_LOG_INFO_V(fmt, args) (void)0
143 #endif
144 
150 #if MICROSTRAIN_LOGGING_MAX_LEVEL >= MICROSTRAIN_LOG_LEVEL_DEBUG
151 #define MICROSTRAIN_LOG_DEBUG(...) MICROSTRAIN_LOG_LOG(MICROSTRAIN_LOG_LEVEL_DEBUG, __VA_ARGS__)
152 #define MICROSTRAIN_LOG_DEBUG_V(fmt, args) MICROSTRAIN_LOG_LOG_V(MICROSTRAIN_LOG_LEVEL_DEBUG, fmt, args)
153 #else
154 #define MICROSTRAIN_LOG_DEBUG(...) (void)0
155 #define MICROSTRAIN_LOG_DEBUG_V(fmt, args) (void)0
156 #endif
157 
163 #if MICROSTRAIN_LOGGING_MAX_LEVEL >= MICROSTRAIN_LOG_LEVEL_TRACE
164 #define MICROSTRAIN_LOG_TRACE(...) MICROSTRAIN_LOG_LOG(MICROSTRAIN_LOG_LEVEL_TRACE, __VA_ARGS__)
165 #define MICROSTRAIN_LOG_TRACE_V(fmt, args) MICROSTRAIN_LOG_LOG_V(MICROSTRAIN_LOG_LEVEL_TRACE, fmt, args)
166 #else
167 #define MICROSTRAIN_LOG_TRACE(...) (void)0
168 #define MICROSTRAIN_LOG_TRACE_V(fmt, args) (void)0
169 #endif
170 
171 
178 #define MICROSTRAIN_LOG_ERROR_WITH_ERRNO(msg) MICROSTRAIN_LOG_ERROR(msg ": %d %s\n", errno, strerror(errno))
179 
188 #define MICROSTRAIN_LOG_ERROR_WITH_ERRNO_EX(msg, ...) MICROSTRAIN_LOG_ERROR(msg ": %d %s\n", __VA_ARGS__, errno, strerror(errno))
189 
200 #define MICROSTRAIN_LOG_BYTES(level, buffer, length, ...) { \
201  MICROSTRAIN_LOG_LOG(level, __VA_ARGS__); \
202  for(size_t i=0; i<length; i++) { \
203  MICROSTRAIN_LOG_LOG(level, " %02X", buffer[i]); \
204  } \
205  MICROSTRAIN_LOG_LOG(level, "\n"); \
206 }
207 
211 
212 #ifdef __cplusplus
213 } // extern "C"
214 #endif
microstrain_logging_user_data
void * microstrain_logging_user_data(void)
Gets the currently active logging user data.
Definition: logging.c:60
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:71
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:28
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:44
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:85
microstrain_log_level
uint8_t microstrain_log_level
Logging level enum.
Definition: logging.h:27
microstrain_logging_level
microstrain_log_level microstrain_logging_level(void)
Gets the currently active logging level.
Definition: logging.c:50
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:101
microstrain_logging_callback
microstrain_log_callback microstrain_logging_callback(void)
Gets the currently active logging callback.
Definition: logging.c:40