Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing to both terminal and file c++

I found this question answered for Python, Java, Linux script, but not C++:

I'd like to write all outputs of my C++ program to both the terminal and an output file. Using something like this:

int main ()
{
freopen ("myfile.txt","w",stdout);
cout<< "Let's try this"; 
fclose (stdout);
return 0;
}

outputs it to only the output file named "myfile.txt", and prevents it from showing on the terminal. How can I make it output to both simultaneously? I use visual studio 2010 express (if that would make any difference).

Thanks in advance!

like image 635
Aly Avatar asked Jan 20 '14 11:01

Aly


3 Answers

Possible solution: use a static stream cout-like object to write both to cout and a file.

Rough example:

struct LogStream 
{
    template<typename T> LogStream& operator<<(const T& mValue)
    {
        std::cout << mValue;
        someLogStream << mValue;
    }
};

inline LogStream& lo() { static LogStream l; return l; }

int main()
{
    lo() << "hello!";
    return 0;
}

You will probably need to explicitly handle stream manipulators, though.

Here is my library implementation.

like image 110
Vittorio Romeo Avatar answered Sep 18 '22 13:09

Vittorio Romeo


There is no built in way to do this in one step. You have to write the data to a file and then write the data out on screen in two steps.

You can write a function that takes in the data and the filename and does this for you, to save you time, some sort of logging function.

like image 35
Ólafur Waage Avatar answered Sep 20 '22 13:09

Ólafur Waage


I have a method to do this, and it is based on a subscriber model.

In this model all your logging goes to a "logging" manager and you then have "subscribers" that decide what to do with the messages. Messages have topics (for me a number) and loggers subscribe to one or more topic.

For your purpose, you create 2 subscribers, one that outputs to the file and one that outputs to the console.

In the logic of your code you simply output the message, and at this level not need to know what is going to be done with it. In my model though you can check first if there are any "listeners" as this is considered cheaper than constructing and outputting messages that will only end up in /dev/null (well you know what I mean).

like image 28
CashCow Avatar answered Sep 18 '22 13:09

CashCow