Settings: Default console logging to running from TTY

This commit is contained in:
Stenzek 2024-09-09 20:55:15 +10:00
parent 163a9d6ae4
commit 2fe4877dbc
No known key found for this signature in database
7 changed files with 26 additions and 17 deletions

View file

@ -18,6 +18,8 @@
#elif defined(__ANDROID__)
#include <android/log.h>
#else
#include <sys/ioctl.h>
#include <termios.h>
#include <unistd.h>
#endif
@ -128,6 +130,20 @@ float Log::GetCurrentMessageTime()
return static_cast<float>(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;

View file

@ -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);

View file

@ -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."),

View file

@ -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;

View file

@ -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;

View file

@ -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);

View file

@ -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;