std::cout wapper

Anything related in any way to game development as a whole is welcome here. Tell us about your game, grace us with your project, show us your new YouTube video, etc.

Moderator: PC Supremacists

Post Reply
shawk
Chaos Rift Newbie
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

Post by shawk »

How would you make wrapper to out std::cout to text file in c++
User avatar
bbguimaraes
Chaos Rift Junior
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

Post 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.
User avatar
dandymcgee
ES Beta Backer
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

Post by dandymcgee »

Use rdbuf to redirect std::cout to a file:
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! :twisted:
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: std::cout wapper

Post 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
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
shawk
Chaos Rift Newbie
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

Post 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
User avatar
Falco Girgis
Elysian Shadows Team
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

Post 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.
User avatar
dandymcgee
ES Beta Backer
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

Post 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.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
Nokurn
Chaos Rift Regular
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

Post 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.
User avatar
Falco Girgis
Elysian Shadows Team
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

Post 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?
User avatar
dandymcgee
ES Beta Backer
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

Post 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.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
Post Reply