Unified Debug
Unified Debug is an internal run-time debugging package for C and JavaScript.
In the spirit of Fred Fish's dbug package, which is widely used in the MySQL
server, this package manages "printf()-style" trace debugging of source code.
It allows a JavaScript user to set a debug output level, enabling trace messages
at that level and above from both native code and JavaScript source files.
Additionally, the user can also enable all messages from individual source files.
By default, messages are written to stderr. They can be redirected to any
Node WritableStream, with some limitations. The current implementation does
not allow redirection when messages originate from C code running in a UV
worker thread.
Package Contents
- unified_debug.js: Node.JS module which declares, documents, and implements
the Unified Debug API for JavaScript.
- example-c/unified_debug.h: header file declaring the C and C++ APIs. The
API is based on preprocessor macros. If the symbol UNIFIED_DEBUG is not
defined at compile-time, a stub API is substituted so that debugging is
disabled entirely.
- example-c/unified_debug.cpp: source file which implements both the C API
and the C-to-JavaScript bridge.
The JavaScript API
function on();
function off();
function level_urgent();
function level_notice();
function level_info();
function level_debug();
function level_detail();
function set_file_level(filename, level);
function getLogger();
Example of the Logger API
To write messages, use getLogger() to obtain a Logger, then use the Logger to
write messages.
var unified_debug = require("unified_debug"),
udebug = unified_debug.getLogger("ThisFile.js");
udebug.log("a debug message"); // same as log_debug()
udebug.log_urgent("urgent message");
udebug.log_notice("notice message");
udebug.log_info("info message");
udebug.log_debug("debug message");
udebug.log_detail("detail message");
udebug.URGENT; // numeric constant values for log levels
udebug.NOTICE;
udebug.INFO;
udebug.DEBUG;
udebug.DETAIL;
udebug.is_urgent(); // returns true if current output level is >= URGENT
udebug.is_notice(); // returns true if current output level is >= NOTICE
udebug.is_info(); // returns true if current output level is >= INFO
udebug.is_debug(); // returns true if current output level is >= DEBUG
udebug.is_detail(); // returns true if current output level is == DETAIL
udebug.set_file_level(x); // Set our own per-file output level to x
Using Unified Debug with C/C++
- Copy unified_debug.h and unified_debug.cpp from example-c into your own
project and build system.
- #DEFINE UNIFIED_DEBUG 1 to enable debugging at compile time.
The C and C++ APIs
In these macros message expands to any printf()-style set of arguments.
The maximum allowed size for a debug message is defined in unified_debug.h;
messages longer than the maximum size will be truncated.
DEBUG_PRINT(message);
DEBUG_PRINT_DETAIL(message);
DEBUG_PRINT_INFO(message);
DEBUG_TRACE();
DEBUG_ENTER();
DEBUG_LEAVE();
DEBUG_MARKER(udeb_level);