CS 326 Operating Systems

debug.c

DownloadView Raw

/**
 * debug.c
 *
 * Debugging using the preprocessor. When DEBUG is set to 0, the LOG statements
 * are completely removed from the binary produced after compliation.
 *
 * Source: https://stackoverflow.com/questions/5765175/macro-to-turn-off-printf-statements
 * 
 * To turn debug on, either:
 * - Add a line like this to your C file:
 *      #define DEBUG 1
 * - Compile as shown below
 *
 * Compile:
 *      gcc -g -Wall -o debug -DDEBUG=1 debug.c
 */

#include <stdio.h>

#define LOG(fmt, ...) \
    do { if (DEBUG) fprintf(stderr, fmt, __VA_ARGS__); } while (0)

/* If we haven't passed -DDEBUG=1 to gcc, then this will be set to 0: */
#ifndef DEBUG
#define DEBUG 0
#endif

int main(void)
{
    int i = 6;

    LOG("Hello world! i = %d\n", i);

    /* Note: if you just want to print a plain old string, this won't work!
     * BAD: LOG("Yo yo!\n");
     * To get around this, you can use:
     */
    LOG("%s", "Yo yo!\n");


#if DEBUG == 0
    printf("Debug is disabled! "
            "Re-compile with `-DDEBUG=1` to see log messages\n");
#endif

    return 0;
}