Giter Site home page Giter Site logo

c_dbg's Introduction

dbg(...)

dbg.h is a clumsy replica of sharkdp/dbg-macro, which mainly target for C89 and above.

This project provides a single header file with two macros dbg(...) and dbge(...) that can be used in all circumstances where you would typically write printf(...) or fprintf(...).

The dbg(...) will prints debugging output to the stdout, yet the dbge(...) will prints to the stderr.

dbg*(...) macro takes two arguments: the former is the expression itself, the latter is the format specifier (just as in printf(3)).

Example

#include <stdio.h>

#define DBG_H_DEF_ONCE
#include "dbg.h"

static int factorial(int n) {
    if (dbg(n <= 1, %d)) return dbg(1, %d);
    return dbg(n * factorial(n - 1), %d);
}

int main(void) {
    int x = 1;
    dbg(x > 0, %d);
    factorial(4);
    const char *s = "hello world";
    dbg(s, %s);
    dbge(sizeof(double), %zu);
    dbg(&main, %p);
    dbg(main == &main, %d);
    dbg("this line is executed", %s);
    return 0;
}

You must #define DBG_H_DEF_ONCE only once before include the "dbg.h".

Note that the format specifier(the second argument) is NOT double-quoted.

Sample output of above code (try it yourself):

dbg.h example output

Feature

  • Easy to read, colorized output (colors will be disable when the output is not an interactive terminal)

  • Prints file name, line number, function name and the original expression

  • Prints format specifier for the printed-out value (which far less powerful than sharkdp/dbg-macro)

  • Can be used inside expressions (passing through the original value)

  • The dbg.h header issues a compiler warning when included (so you don't forget to remove it)

  • Compatible with C89, C99, C11.

Configuration

  • #define DBG_H_NO_WARNING to suppress output of the WARNING: the "dbg.h" header is included in your code base

  • #define DBG_H_DISABLE to disable the debugging output, so dbg(x, fs) will be simply replaced by x itself.

Stability

The exact output printed by this macro should not be relied upon and is subject to future changes.

Panics

Panics if writing to fprintf(3) fails (by calling assert(3), so #define NDEBUG will disable this panic).

P.S. this is extreme unlikely to happen.

Courtesy

This project mainly inspired by sharkdp/dbg-macro, this project cannot succeeded without his effort.

Part of this README inspired by sharkdp/dbg-macro and Rust's std::dbg!().

TODO

_Generic keyword introduced since C11, we may do better in debugging output.

c_dbg's People

Contributors

yazgoo avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.