My Project
|
Define a simple debugging interface to use in C programs. More...
#include <stdio.h>
Go to the source code of this file.
Macros | |
#define | DEBUG_ENABLED 0 |
#define | HERE debug("HERE") |
#define | debugV(name) #name,(name) |
#define | vDebug(fmt, name) debug("%s=(" fmt ")" , debugV(name)) |
#define | debug(fmt, ...) lDebug(1, fmt, ##__VA_ARGS__) |
#define | lDebug(level, fmt, ...) |
Functions | |
void | debugInit (int *argc, const char *argv[]) |
void | debugToFile (const char *fileName) |
void | debugClose (void) |
Variables | |
int | debugLevel |
FILE * | debugFile |
One method of debugging your C program is to add printf()
statements to your code. This file provides a way of including debug output, and being able to turn it on/off either at compile time or at runtime, without making any changes to your code. Two levels of debugging are provided. If the value DEBUG
is defined, your debug calls are compiled into your code. Otherwise, they are removed by the optimizer. There is an additional run time check as to whether to actually print the debugging output. This is controlled by the value debugLevel
.
To use it with gcc
, simply write a printf()
call, but replace the printf()
call by debug()
.
To easily print the value of a single variable, use
vDebug("format", var); // "format" is the specifier (e.g. "%d" or "%s", etc)
To use debug(), but control when it prints, use
lDebug(level, "format", var); // print when debugLevel >= level
#define DEBUG_ENABLED 0 |
This macro controls whether all debugging code is optimized out of the executable, or is compiled and controlled at runtime by the debugLevel
variable. The value (0/1) depends on whether the macro DEBUG
is defined during the compile.
#define debugV | ( | name | ) | #name,(name) |
Expand a name into a string and a value
name | name of variable |
#define HERE debug("HERE") |
Print the file name, line number, function name and "HERE"
#define lDebug | ( | level, | |
fmt, | |||
... | |||
) |
Print this message if the variable debugLevel
is greater than or equal to the parameter.
level | the level at which this information should be printed |
fmt | the formatting string (MUST be a literal) |
Output the name and value of a single variable
fmt | the formatting string (MUST be a literal) |
name | name of the variable to print |
void debugClose | ( | void | ) |
Close the external file and reset debugFile
to stderr
void debugInit | ( | int * | argc, |
const char * | argv[] | ||
) |
Initialize the variable debugLevel
depending on the value of argv[1]
. Normally called from main
with the program arguments. If argv[1]
is or begins with -debug
, the value of debugLevel
is set and argc, argv
are modified appropriately. An entry of -debug5
sets the level to 5. If the function is not called, the user is responsible for setting debugLevel
in other code.
argc | the number of command line arguments |
argv | the array of command line arguments. |
void debugToFile | ( | const char * | fileName | ) |
Send the debug output to a file
fileName | name of file to write debug output to |
FILE* debugFile |
The file where debug output is written. Defaults to stderr
. debugToFile()
allows output to any file.
int debugLevel |
Control how much debug output is produced. Higher values produce more output. See the use in lDebug()
.