changeset 1058:fe2c6666b462

(issue46) Add debug option to trustbridge client application
author Andre Heinecke <andre.heinecke@intevation.de>
date Tue, 09 Sep 2014 17:58:55 +0200
parents 126e5ce4d1d1
children f638eb1f3b0f
files common/logging.h ui/main.cpp ui/sslconnection_curl.cpp ui/tests/downloadertest.cpp
diffstat 4 files changed, 45 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/common/logging.h	Tue Sep 09 17:16:07 2014 +0200
+++ b/common/logging.h	Tue Sep 09 17:58:55 2014 +0200
@@ -26,6 +26,8 @@
 /** @def The name used for logging */
 #define LOG_NAME "TrustBridge"
 
+extern bool g_debug;
+
 #ifdef WIN32
 
 #include <windows.h>
--- a/ui/main.cpp	Tue Sep 09 17:16:07 2014 +0200
+++ b/ui/main.cpp	Tue Sep 09 17:58:55 2014 +0200
@@ -52,6 +52,21 @@
  Q_IMPORT_PLUGIN(QXcbIntegrationPlugin)
 #endif
 
+bool g_debug = false;
+QtMessageHandler g_default_msg_handler = NULL;
+
+void noDebugOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
+{
+    if (type == QtDebugMsg) {
+        return;
+    }
+
+    if (g_default_msg_handler) {
+        (*g_default_msg_handler)(type, context, msg);
+    }
+}
+
+
 int main(int argc, char **argv)
 {
 #ifdef WIN32
@@ -77,12 +92,19 @@
     QApplication::setApplicationName(QString::fromLatin1(APPNAME));
     QApplication::setApplicationVersion(QString::fromLatin1(VERSION));
     QSettings::setDefaultFormat(QSettings::IniFormat);
+    QStringList arguments = QApplication::arguments();
+
+    if (arguments.contains("--debug")) {
+        g_debug = true;
+    } else {
+        g_debug = false;
+        g_default_msg_handler = qInstallMessageHandler(noDebugOutput);
+    }
 
     qDebug() << "Application style is: " << app.style()->metaObject()->className();
     qDebug() << "Available styles: " << QStyleFactory::keys().join(", ");
     qDebug() << "Font is: " << app.font();
 
-    QStringList arguments = QApplication::arguments();
     bool trayMode = arguments.contains("--tray");
 
     if (arguments.contains("--version")) {
--- a/ui/sslconnection_curl.cpp	Tue Sep 09 17:16:07 2014 +0200
+++ b/ui/sslconnection_curl.cpp	Tue Sep 09 17:58:55 2014 +0200
@@ -7,12 +7,11 @@
  */
 
 #include "sslconnection_curl.h"
+#include "logging.h"
 
 #include <polarssl/ssl.h>
 #include <QSaveFile>
 
-#undef CONNECTION_DEBUG
-
 SSLConnectionCurl::SSLConnectionCurl(const QString& url,
                              const QByteArray& certificate):
     SSLConnection (url, certificate),
@@ -83,10 +82,9 @@
         return;
     }
     mInitialized = true;
-
-#ifdef CONNECTION_DEBUG
-    curl_easy_setopt(mCurl, CURLOPT_VERBOSE, 1L);
-#endif
+    if (g_debug) {
+        curl_easy_setopt(mCurl, CURLOPT_VERBOSE, 1L);
+    }
 }
 
 SSLConnectionCurl::~SSLConnectionCurl() {
@@ -151,6 +149,15 @@
     return written;
 }
 
+size_t debug_write(void *ptr, size_t size, size_t nmemb,
+        void *unused)
+{
+    Q_UNUSED(unused);
+
+    qDebug() << QString::fromUtf8((const char *)ptr, size * nmemb);
+    return size *nmemb;
+}
+
 bool SSLConnectionCurl::downloadFile(const QString &resource,
                                      const QString &fileName,
                                      size_t maxSize)
@@ -213,6 +220,11 @@
     QUrl urlCopy = mUrl;
     urlCopy.setPath(resource);
 
+    if (curl_easy_setopt(mCurl, CURLOPT_WRITEFUNCTION, debug_write) != CURLE_OK) {
+        qDebug() << "Failed to set write function";
+        return QDateTime();
+    }
+
     if (curl_easy_setopt(mCurl, CURLOPT_URL, urlCopy.toEncoded().constData()) != CURLE_OK) {
         qDebug() << "Failed to set URL";
         return QDateTime();
--- a/ui/tests/downloadertest.cpp	Tue Sep 09 17:16:07 2014 +0200
+++ b/ui/tests/downloadertest.cpp	Tue Sep 09 17:58:55 2014 +0200
@@ -309,5 +309,7 @@
     }
 }
 
+bool g_debug = true;
+
 QTEST_GUILESS_MAIN (DownloaderTest);
 

http://wald.intevation.org/projects/trustbridge/