From 0c159361199065e93dd719d3fffe5345c312e2c9 Mon Sep 17 00:00:00 2001 From: Martchus Date: Sat, 17 Feb 2024 00:38:09 +0100 Subject: [PATCH] Allow logging of some high-level notifications --- README.md | 1 + syncthingconnector/syncthingnotifier.cpp | 20 +++++++++++++++++--- syncthingconnector/syncthingnotifier.h | 2 ++ 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 7306da1..ff7fb69 100644 --- a/README.md +++ b/README.md @@ -581,6 +581,7 @@ It is possible to turn on logging of the underlying library by setting environme * `LIB_SYNCTHING_CONNECTOR_LOG_API_REPLIES`: log replies from Syncthing's REST-API (except events) * `LIB_SYNCTHING_CONNECTOR_LOG_EVENTS`: log events emitted by Syncthing's events REST-API endpoint * `LIB_SYNCTHING_CONNECTOR_LOG_DIRS_OR_DEVS_RESETTED`: log when folders/devices are internally reset +* `LIB_SYNCTHING_CONNECTOR_LOG_NOTIFICATIONS`: log computed high-level notifications/events * `SYNCTHINGTRAY_LOG_JS_CONSOLE`: log message from the JavaScript console of the built-in web view ### Useful environment variables for development diff --git a/syncthingconnector/syncthingnotifier.cpp b/syncthingconnector/syncthingnotifier.cpp index ef02bea..bb4f438 100644 --- a/syncthingconnector/syncthingnotifier.cpp +++ b/syncthingconnector/syncthingnotifier.cpp @@ -3,11 +3,16 @@ #include "./syncthingprocess.h" #include "./utils.h" +#include "resources/config.h" + #ifdef LIB_SYNCTHING_CONNECTOR_SUPPORT_SYSTEMD #include "./syncthingservice.h" #endif #include +#include + +#include using namespace CppUtilities; @@ -37,6 +42,7 @@ SyncthingNotifier::SyncthingNotifier(const SyncthingConnection &connection, QObj , m_previousStatus(SyncthingStatus::Disconnected) , m_ignoreInavailabilityAfterStart(15) , m_initialized(false) + , m_logOnStderr(qEnvironmentVariableIntValue(PROJECT_VARNAME_UPPER "_LOG_ALL") || qEnvironmentVariableIntValue(PROJECT_VARNAME_UPPER "_LOG_NOTIFICATIONS")) { connect(&connection, &SyncthingConnection::statusChanged, this, &SyncthingNotifier::handleStatusChangedEvent); connect(&connection, &SyncthingConnection::dirCompleted, this, &SyncthingNotifier::emitSyncComplete); @@ -72,7 +78,7 @@ void SyncthingNotifier::handleNewDevEvent(DateTime when, const QString &devId, c return; } - emit newDevice(devId, tr("Device %1 (%2) wants to connect.").arg(devId, address)); + emit newDevice(devId, log(tr("Device %1 (%2) wants to connect.").arg(devId, address))); } void SyncthingNotifier::handleNewDirEvent(DateTime when, const QString &devId, const SyncthingDev *dev, const QString &dirId, const QString &dirLabel) @@ -93,7 +99,7 @@ void SyncthingNotifier::handleNewDirEvent(DateTime when, const QString &devId, c return devPrefix + tr(" wants to share folder %1 (%2).").arg(dirLabel, dirId); } }()); - emit newDir(devId, dirId, message); + emit newDir(devId, dirId, log(message)); } void SyncthingNotifier::handleSyncthingProcessError(QProcess::ProcessError processError) @@ -214,8 +220,16 @@ void SyncthingNotifier::emitSyncComplete(CppUtilities::DateTime when, const Sync // format the notification message const auto message(syncCompleteString(std::vector{ &dir }, remoteDev)); if (!message.isEmpty()) { - emit syncComplete(message); + emit syncComplete(log(message)); } } +const QString &SyncthingNotifier::log(const QString &message) +{ + if (m_logOnStderr) { + std::cerr << EscapeCodes::Phrases::Info << message.toStdString() << EscapeCodes::Phrases::End; + } + return message; +} + } // namespace Data diff --git a/syncthingconnector/syncthingnotifier.h b/syncthingconnector/syncthingnotifier.h index b5084be..78eb762 100644 --- a/syncthingconnector/syncthingnotifier.h +++ b/syncthingconnector/syncthingnotifier.h @@ -110,6 +110,7 @@ private: bool isDisconnectRelevant() const; void emitConnectedAndDisconnected(SyncthingStatus newStatus); void emitSyncComplete(CppUtilities::DateTime when, const SyncthingDir &dir, int index, const Data::SyncthingDev *remoteDev); + const QString &log(const QString &message); const SyncthingConnection &m_connection; #ifdef LIB_SYNCTHING_CONNECTOR_SUPPORT_SYSTEMD @@ -121,6 +122,7 @@ private: SyncthingStatus m_previousStatus; unsigned int m_ignoreInavailabilityAfterStart; bool m_initialized; + bool m_logOnStderr; }; /*!