Prettify qDebug() output for events
authorManuel Nickschas <sputnick@quassel-irc.org>
Fri, 24 Sep 2010 17:27:28 +0000 (19:27 +0200)
committerManuel Nickschas <sputnick@quassel-irc.org>
Wed, 13 Oct 2010 23:06:31 +0000 (01:06 +0200)
src/common/event.cpp
src/common/event.h
src/common/eventmanager.cpp
src/common/eventmanager.h
src/common/ircevent.h
src/common/messageevent.h
src/common/networkevent.h

index 3803ab9..01d9642 100644 (file)
@@ -25,3 +25,14 @@ Event::Event(EventManager::EventType type)
 {
 
 }
+
+QDebug operator<<(QDebug dbg, Event *e) {
+  dbg.nospace() << qPrintable(e->className()) << "("
+                << "type = 0x" << qPrintable(QString::number(e->type(), 16));
+  e->debugInfo(dbg);
+                //<< ", data = " << e->data(); // we don't use data anywhere yet
+  dbg.nospace() << ", flags = 0x" << qPrintable(QString::number(e->flags(), 16))
+                << ")";
+  return dbg.space();
+}
+
index 9fd6373..82cd1ac 100644 (file)
@@ -21,6 +21,8 @@
 #ifndef EVENT_H
 #define EVENT_H
 
+#include <QDebug>
+
 #include "eventmanager.h"
 
 class Event {
@@ -39,15 +41,23 @@ public:
   inline void stop() { setFlag(EventManager::Stopped); }
   inline bool isStopped() { return _flags.testFlag(EventManager::Stopped); }
 
-  inline void setData(const QVariant &data) { _data = data; }
-  inline QVariant data() const { return _data; }
+  //inline void setData(const QVariant &data) { _data = data; }
+  //inline QVariant data() const { return _data; }
+
+protected:
+  virtual inline QString className() const { return "Event"; }
+  virtual inline void debugInfo(QDebug &dbg) const { Q_UNUSED(dbg); }
 
 private:
   EventManager::EventType _type;
   EventManager::EventFlags _flags;
-  QVariant _data;
+  //QVariant _data;
+
+  friend QDebug operator<<(QDebug dbg, Event *e);
 };
 
+QDebug operator<<(QDebug dbg, Event *e);
+
 /*******/
 
 #endif
index 2a20b21..552765a 100644 (file)
@@ -48,6 +48,10 @@ EventManager::EventType EventManager::eventGroupByName(const QString &name) cons
   return type == Invalid? Invalid : static_cast<EventType>(type & EventGroupMask);
 }
 
+QString EventManager::enumName(EventType type) const {
+  return eventEnum().valueToKey(type);
+}
+
 /* NOTE:
    Registering and calling handlers works fine even if they specify a subclass of Event as their parameter.
    However, this most probably is a result from a reinterpret_cast somewhere deep inside Qt, so there is *no*
@@ -102,6 +106,7 @@ void EventManager::registerEventHandler(QList<EventType> events, QObject *object
 
 // not threadsafe! if we should want that, we need to add a mutexed queue somewhere in this general area.
 void EventManager::sendEvent(Event *event) {
+  // qDebug() << "Sending" << event;
   dispatchEvent(event);
 }
 
index d7480d1..73ff8b7 100644 (file)
@@ -105,6 +105,7 @@ public:
 
   EventType eventTypeByName(const QString &name) const;
   EventType eventGroupByName(const QString &name) const;
+  QString enumName(EventType type) const;
 
 public slots:
   void registerObject(QObject *object, Priority priority = NormalPriority, const QString &methodPrefix = "handle");
index 6704013..0f4336d 100644 (file)
@@ -37,6 +37,14 @@ public:
   inline QStringList params() const { return _params; }
   inline void setParams(const QStringList &params) { _params = params; }
 
+protected:
+  virtual inline QString className() const { return "IrcEvent"; }
+  virtual inline void debugInfo(QDebug &dbg) const {
+    NetworkEvent::debugInfo(dbg);
+    dbg << ", prefix = " << qPrintable(prefix())
+        << ", params = " << params();
+  }
+
 private:
   QString _prefix;
   QStringList _params;
@@ -56,6 +64,16 @@ public:
   inline QString target() const { return _target; }
   inline void setTarget(const QString &target) { _target = target; }
 
+protected:
+  virtual inline QString className() const { return "IrcEventNumeric"; }
+  virtual inline void debugInfo(QDebug &dbg) const {
+    dbg << ", num = " << number();
+    NetworkEvent::debugInfo(dbg);
+    dbg << ", target = " << qPrintable(target())
+        << ", prefix = " << qPrintable(prefix())
+        << ", params = " << params();
+  }
+
 private:
   uint _number;
   QString _target;
@@ -75,6 +93,16 @@ public:
   inline QByteArray rawMessage() const { return _rawMessage; }
   inline void setRawMessage(const QByteArray &rawMessage) { _rawMessage = rawMessage; }
 
+protected:
+  virtual inline QString className() const { return "IrcEventRawMessage"; }
+  virtual inline void debugInfo(QDebug &dbg) const {
+    NetworkEvent::debugInfo(dbg);
+    dbg << ", target = " << qPrintable(target())
+        << ", prefix = " << qPrintable(prefix())
+        << ", msg = " << rawMessage();
+  }
+
+
 private:
   QByteArray _rawMessage;
 };
index 10803cb..11e9987 100644 (file)
@@ -52,6 +52,18 @@ public:
   inline void setMsgFlag(Message::Flag flag) { _msgFlags |= flag; }
   inline void setMsgFlags(Message::Flags flags) { _msgFlags = flags; }
 
+protected:
+  virtual inline QString className() const { return "MessageEvent"; }
+  virtual inline void debugInfo(QDebug &dbg) const {
+    NetworkEvent::debugInfo(dbg);
+    dbg.nospace() << ", sender = " << qPrintable(sender())
+                  << ", target = " << qPrintable(target())
+                  << ", text = " << text()
+                  << ", msgtype = " << qPrintable(QString::number(msgType(), 16))
+                  << ", buffertype = " << qPrintable(QString::number(bufferType(), 16))
+                  << ", msgflags = " << qPrintable(QString::number(msgFlags(), 16));
+  }
+
 private:
   BufferInfo::Type bufferTypeByTarget(const QString &target) const;
 
index dfa492d..67b47a0 100644 (file)
@@ -38,6 +38,10 @@ public:
   inline NetworkId networkId() const { return network()? network()->networkId() : NetworkId(); }
   inline Network *network() const { return _network; }
 
+protected:
+  virtual inline QString className() const { return "NetworkEvent"; }
+  virtual inline void debugInfo(QDebug &dbg) const { dbg.nospace() << ", net = " << qPrintable(_network->networkName()); }
+
 private:
   Network *_network;
 };
@@ -53,6 +57,13 @@ public:
   inline Network::ConnectionState connectionState() const { return _state; }
   inline void setConnectionState(Network::ConnectionState state) { _state = state; }
 
+protected:
+  virtual inline QString className() const { return "NetworkConnectionEvent"; }
+  virtual inline void debugInfo(QDebug &dbg) const {
+    NetworkEvent::debugInfo(dbg);
+    dbg.nospace() << ", state = " << qPrintable(QString::number(_state));
+  }
+
 private:
   Network::ConnectionState _state;
 };
@@ -68,6 +79,13 @@ public:
   inline QByteArray data() const { return _data; }
   inline void setData(const QByteArray &data) { _data = data; }
 
+protected:
+  virtual inline QString className() const { return "NetworkDataEvent"; }
+  virtual inline void debugInfo(QDebug &dbg) const {
+    NetworkEvent::debugInfo(dbg);
+    dbg.nospace() << ", data = " << data();
+  }
+
 private:
   QByteArray _data;
 };