My Project
Macros | Functions | Variables
Debug.h File Reference

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
 

Detailed Description

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

Based on code and ideas found here and here.

Author
Fritz Sieker

Macro Definition Documentation

#define debug (   fmt,
  ... 
)    lDebug(1, fmt, ##__VA_ARGS__)

Simple alias for lDebug()

#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

Parameters
namename of variable
#define HERE   debug("HERE")

Print the file name, line number, function name and "HERE"

#define lDebug (   level,
  fmt,
  ... 
)
Value:
do { \
if (DEBUG_ENABLED && (debugLevel >= level)) \
fprintf((debugFile ? debugFile : stderr), "DEBUG %s[%d] %s() " fmt "\n", \
__FILE__, __LINE__, __func__, ##__VA_ARGS__); \
} while(0)
#define DEBUG_ENABLED
Definition: Debug.h:83
int debugLevel
Definition: Debug.c:27
FILE * debugFile
Definition: Debug.c:29

Print this message if the variable debugLevel is greater than or equal to the parameter.

Parameters
levelthe level at which this information should be printed
fmtthe formatting string (MUST be a literal)
#define vDebug (   fmt,
  name 
)    debug("%s=(" fmt ")" , debugV(name))

Output the name and value of a single variable

Parameters
fmtthe formatting string (MUST be a literal)
namename of the variable to print

Function Documentation

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.

Parameters
argcthe number of command line arguments
argvthe array of command line arguments.
void debugToFile ( const char *  fileName)

Send the debug output to a file

Parameters
fileNamename of file to write debug output to

Variable Documentation

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().