Patch: Colored debug

Olivier Goffart ogoffart at kde.org
Thu Aug 30 00:07:37 BST 2007


Hi,

I'd like to commit the attached patch.
It does coloration of the kdebug stream as seen on the screenshot
http://bepointbe.be/files/tmp/kdebug1.png

I think it make the debug output much more easier to read.

Is there any objections ?   or do you want more colors ?


The small problem I have is that I'm not 100% sure the debug will be printed 
on stderr, since a custom QtMsgHandler could override that.
one solution Matthias Kretz proposed is to do 
QtMsgHandler old = qInstallMsgHandler( 0L );
if(!old)  colored=isatty( fileno(stderr) );  
else   qInstallMsgHandler( old );
This is a hack, and i don't think it will be required since I don't know why a 
KDE application could install message handler.

-------------- next part --------------
Index: kdebug.cpp
===================================================================
--- kdebug.cpp	(revision 706173)
+++ kdebug.cpp	(working copy)
@@ -36,6 +36,9 @@
 #include <fcntl.h>
 #include <windows.h>
 #include <wincon.h>
+#else
+#include <unistd.h>
+#include <stdio.h>
 #endif
 
 #ifdef NDEBUG
@@ -86,6 +89,8 @@
 #include "kdebugdbusiface_p.h"
 #include <QMutex>
 
+
+
 KDECORE_EXPORT bool kde_kdebug_enable_dbus_interface = false;
 
 class KNoDebugStream: public QIODevice
@@ -433,7 +438,7 @@
         return QDebug(&lineendstrippingwriter);
     }
 
-    QDebug printHeader(QDebug s, const QString &areaName, const char *, int, const char *funcinfo)
+    QDebug printHeader(QDebug s, const QString &areaName, const char *, int, const char *funcinfo, bool colored)
     {
 #ifdef KDE_EXTENDED_DEBUG_OUTPUT
         QString programName = cache.value(0).name;
@@ -444,6 +449,8 @@
             s << "/" << qPrintable(areaName);
 
         if (funcinfo) {
+            if(colored)
+                s << "\033[0;34m"; //blue
 # ifdef Q_CC_GNU
             // strip the function info down to the base function name
             // note that this throws away the template definitions,
@@ -474,6 +481,8 @@
 # else
             s << " " << funcinfo;
 # endif
+           if(colored)
+               s  << "\033[0m";
         }
 
         s << ":";
@@ -496,6 +505,8 @@
         if (areaName.isEmpty())
             areaName = cache.value(0).name;
 
+        bool colored=false;
+
         QDebug s(&devnull);
         switch (mode) {
         case FileOutput:
@@ -509,13 +520,18 @@
             break;
         case NoOutput:
             s = QDebug(&devnull);
+            return s; //no need to take the time to "print header" if we don't want to output anyway
             break;
         default:                // QtOutput
             s = setupQtWriter(type);
+#ifndef Q_OS_WIN
+            colored=isatty(fileno(stderr));  //only color if the debug goes to a tty.
+            // but does qDebug() always go to stderr by default ?
+#endif
             break;
         }
 
-        return printHeader(s, areaName, debugFile, line, funcinfo);
+        return printHeader(s, areaName, debugFile, line, funcinfo, colored);
     }
 
     QMutex mutex;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part.
URL: <http://mail.kde.org/pipermail/kde-core-devel/attachments/20070830/fc15d90d/attachment.sig>


More information about the kde-core-devel mailing list