mirror of
https://github.com/OPM/ResInsight.git
synced 2025-02-25 18:55:39 -06:00
Redirect stdout and stderr to console using freopen_s
Previous redirection code does not work on VS2015, this commit works on both VS2010 and VS2015
This commit is contained in:
parent
8f3b88c798
commit
21f8b722b1
@ -1216,39 +1216,10 @@ int RiaApplication::launchUnitTestsWithConsole()
|
|||||||
// Only one console can be associated with an app, so should fail if a console is already present.
|
// Only one console can be associated with an app, so should fail if a console is already present.
|
||||||
AllocConsole();
|
AllocConsole();
|
||||||
|
|
||||||
bool redirStdOut = true;
|
FILE* consoleFilePointer;
|
||||||
bool redirStdErr = true;
|
|
||||||
bool redirStdIn = false;
|
|
||||||
|
|
||||||
if (redirStdOut)
|
freopen_s(&consoleFilePointer, "CONOUT$", "w", stdout);
|
||||||
{
|
freopen_s(&consoleFilePointer, "CONOUT$", "w", stderr);
|
||||||
HANDLE stdHandle = GetStdHandle(STD_OUTPUT_HANDLE);
|
|
||||||
int fileDescriptor = _open_osfhandle((intptr_t)stdHandle, _O_TEXT);
|
|
||||||
FILE* fp = _fdopen(fileDescriptor, "w");
|
|
||||||
|
|
||||||
*stdout = *fp;
|
|
||||||
setvbuf(stdout, NULL, _IONBF, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (redirStdErr)
|
|
||||||
{
|
|
||||||
HANDLE stdHandle = GetStdHandle(STD_ERROR_HANDLE);
|
|
||||||
int fileDescriptor = _open_osfhandle((intptr_t)stdHandle, _O_TEXT);
|
|
||||||
FILE* fp = _fdopen(fileDescriptor, "w");
|
|
||||||
|
|
||||||
*stderr = *fp;
|
|
||||||
setvbuf(stderr, NULL, _IONBF, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (redirStdIn)
|
|
||||||
{
|
|
||||||
HANDLE stdHandle = GetStdHandle(STD_INPUT_HANDLE);
|
|
||||||
int fileDescriptor = _open_osfhandle((intptr_t)stdHandle, _O_TEXT);
|
|
||||||
FILE* fp = _fdopen(fileDescriptor, "r");
|
|
||||||
|
|
||||||
*stdin = *fp;
|
|
||||||
setvbuf(stdin, NULL, _IONBF, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Make cout, wcout, cin, wcin, wcerr, cerr, wclog and clog point to console as well
|
// Make cout, wcout, cin, wcin, wcerr, cerr, wclog and clog point to console as well
|
||||||
std::ios::sync_with_stdio();
|
std::ios::sync_with_stdio();
|
||||||
|
Loading…
Reference in New Issue
Block a user