4

I want to create a class which would help me with debugging by providing std::cout or QDebug like functionality using a 3D renderer.

I have the following renderer method which I'm using now

IRenderer::renderText(int posX, int posY, const float* color, const char* text, ...);

// E.g.
int i;
float f;
float color[] = {1, 1, 1, 1};

renderer->renderText(50, 50, color, "Float %f followed by int %i", f, i);

This actually works fine, but I wonder if it's possible to create a class which would allow me to do it like this:

debug() << "My variables: " << i << ", " << "f";

I assume there would be a template function which would build the string to pass to renderText() based on input type, but I'm not quite sure how to implement it.

2 Answers 2

2

An alternative to Rob's answer is to include an ostringstream in your custom logger class, and use the destructor to do the logging:

#include <iostream>
#include <sstream>

class MyLogger
{
protected:
    std::ostringstream ss;

public:
    ~MyLogger()
    {
        std::cout << "Hey ma, I'm a custom logger! " << ss.str();

        //renderer->renderText(50, 50, color, ss.str());
    }

    std::ostringstream& Get()
    {
        return ss;
    }
};

int main()
{
    int foo = 12;
    bool bar = false;
    std::string baz = "hello world";

    MyLogger().Get() << foo << bar << baz << std::endl;

    // less verbose to use a macro:
#define MY_LOG() MyLogger().Get()
    MY_LOG() << baz << bar << foo << std::endl;

    return 0;
}
1
  • Instead of using macros I suggest you to define template<class T> MyLogger & operator << (T const &) for MyLogger class, which would just doing the same with ss
    – kassak
    Feb 19, 2013 at 12:02
0

I like to derive my logging class from std::ostream, so I get all of the stream goodness. The trick is to put all of your application-specific code in the associated streambuf class. Consider this working example. To modify it to meed your needs, simply rewrite CLogBuf::sync(), like so:

int sync() { 
  renderer->renderText(50, 50, color, "%s", str());
  str("");
  return false;
}  

Example:

#include <iostream>
#include <sstream>

class CLogger : public std::ostream {
private:
    class CLogBuf : public std::stringbuf {
    private:
        // or whatever you need for your application
        std::string m_marker;
    public:
        CLogBuf(const std::string& marker) : m_marker(marker) { }
        ~CLogBuf() {  pubsync(); }
        int sync() { std::cout << m_marker << ": " << str(); str("");  return !std::cout; }
    };

public:
    // Other constructors could specify filename, etc
    // just remember to pass whatever you need to CLogBuf
    CLogger(const std::string& marker) : std::ostream(new CLogBuf(marker)) {}
    ~CLogger() { delete rdbuf(); }
};

int main()
{
    CLogger hi("hello");
    CLogger bye("goodbye");

    hi << "hello, world" << std::endl;
    hi << "Oops, forgot to flush.\n";
    bye << "goodbye, cruel world\n" << std::flush;
    bye << "Cough, cough.\n";
}
4
  • You don't even need to create the temporary logger: CLogger("hello") << "hello, world" << std::endl; Feb 19, 2013 at 3:57
  • @CongXu - Yes, you do need the named object. Temporaries can't bind to non-const references like operator>>(ostream&, const char*). See stackoverflow.com/questions/14381311/…
    – Robᵩ
    Feb 19, 2013 at 4:03
  • Any way to do this without using streams?
    – jaho
    Feb 25, 2013 at 19:19
  • Because the code I'm working on uses stlport with iostreams disabled. I have a working solution now using va_list, still wonder how could I implement one using << operator.
    – jaho
    Feb 28, 2013 at 22:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.