mbed_debug - logging levels

27 Apr 2014

Hi, Is it possible to provide 2-3 logging levels to mbed_debug, soemthing similar to http://lxr.free-electrons.com/source/include/linux/kern_levels.h.

Thanks uCFreak

27 Apr 2014

Based largely on the work of others, I derived this -

// undef to disable, use 4-char name (representing the file) for consistency across projects.
#define DEBUG "HTCLi"
// the following could all be in a header file, so long as you defined #DEBUG "Name" before including the header file.
#include <cstdio>
#if (defined(DEBUG) && !defined(TARGET_LPC11U24))
#define DBG(x, ...)  std::printf("[DBG %s %3d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__);
#define WARN(x, ...) std::printf("[WRN %s %3d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__);
#define ERR(x, ...)  std::printf("[ERR %s %3d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__);
#define INFO(x, ...) std::printf("[INF %s %3d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__);
#else
#define DBG(x, ...)
#define WARN(x, ...)
#define ERR(x, ...)
#define INFO(x, ...)
#endif

To my knowledge, this is not part of any standard library, but you may find this scrap, or something similar, in various cpp files. Oh, and a small caution. I only have the LPC1768, so tried to carry the LPT11U24 faithfully along - hoping I didn't damage that implementation.

To use it, be sure the #DEBUG is defined, then use construction like this:

      DBG("Read header : %s: %s", key, value);

On the terminal window, you might then see something like this:

[DBG HTCL 369] Read header : Last-Modified: Tue, 22 Apr 2014 23:50:15 GMT
[DBG HTCL 369] Read header : Accept-Ranges: bytes
[DBG HTCL 369] Read header : Content-Length: 2900

Points of interest:

  1. HTCL hints to the filename, based on the original #define DEBUG "HTCL", which is in a single source file. Note the macros don't use FILE, because it creates quite a long path name, making it word-wrap more than I like. [no other reason than that]
  2. 369 is the line number in that file where this DBG macro was used.
  3. normal printf arguments can be used, which makes it very flexible.
27 Apr 2014

Please ignore this suggestion, I guess we can #define different levels ourselves:

debug levels defined in a header

/* Log settings for this driver */
#define DEBUG_EMERG      false    	/* system is unusable */
#define DEBUG_ALERT      false    	/* action must be taken immediately */
#define DEBUG_CRIT       false    	/* critical conditions */
#define DEBUG_ERR        false    	/* error conditions */
#define DEBUG_WARNING    false    	/* warning conditions */
#define DEBUG_NOTICE     false    	/* normal but significant condition */
#define DEBUG_INFO       true    	/* informational */
#define DEBUG_DEBUG      false    	/* debug-level messages */

debug levels used in source code

debug_if( DEBUG_NOTICE, "+%s )", __FUNCTION__ );
debug_if( DEBUG_EMERG, "%s )", __FUNCTION__ );

Thanks, uCFreak

27 Apr 2014

Hi David, Your solution is pretty good, I'll try it out on my Nucleo.

Thanks uCFreak