[Telepathy-commits] [telepathy-qt4/master] Make debugging output controllable by application code at runtime

Olli Salli olli.salli at collabora.co.uk
Mon Aug 25 13:12:04 PDT 2008


---
 TelepathyQt4/Debug       |    1 +
 TelepathyQt4/Makefile.am |    3 ++
 TelepathyQt4/debug.cpp   |   88 ++++++++++++++++++++++++++++++++++++++++++++++
 TelepathyQt4/debug.h     |   66 ++++++++++++++++++++++++++++++++++
 TelepathyQt4/debug.hpp   |   16 +++++---
 5 files changed, 168 insertions(+), 6 deletions(-)
 create mode 100644 TelepathyQt4/Debug
 create mode 100644 TelepathyQt4/debug.cpp
 create mode 100644 TelepathyQt4/debug.h

diff --git a/TelepathyQt4/Debug b/TelepathyQt4/Debug
new file mode 100644
index 0000000..5549699
--- /dev/null
+++ b/TelepathyQt4/Debug
@@ -0,0 +1 @@
+#include <TelepathyQt4/debug.h>
diff --git a/TelepathyQt4/Makefile.am b/TelepathyQt4/Makefile.am
index 68e9a03..0b300a5 100644
--- a/TelepathyQt4/Makefile.am
+++ b/TelepathyQt4/Makefile.am
@@ -36,6 +36,7 @@ libtelepathy_qt4_la_SOURCES = \
     cli-media-session-handler.cpp \
     cli-media-stream-handler.cpp \
     cli-properties.cpp \
+    debug.cpp \
     debug.hpp \
     types.cpp \
     header-compile-test.cpp
@@ -59,6 +60,7 @@ nodist_libtelepathy_qt4_la_SOURCES = \
 
 tpqt4include_HEADERS = \
     Constants \
+    Debug \
     Types \
     cli-channel.h \
     cli-connection.h \
@@ -68,6 +70,7 @@ tpqt4include_HEADERS = \
     cli-media-stream-handler.h \
     cli-properties.h \
     constants.h \
+    debug.h \
     types.h
 
 tpqt4clientinclude_HEADERS = \
diff --git a/TelepathyQt4/debug.cpp b/TelepathyQt4/debug.cpp
new file mode 100644
index 0000000..9d6cbf7
--- /dev/null
+++ b/TelepathyQt4/debug.cpp
@@ -0,0 +1,88 @@
+/*
+ * This file is part of TelepathyQt4
+ *
+ * Copyright (C) 2008 Collabora Ltd. <http://www.collabora.co.uk/>
+ * Copyright (C) 2008 Nokia Corporation
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#include "debug.h"
+#include "debug.hpp"
+
+#include <QIODevice>
+
+namespace Telepathy
+{
+#ifdef ENABLE_DEBUG
+
+namespace
+{
+struct DiscardDevice : public QIODevice
+{
+    qint64 readData(char* data, qint64 maxSize)
+    {
+        return 0;
+    }
+
+    qint64 writeData(const char* data, qint64 maxsize)
+    {
+        return maxsize;
+    }
+} discard;
+
+bool debugEnabled = false;
+bool warningsEnabled = true;
+}
+
+void enableDebug(bool enable)
+{
+    debugEnabled = enable;
+}
+
+void enableWarnings(bool enable)
+{
+    warningsEnabled = enable;
+}
+
+QDebug enabledDebug()
+{
+    if (debugEnabled)
+        return qDebug();
+    else
+        return QDebug(&discard);
+}
+
+QDebug enabledWarning()
+{
+    if (warningsEnabled)
+        return qWarning();
+    else
+        return QDebug(&discard);
+}
+
+#else /* #ifdef ENABLE_DEBUG */
+
+void enableDebug(bool enable)
+{
+}
+
+void enableWarnings(bool enable)
+{
+}
+
+#endif /* #ifdef ENABLE_DEBUG */
+
+} /* namespace Telepathy */
diff --git a/TelepathyQt4/debug.h b/TelepathyQt4/debug.h
new file mode 100644
index 0000000..a4238c5
--- /dev/null
+++ b/TelepathyQt4/debug.h
@@ -0,0 +1,66 @@
+/*
+ * This file is part of TelepathyQt4
+ *
+ * Copyright (C) 2008 Collabora Ltd. <http://www.collabora.co.uk/>
+ * Copyright (C) 2008 Nokia Corporation
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ */
+
+#ifndef _TelepathyQt4_Debug_HEADER_GUARD_
+#define _TelepathyQt4_Debug_HEADER_GUARD_
+
+#include <config.h>
+
+/**
+ * \defgroup debug Common debug support
+ *
+ * TelepathyQt4 has an internal mechanism for displaying debugging output. It
+ * uses the Qt4 debugging subsystem, so if you want to redirect the messages,
+ * use qInstallMsgHandler() from <QtGlobal>.
+ *
+ * Debugging output is divided into two categories: normal debug output and
+ * warning messages. Normal debug output results in the normal operation of the
+ * library, warning messages are output only when something goes wrong. Each
+ * category can be invidually enabled.
+ */
+
+namespace Telepathy
+{
+/**
+ * Enable or disable normal debug output from the library. If the library is not
+ * compiled with debug support enabled, this has no effect; no output is
+ * produced in any case.
+ *
+ * The default is <code>false</code> ie. no debug output.
+ *
+ * \param enable Whether debug output should be enabled or not.
+ */
+void enableDebug(bool enable);
+
+/**
+ * Enable or disable warning output from the library. If the library is not
+ * compiled with debug support enabled, this has no effect; no output is
+ * produced in any case.
+ *
+ * The default is <code>true</code> ie. warning output enabled.
+ *
+ * \param enable Whether warnings should be enabled or not.
+ */
+void enableWarnings(bool enable);
+
+} /* namespace Telepathy */
+
+#endif
diff --git a/TelepathyQt4/debug.hpp b/TelepathyQt4/debug.hpp
index 9e11945..d47742c 100644
--- a/TelepathyQt4/debug.hpp
+++ b/TelepathyQt4/debug.hpp
@@ -22,23 +22,27 @@
 #ifndef _TelepathyQt4_debug_HEADER_GUARD_
 #define _TelepathyQt4_debug_HEADER_GUARD_
 
-#include <config.h>
+#include <QDebug>
 
 namespace Telepathy
 {
-
 #ifdef ENABLE_DEBUG
 
-#include <QDebug>
+QDebug enabledDebug();
+QDebug enabledWarning();
 
 inline QDebug debug()
 {
-    return qDebug() << PACKAGE_NAME " (version " PACKAGE_VERSION ") DEBUG:";
+    QDebug debug = enabledDebug();
+    debug.nospace() << PACKAGE_NAME " (version " PACKAGE_VERSION ") DEBUG:";
+    return debug.space();
 }
 
 inline QDebug warning()
 {
-    return qWarning() << PACKAGE_NAME " (version " PACKAGE_VERSION ") WARNING:";
+    QDebug warning = enabledWarning();
+    warning.nospace() << PACKAGE_NAME " (version " PACKAGE_VERSION ") WARNING:";
+    return warning.space();
 }
 
 #else /* #ifdef ENABLE_DEBUG */
@@ -56,7 +60,7 @@ struct NoDebug
         return *this;
     }
 
-    NoDebug& noSpace()
+    NoDebug& nospace()
     {
         return *this;
     }
-- 
1.5.6.3




More information about the Telepathy-commits mailing list