Qt: Add file logging option

This commit is contained in:
Connor McLaughlin 2022-05-24 22:02:05 +10:00 committed by refractionpcsx2
parent 1f802eca46
commit 0bab9474ae
3 changed files with 219 additions and 129 deletions

View File

@ -216,6 +216,9 @@ void MainWindow::connectSignals()
connect(m_ui.actionEnableEEConsoleLogging, &QAction::triggered, this, &MainWindow::onLoggingOptionChanged);
SettingWidgetBinder::BindWidgetToBoolSetting(nullptr, m_ui.actionEnableIOPConsoleLogging, "Logging", "EnableIOPConsole", true);
connect(m_ui.actionEnableIOPConsoleLogging, &QAction::triggered, this, &MainWindow::onLoggingOptionChanged);
SettingWidgetBinder::BindWidgetToBoolSetting(nullptr, m_ui.actionEnableFileLogging, "Logging", "EnableFileLogging", false);
connect(m_ui.actionEnableFileLogging, &QAction::triggered, this, &MainWindow::onLoggingOptionChanged);
SettingWidgetBinder::BindWidgetToBoolSetting(nullptr, m_ui.actionEnableCDVDVerboseReads, "EmuCore", "CdvdVerboseReads", false);
connect(m_ui.actionSaveGSDump, &QAction::triggered, this, &MainWindow::onSaveGSDumpActionTriggered);

View File

@ -133,9 +133,11 @@
<addaction name="actionReloadPatches"/>
<addaction name="separator"/>
<addaction name="actionEnableSystemConsole"/>
<addaction name="actionEnableFileLogging"/>
<addaction name="actionEnableVerboseLogging"/>
<addaction name="actionEnableEEConsoleLogging"/>
<addaction name="actionEnableIOPConsoleLogging"/>
<addaction name="actionEnableCDVDVerboseReads"/>
</widget>
<widget class="QMenu" name="menu_View">
<property name="title">
@ -780,6 +782,22 @@
<string>Input Recording Logs</string>
</property>
</action>
<action name="actionEnableFileLogging">
<property name="checkable">
<bool>true</bool>
</property>
<property name="text">
<string>Enable &amp;File Logging</string>
</property>
</action>
<action name="actionEnableCDVDVerboseReads">
<property name="checkable">
<bool>true</bool>
</property>
<property name="text">
<string>Enable CDVD Read Logging</string>
</property>
</action>
</widget>
<resources>
<include location="resources/resources.qrc"/>

View File

@ -34,6 +34,7 @@
#include "common/SettingsWrapper.h"
#include "common/StringUtil.h"
#include "pcsx2/DebugTools/Debug.h"
#include "pcsx2/Frontend/GameList.h"
#include "pcsx2/Frontend/INISettingsInterface.h"
#include "pcsx2/HostSettings.h"
@ -45,8 +46,6 @@
#include "QtHost.h"
#include "svnrev.h"
#include "pcsx2/DebugTools/Debug.h"
static constexpr u32 SETTINGS_VERSION = 1;
static constexpr u32 SETTINGS_SAVE_DELAY = 1000;
@ -105,6 +104,12 @@ void QtHost::Shutdown()
g_main_window->close();
delete g_main_window;
}
if (emuLog)
{
std::fclose(emuLog);
emuLog = nullptr;
}
}
bool QtHost::SetCriticalFolders()
@ -519,6 +524,38 @@ void QtHost::HookSignals()
std::signal(SIGTERM, SignalHandler);
}
// Used on both Windows and Linux.
#ifdef _WIN32
static const wchar_t s_console_colors[][ConsoleColors_Count] = {
#define CC(x) L ## x
#else
static const char s_console_colors[][ConsoleColors_Count] = {
#define CC(x) x
#endif
CC("\033[0m"), // default
CC("\033[30m\033[1m"), // black
CC("\033[32m"), // green
CC("\033[31m"), // red
CC("\033[34m"), // blue
CC("\033[35m"), // magenta
CC("\033[35m"), // orange (FIXME)
CC("\033[37m"), // gray
CC("\033[36m"), // cyan
CC("\033[33m"), // yellow
CC("\033[37m"), // white
CC("\033[30m\033[1m"), // strong black
CC("\033[31m\033[1m"), // strong red
CC("\033[32m\033[1m"), // strong green
CC("\033[34m\033[1m"), // strong blue
CC("\033[35m\033[1m"), // strong magenta
CC("\033[35m\033[1m"), // strong orange (FIXME)
CC("\033[37m\033[1m"), // strong gray
CC("\033[36m\033[1m"), // strong cyan
CC("\033[33m\033[1m"), // strong yellow
CC("\033[37m\033[1m") // strong white
};
#undef CC
// Replacement for Console so we actually get output to our console window on Windows.
#ifdef _WIN32
@ -530,102 +567,6 @@ static HANDLE s_old_console_stdin = NULL;
static HANDLE s_old_console_stdout = NULL;
static HANDLE s_old_console_stderr = NULL;
static void ConsoleWinQt_SetTitle(const char* title)
{
SetConsoleTitleW(StringUtil::UTF8StringToWideString(title).c_str());
}
static void ConsoleWinQt_DoSetColor(ConsoleColors color)
{
if (!s_console_handle)
return;
static constexpr wchar_t colors[][ConsoleColors_Count] = {
L"\033[0m", // default
L"\033[30m\033[1m", // black
L"\033[32m", // green
L"\033[31m", // red
L"\033[34m", // blue
L"\033[35m", // magenta
L"\033[35m", // orange (FIXME)
L"\033[37m", // gray
L"\033[36m", // cyan
L"\033[33m", // yellow
L"\033[37m", // white
L"\033[30m\033[1m", // strong black
L"\033[31m\033[1m", // strong red
L"\033[32m\033[1m", // strong green
L"\033[34m\033[1m", // strong blue
L"\033[35m\033[1m", // strong magenta
L"\033[35m\033[1m", // strong orange (FIXME)
L"\033[37m\033[1m", // strong gray
L"\033[36m\033[1m", // strong cyan
L"\033[33m\033[1m", // strong yellow
L"\033[37m\033[1m", // strong white
};
const wchar_t* colortext = colors[static_cast<u32>(color)];
DWORD written;
WriteConsoleW(s_console_handle, colortext, std::wcslen(colortext), &written, nullptr);
}
static void ConsoleWinQt_Newline()
{
if (!s_console_handle)
return;
if (s_debugger_attached)
OutputDebugStringW(L"\n");
DWORD written;
WriteConsoleW(s_console_handle, L"\n", 1, &written, nullptr);
}
static void ConsoleWinQt_DoWrite(const char* fmt)
{
if (!s_console_handle)
return;
// TODO: Put this on the stack.
std::wstring wfmt(StringUtil::UTF8StringToWideString(fmt));
if (s_debugger_attached)
OutputDebugStringW(wfmt.c_str());
DWORD written;
WriteConsoleW(s_console_handle, wfmt.c_str(), static_cast<DWORD>(wfmt.length()), &written, nullptr);
}
static void ConsoleWinQt_DoWriteLn(const char* fmt)
{
if (!s_console_handle)
return;
// TODO: Put this on the stack.
std::wstring wfmt(StringUtil::UTF8StringToWideString(fmt));
if (s_debugger_attached)
{
OutputDebugStringW(wfmt.c_str());
OutputDebugStringW(L"\n");
}
DWORD written;
WriteConsoleW(s_console_handle, wfmt.c_str(), static_cast<DWORD>(wfmt.length()), &written, nullptr);
WriteConsoleW(s_console_handle, L"\n", 1, &written, nullptr);
}
static const IConsoleWriter ConsoleWriter_WinQt =
{
ConsoleWinQt_DoWrite,
ConsoleWinQt_DoWriteLn,
ConsoleWinQt_DoSetColor,
ConsoleWinQt_DoWrite,
ConsoleWinQt_Newline,
ConsoleWinQt_SetTitle,
};
static BOOL WINAPI ConsoleCtrlHandler(DWORD dwCtrlType)
{
Console.WriteLn("Handler %u", dwCtrlType);
@ -652,11 +593,132 @@ static bool EnableVirtualTerminalProcessing(HANDLE hConsole)
return SetConsoleMode(hConsole, old_mode | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
}
static void SetSystemConsoleEnabled(bool enabled)
#endif
static void ConsoleQt_SetTitle(const char* title)
{
if (enabled)
#ifdef _WIN32
SetConsoleTitleW(StringUtil::UTF8StringToWideString(title).c_str());
#else
std::fprintf(stdout, "\033]0;%s\007", title);
#endif
}
static void ConsoleQt_DoSetColor(ConsoleColors color)
{
#ifdef _WIN32
if (s_console_handle == INVALID_HANDLE_VALUE)
return;
const wchar_t* colortext = s_console_colors[static_cast<u32>(color)];
DWORD written;
WriteConsoleW(s_console_handle, colortext, std::wcslen(colortext), &written, nullptr);
#else
const char* colortext = s_console_colors[static_cast<u32>(color)];
std::fputs(colortext, stdout);
#endif
}
static void ConsoleQt_Newline()
{
#ifdef _WIN32
if (s_debugger_attached)
OutputDebugStringW(L"\n");
if (s_console_handle != INVALID_HANDLE_VALUE)
{
DWORD written;
WriteConsoleW(s_console_handle, L"\n", 1, &written, nullptr);
}
#else
std::fputc('\n', stdout);
#endif
if (emuLog)
{
std::fputs("\n", emuLog);
}
}
static void ConsoleQt_DoWrite(const char* fmt)
{
#ifdef _WIN32
if (s_console_handle != INVALID_HANDLE_VALUE || s_debugger_attached)
{
// TODO: Put this on the stack.
std::wstring wfmt(StringUtil::UTF8StringToWideString(fmt));
if (s_debugger_attached)
OutputDebugStringW(wfmt.c_str());
if (s_console_handle != INVALID_HANDLE_VALUE)
{
DWORD written;
WriteConsoleW(s_console_handle, wfmt.c_str(), static_cast<DWORD>(wfmt.length()), &written, nullptr);
}
}
#else
std::fputs(fmt, stdout);
std::fputc('\n', stdout);
#endif
if (emuLog)
{
std::fputs(fmt, emuLog);
}
}
static void ConsoleQt_DoWriteLn(const char* fmt)
{
#ifdef _WIN32
if (s_console_handle != INVALID_HANDLE_VALUE || s_debugger_attached)
{
// TODO: Put this on the stack.
std::wstring wfmt(StringUtil::UTF8StringToWideString(fmt));
if (s_debugger_attached)
{
OutputDebugStringW(wfmt.c_str());
OutputDebugStringW(L"\n");
}
if (s_console_handle != INVALID_HANDLE_VALUE)
{
DWORD written;
WriteConsoleW(s_console_handle, wfmt.c_str(), static_cast<DWORD>(wfmt.length()), &written, nullptr);
WriteConsoleW(s_console_handle, L"\n", 1, &written, nullptr);
}
}
#else
std::fputs(fmt, stdout);
std::fputc('\n', stdout);
#endif
if (emuLog)
{
std::fputs(fmt, emuLog);
std::fputc('\n', emuLog);
}
}
static const IConsoleWriter ConsoleWriter_WinQt =
{
ConsoleQt_DoWrite,
ConsoleQt_DoWriteLn,
ConsoleQt_DoSetColor,
ConsoleQt_DoWrite,
ConsoleQt_Newline,
ConsoleQt_SetTitle,
};
static void UpdateLoggingSinks(bool system_console, bool file_log)
{
#ifdef _WIN32
const bool debugger_attached = IsDebuggerPresent();
s_debugger_attached = debugger_attached;
if (system_console)
{
s_debugger_attached = IsDebuggerPresent();
if (!s_console_handle_set)
{
s_old_console_stdin = GetStdHandle(STD_INPUT_HANDLE);
@ -676,6 +738,7 @@ static void SetSystemConsoleEnabled(bool enabled)
if (s_console_handle != INVALID_HANDLE_VALUE)
{
s_console_handle_set = true;
SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE);
// This gets us unix-style coloured output.
EnableVirtualTerminalProcessing(GetStdHandle(STD_OUTPUT_HANDLE));
@ -690,25 +753,15 @@ static void SetSystemConsoleEnabled(bool enabled)
}
}
if (!s_console_handle_set && !s_debugger_attached)
{
Console_SetActiveHandler(ConsoleWriter_Null);
SetConsoleCtrlHandler(ConsoleCtrlHandler, FALSE);
// just in case it fails
system_console = s_console_handle_set;
}
else
{
Console_SetActiveHandler(ConsoleWriter_WinQt);
SetConsoleCtrlHandler(ConsoleCtrlHandler, TRUE);
}
}
else
{
Console_SetActiveHandler(ConsoleWriter_Null);
SetConsoleCtrlHandler(ConsoleCtrlHandler, FALSE);
if (s_console_handle_set)
{
s_console_handle_set = false;
SetConsoleCtrlHandler(ConsoleCtrlHandler, FALSE);
// redirect stdout/stderr back to null.
std::FILE* fp;
@ -730,38 +783,54 @@ static void SetSystemConsoleEnabled(bool enabled)
}
}
}
#else
const bool debugger_attached = false;
#endif
if (file_log)
{
if (!emuLog)
{
emuLogName = Path::Combine(EmuFolders::Logs, "emulog.txt");
emuLog = FileSystem::OpenCFile(emuLogName.c_str(), "wb");
file_log = (emuLog != nullptr);
}
}
else
{
if (emuLog)
{
std::fclose(emuLog);
emuLog = nullptr;
emuLogName = {};
}
}
#else
// Unix doesn't need any special handling for console.
static void SetSystemConsoleEnabled(bool enabled)
{
if (enabled)
Console_SetActiveHandler(ConsoleWriter_Stdout);
// Discard logs completely if there's no sinks.
if (debugger_attached || system_console || file_log)
Console_SetActiveHandler(ConsoleWriter_WinQt);
else
Console_SetActiveHandler(ConsoleWriter_Null);
}
#endif
void QtHost::InitializeEarlyConsole()
{
SetSystemConsoleEnabled(true);
UpdateLoggingSinks(true, false);
}
void QtHost::UpdateLogging()
{
const bool system_console_enabled = QtHost::GetBaseBoolSettingValue("Logging", "EnableSystemConsole", false);
const bool file_logging_enabled = QtHost::GetBaseBoolSettingValue("Logging", "EnableFileLogging", false);
const bool any_logging_sinks = system_console_enabled;
DevConWriterEnabled = any_logging_sinks && QtHost::GetBaseBoolSettingValue("Logging", "EnableVerbose", false);
SysConsole.eeConsole.Enabled = any_logging_sinks && QtHost::GetBaseBoolSettingValue("Logging", "EnableEEConsole", true);
SysConsole.iopConsole.Enabled = any_logging_sinks && QtHost::GetBaseBoolSettingValue("Logging", "EnableIOPConsole", true);
const bool any_logging_sinks = system_console_enabled || file_logging_enabled;
DevConWriterEnabled = any_logging_sinks && (IsDevBuild || QtHost::GetBaseBoolSettingValue("Logging", "EnableVerbose", false));
SysConsole.eeConsole.Enabled = any_logging_sinks && QtHost::GetBaseBoolSettingValue("Logging", "EnableEEConsole", false);
SysConsole.iopConsole.Enabled = any_logging_sinks && QtHost::GetBaseBoolSettingValue("Logging", "EnableIOPConsole", false);
// Input Recording Logs
SysConsole.recordingConsole.Enabled = any_logging_sinks && QtHost::GetBaseBoolSettingValue("Logging", "EnableInputRecordingLogs", true);
SysConsole.controlInfo.Enabled = any_logging_sinks && QtHost::GetBaseBoolSettingValue("Logging", "EnableControllerLogs", false);
SetSystemConsoleEnabled(system_console_enabled);
UpdateLoggingSinks(system_console_enabled, file_logging_enabled);
}