debug: fancy debug logs in C

hacker_compDevelopers spend a lot of their time looking into debug logs to find problems in code. debug is a single C header file ready to be included in your project to add the support for meaningful and easy debug logs. debug uses the MIT license.

Before going into the type of logs debug supports, let’s take a look into how the library can be included.

To download the project:

$ git clone https://github.com/esneider/debug.git

Include debug.h anywhere in your project. For example:

void function(void) {
    ...
#include "debug.h"
    debug(var); // => var = XXX
    ...
}

To disable it:

#define NDEBUG

before the inclusion.

Usage

#include "debug.h"

int answer(void) {
    return 42;
}

int main(void) {
    int num = 1;
    char *str = "hello";

    debug(num);
    // => num = 1

    debug(num, str, answer());
    // => num = 1
    // str = hello
    // answer() = 42

    debug_raw("counting:", 1, -2, 3 + 0.4);
    // => counting: 1 -2 3.4

    debug_raw(str, "world!");
    // => hello world!

    idebug(num);
    // => example.c:26: num = 1

    idebug(num, str);
    // => example.c:29:
    // num = 1
    // str = hello

    idebug_raw("The answer is", answer());
    // => example.c:34: The answer is 42

    return 0;
}

Limitations

  • debug requires C11 support (uses _Generic). Consider using the flag -std=c11 when compiling.
  • Literal chars have type int, cast them to char to print them as actual chars:
    debug_raw("Find the", (char)'X'); // => Find the X

On GitHub: debug

Leave a Reply

Your email address will not be published. Required fields are marked *