Allow logging all API calls

This commit is contained in:
Martchus 2018-10-14 22:46:40 +02:00
parent 0ee25c1ae2
commit e410991012
3 changed files with 17 additions and 13 deletions

View File

@ -123,11 +123,11 @@ if(SYNCTHING_CONNECTION_LOG_SYNCTHING_EVENTS)
endif()
# configure whether POSTs should be logged
option(SYNCTHING_CONNECTION_LOG_POSTS "enables logging POSTs done by the SyncthingConnector (enable only for debugging!)" OFF)
if(SYNCTHING_CONNECTION_LOG_POSTS)
option(SYNCTHING_CONNECTION_LOG_API_CALLS "enables logging API calls done by the SyncthingConnector (enable only for debugging!)" OFF)
if(SYNCTHING_CONNECTION_LOG_API_CALLS)
set_source_files_properties(
syncthingconnection.cpp
PROPERTIES COMPILE_DEFINITIONS ${META_PROJECT_VARNAME_UPPER}_LOG_POSTS
PROPERTIES COMPILE_DEFINITIONS ${META_PROJECT_VARNAME_UPPER}_LOG_API_CALLS
)
message(WARNING "SyncthingConnection class will log event data to stdout")
endif()

View File

@ -10,7 +10,7 @@
#include <c++utilities/conversion/conversionexception.h>
#include <c++utilities/conversion/stringconversion.h>
#if defined(LIB_SYNCTHING_CONNECTOR_LOG_SYNCTHING_EVENTS) || defined(LIB_SYNCTHING_CONNECTOR_LOG_POSTS)
#if defined(LIB_SYNCTHING_CONNECTOR_LOG_SYNCTHING_EVENTS) || defined(LIB_SYNCTHING_CONNECTOR_LOG_API_CALLS)
#include <c++utilities/io/ansiescapecodes.h>
#endif
@ -32,7 +32,7 @@
using namespace std;
using namespace ChronoUtilities;
using namespace ConversionUtilities;
#if defined(LIB_SYNCTHING_CONNECTOR_LOG_SYNCTHING_EVENTS) || defined(LIB_SYNCTHING_CONNECTOR_LOG_POSTS)
#if defined(LIB_SYNCTHING_CONNECTOR_LOG_SYNCTHING_EVENTS) || defined(LIB_SYNCTHING_CONNECTOR_LOG_API_CALLS)
using namespace EscapeCodes;
#endif
@ -455,7 +455,10 @@ QNetworkRequest SyncthingConnection::prepareRequest(const QString &path, const Q
QNetworkReply *SyncthingConnection::requestData(const QString &path, const QUrlQuery &query, bool rest)
{
#ifndef LIB_SYNCTHING_CONNECTOR_CONNECTION_MOCKED
auto *reply = networkAccessManager().get(prepareRequest(path, query, rest));
auto *const reply = networkAccessManager().get(prepareRequest(path, query, rest));
#ifdef LIB_SYNCTHING_CONNECTOR_LOG_API_CALLS
cout << Phrases::Info << "GETing: " << reply->url().toString().toStdString() << Phrases::EndFlush;
#endif
reply->ignoreSslErrors(m_expectedSslErrors);
return reply;
#else
@ -468,11 +471,11 @@ QNetworkReply *SyncthingConnection::requestData(const QString &path, const QUrlQ
*/
QNetworkReply *SyncthingConnection::postData(const QString &path, const QUrlQuery &query, const QByteArray &data)
{
auto *reply = networkAccessManager().post(prepareRequest(path, query), data);
reply->ignoreSslErrors(m_expectedSslErrors);
#ifdef LIB_SYNCTHING_CONNECTOR_LOG_POSTS
cout << Phrases::Info << "POSTing:" << Phrases::End << data.data() << endl;
auto *const reply = networkAccessManager().post(prepareRequest(path, query), data);
#ifdef LIB_SYNCTHING_CONNECTOR_LOG_API_CALLS
cout << Phrases::Info << "POSTing: " << reply->url().toString().toStdString() << Phrases::End << data.data() << endl;
#endif
reply->ignoreSslErrors(m_expectedSslErrors);
return reply;
}
@ -2520,6 +2523,7 @@ void SyncthingConnection::emitMyIdChanged(const QString &newId)
void SyncthingConnection::handleFatalConnectionError()
{
setStatus(SyncthingStatus::Disconnected);
abortAllRequests();
if (m_autoReconnectTimer.interval()) {
m_autoReconnectTimer.start();
}

View File

@ -350,14 +350,14 @@ void ConnectionTests::testErrorCases()
cerr << "\n - Error handling in case of wrong credentials ..." << endl;
waitForConnection(defaultConnect(), 5000, connectionSignal(&SyncthingConnection::error, errorHandler));
while (!authErrorStatus || !authErrorConfig) {
while (!authErrorStatus && !authErrorConfig) {
waitForSignals(noop, 5000, connectionSignal(&SyncthingConnection::error, errorHandler));
}
cerr << "\n - Error handling in case of wrong API key ..." << endl;
m_connection.setCredentials(QStringLiteral("nobody"), QStringLiteral("supersecret"));
waitForConnection(defaultConnect(), 5000, connectionSignal(&SyncthingConnection::error, errorHandler));
while (!apiKeyErrorStatus || !apiKeyErrorConfig) {
while (!apiKeyErrorStatus && !apiKeyErrorConfig) {
waitForSignals(noop, 5000, connectionSignal(&SyncthingConnection::error, errorHandler));
}
}
@ -636,7 +636,7 @@ void ConnectionTests::testRequestingRescan()
CPPUNIT_ASSERT_EQUAL(QStringLiteral("test2"), dir);
rescanTriggered = true;
};
waitForSignals(bind(&SyncthingConnection::rescanAllDirs, &m_connection), 5000,
waitForSignalsOrFail(bind(&SyncthingConnection::rescanAllDirs, &m_connection), 5000, connectionSignal(&SyncthingConnection::error),
connectionSignal(&SyncthingConnection::rescanTriggered, rescanTriggeredHandler, &rescanTriggered));
bool errorOccured = false;