From 58189e5403c8518510baed78c21c97e56790c9be Mon Sep 17 00:00:00 2001 From: Nik Date: Sun, 22 Oct 2023 23:39:14 +0200 Subject: [PATCH] impr: Allow console output to work when debugging (#1382) --- cmake/build_helpers.cmake | 5 ++++- lib/libimhex/include/hex/api/imhex_api.hpp | 6 ++++++ lib/libimhex/source/api/imhex_api.cpp | 8 ++++++++ main/gui/source/window/win_window.cpp | 14 ++++++++++---- 4 files changed, 28 insertions(+), 5 deletions(-) diff --git a/cmake/build_helpers.cmake b/cmake/build_helpers.cmake index edce5cdfc..84667a4a5 100644 --- a/cmake/build_helpers.cmake +++ b/cmake/build_helpers.cmake @@ -98,7 +98,10 @@ macro(configurePackingResources) endif() if (WIN32) - set(APPLICATION_TYPE WIN32) + if (NOT (CMAKE_BUILD_TYPE STREQUAL "Debug")) + set(APPLICATION_TYPE WIN32) + endif () + set(IMHEX_ICON "${IMHEX_BASE_FOLDER}/resources/resource.rc") if (CREATE_PACKAGE) diff --git a/lib/libimhex/include/hex/api/imhex_api.hpp b/lib/libimhex/include/hex/api/imhex_api.hpp index 5e8d67214..742e997e8 100644 --- a/lib/libimhex/include/hex/api/imhex_api.hpp +++ b/lib/libimhex/include/hex/api/imhex_api.hpp @@ -537,6 +537,12 @@ namespace hex { */ std::string getCommitBranch(); + /** + * @brief Checks if ImHex was built in debug mode + * @return True if ImHex was built in debug mode, false otherwise + */ + bool isDebugBuild(); + enum class UpdateType { Stable, Nightly diff --git a/lib/libimhex/source/api/imhex_api.cpp b/lib/libimhex/source/api/imhex_api.cpp index 2f02a5f12..2e1723037 100644 --- a/lib/libimhex/source/api/imhex_api.cpp +++ b/lib/libimhex/source/api/imhex_api.cpp @@ -641,6 +641,14 @@ namespace hex { #endif } + bool isDebugBuild() { + #if defined DEBUG + return true; + #else + return false; + #endif + } + bool updateImHex(UpdateType updateType) { // Get the path of the updater executable std::fs::path executablePath; diff --git a/main/gui/source/window/win_window.cpp b/main/gui/source/window/win_window.cpp index 95860efbc..0aeca4ddb 100644 --- a/main/gui/source/window/win_window.cpp +++ b/main/gui/source/window/win_window.cpp @@ -232,6 +232,7 @@ namespace hex { } + [[maybe_unused]] static void reopenConsoleHandle(u32 stdHandleNumber, i32 stdFileDescriptor, FILE *stdStream) { // Get the Windows handle for the standard stream HANDLE handle = ::GetStdHandle(stdHandleNumber); @@ -259,12 +260,17 @@ namespace hex { void Window::initNative() { - // Check for the __IMHEX_FORWARD_CONSOLE__ environment variable that was set by the forwarder application - // If it's present attach to its console window - if (hex::getEnvironmentVariable("__IMHEX_FORWARD_CONSOLE__") == "1") { + if (ImHexApi::System::isDebugBuild()) { + // If the application is running in debug mode, ImHex runs under the CONSOLE subsystem, + // so we don't need to do anything besides enabling ANSI colors + log::impl::enableColorPrinting(); + } else if (hex::getEnvironmentVariable("__IMHEX_FORWARD_CONSOLE__") == "1") { + // Check for the __IMHEX_FORWARD_CONSOLE__ environment variable that was set by the forwarder application + + // If it's present, attach to its console window ::AttachConsole(ATTACH_PARENT_PROCESS); - // Reopen stdin, stdout and stderr to the console + // Reopen stdin, stdout and stderr to the console if not in debug mode reopenConsoleHandle(STD_INPUT_HANDLE, STDIN_FILENO, stdin); reopenConsoleHandle(STD_OUTPUT_HANDLE, STDOUT_FILENO, stdout);