Reputation: 3737
I'm working on embedded project and it is very convenient to log information in your code for debugging purposes, but fully blown C++ logger libraries are hardly usable in embedded environment. Can you suggest how to implement simple, clean and fast logging module? I don't really need fine control over logging backends, outputs, levels etc. I just want to enable and disable logging statements without obscuring the code. Currently I'm doing it this way:
#define DEBUG_ENCODER_PRINTF
// ...
#ifdef DEBUG_ENCODER_PRINTF
debug_printf("enc: %d %d\r\n",
get_encoder_unwrapped(0),
get_encoder_unwrapped(1));
#endif
But I already have dozens of different logging statements, and a lot of preprocessor definitions make code look ugly.
Upvotes: 2
Views: 781
Reputation: 3737
I did it like this in the end:
#define DEBUG_PRINTF_ENABLED
#define DEBUG_SONAR_PRINTF false
#define DEBUG_ENCODER_PRINTF true
// ...
#ifdef DEBUG_PRINTF_ENABLED
# define DEBUG_PRINTF(logger, pFormat, ...)\
{ if (DEBUG_##logger##_PRINTF) debug_printf(pFormat, __VA_ARGS__); }
#else
# define DEBUG_PRINTF(...)
#endif
// ...
DEBUG_PRINTF(ENCODER,
"enc: %d %d\r\n",
get_encoder_unwrapped(0),
get_encoder_unwrapped(1));
Upvotes: 3
Reputation: 58929
You can eliminate those pesky #ifdef
s and #endif
s if you do something like this:
#ifdef DEBUG_ENCODER_PRINTF
#define debug_encoder_printf debug_printf
#else
#define debug_encoder_printf(...)
#endif
debug_encoder_printf("hi")
will now call debug_printf
when DEBUG_ENCODER_PRINTF
is defined, but if that is not defined, the whole "call" will be removed by the preprocessor (replaced with nothing).
Upvotes: 5