[prev in list] [next in list] [prev in thread] [next in thread] 

List:       koffice-devel
Subject:    KDE/kdelibs/kdecore
From:       David Faure <faure () kde ! org>
Date:       2010-11-04 19:09:38
Message-ID: 20101104190938.590B9AC89B () svn ! kde ! org
[Download RAW message or body]

SVN commit 1193132 by dfaure:

Add support for tracing entering/leaving blocks, and indenting debug output inside \
the block. Inspired by Mark Kretschmann's merge request for koffice (amarok's code).
Easy to use, just put a KDEBUG_BLOCK at the beginning of the relevant methods.
CCMAIL: koffice-devel@kde.org


 M  +33 -0     io/kdebug.cpp  
 M  +51 -0     io/kdebug.h  
 M  +16 -0     tests/kdebugtest.cpp  


--- trunk/KDE/kdelibs/kdecore/io/kdebug.cpp #1193131:1193132
@@ -544,6 +544,8 @@
             s << areaName.constData();
         }
 
+        s << m_indentString.toLatin1().constData();
+
         if (printFileLine) {
             s << ' ' << file << ':' << line << ' ';
         }
@@ -674,6 +676,7 @@
     Cache cache;
     bool m_disableAll;
     int m_nullOutputYesNoCache[8];
+    QString m_indentString;
 
     KNoDebugStream devnull;
     QThreadStorage<KSyslogDebugStream*> syslogwriter;
@@ -851,3 +854,33 @@
     d->writeGroupForNamedArea(areaName, enabled);
     return areaNumber;
 }
+
+KDebug::Block::Block(const char* label, int area)
+    : m_label(label), m_area(area), d(0)
+{
+    m_startTime.start();
+    kDebug(area) << "BEGIN:" << label;
+    QMutexLocker locker(&kDebug_data->mutex);
+    kDebug_data->m_indentString += QLatin1String("  ");
+}
+
+KDebug::Block::~Block()
+{
+    const double duration = (double)m_startTime.elapsed() / (double)1000.0;
+    kDebug_data->mutex.lock();
+    kDebug_data->m_indentString.truncate(kDebug_data->m_indentString.length() - 2);
+    kDebug_data->mutex.unlock();
+
+    // Print timing information, and a special message (DELAY) if the method took \
longer than 5s +    if (duration < 5.0) {
+        kDebug(m_area)
+            << "END__:"
+            << m_label
+            << QString::fromLatin1("[Took: %3s]").arg(QString::number(duration, 'g', \
2) ); +    } else {
+        kDebug(m_area)
+            << "END__:"
+            << m_label
+            << QString::fromLatin1("[DELAY Took (quite long) \
%3s]").arg(QString::number(duration, 'g', 2)); +    }
+}
--- trunk/KDE/kdelibs/kdecore/io/kdebug.h #1193131:1193132
@@ -25,6 +25,7 @@
 #include <kdecore_export.h>
 
 #include <QtCore/QDebug>
+#include <QtCore/QTime>
 
 /**
  * \addtogroup kdebug Debug message generators
@@ -34,6 +35,10 @@
  * QT_NO_DEBUG when compiling your source. If QT_NO_DEBUG is defined then debug
  * messages are not printed by default but can still be enabled by runtime
  * configuration, e.g. via kdebugdialog or by editing kdebugrc.
+ *
+ * You can also control what you see: process name, area name, method name,
+ * file and line number, timestamp, etc. using environment variables.
+ * See http://techbase.kde.org/SysAdmin/Environment_Variables#KDE_DEBUG_NOPROCESSINFO
                
  */
 
 #if !defined(KDE_NO_DEBUG_OUTPUT)
@@ -242,6 +247,7 @@
     int line;
     QtMsgType level;
 public:
+    class Block;
     explicit inline KDebug(QtMsgType type, const char *f = 0, int l = -1, const char \
*info = 0)  : file(f), funcinfo(info), line(l), level(type)
         {
@@ -291,6 +297,7 @@
      * in other files (with a better name for the function of course).
      */
     static KDECORE_EXPORT int registerArea(const QByteArray& areaName, bool enabled \
= true); +
 private:
     WrongSyntax operator()(const char*) {return WrongSyntax();} // error! Use \
kDebug() << "..." or kWarning() << "..." instead.  };
@@ -314,6 +321,50 @@
 # define kWarning    while (false) kWarning
 #endif
 
+
+/**
+ * @class KDebug::Block
+ * @short Use this to label sections of your code
+ * @since 4.6
+ *
+ * Usage:
+ * <code>
+ *     void function()
+ *     {
+ *         KDebug::Block myBlock( "section" );
+ *
+ *         debug() << "output1" << endl;
+ *         debug() << "output2" << endl;
+ *     }
+ * </code>
+ *
+ * Will output:
+ *
+ *     app: BEGIN: section
+ *     app:  [prefix] output1
+ *     app:  [prefix] output2
+ *     app: END: section - Took 0.1s
+ *
+ * Alternatively, use the KDEBUG_BLOCK macro, for automatic naming.
+ */
+class KDECORE_EXPORT KDebug::Block
+{
+public:
+    Block(const char* label, int area = KDE_DEFAULT_DEBUG_AREA);
+    ~Block();
+
+private:
+    QTime m_startTime;
+    const char *m_label;
+    int m_area;
+    int m_color;
+    class Private;
+    Private* const d;
+};
+
+/// Convenience macro for making a standard KDebug::Block
+#define KDEBUG_BLOCK KDebug::Block \
uniquelyNamedStackAllocatedStandardBlock(Q_FUNC_INFO); +
 /** @} */
 
 #endif
--- trunk/KDE/kdelibs/kdecore/tests/kdebugtest.cpp #1193131:1193132
@@ -78,6 +78,19 @@
     uglypmf func_uglypmf(uglypmf = 0) { kDebug(); return 0; }
     QMap<QString, uglypmf> func_uglypmf2() { kDebug(); return QMap<QString, \
uglypmf>(); }  
+    void testBlock()
+    {
+        KDEBUG_BLOCK
+        func_int();
+        testNestedBlock();
+    }
+
+    void testNestedBlock()
+    {
+        KDEBUG_BLOCK
+        func_void();
+    }
+
 public:
     TestClass1()
         {
@@ -275,6 +288,9 @@
         using namespace N;
         func6();
     }
+
+    TestClass1 c1;
+    c1.testBlock();
 }
 
 // Concurrency testing, based on code from bug 133026
_______________________________________________
koffice-devel mailing list
koffice-devel@kde.org
https://mail.kde.org/mailman/listinfo/koffice-devel


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic