Save server logs

This commit is contained in:
Guillaume Meunier
2025-03-25 21:15:01 +01:00
parent 316a6ab0f4
commit 8648e41a5a
5 changed files with 160 additions and 100 deletions
-2
View File
@@ -21,9 +21,7 @@
#include <QCoroCore>
#include <QCoroQmlTask>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QObject>
#include <QProcess>
#include <QtCore>
#include <QtQml/qqmlregistration.h>
+46 -14
View File
@@ -1,7 +1,6 @@
import QtCore as Core
import QtQuick
import QtQuick.Layouts
import QtQuick.Dialogs as Dialogs
import QtQuick.Controls as Controls
import Qt.labs.platform
import org.kde.kirigami as Kirigami
@@ -31,8 +30,10 @@ Kirigami.ApplicationWindow {
Core.Settings {
id: settings
property alias first_run: root.first_run
property alias last_run_version: root.last_run_version
}
property bool first_run: true
property string last_run_version
width: 900
height: 800
@@ -51,8 +52,13 @@ Kirigami.ApplicationWindow {
function onServerStatusChanged(value) {
var started = value == WivrnServer.Started;
// Only set the switch value if it has changed, to avoid loops
if (switch_running.checked != started)
switch_running.checked = started;
// Set the visible property here because dismissing the message removes the binding
if (value == WivrnServer.FailedToStart)
message_failed_to_start.visible = true;
}
function onPairingEnabledChanged(value) {
@@ -74,13 +80,15 @@ Kirigami.ApplicationWindow {
if (WivrnServer.serverStatus == WivrnServer.Stopped)
WivrnServer.start_server();
console.log("Settings at " + settings.location);
if (root.first_run)
{
if (root.first_run) {
console.log("First run");
root.pageStack.push(Qt.resolvedUrl("WizardPage.qml"));
root.first_run = false;
}
if (root.last_run_version != ApkInstaller.currentVersion) {
root.last_run_version = ApkInstaller.currentVersion;
}
}
pageStack.globalToolBar.showNavigationButtons: Kirigami.ApplicationHeaderStyle.NoNavigationButtons
@@ -123,14 +131,6 @@ Kirigami.ApplicationWindow {
]
}
Kirigami.InlineMessage {
Layout.fillWidth: true
text: i18n("ADB is not installed.")
type: Kirigami.MessageType.Information
showCloseButton: true
visible: !Adb.adbInstalled
}
Kirigami.InlineMessage {
Layout.fillWidth: true
text: i18n("NVIDIA GPUs require at least driver version 565.77, current version is %1", VulkanInfo.driverVersion)
@@ -153,6 +153,21 @@ Kirigami.ApplicationWindow {
visible: VulkanInfo.driverId == "AmdProprietary" || VulkanInfo.driverId == "AmdOpenSource"
}
Kirigami.InlineMessage {
id: message_failed_to_start
Layout.fillWidth: true
text: i18n("Server failed to start")
type: Kirigami.MessageType.Error
showCloseButton: true
// visible: WivrnServer.serverStatus == WivrnServer.FailedToStart
actions: [
Kirigami.Action {
text: i18n("Open server logs")
onTriggered: WivrnServer.open_server_logs()
}
]
}
// RowLayout {
// Controls.Button {
// text: "refresh latest version"
@@ -222,8 +237,18 @@ Kirigami.ApplicationWindow {
Layout.column: 0
text: i18n("Connect by USB")
onClicked: select_usb_device.connect()
enabled: root.server_started && Adb.adbInstalled && !WivrnServer.headsetConnected && select_usb_device.connected_headset_count > 0
enabled: root.server_started && Adb.adbInstalled && select_usb_device.connected_headset_count > 0
visible: !WivrnServer.headsetConnected
Controls.ToolTip.visible: root.server_started && hovered
Controls.ToolTip.delay: Kirigami.Units.toolTipDelay
Controls.ToolTip.text: {
if (!Adb.adbInstalled)
return i18n("ADB is not installed.");
if (select_usb_device.connected_headset_count == 0)
return i18n("No headset is connected or your headset is not in developer mode.");
return "";
}
}
Controls.Button {
@@ -297,6 +322,13 @@ Kirigami.ApplicationWindow {
onTriggered: root.pageStack.push(Qt.resolvedUrl("ApkInstallPage.qml"))
visible: root.pageStack.depth == 1
enabled: root.server_started && Adb.adbInstalled
tooltip: {
if (!root.server_started)
return "";
if (!Adb.adbInstalled)
return i18n("ADB is not installed.");
return "";
}
},
// Kirigami.Action {
// text: i18n("Statistics")
@@ -324,7 +356,7 @@ Kirigami.ApplicationWindow {
text: i18n("Troubleshoot")
icon.name: "help-contents-symbolic"
onTriggered: root.pageStack.push(Qt.resolvedUrl("TroubleshootPage.qml"))
enabled: root.server_started && root.pageStack.depth == 1
enabled: root.pageStack.depth == 1
}
]
}
+5
View File
@@ -72,6 +72,11 @@ Kirigami.ScrollablePage {
text: i18n("Ask on Discord")
icon.source: Qt.resolvedUrl("Discord.svg")
onTriggered: Qt.openUrlExternally("https://discord.gg/EHAYe3tTYa")
},
Kirigami.Action {
text: i18n("Open server logs")
icon.name: "viewlog-symbolic"
onTriggered: WivrnServer.open_server_logs()
}
]
+94 -83
View File
@@ -22,7 +22,7 @@
#include "wivrn_server_dbus.h"
#include <QApplication>
#include <QClipboard>
#include <QCoroNetworkReply>
#include <QDesktopServices>
#include <QtLogging>
#include <cassert>
#include <memory>
@@ -50,10 +50,6 @@ static bool has_cap_sys_nice()
if (not caps)
return false;
// char * cap_text = cap_to_text(caps, nullptr);
// qDebug() << "Server capabilities:" << cap_text;
// cap_free(cap_text);
cap_flag_value_t value{};
if (cap_get_flag(caps, CAP_SYS_NICE, CAP_EFFECTIVE, &value) < 0)
return false;
@@ -93,28 +89,64 @@ wivrn_server::~wivrn_server()
}
}
std::unique_ptr<QFile> get_server_log_file()
{
QString state_path = QStandardPaths::writableLocation(QStandardPaths::StateLocation);
QDir{}.mkpath(state_path);
QFileInfoList files = QDir{state_path}.entryInfoList({"server_logs_*.txt"}, QDir::Files, QDir::Name);
while (files.size() >= 10)
{
qDebug() << "Removing log file" << files.front().absoluteFilePath();
QFile{files.front().absoluteFilePath()}.remove();
files.pop_front();
}
std::unique_ptr<QFile> log_file = std::make_unique<QFile>(state_path + "/server_logs_" + QDateTime::currentDateTime().toString(Qt::ISODate) + ".txt");
log_file->open(QFile::WriteOnly | QFile::Truncate);
qDebug() << "Saving logs in" << log_file->fileName();
return log_file;
}
void wivrn_server::start_server()
{
switch (serverStatus())
{
case Status::FailedToStart:
case Status::Stopped:
serverStatusChanged(m_serverStatus = Status::Starting);
[[fallthrough]];
case Status::Restarting:
assert(server_process == nullptr);
case Status::Restarting: {
server_process = new QProcess(this);
server_process = std::make_unique<QProcess>();
server_process->setProcessChannelMode(QProcess::MergedChannels);
server_output.clear();
server_log_file = get_server_log_file();
// connect(server_process, &QProcess::finished, this, &main_window::on_server_finished);
// connect(server_process, &QProcess::errorOccurred, this, &main_window::on_server_error_occurred);
connect(server_process, &QProcess::readyReadStandardOutput, this, &wivrn_server::on_server_ready_read_standard_output);
connect(server_process, &QProcess::finished, this, [this, p = server_process]() {
p->deleteLater();
server_process->setProcessChannelMode(QProcess::ForwardedChannels);
if (server_process == p)
{
server_process = nullptr;
if (m_serverStatus == Status::Starting)
{
qDebug() << "Server finished before registering on dbus";
serverStatusChanged(m_serverStatus = Status::FailedToStart);
}
}
});
server_process->start(server_path(), QApplication::arguments().mid(1));
// server_process_timeout->start();
}
break;
break;
case Status::Starting:
case Status::Started:
@@ -147,6 +179,46 @@ void wivrn_server::restart_server()
qWarning() << "restart_server: unexpected status " << std::string(magic_enum::enum_name(serverStatus()));
}
void wivrn_server::on_server_ready_read_standard_output()
{
QByteArray output = server_process->readAllStandardOutput();
qsizetype index;
do
{
index = output.indexOf('\n');
QString line;
if (index >= 0)
{
line = QString::fromLatin1(output.first(index + 1));
output.remove(0, index + 1);
}
else
{
line = QString::fromLatin1(output);
output.clear();
}
if (server_output.empty() or server_output.back().endsWith("\n"))
{
// Start a new line
QString timestamp = QDateTime::currentDateTime().toString(Qt::ISODateWithMs);
server_output.push_back("[" + timestamp + "] " + line);
server_log_file->write(("[" + timestamp + "] " + line).toLatin1());
}
else
{
server_output.back() += line;
server_log_file->write(line.toLatin1());
}
} while (index >= 0 and output.size() > 0);
server_log_file->flush();
serverLogsChanged(serverLogs());
}
void wivrn_server::on_server_dbus_registered()
{
serverStatusChanged(m_serverStatus = Status::Started);
@@ -171,7 +243,7 @@ void wivrn_server::on_server_dbus_unregistered()
serverStatusChanged(m_serverStatus = Status::Stopped);
if (server_process)
server_process.release()->deleteLater();
server_process = nullptr;
if (server_interface)
server_interface.release()->deleteLater();
@@ -230,75 +302,14 @@ void wivrn_server::grant_cap_sys_nice()
#endif
}
// static std::vector<std::filesystem::path> list_files(const std::vector<std::filesystem::path> & dirs)
// {
// std::vector<std::filesystem::path> files;
//
// if (wivrn::flatpak_key(wivrn::flatpak::section::session_bus_policy, "org.freedesktop.Flatpak") == "talk")
// {
// QProcess flatpak_spawn;
// QStringList args{"--host", "find"};
// for (const std::filesystem::path & dir: dirs)
// args.push_back(QString::fromStdString(dir));
// args.push_back("-name");
// args.push_back("*.json");
//
// flatpak_spawn.start("flatpak-spawn", args);
// flatpak_spawn.waitForFinished();
//
// qDebug() << args;
//
// for (const std::string & file: utils::split(flatpak_spawn.readAllStandardOutput().toStdString()))
// {
// if (file != "")
// files.push_back(file);
// }
// }
// else
// {
// for (const std::filesystem::path & dir: dirs)
// {
// try
// {
// for (auto const & dir_entry: std::filesystem::directory_iterator{dir})
// {
// if (dir_entry.path().extension() != ".json")
// continue;
//
// files.push_back(dir / dir_entry.path());
// }
// }
// catch (...)
// {
// // Ignore non existing directories
// }
// }
// }
//
// return files;
// }
//
// static std::string read_file(const std::filesystem::path & path)
// {
// if (wivrn::flatpak_key(wivrn::flatpak::section::session_bus_policy, "org.freedesktop.Flatpak") == "talk")
// {
// QProcess flatpak_spawn;
// flatpak_spawn.start("flatpak-spawn", {"--host", "cat", QString::fromStdString(path)});
// flatpak_spawn.waitForFinished();
// return flatpak_spawn.readAllStandardOutput().toStdString();
// }
// else
// {
// std::ifstream f(path);
// std::istreambuf_iterator<char> begin{f}, end;
//
// return {begin, end};
// }
// }
// QString wivrn_server::apkUrl() const
// {
// }
void wivrn_server::open_server_logs()
{
if (server_log_file)
{
qDebug() << "Opening" << server_log_file->fileName();
QDesktopServices::openUrl(QUrl::fromLocalFile(server_log_file->fileName()));
}
}
void wivrn_server::refresh_server_properties()
{
+15 -1
View File
@@ -106,7 +106,7 @@ class wivrn_server : public QObject
QDBusServiceWatcher dbus_watcher;
std::unique_ptr<QDBusPendingCallWatcher> get_all_properties_call_watcher;
std::unique_ptr<QProcess> server_process;
QProcess * server_process;
std::unique_ptr<QProcess> setcap_process;
public:
@@ -117,6 +117,7 @@ public:
enum class Status
{
FailedToStart,
Stopped,
Started,
Stopping,
@@ -131,10 +132,12 @@ public:
Q_PROPERTY(bool capSysNice READ capSysNice NOTIFY capSysNiceChanged)
Q_PROPERTY(bool headsetConnected READ isHeadsetConnected NOTIFY headsetConnectedChanged)
Q_PROPERTY(QString jsonConfiguration READ jsonConfiguration WRITE setJsonConfiguration NOTIFY jsonConfigurationChanged)
Q_PROPERTY(QString serverLogs READ serverLogs NOTIFY serverLogsChanged)
Q_INVOKABLE void start_server();
Q_INVOKABLE void stop_server();
Q_INVOKABLE void restart_server();
Q_INVOKABLE void grant_cap_sys_nice();
Q_INVOKABLE void open_server_logs();
// Authentication
Q_PROPERTY(QString pin READ pin NOTIFY pinChanged)
@@ -273,6 +276,11 @@ public:
QString hostname();
QString serverLogs()
{
return server_output.join("");
}
Q_INVOKABLE void disconnect_headset();
Q_INVOKABLE void copy_steam_command();
@@ -281,6 +289,11 @@ private:
void on_server_dbus_unregistered();
void on_server_properties_changed(const QString & interface_name, const QVariantMap & changed_properties, const QStringList & invalidated_properties);
void on_server_ready_read_standard_output();
std::unique_ptr<QFile> server_log_file;
QStringList server_output;
void refresh_server_properties();
Status m_serverStatus{Status::Stopped};
@@ -331,4 +344,5 @@ Q_SIGNALS:
void speakerSampleRateChanged(int);
void supportedCodecsChanged(QStringList);
void steamCommandChanged(QString);
void serverLogsChanged(QString);
};