Engine/src/LogStream.cpp
/**
* Redirect standard output to a console window.
* Original source: http://dslweb.nwnexus.com/~ast/dload/guicon.htm
* With modifications from: http://stackoverflow.com/questions/311955/redirecting-cout-to-a-console-in-windows
* @date: December 28, 2015
*/
Redirect standard output to a console window.
Redirect standard output to a console window .
LogStreamConsole::LogStreamConsole()
{
    if ( AllocConsole() )
    {
        HANDLE lStdHandle = GetStdHandle( STD_OUTPUT_HANDLE );
        CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
        GetConsoleScreenBufferInfo( lStdHandle, &consoleInfo );
        consoleInfo.dwSize.Y = MAX_CONSOLE_LINES;
        SetConsoleScreenBufferSize( lStdHandle, consoleInfo.dwSize );
        SetConsoleCursorPosition( lStdHandle, { 0, 0 } );
        int hConHandle = _open_osfhandle( (intptr_t)lStdHandle, _O_TEXT );
        FILE* fp = _fdopen( hConHandle, "w" );
        freopen_s( &fp, "CONOUT$", "w", stdout );
        setvbuf( stdout, nullptr, _IONBF, 0 );
        lStdHandle = GetStdHandle( STD_INPUT_HANDLE );
        hConHandle = _open_osfhandle( (intptr_t)lStdHandle, _O_TEXT );
        fp = _fdopen( hConHandle, "r" );
        freopen_s( &fp, "CONIN$", "r", stdin );
        setvbuf( stdin, nullptr, _IONBF, 0 );
        lStdHandle = GetStdHandle( STD_ERROR_HANDLE );
        hConHandle = _open_osfhandle( (intptr_t)lStdHandle, _O_TEXT );
        fp = _fdopen( hConHandle, "w" );
        freopen_s( &fp, "CONOUT$", "w", stderr );
        setvbuf( stderr, nullptr, _IONBF, 0 );
        std::wcout.clear();
        std::cout.clear();
        std::wcerr.clear();
        std::cerr.clear();
        std::wcin.clear();
        std::cin.clear();
    }
}
LogStreamConsole :: LogStreamConsole ( ) { if ( AllocConsole ( ) ) { HANDLE lStdHandle = GetStdHandle ( STD_OUTPUT_HANDLE ) ; CONSOLE_SCREEN_BUFFER_INFO consoleInfo ; GetConsoleScreenBufferInfo ( lStdHandle , & consoleInfo ) ; consoleInfo . dwSize . Y = MAX_CONSOLE_LINES ; SetConsoleScreenBufferSize ( lStdHandle , consoleInfo . dwSize ) ; SetConsoleCursorPosition ( lStdHandle , { 0 , 0 } ) ; int hConHandle = _open_osfhandle ( ( intptr_t ) lStdHandle , _O_TEXT ) ; FILE * fp = _fdopen ( hConHandle , " " ) ; freopen_s ( & fp , " " , " " , stdout ) ; setvbuf ( stdout , nullptr , _IONBF , 0 ) ; lStdHandle = GetStdHandle ( STD_INPUT_HANDLE ) ; hConHandle = _open_osfhandle ( ( intptr_t ) lStdHandle , _O_TEXT ) ; fp = _fdopen ( hConHandle , " " ) ; freopen_s ( & fp , " " , " " , stdin ) ; setvbuf ( stdin , nullptr , _IONBF , 0 ) ; lStdHandle = GetStdHandle ( STD_ERROR_HANDLE ) ; hConHandle = _open_osfhandle ( ( intptr_t ) lStdHandle , _O_TEXT ) ; fp = _fdopen ( hConHandle , " " ) ; freopen_s ( & fp , " " , " " , stderr ) ; setvbuf ( stderr , nullptr , _IONBF , 0 ) ; std :: wcout . clear ( ) ; std :: cout . clear ( ) ; std :: wcerr . clear ( ) ; std :: cerr . clear ( ) ; std :: wcin . clear ( ) ; std :: cin . clear ( ) ; } }
Redirect standard output to a console window.
Redirect standard output to a console window .
// Allocate a console.  // Increase screen buffer to allow more lines of text than the default. // Redirect unbuffered STDOUT to the console. // Redirect unbuffered STDIN to the console. // Redirect unbuffered STDERR to the console. //Clear the error state for each of the C++ standard stream objects. We need to do this, as //attempts to access the standard streams before they refer to a valid target will cause the //iostream objects to enter an error state. In versions of Visual Studio after 2005, this seems //to always occur during startup regardless of whether anything has been read from or written to //the console or not.