annotate d2/qtd/Debug.d @ 374:681af90e1d53

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