comparison d2/qtd/Debug.d @ 372:a032df77b6ab

Simple debug helper. Unittests. Meta-object for polymorphic non-QObjects
author Max Samukha <maxter@spambox.com>
date Thu, 08 Jul 2010 17:19:05 +0300
parents
children 681af90e1d53
comparison
equal deleted inserted replaced
370:7fd4b69378bf 372:a032df77b6ab
1 module qtd.Debug;
2
3 version (QtdUnittest)
4 debug = UseQtdDebug;
5 else debug (QtdDebug)
6 debug = UseQtdDebug;
7
8 debug (UseQtdDebug)
9 {
10 import
11 std.string,
12 qtd.QtdObject;
13 }
14
15 string debugHandler(string handler, string[] args...)
16 {
17 debug (UseQtdDebug)
18 {
19 string result = "qtdDebug." ~ handler ~ "(";
20 foreach (i, arg; args)
21 {
22 if (i)
23 result ~= ", ";
24 result ~= arg;
25 }
26 return result ~ ");";
27 }
28 else
29 return "";
30 }
31
32 debug (UseQtdDebug)
33 {
34 debug (QtdVerbose)
35 {
36 import
37 std.stdio,
38 std.string;
39 }
40
41 final shared class QtdDebug
42 {
43 enum MessageType
44 {
45 info,
46 warning,
47 error
48 }
49
50 private
51 {
52 static immutable msgTypeStrs = ["Info", "Warning", "Error"];
53 int wrapperCount_;
54 int nativeDeletedCount_;
55
56 this() {}
57 }
58
59 string wrapperToString(QtdObject wrapper)
60 {
61 static assert (QtdObjectFlags.sizeof == ubyte.sizeof);
62 auto flags = wrapper.qtdFlags;
63 return format("%s (nativeId: %s, this ptr: %s, flags: %b)", this, wrapper.qtdNativeId
64 , cast(void*)this, *cast(ubyte*)&flags);
65 }
66
67 void onWrapperConstructed(QtdObject wrapper)
68 {
69 info("QtdObject constructed: " ~ wrapperToString(wrapper));
70 wrapperCount_++;
71 }
72
73 void onWrapperDestruction(QtdObject wrapper)
74 {
75 info("Entering QtdObject destructor: " ~ wrapperToString(wrapper));
76 }
77
78 void onWrapperDestroyed(QtdObject wrapper)
79 {
80 info("Leaving QtdObject destructor: " ~ wrapperToString(wrapper));
81 wrapperCount_--;
82 }
83
84 void onNativeDeleted(QtdObject wrapper)
85 {
86 info("Native object deleted: " ~ wrapperToString(wrapper));
87 nativeDeletedCount_++;
88 }
89
90 void onWrapperOwnershipChanged(QtdObject wrapper)
91 {
92 info("Wrapper ownership chaged: " ~ wrapperToString(wrapper));
93 }
94
95 void onDeletingWrapperFromNative(void* dId)
96 {
97 info(format("Wrapper deletion initiated from C++ (wrapper id: %s)", dId));
98 }
99
100 @property int wrapperCount()
101 {
102 return wrapperCount_;
103 }
104
105 @property int nativeDeletedCount()
106 {
107 return nativeDeletedCount_;
108 }
109
110 void reset()
111 {
112 wrapperCount_ = 0;
113 nativeDeletedCount_ = 0;
114 }
115
116 static void info(lazy string msg)
117 {
118 message(msg, MessageType.info);
119 }
120
121 static void warning(lazy string msg)
122 {
123 message(msg, MessageType.warning);
124 }
125
126 static void error(lazy string msg)
127 {
128 message(msg, MessageType.error);
129 }
130
131 static void message(lazy string msg, MessageType msgType = MessageType.info)
132 {
133 // The check is deliberately placed here and not at the call sites.
134 // For simplicity, there are only two levels of debug verbosity:
135 // verbose and silent.
136 debug (QtdVerbose)
137 writefln("[QtD %s] %s", msgTypeStrs[msgType], msg);
138 }
139 }
140
141 private shared QtdDebug _qtdDebug;
142 static QtdDebug qtdDebug()
143 {
144 return _qtdDebug;
145 }
146
147 shared static this()
148 {
149 _qtdDebug = new QtdDebug;
150 }
151 }
152