UNIX :: cl_debug

Download icon

cl_debug is a very light-weight C library for logging and "printf debugging". The only requirements are some functions of the C standard library (from stdio.h, string.h and assert.h).

Cl_debug solved a need I had while writing software to support hardware input devices. In that situation it was easier to debug certain parts of my multithreaded code using a "printf" batch log rather than using breakpoints in the debugger. Some other times the debugger wouldn't "attach" to the executable I wanted to inspect, or some nasty bugs continued to appear only in release builds.

For all these situations and more, one can be assured that the old school way of printf-debugging will always be available. Hence, cl_debug was born. It has been used successfully on UN*X systems and Windows.

Additional macros are provided for CoreFoundation and Cocoa (Mac OS X) and the Adobe InDesign CS2 SDK. Other per-app / per-framework customizations could be added easily.

License

cl_debug is licensed under the nice revised BSD license.

Quick facts

API

/*
 * Initialization
 */
 
cl_debug_init(const char *logfilename);


/*
 * Logging
 * (on stderr or separate file)
 */

LOG(const char *formatString, ...);
LOG_CF(const char *stringNoFormatting, CFStringRef theStrToLog);
LOG_NS(NSString *formatString, ...);

/*
 * Debugging
 * (on stderr)
 */

debug_enter(const char *debugString);
debug_exit(const char *debugString);

debug0msg(const char *formatString, ...);
debug0cocoa(NSString *formatString, ...);

debug1msg(const char *formatString, ...);
debug1cocoa(NSString *formatString, ...);

debug2msg(const char *formatString, ...);
debug2cocoa(NSString *formatString, ...);

/*
 * The following macros are provided for compilers with no GCC extensions 
 * (C99 doesn't allow an empty variable argument list). 
 */

LOG0(const char *logString);
debug0msg0(const char *debugString);
debug1msg0(const char *debugString);
debug2msg0(const char *debugString);

Examples

Usage is very straightforward. An example will make things ever more straightforward.

/* my_foo_file.c */

#if TARGET_OS_MAC
#include <Cocoa/Cocoa.h>
#endif

#include "cl_debug.h"

const char *kOurProductName = "Foobar";

void foo()
{
    debug_enter("foo");
    debug0msg("about to perform some important operation %s", "(low priority)");
    /* ... */
    debug2msg("about to perform critical operation %s", "(high priority!)");
    /* ... */
    debug1msg0("CodeWarrior is bitching if a variadic macro is used with 0 args!");
    /* ... */
    LOG("%d: I need to log this on my log file.", 1);
    debug_exit("foo");
}
    
int main()
{
    cl_debug_init("my-log-file.txt");
    foo();
    return 0;
}

Output on stderr:

#Foobar# ./test_cl_debug.m[20]::foo ENTER
#Foobar# ./test_cl_debug.m[21] about to perform some important operation (low priority)
#Foobar# ./test_cl_debug.m[23] about to perform critical operation (high priority!)
#Foobar# ./test_cl_debug.m[25] CodeWarrior is bitching if a variadic macro is used with 0 args!
#Foobar# ./test_cl_debug.m[28]::foo EXIT

Output on my-log-file.txt:

Foobar: [27] 1: I need to log this on my log file.


View individual files

v1.0: cl_debug.h   cl_debug.c

Support

Feel free to send bug reports (and suggestions).





Valid XHTML 1.0! Valid CSS!