Sign in to follow this  
bjarnia

SDL: printf/cout ?

Recommended Posts

I just started playing with SDL, as I think GLUT is quite limiting. I however did like the console window that GLUT spawned, where I could print out debugging information. I'm perfecetly capable of placing breakpoints and such, but sometimes printing out to a console is a better and faster way. Is it possible to spawn such a window in SDL? Where do printf() lines go anyway? /dev/null? :o Ps.. I know that singleton file logging is pretty effective for debugging.. But I prefer console method for quick and dirty debugging... I use file logging as well for the more obvious stuff and error handling.

Share this post


Link to post
Share on other sites
How SDL handles stdout and stderr depends on the platform. For example, on Windows, if the application is compiled for the Windows subsystem, SDL redirects stdout and stderr to text files.

Share this post


Link to post
Share on other sites
Don't use a singleton log file.

This code works for me:

#ifdef _WIN32

const char * confile = "CONOUT$";

FreeConsole();
if( !AllocConsole() )
{
// handle error
}

freopen(confile, "w", stdout);
freopen(confile, "w", stderr);

#endif



However, this way you lose logging to files, which is handy. If you are feeling adventurous, you can write a custom std::streambuf that redirects printing to std::cout (or any stream) to an arbitrary location. With a bit of work, you can write a streambuf that writes to a console and to a file. However, you would have to switch from printf to C++ streams. Which is probably a good idea.

Share this post


Link to post
Share on other sites
Ok.. In that case, I'll probably just log everything then..

However.. does anyone know of a good lightweight "log watching" program? that would function similarly to a console window.. I could just let it sit on my other monitor and instantly print out to a window anything that was written to the log?

EDIT:
The code above worked. Thanks a ton, that's awesome!

However.. Still, if anyone does happen to know of a log watching program that I described above, feel free to post :)

Share this post


Link to post
Share on other sites
Thanks. That was exactly what I was looking for.

Ps yes I am on windows... I've used tail before on linux but it didn't ring a bell this time, or else it would've been easier to google :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this