Changed the terminal initialization code, to make it work with VS2015.
This commit is contained in:
parent
c5fea401bd
commit
ea12214b1b
@ -99,36 +99,6 @@ static const char *clean_str(char *str)
|
|||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
|
||||||
// HELPER MACROS
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
|
||||||
|
|
||||||
#define REPLACE_STANDARD_STREAM(TYPE, HANDLE) do \
|
|
||||||
{ \
|
|
||||||
const int fd_##TYPE = _open_osfhandle((intptr_t)GetStdHandle(HANDLE), flags); \
|
|
||||||
FILE *const file_##TYPE = (fd_##TYPE >= 0) ? _fdopen(fd_##TYPE, "wb") : NULL; \
|
|
||||||
if(file_##TYPE) \
|
|
||||||
{ \
|
|
||||||
g_terminal_backup_file_##TYPE = *(std##TYPE); \
|
|
||||||
*(std##TYPE) = *(file_##TYPE); \
|
|
||||||
g_terminal_filebuf_##TYPE.reset(new std::filebuf(file_##TYPE)); \
|
|
||||||
g_terminal_backup_fbuf_##TYPE = std::c##TYPE.rdbuf(); \
|
|
||||||
std::c##TYPE.rdbuf(g_terminal_filebuf_##TYPE.data()); \
|
|
||||||
} \
|
|
||||||
} \
|
|
||||||
while(0)
|
|
||||||
|
|
||||||
#define RESTORE_STANDARD_STREAM(TYPE) do \
|
|
||||||
{ \
|
|
||||||
if(!g_terminal_filebuf_##TYPE.isNull()) \
|
|
||||||
{ \
|
|
||||||
*(std##TYPE) = g_terminal_backup_file_##TYPE; \
|
|
||||||
std::c##TYPE.rdbuf(g_terminal_backup_fbuf_##TYPE); \
|
|
||||||
g_terminal_filebuf_##TYPE.reset(NULL); \
|
|
||||||
} \
|
|
||||||
} \
|
|
||||||
while(0)
|
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
// TERMINAL VARIABLES
|
// TERMINAL VARIABLES
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
@ -136,32 +106,38 @@ while(0)
|
|||||||
//Critical section
|
//Critical section
|
||||||
static MUtils::Internal::CriticalSection g_terminal_lock;
|
static MUtils::Internal::CriticalSection g_terminal_lock;
|
||||||
|
|
||||||
|
//Is terminal attached?
|
||||||
|
static volatile bool g_terminal_attached = false;
|
||||||
|
|
||||||
//Terminal replacement streams
|
//Terminal replacement streams
|
||||||
static bool g_terminal_attached = false;
|
|
||||||
static QScopedPointer<std::filebuf> g_terminal_filebuf_out;
|
static QScopedPointer<std::filebuf> g_terminal_filebuf_out;
|
||||||
static QScopedPointer<std::filebuf> g_terminal_filebuf_err;
|
static QScopedPointer<std::filebuf> g_terminal_filebuf_err;
|
||||||
|
|
||||||
//Backup of original streams
|
|
||||||
static FILE g_terminal_backup_file_out;
|
|
||||||
static FILE g_terminal_backup_file_err;
|
|
||||||
static std::streambuf* g_terminal_backup_fbuf_out;
|
|
||||||
static std::streambuf* g_terminal_backup_fbuf_err;
|
|
||||||
|
|
||||||
//The log file
|
//The log file
|
||||||
static QScopedPointer<QFile> g_terminal_log_file;
|
static QScopedPointer<QFile> g_terminal_log_file;
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
// TERMINAL EXIT
|
// TERMINAL UTILS
|
||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
static void terminal_restore(void)
|
static inline void terminal_connect(FILE *const fp, std::ostream &os, QScopedPointer<std::filebuf> &buff)
|
||||||
|
{
|
||||||
|
FILE *temp = NULL;
|
||||||
|
if (freopen_s(&temp, "CONOUT$", "wb", fp) == 0)
|
||||||
|
{
|
||||||
|
setvbuf(temp, NULL, _IONBF, 0);
|
||||||
|
buff.reset(new std::filebuf(temp));
|
||||||
|
os.rdbuf(buff.data());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
static inline void terminal_restore(void)
|
||||||
{
|
{
|
||||||
MUtils::Internal::CSLocker lock(g_terminal_lock);
|
MUtils::Internal::CSLocker lock(g_terminal_lock);
|
||||||
|
|
||||||
if(g_terminal_attached)
|
if(g_terminal_attached)
|
||||||
{
|
{
|
||||||
RESTORE_STANDARD_STREAM(out);
|
g_terminal_filebuf_out.reset();
|
||||||
RESTORE_STANDARD_STREAM(err);
|
g_terminal_filebuf_err.reset();
|
||||||
FreeConsole();
|
FreeConsole();
|
||||||
g_terminal_attached = false;
|
g_terminal_attached = false;
|
||||||
}
|
}
|
||||||
@ -232,9 +208,8 @@ void MUtils::Terminal::setup(int &argc, char **argv, const char* const appName,
|
|||||||
//-------------------------------------------------------------------
|
//-------------------------------------------------------------------
|
||||||
//See: http://support.microsoft.com/default.aspx?scid=kb;en-us;105305
|
//See: http://support.microsoft.com/default.aspx?scid=kb;en-us;105305
|
||||||
//-------------------------------------------------------------------
|
//-------------------------------------------------------------------
|
||||||
const int flags = _O_WRONLY | _O_U8TEXT;
|
terminal_connect(stdout, std::cout, g_terminal_filebuf_out);
|
||||||
REPLACE_STANDARD_STREAM(out, STD_OUTPUT_HANDLE);
|
terminal_connect(stderr, std::cerr, g_terminal_filebuf_err);
|
||||||
REPLACE_STANDARD_STREAM(err, STD_ERROR_HANDLE );
|
|
||||||
atexit(terminal_restore);
|
atexit(terminal_restore);
|
||||||
|
|
||||||
const HWND hwndConsole = GetConsoleWindow();
|
const HWND hwndConsole = GetConsoleWindow();
|
||||||
|
Loading…
Reference in New Issue
Block a user