Home
Reading
Searching
Subscribe
Sponsors
Statistics
Posting
Contact
Spam
Lists
Links
About
Hosting
Filtering
Features Download
Marketing
Archives
FAQ
Blog
 
Gmane
From: Brede Johansen <bredej-Re5JQEeQqe8AvxtiuMwx3w <at> public.gmane.org>
Subject: Re: Catching notify output
Newsgroups: gmane.comp.graphics.openscenegraph.user
Date: Monday 19th June 2006 14:06:11 UTC (over 10 years ago)
Hi jOan,

> if some one has a link to osgConsole where this should be implemented I'm
> interested.
> I can't find it.

Here's my notes on the subject based on posts by Marco and others.


How-to forward cout and cerr to file:

ofstream log("Message.log");
streambuf* out = cout.rdbuf(log.rdbuf());
streambuf* err = cerr.rdbuf(log.rdbuf());


How-to forward cout and cerr to debugger using custom streambuf:
Post on OSG mailing list by Marco and later patched by Ben D.

#include 

void Log(const char *msg)
{
#ifdef _MSC_VER
#ifdef _UNICODE
    wchar_t buf[1024];
    MultiByteToWideChar(CP_ACP, 0, msg, -1, buf, 1024);
    OutputDebugString(buf);
#else
    OutputDebugString(msg);
#endif
#endif
}

class OsgMsgTrap : public std::streambuf
{
public:
    inline virtual int_type overflow(int_type c =
std::streambuf::traits_type::eof())
    {
        if (c == std::streambuf::traits_type::eof()) return
std::streambuf::traits_type::not_eof(c);
        char str[2];
        str[0] = c;
        str[1] = 0;
        Log(str);
        return c;
    }
} g_Trap;

void main()
{
    // Redirect cout messages (where OSG sends its messages) to our own log
    std::cout.rdbuf(&g_Trap);
    std::cerr.rdbuf(&g_Trap);
}
 
CD: 4ms