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:
- 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]
- 369 is the line number in that file where this DBG macro was used.
- normal printf arguments can be used, which makes it very flexible.
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