From fdb6145bbd341c32762bfda649766843dec0c6aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Malte=20J=C3=BCrgens?= Date: Tue, 2 Aug 2022 12:41:32 +0200 Subject: [PATCH] improve logging --- .gitignore | 1 + CMakeLists.txt | 1 + assets/userscript.js | 6 ++++-- src/discordpage.cpp | 15 +++++++++------ src/log.cpp | 6 ++++++ src/log.h | 8 ++++++++ src/main.cpp | 3 +++ src/virtmic.cpp | 40 +++++++++++++++++++++++----------------- 8 files changed, 55 insertions(+), 25 deletions(-) create mode 100644 src/log.cpp create mode 100644 src/log.h diff --git a/.gitignore b/.gitignore index 796b96d..cb1b9b8 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /build +.vscode diff --git a/CMakeLists.txt b/CMakeLists.txt index 528dacb..7e9fae2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -30,6 +30,7 @@ set(discord-screenaudio_SRC src/virtmic.cpp src/discordpage.cpp src/streamdialog.cpp + src/log.cpp resources.qrc ) diff --git a/assets/userscript.js b/assets/userscript.js index b21c60f..1227c5d 100644 --- a/assets/userscript.js +++ b/assets/userscript.js @@ -16,10 +16,12 @@ const getAudioDevice = async (nameOfAudioDevice) => { let devices = await navigator.mediaDevices.enumerateDevices(); audioDevice = devices.find(({ label }) => label === nameOfAudioDevice); if (!audioDevice) - console.log(`Did not find '${nameOfAudioDevice}', trying again in 100ms`); + console.log( + `dsa: Did not find '${nameOfAudioDevice}', trying again in 100ms` + ); await sleep(100); } - console.log(`Found '${nameOfAudioDevice}'`); + console.log(`dsa: Found '${nameOfAudioDevice}'`); return audioDevice; }; diff --git a/src/discordpage.cpp b/src/discordpage.cpp index c71ff48..d099a35 100644 --- a/src/discordpage.cpp +++ b/src/discordpage.cpp @@ -1,4 +1,5 @@ #include "discordpage.h" +#include "log.h" #include "virtmic.h" #include @@ -43,7 +44,7 @@ DiscordPage::DiscordPage(QWidget *parent) : QWebEnginePage(parent) { } void DiscordPage::injectScript(QString source) { - qDebug() << "[main ] Injecting " << source; + qDebug(mainLog) << "Injecting " << source; QFile userscript(source); @@ -87,8 +88,8 @@ void DiscordPage::featurePermissionRequested(const QUrl &securityOrigin, if (feature == QWebEnginePage::Feature::MediaAudioCapture) { if (m_virtmicProcess.state() == QProcess::NotRunning) { - qDebug() << "[virtmic] Starting Virtmic with no target to make sure " - "Discord can find all the audio devices"; + qDebug(virtmicLog) << "Starting Virtmic with no target to make sure " + "Discord can find all the audio devices"; m_virtmicProcess.start(QApplication::arguments()[0], {"--virtmic", "None"}); } @@ -119,7 +120,7 @@ QWebEnginePage *DiscordPage::createWindow(QWebEnginePage::WebWindowType type) { void DiscordPage::stopVirtmic() { if (m_virtmicProcess.state() == QProcess::Running) { - qDebug() << "[virtmic] Stopping Virtmic"; + qDebug(virtmicLog) << "Stopping Virtmic"; m_virtmicProcess.kill(); m_virtmicProcess.waitForFinished(); } @@ -127,7 +128,7 @@ void DiscordPage::stopVirtmic() { void DiscordPage::startVirtmic(QString target) { if (target != "None") { - qDebug() << "[virtmic] Starting Virtmic with target" << target; + qDebug(virtmicLog) << "Starting Virtmic with target" << target; m_virtmicProcess.start(QApplication::arguments()[0], {"--virtmic", target}); } } @@ -143,8 +144,10 @@ void DiscordPage::javaScriptConsoleMessage( m_streamDialog.updateTargets(); } else if (message == "!discord-screenaudio-stream-stopped") { stopVirtmic(); + } else if (message.startsWith("dsa: ")) { + qDebug(userscriptLog) << message.mid(5).toUtf8().constData(); } else { - qDebug() << "[discord]" << message; + qDebug(discordLog) << message; } } diff --git a/src/log.cpp b/src/log.cpp new file mode 100644 index 0000000..fef9213 --- /dev/null +++ b/src/log.cpp @@ -0,0 +1,6 @@ +#include "log.h" + +Q_LOGGING_CATEGORY(mainLog, "main"); +Q_LOGGING_CATEGORY(discordLog, "discord"); +Q_LOGGING_CATEGORY(userscriptLog, "userscript"); +Q_LOGGING_CATEGORY(virtmicLog, "virtmic"); diff --git a/src/log.h b/src/log.h new file mode 100644 index 0000000..f96eeba --- /dev/null +++ b/src/log.h @@ -0,0 +1,8 @@ +#pragma once + +#include + +Q_DECLARE_LOGGING_CATEGORY(mainLog); +Q_DECLARE_LOGGING_CATEGORY(discordLog); +Q_DECLARE_LOGGING_CATEGORY(userscriptLog); +Q_DECLARE_LOGGING_CATEGORY(virtmicLog); diff --git a/src/main.cpp b/src/main.cpp index 897c343..99d035d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,6 +3,7 @@ #include #include +#include int main(int argc, char *argv[]) { QApplication app(argc, argv); @@ -12,6 +13,8 @@ int main(int argc, char *argv[]) { QApplication::setApplicationVersion(DISCORD_SCEENAUDIO_VERSION_FULL); QApplication::setDesktopFileName("de.shorsh.discord-screenaudio"); + qSetMessagePattern("[%{category}] %{message}"); + QCommandLineParser parser; parser.setApplicationDescription( "Custom Discord client with the ability to stream audio on Linux"); diff --git a/src/virtmic.cpp b/src/virtmic.cpp index 6ca0340..5289fb5 100644 --- a/src/virtmic.cpp +++ b/src/virtmic.cpp @@ -1,4 +1,5 @@ #include "virtmic.h" +#include "log.h" #include #include @@ -66,18 +67,18 @@ void start(QString _target) { auto &parent = nodes.at(parent_id); if (parent.props["node.name"].find(target) != std::string::npos) { - std::cout << "[virtmic] " - << "Link : " << target << ":" << port_id << " -> "; - - if (port.info().props["audio.channel"] == "FL") { - links.emplace(port_id, core.create( - {virt_fl->info().id, port_id})); - std::cout << "[virtmic] " << virt_fl->info().id << std::endl; - } else { - links.emplace(port_id, core.create( - {virt_fr->info().id, port_id})); - std::cout << "[virtmic] " << virt_fr->info().id << std::endl; - } + auto fl = port.info().props["audio.channel"] == "FL"; + links.emplace( + port_id, + core.create( + {fl ? virt_fl->info().id : virt_fr->info().id, port_id})); + qDebug(virtmicLog) << QString("Link: %1:%2 -> %3") + .arg(QString::fromStdString(target)) + .arg(port_id) + .arg(fl ? virt_fl->info().id + : virt_fr->info().id) + .toUtf8() + .data(); } } }; @@ -105,9 +106,11 @@ void start(QString _target) { [&](const pipewire::global &global) { if (global.type == pipewire::node::type) { auto node = reg.bind(global.id); - std::cout << "[virtmic] " - << "Added : " << node.info().props["node.name"] - << std::endl; + qDebug(virtmicLog) << QString("Added: %1") + .arg(QString::fromStdString( + node.info().props["node.name"])) + .toUtf8() + .data(); if (!nodes.count(global.id)) { nodes.emplace(global.id, node.info()); @@ -141,8 +144,11 @@ void start(QString _target) { [&](const std::uint32_t id) { if (nodes.count(id)) { auto info = nodes.at(id); - std::cout << "[virtmic] " - << "Removed: " << info.props["node.name"] << std::endl; + qDebug(virtmicLog) << QString("Removed: %1") + .arg(QString::fromStdString( + info.props["node.name"].data())) + .toUtf8() + .data(); nodes.erase(id); } if (ports.count(id)) {