Zeeshan Hayat
Zeeshan Hayat

Reputation: 431

Controlling print outs in a program with command line argument

I am trying to control the print outs in my program. I wrote a small script for that which takes a command line argument and decides to print or not to print depending upon the flag -v passed to it. Now I have to write out(bool) in my actual program everywhere. Is it possible to make the decision in the start of the program and just to use out instead of out(bool).

class mystreambuf : public std::streambuf
{
};
mystreambuf nostreambuf;
std::ostream nocout(&nostreambuf);
#define out(b) ((b==true)?std::cout : nocout )

int main(int argc, char* argv[])
{

    std::cout << "Program name is: " << argv[0] << "\n";

    int counter;
    bool verbose;

    if (argc == 1)
    {
        std::cout << "No command line argument passed" << argv[0] << "\n";
    }

    if (argc >= 2)
    {
        for (counter = 0; counter < argc; counter++)
        {
            std::cout << "Argument passed are: " << argv[counter] << "\n";
            if (strcmp(argv[counter], "-v") == 0)
            {
                verbose = true;
            }
            else
            {
                verbose = false;
            }
        }
    }

    out(verbose) << "\n hello world \n";

    system("pause");
}

Upvotes: 1

Views: 129

Answers (3)

Serge Ballesta
Serge Ballesta

Reputation: 149125

A simple way would be to build a minimal class containing the boolean flag and a reference to the actual stream. Then the << operator would be either a no-op or a delegation to the actual stream. It could be:

class LogStream {
    std::ostream& out;
    bool verbose;
public:
    LogStream(std::ostream& out = std::cout): out(out) {}
    void set_verbose(bool verbose) {
    this->verbose = verbose;
    }
    template <class T>
    LogStream& operator << (const T& val) {
    if (verbose) {
        out << val;
    }
    return *this;
    }
};

It can then be used that way:

int main(int argc, char* argv[])
{

    LogStream out;
    ...
    out.set_verbose(verbose);
    out << "\n hello world \n";
    ...
}

Upvotes: 1

acraig5075
acraig5075

Reputation: 10756

Simply make out a variable being one of the two options:

std::ostream &out = verbose ? std::cout : nocout;

and continue printing with out <<

Upvotes: 0

Fantastic Mr Fox
Fantastic Mr Fox

Reputation: 33924

You could make a singleton instance that you setup at the beginning and log through. Something like:

class Printer {
    public: static std::streambuf getInstance() {
        if (_instance._printing) {
           return std::cout;
        }
        return _instance._nostreambuf;
    }
    static void setPrinting(bool set) {
        _instance._printing = set;
    }
    private:
    static Printer _instance;
    mystreambuf _nostreambuf;
    bool _printing;
    // delete constructors and assignment operators.
}

Note: Rapid pseudo code, not tested.

Which you would use like:

Printer::setPrinting(verboseBool);
Printer::getInstance() << "Print text";

But what you would be making is a logger. Of which there are plenty. Here is a nice list.

Upvotes: 1

Related Questions