I'm working on a modified version of CSFML to use for my binding, and one of the things I wanted to be able to do was to get the text inside sf::err() and send it to D. I did some experiments with redirecting sf::err() to a ostringstream, and then getting the text from that when I know something had been written to sf::err(). This actually works,
until SFML writes to sf::err() internally. At that point, the redirection I did earlier doesn't seem to apply anymore. Is there something I'm missing?
I'm not sure how much this will help, but here's some code to explain what I mean.
Err.cpp(added to my modified CSFML):
//Headers
#include <SFML/System/Err.h>
#include <SFML/System/Err.hpp>
#include <sstream>
#include <string>
namespace
{
std::ostringstream outputStream;//stream sf::err() get's redirected to
std::string outputString;//string for storing output
}
void sfErr_redirect(void)
{
//redirect sf::err() to write to outputStream
sf::err().rdbuf(outputStream.rdbuf());
}
const char* sfErr_getOutput(void)
{
//get the contents of outputstream
outputString = outputStream.str();
//and then clear the stream
outputStream.str("");
return outputString.c_str();
}
//test function to make sure sf::err() has been redirected correctly
void sfErr_writeTo(const char* text)
{
sf::err() << text << std::endl;
}
This code in D works as expected(nothing is written to the console):
void main(string[] args)
{
sfErr_redirect();
sfErr_writeTo("Writting some text to sf::err() from D!");
}
This D code also works as expected(grabs the contents and outputs it to the console):
void main(string[] args)
{
sfErr_redirect();
sfErr_writeTo("Writting some text to sf::err() from D!");
writeln(text(sfErr_getOutput()));
}
However, like I said above, something that uses sf::err() internally seems to ignore the redirect. This shouldn't write anything to the console, but it does(says it failed to load the font face):
void main(string[] args)
{
sfErr_redirect();
Font font = new Font();
if(!font.loadFromFile("fontThatDoesntExist.ttf"))
{
return;
}
}
SFML seems to have its own version of sf::err() separate from the one I am accessing. The following code tells me that it failed to load the font face, but the code I personally write to sf::err()
does get redirected:
void main(string[] args)
{
sfErr_redirect();
Font font = new Font();
if(!font.loadFromFile("fontThatDoesntExist.ttf"))
{
sfErr_writeTo("Writting some text to sf::err() from D!");
return;
}
}
I thought I was on to something, but now I'm at a loss. Any suggestions for how I can proceed with this?