D Paste by extrawurst
Description: RedirectToConsole
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | import std.c.stdio; import std.string:toStringz; import win32.windows; char* toStringz_us(in string _s){return cast(char*).toStringz(_s);} extern(C) int _open_osfhandle(int osfhandle,int flags); void RedirectIOToConsole( in string _title ) { enum MAX_CONSOLE_LINES = 1024; enum _O_TEXT = 0x4000; static _iobuf newOut; static _iobuf newErr; CONSOLE_SCREEN_BUFFER_INFO coninfo; // allocate a console for this app assert( AllocConsole() ); // set the screen buffer to be big enough to let us scroll text GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE),&coninfo); // increase lines shown in console coninfo.dwSize.Y = MAX_CONSOLE_LINES; SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE),coninfo.dwSize); // change console window title SetConsoleTitle(.toStringz_us(_title)); // redirect unbuffered STDOUT to the console { int hConHandle; HANDLE lStdHandle; lStdHandle = GetStdHandle(STD_OUTPUT_HANDLE); hConHandle = _open_osfhandle(cast(int)lStdHandle, _O_TEXT); assert( hConHandle != -1); newOut._flag = 2; newOut._file = hConHandle; *stdout = newOut; auto res = setvbuf( stdout, null, _IONBF, 0 ); assert(res==0); fprintf(stdout,toStringz("redirected stdout!\n")); } // redirect unbuffered STDERR to the console { int hConHandle; HANDLE lStdHandle; lStdHandle = GetStdHandle(STD_ERROR_HANDLE); hConHandle = _open_osfhandle(cast(int)lStdHandle, _O_TEXT); assert( hConHandle != -1); newErr._flag = 2; newErr._file = hConHandle; *stderr = newErr; auto res = setvbuf( stderr, null, _IONBF, 0 ); assert(res==0); fprintf(stderr,toStringz("redirected stderr!\n")); } } |