From 76b3b6c0b6e819dee408796e70696156fb23d2a0 Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Tue, 11 Jul 2017 20:03:24 +0200 Subject: [PATCH] Utility function to directly log warning when network not accessible --- src/blackcore/db/databasereader.cpp | 12 ++---------- src/blackcore/threadedreader.cpp | 10 ++++++++-- src/blackcore/threadedreader.h | 3 ++- src/blackcore/vatsim/vatsimbookingreader.cpp | 6 +----- src/blackcore/vatsim/vatsimdatafilereader.cpp | 6 +----- src/blackcore/vatsim/vatsimmetarreader.cpp | 6 +----- src/blackcore/vatsim/vatsimstatusfilereader.cpp | 6 +----- 7 files changed, 16 insertions(+), 33 deletions(-) diff --git a/src/blackcore/db/databasereader.cpp b/src/blackcore/db/databasereader.cpp index 54cc2453a..b6c3047a0 100644 --- a/src/blackcore/db/databasereader.cpp +++ b/src/blackcore/db/databasereader.cpp @@ -179,11 +179,7 @@ namespace BlackCore // ps_read is implemented in the derived classes if (entities == CEntityFlags::NoEntity) { return; } - if (!this->isNetworkAccessible()) - { - CLogMessage(this).warning("No network, will not read %1") << CEntityFlags::flagToString(entities); - return; - } + if (!this->isNetworkAccessible(QString("No network, will not read %1").arg(CEntityFlags::flagToString(entities)))) { return; } const bool s = QMetaObject::invokeMethod(this, "ps_read", Q_ARG(BlackMisc::Network::CEntityFlags::Entity, entities), @@ -355,11 +351,7 @@ namespace BlackCore bool CDatabaseReader::requestHeadersOfSharedFiles(CEntityFlags::Entity entities) { - if (!this->isNetworkAccessible()) - { - CLogMessage(this).warning("No network, will not read shared file headers for %1") << CEntityFlags::flagToString(entities); - return false; - } + if (!this->isNetworkAccessible(QString("No network, will not read shared file headers for %1").arg(CEntityFlags::flagToString(entities)))) { return false; } CEntityFlags::Entity allEntities(this->maskBySupportedEntities(entities)); CEntityFlags::Entity currentEntity = CEntityFlags::iterateDbEntities(allEntities); diff --git a/src/blackcore/threadedreader.cpp b/src/blackcore/threadedreader.cpp index a5e9699ed..70c07b474 100644 --- a/src/blackcore/threadedreader.cpp +++ b/src/blackcore/threadedreader.cpp @@ -11,6 +11,7 @@ #include "blackcore/application.h" #include "blackmisc/network/networkutils.h" #include "blackmisc/threadutils.h" +#include "blackmisc/logmessage.h" #include #include @@ -66,9 +67,14 @@ namespace BlackCore return delta <= timeLastMs; } - bool CThreadedReader::isNetworkAccessible() const + bool CThreadedReader::isNetworkAccessible(const QString &logWarningMessage) const { - return sApp->isNetworkAccessible(); + const bool a = sApp->isNetworkAccessible(); + if (!a && !logWarningMessage.isEmpty()) + { + CLogMessage(this).warning(logWarningMessage); + } + return a; } void CThreadedReader::startReader() diff --git a/src/blackcore/threadedreader.h b/src/blackcore/threadedreader.h index 9d24d299d..a03835f04 100644 --- a/src/blackcore/threadedreader.h +++ b/src/blackcore/threadedreader.h @@ -53,7 +53,8 @@ namespace BlackCore bool updatedWithinLastMs(qint64 timeLastMs); //! Network accessible? - bool isNetworkAccessible() const; + //! \param logWarningMessage optional warning if not accessible + bool isNetworkAccessible(const QString &logWarningMessage = {}) const; //! Is marked as read failed //! \threadsafe diff --git a/src/blackcore/vatsim/vatsimbookingreader.cpp b/src/blackcore/vatsim/vatsimbookingreader.cpp index 8170fc695..dc6248485 100644 --- a/src/blackcore/vatsim/vatsimbookingreader.cpp +++ b/src/blackcore/vatsim/vatsimbookingreader.cpp @@ -64,11 +64,7 @@ namespace BlackCore { this->threadAssertCheck(); if (!this->doWorkCheck()) { return; } - if (!this->isNetworkAccessible()) - { - CLogMessage(this).warning("No network, cannot read VATSIM bookings"); - return; - } + if (!this->isNetworkAccessible("No network, cannot read VATSIM bookings")) { return; } Q_ASSERT_X(sApp, Q_FUNC_INFO, "No application"); const QUrl url(sApp->getGlobalSetup().getVatsimBookingsUrl()); diff --git a/src/blackcore/vatsim/vatsimdatafilereader.cpp b/src/blackcore/vatsim/vatsimdatafilereader.cpp index a0dc62559..5fbf5f7e1 100644 --- a/src/blackcore/vatsim/vatsimdatafilereader.cpp +++ b/src/blackcore/vatsim/vatsimdatafilereader.cpp @@ -183,11 +183,7 @@ namespace BlackCore { this->threadAssertCheck(); if (!this->doWorkCheck()) { return; } - if (!this->isNetworkAccessible()) - { - CLogMessage(this).warning("No network, cannot read VATSIM data file"); - return; - } + if (!this->isNetworkAccessible("No network, cannot read VATSIM data file")) { return; } // round robin for load balancing // remark: Don't use QThread to run network operations in the background diff --git a/src/blackcore/vatsim/vatsimmetarreader.cpp b/src/blackcore/vatsim/vatsimmetarreader.cpp index d28282219..9bea1361c 100644 --- a/src/blackcore/vatsim/vatsimmetarreader.cpp +++ b/src/blackcore/vatsim/vatsimmetarreader.cpp @@ -76,11 +76,7 @@ namespace BlackCore { this->threadAssertCheck(); if (!this->doWorkCheck()) { return; } - if (!this->isNetworkAccessible()) - { - CLogMessage(this).warning("No network, cannot read METARs"); - return; - } + if (!this->isNetworkAccessible("No network, cannot read METARs")) { return; } CFailoverUrlList urls(sApp->getVatsimMetarUrls()); const CUrl url(urls.obtainNextWorkingUrl(true)); diff --git a/src/blackcore/vatsim/vatsimstatusfilereader.cpp b/src/blackcore/vatsim/vatsimstatusfilereader.cpp index b3c998c48..d89bc66fb 100644 --- a/src/blackcore/vatsim/vatsimstatusfilereader.cpp +++ b/src/blackcore/vatsim/vatsimstatusfilereader.cpp @@ -66,11 +66,7 @@ namespace BlackCore { this->threadAssertCheck(); if (!this->doWorkCheck()) { return; } - if (!this->isNetworkAccessible()) - { - CLogMessage(this).warning("No network, cannot read VATSIM status file"); - return; - } + if (!this->isNetworkAccessible("No network, cannot read VATSIM status file")) { return; } Q_ASSERT_X(sApp, Q_FUNC_INFO, "Missing application"); CFailoverUrlList urls(sApp->getGlobalSetup().getVatsimStatusFileUrls());