Page 1 of 1

std::cout wapper

Posted: Fri Dec 07, 2012 3:20 pm
by shawk
How would you make wrapper to out std::cout to text file in c++

Re: std::cout wapper

Posted: Fri Dec 07, 2012 5:29 pm
by bbguimaraes
If you look here, you can see that std::cout is a subclass of std::ostream, as is std::ofstream (the class you can use to output to a file). So, anywhere you need methods from std::ostream (where you use things like 'std::cout << 3.14159'), you can use any of them:
#include <iostream>
#include <fstream>

void print(std::ostream & os) {
    os << 3.14159;
}

int main(int argc, char ** argv) {
    print(std::cout);
    std::ofstream ofs("somefile");
    print(ofs);
    return 0;
}
I hope that helps.

Re: std::cout wapper

Posted: Fri Dec 07, 2012 7:26 pm
by dandymcgee
Use rdbuf to redirect std::cout to a file:
http://www.cplusplus.com/reference/ios/ios/rdbuf/

Re: std::cout wapper

Posted: Fri Dec 07, 2012 11:26 pm
by MarauderIIC
dandymcgee wrote:Use rdbuf to redirect std::cout to a file:
http://www.cplusplus.com/reference/ios/ios/rdbuf/
I vote for using fsteams like bb wrote in his post

Re: std::cout wapper

Posted: Sat Dec 08, 2012 7:50 am
by shawk
But I need lua to print to it also so if lua print function only uses std::cout I would have to make a different function for lua to access the ofstream

Re: std::cout wapper

Posted: Sat Dec 08, 2012 9:51 am
by Falco Girgis
Uh, I'm with Marauder.

I would make your print function use printf()/cout/whatever standard console output you want, then have an OPTIONAL FLAG that you can set to enable logging, where the print function also uses fstream to print to a file.

Re: std::cout wapper

Posted: Sat Dec 08, 2012 10:23 am
by dandymcgee
shawk wrote:But I need lua to print to it also so if lua print function only uses std::cout I would have to make a different function for lua to access the ofstream
This is exactly why I recommended rdbuf. Marauder and Falco are overlooking the fact that certain libraries assume std::cout INTERNALLY. I'm not sure that Lua does this, but I know for a fact that SDL does. Though you could try to change and recompile them, it is much easier to just forward the standard out stream to a log file.

For your own print/output functions, fstreams are the right choice.

Re: std::cout wapper

Posted: Sat Dec 08, 2012 2:57 pm
by Nokurn
While dandymcgee's reasons are perfectly valid, I would try to avoid fiddling with std::cout as much as possible. Here's a class I wrote a while ago for doing C++-style logging with time stamps and the option to stream to std::cout as well: https://gist.github.com/4241898.

It's used like this:
Log debuglog("debug.log", true);
debuglog << "Player triggered warp tile at (" << ev.x << "," << ev.y << ")" << std::endl;
Of course, you probably shouldn't instantiate a log in a function that runs more than once, unless you change Log::open to use append mode.

Which will generate some output like:

Code: Select all

[2012-12-08 12:49:32] Player triggered warp tile at 73,22
in debug.log and std::cout.

The equivalent functionality is simple to replicate in C-style using stdarg, vprintf, and vfprintf.

Re: std::cout wapper

Posted: Sat Dec 08, 2012 11:24 pm
by Falco Girgis
dandymcgee wrote:
shawk wrote:But I need lua to print to it also so if lua print function only uses std::cout I would have to make a different function for lua to access the ofstream
This is exactly why I recommended rdbuf. Marauder and Falco are overlooking the fact that certain libraries assume std::cout INTERNALLY. I'm not sure that Lua does this, but I know for a fact that SDL does.
1) Lua is not one of them. It is overridable. 2) SDL, a C library, uses cout, a function of the C++ standard library? Orly?

Re: std::cout wapper

Posted: Sun Dec 09, 2012 10:18 am
by dandymcgee
Falco Girgis wrote:2) SDL, a C library, uses cout, a function of the C++ standard library? Orly?
Actually, I seem to have remembered incorrectly. By default, SDL redirects stdout to stdout.txt in the working directory. You have to use some similar hack if you want stdout to go to a console window instead without having to recompile the library with NO_STDIO_REDIRECT set. So it's essentially the opposite of the OP's problem, my bad.