Add event traces.
[dorian] / trace.h
1 #ifndef TRACE_H
2 #define TRACE_H
3
4 #include <QtDebug>
5 #include <QString>
6 #include <QTime>
7 #include <QEvent>
8
9 class Trace
10 {
11 public:
12     Trace(const QString &s): name(s) {
13         qDebug() << prefix() + ">" + name;
14         indent++;
15     }
16     ~Trace() {
17         if (--indent < 0) {
18             indent = 0;
19         }
20         qDebug() << prefix() + "<" + name;
21     }
22     void trace(const QString &s) const {
23         qDebug() << prefix()  + name + ": " + s;
24     }
25
26     static void debug(const QString &s) {
27         qDebug() << prefix()  + s;
28     }
29
30     static QString event(QEvent::Type t) {
31         for (int i = 0; eventTab[i].name; i++) {
32             if (eventTab[i].type == t) {
33                 return eventTab[i].name;
34             }
35         }
36         if (t >= QEvent::User) {
37             return QString("QEvent::User+%1").arg(t - QEvent::User);
38         } else {
39             return QString("Unknown event %1").arg(t);
40         }
41     }
42
43 protected:
44     static QString prefix() {
45         return QTime::currentTime().toString("hh:mm:ss.zzz ") +
46             QString(" ").repeated(indent);
47     }
48     QString name;
49     static int indent;
50     typedef struct {int type; const char *name;} EventName;
51     static EventName eventTab[];
52 };
53
54 #endif // TRACE_H