Jump to content
  • Advertisement
Sign in to follow this  
Maxamor

Redirecting stdout and std::cout to the same place

This topic is 2611 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

My game project is written in C++ but makes use of Lua, which is written in C. My logging functions use std::cout to write messages to a log file, but I'm not able to capture print messages that take place in Lua scripts in the same log file, since the Lua library uses sprintf and the like.

I am using this code to redirect stdout but am receiving warnings from MSVC about freopen being unsafe.

freopen( "file.txt", "w", stdout );

The warnings are not uncommon, and having found a thread about the exact warnings I decided to redirect stdout using a "stream redirection guard" as described in the thread. The redirection works for std::cout but not for stdout. This makes sense to me but I'm not sure how to achieve what I want.

So is it possible to get both pointing to the same stream or file?

Share this post


Link to post
Share on other sites
Advertisement
Close stdout and stderr, open your new file and then use dup2() to copy the new file descriptor into stdout and stdin.

Share this post


Link to post
Share on other sites
Have you tried playing with http://www.cplusplus.com/reference/iostream/ios_base/sync_with_stdio/ ?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!