From 2fe4877dbc1a9da445354bfc9a4472042b4bba78 Mon Sep 17 00:00:00 2001 From: Stenzek Date: Mon, 9 Sep 2024 20:55:15 +1000 Subject: [PATCH] Settings: Default console logging to running from TTY --- src/common/log.cpp | 16 ++++++++++++++++ src/common/log.h | 1 + src/core/fullscreen_ui.cpp | 2 +- src/core/settings.cpp | 5 +---- src/core/settings.h | 9 +-------- src/core/system.cpp | 5 +++++ src/duckstation-qt/qthost.cpp | 5 +---- 7 files changed, 26 insertions(+), 17 deletions(-) diff --git a/src/common/log.cpp b/src/common/log.cpp index 993b2bb5e..0c833e616 100644 --- a/src/common/log.cpp +++ b/src/common/log.cpp @@ -18,6 +18,8 @@ #elif defined(__ANDROID__) #include #else +#include +#include #include #endif @@ -128,6 +130,20 @@ float Log::GetCurrentMessageTime() return static_cast(Common::Timer::ConvertValueToSeconds(Common::Timer::GetCurrentValue() - s_start_timestamp)); } +bool Log::IsConsoleOutputCurrentlyAvailable() +{ +#ifdef _WIN32 + const HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE); + return (h != NULL && h != INVALID_HANDLE_VALUE); +#elif defined(__ANDROID__) + return false; +#else + // standard output isn't really reliable because it could be redirected to a file. check standard input for tty. + struct termios attr; + return (tcgetattr(STDIN_FILENO, &attr) == 0); +#endif +} + bool Log::IsConsoleOutputEnabled() { return s_console_output_enabled; diff --git a/src/common/log.h b/src/common/log.h index 1f73234c8..63b43f257 100644 --- a/src/common/log.h +++ b/src/common/log.h @@ -41,6 +41,7 @@ void UnregisterCallback(CallbackFunctionType callbackFunction, void* pUserParam) float GetCurrentMessageTime(); // adds a standard console output +bool IsConsoleOutputCurrentlyAvailable(); bool IsConsoleOutputEnabled(); void SetConsoleOutputParams(bool enabled, bool timestamps = true); diff --git a/src/core/fullscreen_ui.cpp b/src/core/fullscreen_ui.cpp index 66f4710fb..c8fb802ca 100644 --- a/src/core/fullscreen_ui.cpp +++ b/src/core/fullscreen_ui.cpp @@ -5146,7 +5146,7 @@ void FullscreenUI::DrawAdvancedSettingsPage() "LogLevel", Settings::DEFAULT_LOG_LEVEL, &Settings::ParseLogLevelName, &Settings::GetLogLevelName, &Settings::GetLogLevelDisplayName, LOGLEVEL_COUNT); DrawToggleSetting(bsi, FSUI_CSTR("Log To System Console"), FSUI_CSTR("Logs messages to the console window."), - FSUI_CSTR("Logging"), "LogToConsole", Settings::DEFAULT_LOG_TO_CONSOLE); + FSUI_CSTR("Logging"), "LogToConsole", false); DrawToggleSetting(bsi, FSUI_CSTR("Log To Debug Console"), FSUI_CSTR("Logs messages to the debug console where supported."), "Logging", "LogToDebug", false); DrawToggleSetting(bsi, FSUI_CSTR("Log To File"), FSUI_CSTR("Logs messages to duckstation.log in the user directory."), diff --git a/src/core/settings.cpp b/src/core/settings.cpp index 35f34f29c..0013bd7ca 100644 --- a/src/core/settings.cpp +++ b/src/core/settings.cpp @@ -421,7 +421,7 @@ void Settings::Load(SettingsInterface& si, SettingsInterface& controller_si) .value_or(DEFAULT_LOG_LEVEL); log_filter = si.GetStringValue("Logging", "LogFilter", ""); log_timestamps = si.GetBoolValue("Logging", "LogTimestamps", true); - log_to_console = si.GetBoolValue("Logging", "LogToConsole", DEFAULT_LOG_TO_CONSOLE); + log_to_console = si.GetBoolValue("Logging", "LogToConsole", false); log_to_debug = si.GetBoolValue("Logging", "LogToDebug", false); log_to_window = si.GetBoolValue("Logging", "LogToWindow", false); log_to_file = si.GetBoolValue("Logging", "LogToFile", false); @@ -450,9 +450,6 @@ void Settings::Load(SettingsInterface& si, SettingsInterface& controller_si) si.GetIntValue("TextureReplacements", "DumpVRAMWriteHeightThreshold", 128); #ifdef __ANDROID__ - // No expansion due to license incompatibility. - audio_expansion_mode = AudioExpansionMode::Disabled; - // Android users are incredibly silly and don't understand that stretch is in the aspect ratio list... if (si.GetBoolValue("Display", "Stretch", false)) display_aspect_ratio = DisplayAspectRatio::MatchWindow; diff --git a/src/core/settings.h b/src/core/settings.h index d55dfcf7e..992e717cf 100644 --- a/src/core/settings.h +++ b/src/core/settings.h @@ -277,7 +277,7 @@ struct Settings LOGLEVEL log_level = DEFAULT_LOG_LEVEL; std::string log_filter; bool log_timestamps : 1 = true; - bool log_to_console : 1 = DEFAULT_LOG_TO_CONSOLE; + bool log_to_console : 1 = false; bool log_to_debug : 1 = false; bool log_to_window : 1 = false; bool log_to_file : 1 = false; @@ -529,13 +529,6 @@ struct Settings static constexpr u32 DEFAULT_MEDIA_CAPTURE_AUDIO_BITRATE = 128; #endif - // Enable console logging by default on Linux platforms. -#if defined(__linux__) && !defined(__ANDROID__) - static constexpr bool DEFAULT_LOG_TO_CONSOLE = true; -#else - static constexpr bool DEFAULT_LOG_TO_CONSOLE = false; -#endif - // Android doesn't create settings until they're first opened, so we have to override the defaults here. #ifndef __ANDROID__ static constexpr bool DEFAULT_SAVE_STATE_BACKUPS = true; diff --git a/src/core/system.cpp b/src/core/system.cpp index 4b350ea7f..ec8716e5f 100644 --- a/src/core/system.cpp +++ b/src/core/system.cpp @@ -1347,6 +1347,11 @@ void System::SetDefaultSettings(SettingsInterface& si) temp.Save(si, false); +#if !defined(_WIN32) && !defined(__ANDROID__) + // On Linux, default the console to whether standard input is currently available. + si.SetBoolValue("Logging", "LogToConsole", Log::IsConsoleOutputCurrentlyAvailable()); +#endif + #ifndef __ANDROID__ si.SetStringValue("MediaCapture", "Backend", MediaCapture::GetBackendName(Settings::DEFAULT_MEDIA_CAPTURE_BACKEND)); si.SetStringValue("MediaCapture", "Container", Settings::DEFAULT_MEDIA_CAPTURE_CONTAINER); diff --git a/src/duckstation-qt/qthost.cpp b/src/duckstation-qt/qthost.cpp index ddfe2feab..2b3c9cef9 100644 --- a/src/duckstation-qt/qthost.cpp +++ b/src/duckstation-qt/qthost.cpp @@ -472,11 +472,8 @@ bool QtHost::InitializeConfig(std::string settings_filename) MigrateSettings(); // We need to create the console window early, otherwise it appears in front of the main window. - if (!Log::IsConsoleOutputEnabled() && - s_base_settings_interface->GetBoolValue("Logging", "LogToConsole", Settings::DEFAULT_LOG_TO_CONSOLE)) - { + if (!Log::IsConsoleOutputEnabled() && s_base_settings_interface->GetBoolValue("Logging", "LogToConsole", false)) Log::SetConsoleOutputParams(true, s_base_settings_interface->GetBoolValue("Logging", "LogTimestamps", true)); - } UpdateApplicationLanguage(nullptr); return true;