GithubHelp home page GithubHelp logo

dbg-macro's Introduction

buildstatus_ codecov_ nala_

About

A few macros that prints and returns the value of a given expression for quick and dirty debugging, inspired by Rusts dbg!(…) macro and its C++ variant.

dbg(expr) for primitive data types (int, float, etc.), strings and pointers.

dbgb(expr) to force boolean true/false output.

dbga(expr, length) for array of primitive data types.

dbgh(expr, size) for hexdump output.

dbge(expr) for negative error codes.

dbgbt() for a backtrace.

Just include dbg.h in your project to use it.

Example

tryit_

See examples for the files used in this example.

#include "dbg.h"

static int factorial(int n)
{
    if (dbgb(n <= 1)) {
        return dbg(1);
    } else {
        return dbg(n * factorial(n - 1));
    }
}

int main()
{
    char message[] = "hello";
    dbg(message);  // main.c:15: message = "hello"
    dbgh(message, sizeof(message));

    const int a = 2;
    const int b = dbg(3 * a) + 1;  // main.c:19: 3 * a = 6 (0x6)

    int numbers[2] = { b, 13 };
    dbga(numbers, 2);  // main.c:22: numbers = [7, 13] (length: 2)

    dbg(factorial(4));
    dbge(-EINVAL);
    dbgbt();

    return (0);
}

Build and run:

image

Compile time configuration

Define NDBG to make the macros a no-op.

Define DBG_NCOLOR for colorless output.

Define DBG_OSTREAM=<my-ostream> for custom output stream (stderr by default).

dbg-macro's People

Contributors

eerimoq avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  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.