Utility functions and classes for C++, with emphasis on debug utilities.
Three header files are provided:
simple_debug.hpp: simple version, with easy-to-copy echo and trace macros. Mainly used when you don't want an extra header file in your project for some reason and/or only need very basic functionality.debug.hpp: complete version. Simply#includethe file and it's ready to use. Complete usage instructions below.xtrace.hpp: optional add-on to the complete version. Usage instructions below.
ECHO: any number of arguments. Prints them all with a line break after each of them.ECHOI: indented echo. Receives a number specifying the indentation (the number of tabs) and one or more extra arguments that are printed with the specified indentation.TRACE: two possible usages:TRACE(expression): prints the expression itself and its value, followed by a line break. For example:
char k = 'A'; TRACE(k); // prints "k = A"
TRACE(expression, custom_formatter): similar toTRACE(expression), but prints the result of custom_formatter(expression) instead of the value of the expression itself. Anything with a valid operator()(expression) is allowed.
TRACE_L: two possible usages:TRACE_L(name, expression): equivalent toTRACE(expression), but displays the value ofnameas if it was the name ofexpression.TRACE_L(name, expression, custom_formatter): same as above, but using a custom formatter.
BLANK: alias toECHO(). Prints a line break.DEBUG: write this before a line of code to represent a "checkpoint". If the program breaks prematurely (e.g if a segfault occurs), the following informations are printed regarding the last successful usage ofDEBUG(seetests/debug_macro_test.cppfor an example):- the line number
- the file name
- the function name
- a string describing the problem.
DEBUG_REDIRECT: two possible usages:DEBUG_REDIRECT(stream): redirects all the following debug output to a given output stream, which must be compatible withdecltype(std::cout).DEBUG_REDIRECT(function): every debug message will be forwarded to a given function, which must be compatible withstd::function<void(const std::string&)>.
DEBUG_EXEC: executes an arbitrary code. Useful to execute some code only if the debug mode is active.XTRACE: found only inxtrace.hpp. Use it likeXTRACE(expression). Works likeTRACE(expression), but it can pretty-print several other types likestd::vector,std::tuple, etc.
- The following macros can either be set directly in
debug.hppor overriden during compilation:ALLOW_DEBUG_USAGE: if set to 0, every macro usage will do nothing but declare a unused variable inside a new block (to trigger "unused variable" warnings during compilation to make it easy to find all macro usages).DEBUG_ENABLED: if set to 0, every macro usage will do nothing at all.