Listing 2 Member functions of MyDebugStreamBuf

MyDebugStream::MyDebugStream(

    BOOL UseMessageBox,                        // default output specifier

    char* pTitle )                             // possible title for MessageBox

    : m_buf( UseMessageBox, pTitle ),          // pass arguments to
                                        // MyDebugStreambuf constructor

    ostream((streambuf*) &m_buf )              // pass MyDebugStreambuf
                                        // to ostream constructor
{
    ASSERT( this );
}

MyDebugStreambuf::MyDebugStreambuf(BOOL UseMessageBox, char* pTitle )

    : strstreambuf()
{
    ASSERT( this );
    m_UseMessageBox = UseMessageBox;             // store the default
                                          // output flag

    m_pTitle = pTitle;                           // store pointer to the
                                          // MessageBox title

    m_BufferSize = DEBUG_BUFFER_SIZE;            // store the buffer size

    m_pBuffer = new char[m_BufferSize + 1];      // get one extra for
                                          // NULL terminator

    ASSERT(m_pBuffer );                          // paranoia check

    setp(m_pBuffer, m_pBuffer + m_BufferSize );  // init put pointers
}

int MyDebugStreambuf::overflow( int ch )
{
    ASSERT( this );
    if( out_waiting() )
        Flush();                               // data in buffer
    return strstreambuf::overflow( ch );
}

int MyDebugStreambuf::sync()
{
    ASSERT( this );
    if( out_waiting() )
        Flush();                               // data in buffer
    return strstreambuf::sync();
}

void MyDebugStreambuf::Flush()
{
    ASSERT( this );
    BOOL UseMessageBox = m_UseMessageBox;      // get default
    
    char* p = pptr() - 1;                     // get pointer to
                                        // last character
    if( *p == (char) FLUSH_TO_BOX )
    {
        UseMessageBox = TRUE;                 // set output flag

        *p = '\0';                            // discard FLUSH_TO_BOX
    }
    else if( *p == (char) FLUSH_TO_DEBUG )
    {
        UseMessageBox = FALSE;                // set output flag

        *p = '\0'    ;                        // discard FLUSH_TO_DEBUG
    }
    else
        *pptr() = '\0';                       // NULL terminate

    if( UseMessageBox )                        // do the output
        MessageBox( NULL, m_pBuffer, m_pTitle, MB_OK|MB_ICONINFORMATION );
    else
        ::OutputDebugString( m_pBuffer );

    setp( m_pBuffer, m_pBuffer + m_BufferSize ); // reset buffer pointers
}

ostream& BoxFlush( ostream& os )
{
    return os << (char) FLUSH_TO_BOX << flush;
}

ostream& DebugFlush( ostream& os )
{
    return os << "\r\n"
          << (char) FLUSH_TO_DEBUG << flush;
}
// End of File