std::cout wapper
Moderator: PC Supremacists
-
- Chaos Rift Newbie
- Posts: 2
- Joined: Fri Dec 07, 2012 3:12 pm
- Current Project: A mmorpg empire fantasy ruling game
- Favorite Gaming Platforms: PC, Mac, Xbox
- Programming Language of Choice: c++, lua
std::cout wapper
How would you make wrapper to out std::cout to text file in c++
- bbguimaraes
- Chaos Rift Junior
- Posts: 294
- Joined: Wed Apr 11, 2012 4:34 pm
- Programming Language of Choice: c++
- Location: Brazil
- Contact:
Re: std::cout wapper
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.
- dandymcgee
- ES Beta Backer
- Posts: 4709
- Joined: Tue Apr 29, 2008 3:24 pm
- Current Project: https://github.com/dbechrd/RicoTech
- Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
- Programming Language of Choice: C
- Location: San Francisco
- Contact:
Re: std::cout wapper
Use rdbuf to redirect std::cout to a file:
http://www.cplusplus.com/reference/ios/ios/rdbuf/
http://www.cplusplus.com/reference/ios/ios/rdbuf/
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches!
- MarauderIIC
- Respected Programmer
- Posts: 3406
- Joined: Sat Jul 10, 2004 3:05 pm
- Location: Maryland, USA
Re: std::cout wapper
I vote for using fsteams like bb wrote in his postdandymcgee wrote:Use rdbuf to redirect std::cout to a file:
http://www.cplusplus.com/reference/ios/ios/rdbuf/
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
-
- Chaos Rift Newbie
- Posts: 2
- Joined: Fri Dec 07, 2012 3:12 pm
- Current Project: A mmorpg empire fantasy ruling game
- Favorite Gaming Platforms: PC, Mac, Xbox
- Programming Language of Choice: c++, lua
Re: std::cout wapper
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
- Falco Girgis
- Elysian Shadows Team
- Posts: 10294
- Joined: Thu May 20, 2004 2:04 pm
- Current Project: Elysian Shadows
- Favorite Gaming Platforms: Dreamcast, SNES, NES
- Programming Language of Choice: C/++
- Location: Studio Vorbis, AL
- Contact:
Re: std::cout wapper
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.
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.
- dandymcgee
- ES Beta Backer
- Posts: 4709
- Joined: Tue Apr 29, 2008 3:24 pm
- Current Project: https://github.com/dbechrd/RicoTech
- Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
- Programming Language of Choice: C
- Location: San Francisco
- Contact:
Re: std::cout wapper
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.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
For your own print/output functions, fstreams are the right choice.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches!
- Nokurn
- Chaos Rift Regular
- Posts: 164
- Joined: Mon Jan 31, 2011 12:08 pm
- Favorite Gaming Platforms: PC, SNES, Dreamcast, PS2, N64
- Programming Language of Choice: Proper C++
- Location: Southern California
- Contact:
Re: std::cout wapper
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:
Which will generate some output like:
in debug.log and std::cout.
The equivalent functionality is simple to replicate in C-style using stdarg, vprintf, and vfprintf.
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
The equivalent functionality is simple to replicate in C-style using stdarg, vprintf, and vfprintf.
- Falco Girgis
- Elysian Shadows Team
- Posts: 10294
- Joined: Thu May 20, 2004 2:04 pm
- Current Project: Elysian Shadows
- Favorite Gaming Platforms: Dreamcast, SNES, NES
- Programming Language of Choice: C/++
- Location: Studio Vorbis, AL
- Contact:
Re: std::cout wapper
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?dandymcgee wrote: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.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
- dandymcgee
- ES Beta Backer
- Posts: 4709
- Joined: Tue Apr 29, 2008 3:24 pm
- Current Project: https://github.com/dbechrd/RicoTech
- Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
- Programming Language of Choice: C
- Location: San Francisco
- Contact:
Re: std::cout wapper
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.Falco Girgis wrote:2) SDL, a C library, uses cout, a function of the C++ standard library? Orly?
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches!