1
0
mirror of synced 2024-12-01 02:37:18 +01:00

fix: Log was not being output to the console if one was open

This commit is contained in:
WerWolv 2021-11-28 13:43:44 +01:00
parent 243820ca95
commit efad16f2c0
2 changed files with 22 additions and 9 deletions

View File

@ -114,7 +114,7 @@ macro(configurePackageCreation)
set(application_type)
set(imhex_icon "${CMAKE_SOURCE_DIR}/res/resource.rc")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wl,--allow-multiple-definition")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Wl,-subsystem,windows")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wl,-subsystem,windows")
if (CREATE_PACKAGE)
set(CPACK_GENERATOR "WIX")

View File

@ -170,15 +170,28 @@
void Window::initNative() {
auto hConsoleWindow = ::GetConsoleWindow();
::ShowWindow(hConsoleWindow, FALSE);
// Attach to parent console if one exists
AttachConsole(ATTACH_PARENT_PROCESS);
auto hConsole = ::GetStdHandle(STD_OUTPUT_HANDLE);
if (hConsole != INVALID_HANDLE_VALUE) {
DWORD mode = 0;
if (::GetConsoleMode(hConsole, &mode)) {
mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
::SetConsoleMode(hConsole, mode);
// Redirect cin, cout and cerr to that console
freopen("CONIN$", "w", stdin);
freopen("CONOUT$", "w", stdout);
freopen("CONERR$", "w", stderr);
setvbuf(stdin, nullptr, _IONBF, 0);
setvbuf(stdout, nullptr, _IONBF, 0);
setvbuf(stderr, nullptr, _IONBF, 0);
fmt::print("\n");
// Enable color format specifiers in console
{
auto hConsole = ::GetStdHandle(STD_OUTPUT_HANDLE);
if (hConsole != INVALID_HANDLE_VALUE) {
DWORD mode = 0;
if (::GetConsoleMode(hConsole, &mode)) {
mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING | ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT;
::SetConsoleMode(hConsole, mode);
}
}
}
}