Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Problems redirecting sf::err() in another language.  (Read 1692 times)

0 Members and 1 Guest are viewing this topic.

Jebbs

  • Sr. Member
  • ****
  • Posts: 358
  • DSFML Developer
    • View Profile
    • Email
Problems redirecting sf::err() in another language.
« on: August 20, 2013, 07:24:57 pm »
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?
DSFML - SFML for the D Programming Language.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Problems redirecting sf::err() in another language.
« Reply #1 on: August 20, 2013, 07:54:41 pm »
Hmm... since SFML is linked statically to CSFML, there may be one instance of sf::err() in each CSFML DLL. This is annoying ;D

You can test with SFML dynamically linked to CSFML, to verify this.
Laurent Gomila - SFML developer

Jebbs

  • Sr. Member
  • ****
  • Posts: 358
  • DSFML Developer
    • View Profile
    • Email
Re: Problems redirecting sf::err() in another language.
« Reply #2 on: August 20, 2013, 11:54:23 pm »
Hmm... since SFML is linked statically to CSFML, there may be one instance of sf::err() in each CSFML DLL.

That's exactly what it was! In order to redirect it and capture the output properly I have to define those functions for each shared library. This is totally annoying, but it works now.
DSFML - SFML for the D Programming Language.