Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an established alternative to cout in C++ that behaves similarly to qDebug? [duplicate]

Tags:

c++

qt

I like how easy it is to write some variables to console output in C++ using qDebug from Qt:

int a = b = c = d = e = f = g = 1;
qDebug() << a << b << c << d << e << f << g;

Result:

1 1 1 1 1 1 1

In comparison, using std::cout would require me to add the spacing and newline manually to get the same result:

std::cout << a << " " << b << " " << c << " " << d << " " << e << " " << f << " " << g << "\n";

While I often use Qt, I sometimes work on projects where adding the Qt framework just to get access to qDebug would be overkill. And although it is not hard to write a new class that behaves similar to qDebug, I am wondering if any established alternative to std::cout with similar behavior to qDebug already exists?

Edit: What I am looking for is ideally an established library (or snippet, but I prefer something existing over rolling my own) that I can always use as my go-to solution when I need something like this. It could be header-only, or a large logging library that is much used and well-tested, or a simple, small snippet. The point is that it should be small and/or standard enough that other collaborators would be okay with including it in a project just for debugging/logging purposes.

Edit 2: To clarify: It would be great to have a solution that both inserts spaces between the variables and newlines for each statement:

myDebug << 1 << 2 << 3;
myDebug << 4 << 5 << 6;

Should return:

1 2 3
4 5 6
like image 370
dragly Avatar asked Dec 23 '22 08:12

dragly


2 Answers

struct debugcout { };

template <typename T>
debugcout& operator<<(debugcout& os, const T& x)
{
    std::cout << x << ' ';
    return os;
}

inline debugcout debug{};

Usage:

int main()
{
    debug << 1 << 2 << 3;
}
like image 161
Vittorio Romeo Avatar answered Dec 28 '22 08:12

Vittorio Romeo


#include <iostream>

class myDebug {
    bool is_first{true};
    bool is_last{true};
public:
    myDebug() = default;
    myDebug(myDebug const &) = delete;
    myDebug & operator = (myDebug const &) = delete;
    myDebug & operator = (myDebug &&) = delete;
    myDebug(myDebug && dc) noexcept 
      : is_first{false} {
        dc.is_last = false;
    }
    ~myDebug() {
        if (is_last)
            std::cout << '\n';
    }
    template <typename T>
    friend myDebug operator<<(myDebug db, const T& x) {
        if (db.is_first)
            db.is_first = false;
        else
            std::cout << ' ';

        std::cout << x;
        return db;
    }
};
int main() {
    myDebug() << 1 << 2 << 3;
    myDebug() << 4 << 5 << 6;
}

Demo

like image 21
O'Neil Avatar answered Dec 28 '22 06:12

O'Neil