diff --git a/src/common/log.cpp b/src/common/log.cpp index a4c224006..e1aaa2aa5 100644 --- a/src/common/log.cpp +++ b/src/common/log.cpp @@ -75,6 +75,16 @@ void UnregisterCallback(CallbackFunctionType callbackFunction, void* pUserParam) } } +bool IsConsoleOutputEnabled() +{ + return s_consoleOutputEnabled; +} + +bool IsDebugOutputEnabled() +{ + return s_debugOutputEnabled; +} + static void ExecuteCallbacks(const char* channelName, const char* functionName, LOGLEVEL level, const char* message) { std::lock_guard guard(s_callback_mutex); @@ -273,8 +283,15 @@ void SetConsoleOutputParams(bool Enabled, const char* ChannelFilter, LOGLEVEL Le if (GetConsoleWindow() == NULL) { DebugAssert(!console_was_allocated); + + // Attach to the parent console if we're running from a command window + if (!AttachConsole(ATTACH_PARENT_PROCESS)) + { + if (!AllocConsole()) + return; + } + console_was_allocated = true; - AllocConsole(); std::FILE* fp; freopen_s(&fp, "CONIN$", "r", stdin); diff --git a/src/common/log.h b/src/common/log.h index 454b8ba5a..a0257b50c 100644 --- a/src/common/log.h +++ b/src/common/log.h @@ -30,9 +30,11 @@ void RegisterCallback(CallbackFunctionType callbackFunction, void* pUserParam); void UnregisterCallback(CallbackFunctionType callbackFunction, void* pUserParam); // adds a standard console output +bool IsConsoleOutputEnabled(); void SetConsoleOutputParams(bool enabled, const char* channelFilter = nullptr, LOGLEVEL levelFilter = LOGLEVEL_TRACE); // adds a debug console output [win32/android only] +bool IsDebugOutputEnabled(); void SetDebugOutputParams(bool enabled, const char* channelFilter = nullptr, LOGLEVEL levelFilter = LOGLEVEL_TRACE); // adds a file output @@ -48,34 +50,28 @@ void Writef(const char* channelName, const char* functionName, LOGLEVEL level, c void Writev(const char* channelName, const char* functionName, LOGLEVEL level, const char* format, va_list ap); } // namespace Log -#ifdef Y_BUILD_CONFIG_SHIPPING -#define LOG_MESSAGE_FUNCTION_NAME "" -#else -#define LOG_MESSAGE_FUNCTION_NAME __FUNCTION__ -#endif - // log wrappers #define Log_SetChannel(ChannelName) static const char* ___LogChannel___ = #ChannelName; -#define Log_ErrorPrint(msg) Log::Write(___LogChannel___, LOG_MESSAGE_FUNCTION_NAME, LOGLEVEL_ERROR, msg) -#define Log_ErrorPrintf(...) Log::Writef(___LogChannel___, LOG_MESSAGE_FUNCTION_NAME, LOGLEVEL_ERROR, __VA_ARGS__) -#define Log_WarningPrint(msg) Log::Write(___LogChannel___, LOG_MESSAGE_FUNCTION_NAME, LOGLEVEL_WARNING, msg) -#define Log_WarningPrintf(...) Log::Writef(___LogChannel___, LOG_MESSAGE_FUNCTION_NAME, LOGLEVEL_WARNING, __VA_ARGS__) -#define Log_SuccessPrint(msg) Log::Write(___LogChannel___, LOG_MESSAGE_FUNCTION_NAME, LOGLEVEL_SUCCESS, msg) -#define Log_SuccessPrintf(...) Log::Writef(___LogChannel___, LOG_MESSAGE_FUNCTION_NAME, LOGLEVEL_SUCCESS, __VA_ARGS__) -#define Log_InfoPrint(msg) Log::Write(___LogChannel___, LOG_MESSAGE_FUNCTION_NAME, LOGLEVEL_INFO, msg) -#define Log_InfoPrintf(...) Log::Writef(___LogChannel___, LOG_MESSAGE_FUNCTION_NAME, LOGLEVEL_INFO, __VA_ARGS__) -#define Log_PerfPrint(msg) Log::Write(___LogChannel___, LOG_MESSAGE_FUNCTION_NAME, LOGLEVEL_PERF, msg) -#define Log_PerfPrintf(...) Log::Writef(___LogChannel___, LOG_MESSAGE_FUNCTION_NAME, LOGLEVEL_PERF, __VA_ARGS__) -#define Log_DevPrint(msg) Log::Write(___LogChannel___, LOG_MESSAGE_FUNCTION_NAME, LOGLEVEL_DEV, msg) -#define Log_DevPrintf(...) Log::Writef(___LogChannel___, LOG_MESSAGE_FUNCTION_NAME, LOGLEVEL_DEV, __VA_ARGS__) -#define Log_ProfilePrint(msg) Log::Write(___LogChannel___, LOG_MESSAGE_FUNCTION_NAME, LOGLEVEL_PROFILE, msg) -#define Log_ProfilePrintf(...) Log::Writef(___LogChannel___, LOG_MESSAGE_FUNCTION_NAME, LOGLEVEL_PROFILE, __VA_ARGS__) +#define Log_ErrorPrint(msg) Log::Write(___LogChannel___, __func__, LOGLEVEL_ERROR, msg) +#define Log_ErrorPrintf(...) Log::Writef(___LogChannel___, __func__, LOGLEVEL_ERROR, __VA_ARGS__) +#define Log_WarningPrint(msg) Log::Write(___LogChannel___, __func__, LOGLEVEL_WARNING, msg) +#define Log_WarningPrintf(...) Log::Writef(___LogChannel___, __func__, LOGLEVEL_WARNING, __VA_ARGS__) +#define Log_SuccessPrint(msg) Log::Write(___LogChannel___, __func__, LOGLEVEL_SUCCESS, msg) +#define Log_SuccessPrintf(...) Log::Writef(___LogChannel___, __func__, LOGLEVEL_SUCCESS, __VA_ARGS__) +#define Log_InfoPrint(msg) Log::Write(___LogChannel___, __func__, LOGLEVEL_INFO, msg) +#define Log_InfoPrintf(...) Log::Writef(___LogChannel___, __func__, LOGLEVEL_INFO, __VA_ARGS__) +#define Log_PerfPrint(msg) Log::Write(___LogChannel___, __func__, LOGLEVEL_PERF, msg) +#define Log_PerfPrintf(...) Log::Writef(___LogChannel___, __func__, LOGLEVEL_PERF, __VA_ARGS__) +#define Log_DevPrint(msg) Log::Write(___LogChannel___, __func__, LOGLEVEL_DEV, msg) +#define Log_DevPrintf(...) Log::Writef(___LogChannel___, __func__, LOGLEVEL_DEV, __VA_ARGS__) +#define Log_ProfilePrint(msg) Log::Write(___LogChannel___, __func__, LOGLEVEL_PROFILE, msg) +#define Log_ProfilePrintf(...) Log::Writef(___LogChannel___, __func__, LOGLEVEL_PROFILE, __VA_ARGS__) #ifdef _DEBUG -#define Log_DebugPrint(msg) Log::Write(___LogChannel___, LOG_MESSAGE_FUNCTION_NAME, LOGLEVEL_DEBUG, msg) -#define Log_DebugPrintf(...) Log::Writef(___LogChannel___, LOG_MESSAGE_FUNCTION_NAME, LOGLEVEL_DEBUG, __VA_ARGS__) -#define Log_TracePrint(msg) Log::Write(___LogChannel___, LOG_MESSAGE_FUNCTION_NAME, LOGLEVEL_TRACE, msg) -#define Log_TracePrintf(...) Log::Writef(___LogChannel___, LOG_MESSAGE_FUNCTION_NAME, LOGLEVEL_TRACE, __VA_ARGS__) +#define Log_DebugPrint(msg) Log::Write(___LogChannel___, __func__, LOGLEVEL_DEBUG, msg) +#define Log_DebugPrintf(...) Log::Writef(___LogChannel___, __func__, LOGLEVEL_DEBUG, __VA_ARGS__) +#define Log_TracePrint(msg) Log::Write(___LogChannel___, __func__, LOGLEVEL_TRACE, msg) +#define Log_TracePrintf(...) Log::Writef(___LogChannel___, __func__, LOGLEVEL_TRACE, __VA_ARGS__) #else #define Log_DebugPrint(msg) \ do \ diff --git a/src/frontend-common/common_host_interface.cpp b/src/frontend-common/common_host_interface.cpp index 07c74b7f3..dfb71437b 100644 --- a/src/frontend-common/common_host_interface.cpp +++ b/src/frontend-common/common_host_interface.cpp @@ -204,13 +204,24 @@ void CommonHostInterface::PowerOffSystem() static void PrintCommandLineVersion(const char* frontend_name) { + const bool was_console_enabled = Log::IsConsoleOutputEnabled(); + if (!was_console_enabled) + Log::SetConsoleOutputParams(true); + std::fprintf(stderr, "%s Version %s (%s)\n", frontend_name, g_scm_tag_str, g_scm_branch_str); std::fprintf(stderr, "https://github.com/stenzek/duckstation\n"); std::fprintf(stderr, "\n"); + + if (!was_console_enabled) + Log::SetConsoleOutputParams(false); } static void PrintCommandLineHelp(const char* progname, const char* frontend_name) { + const bool was_console_enabled = Log::IsConsoleOutputEnabled(); + if (!was_console_enabled) + Log::SetConsoleOutputParams(true); + PrintCommandLineVersion(frontend_name); std::fprintf(stderr, "Usage: %s [parameters] [--] [boot filename]\n", progname); std::fprintf(stderr, "\n"); @@ -234,6 +245,9 @@ static void PrintCommandLineHelp(const char* progname, const char* frontend_name " parameters make up the filename. Use when the filename contains\n" " spaces or starts with a dash.\n"); std::fprintf(stderr, "\n"); + + if (!was_console_enabled) + Log::SetConsoleOutputParams(false); } bool CommonHostInterface::ParseCommandLineParameters(int argc, char* argv[],