Debug library for instrumentation a.k.a printf debugging

Dependents:   pyrocommander Projektni_zadatak_Analogni_sat ProjetOctopode

A debug library for instrumentation a.k.a printf debugging.

The aims of the library are:

  • Provide functions for instrumentation debugging that are simple and practical
  • Avoid people reinventing the wheel. Without a standard approach to debugging, each projects invents their own DBG, xprintf, dprintf type macros
    • And they are all slightly different, and possibly non-composable/compatible - consistency is good!
  • Provide support for statically selective debugging, such as enabling debug in a module or class (vs. all or nothing)
    • Allow debug to be included as part of library code bases, and only enabled when needed (rather than used during development, then commented out/removed)
    • Eliminate at compile time the calls/message strings of disabled debug, so instrumentation can be left in at zero cost
  • Allow options for dynamic debugging (can be programatically enabled)
  • Use stderr as debug stream, and ensure it can be re-routed to different stream devices

Usage

Basic debug

#include "mbed.h"
#include "debug.h"

int main() {    
    debug("Hello debug world");

    int v = 5;
    debug("Hello debug world, v = %d", v);
}

Conditional debug (static)

#include "mbed.h"
#include "debug.h"

#define DEBUG_MAIN 1

int main() {    
    debug(DEBUG_MAIN, "Hello debug world");

    int v = 5;
    debug(DEBUG_MAIN, "Hello debug world, v = %d", v);
}
  • Recommended define format: DEBUG_<module> or DEBUG_<module>_<aspect>
    • e.g. DEBUG_LCD, DEBUG_LCD_REDRAW

Conditional debug (dynamic)

#include "mbed.h"
#include "debug.h"

int main() {    
    bool debug_main = 0;
    debug(debug_main, "Hello debug world");
    debug_main = 1;
    int v = 5;
    debug(debug_main, "Hello debug world, v = %d", v);

    for(int i=0; i<10; i++) {
        debug(i > 7, "Debug something");
    }
}

Redirecting stderr

#include "mbed.h"
#include "debug.h"

LocalFileSystem local("local");

int main() {    
    freopen("/local/debug.txt", "w", stderr);
    debug("Hello debug file");
    fclose(stderr);
} 
Revision:
1:12ff10369961
Parent:
0:aa1c8e69d98f
Child:
2:24afdea2d903
--- a/debug.h	Sat Aug 04 21:04:21 2012 +0000
+++ b/debug.h	Sat Aug 04 21:59:29 2012 +0000
@@ -24,6 +24,10 @@
 #include <stdarg.h>
 #include <stdio.h>
 
+/** Output a debug message
+ * 
+ * @param format printf-style format string, followed by variables
+ */
 static inline void debug(const char *format, ...) {
     va_list args;
     va_start(args, format);
@@ -31,6 +35,11 @@
     va_end(args);
 }
 
+/** Conditionally output a debug message
+ * 
+ * @param condition output only if condition is true
+ * @param format printf-style format string, followed by variables
+ */
 static inline void debug(bool condition, const char *format, ...) {
     if(condition) {
         va_list args;