KVIrc 5.2.6
Developer APIs
kvi_debug.h
Go to the documentation of this file.
1#ifndef _KVI_DEBUG_H_
2#define _KVI_DEBUG_H_
3//=============================================================================
4//
5// File : kvi_debug.h
6// Creation date : Fri Mar 19 1999 03:10:39 CEST by Szymon Stefanek
7//
8// This file is part of the KVIrc IRC client distribution
9// Copyright (C) 1999-2010 Szymon Stefanek (pragma at kvirc dot net)
10//
11// This program is FREE software. You can redistribute it and/or
12// modify it under the terms of the GNU General Public License
13// as published by the Free Software Foundation; either version 2
14// of the License, or (at your option) any later version.
15//
16// This program is distributed in the HOPE that it will be USEFUL,
17// but WITHOUT ANY WARRANTY; without even the implied warranty of
18// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19// See the GNU General Public License for more details.
20//
21// You should have received a copy of the GNU General Public License
22// along with this program. If not, write to the Free Software Foundation,
23// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24//
25//=============================================================================
26
27#include <QtGlobal>
28
29#include "kvi_sysconfig.h"
30
36
37#include <stdlib.h> // abort
38
39#ifdef __GNUC__
40
41#define kvi_debug(fmt, arg...) qDebug(fmt, ##arg)
42#define kvi_warning(fmt, arg...) qWarning(fmt, ##arg)
43#define kvi_fatal(fmt, arg...) \
44 do \
45 { \
46 qFatal(fmt, ##arg); \
47 abort(); \
48 } while(0)
49#define KVI_PRETTY_FUNCTION __PRETTY_FUNCTION__
50
51#else
52
53// assume MSVC
54
55#define kvi_debug(fmt, ...) qDebug(fmt, __VA_ARGS__)
56#define kvi_warning(fmt, ...) qWarning(fmt, __VA_ARGS__)
57#define kvi_fatal(fmt, ...) \
58 do \
59 { \
60 qFatal(fmt, __VA_ARGS__); \
61 abort(); \
62 } while(0)
63#define KVI_PRETTY_FUNCTION __FUNCTION__
64
65#endif
66
67#ifdef COMPILE_DEBUG_MODE
68
69#define KVI_ASSERT(condition) \
70 do \
71 { \
72 if(!(condition)) \
73 qFatal("[ASSERT FAILED] (" #condition ") in %s at %s:%u", KVI_PRETTY_FUNCTION, __FILE__, __LINE__); \
74 } while(0)
75
76#define KVI_ASSERT_MSG(condition, message) \
77 do \
78 { \
79 if(!(condition)) \
80 { \
81 qFatal("[ASSERT FAILED] (" #condition ") in %s at %s:%u", KVI_PRETTY_FUNCTION, __FILE__, __LINE__); \
82 qFatal("[ASSERT FAILED] " message); \
83 } \
84 } while(0)
85
86#include "KviDebugContext.h"
87
88// The following two macros are used to create unique variable names
89// by the means of the __LINE__ builtin macro.
90// The ## token paste operator must be called inside a macro and must
91// precede a macro parameter. This is why we can't use directly
92//
93// #define UNIQUEVARIABLE int name ## __LINE__
94//
95// We need something like
96//
97// #define PASTE(x,y) x ## y
98// #define UNIQUEVARIABLE int PASTE(x,__LINE__)
99//
100// But this doesn't work since the specification of the token pasting operator is
101//
102// "If a formal parameter in a macro definition is preceded or followed by the token-pasting operator,
103// the formal parameter is immediately replaced by the __unexpanded__ actual argument. Macro expansion
104// is __not performed__ on the argument prior to replacement."
105//
106// So to actually have __LINE__ expanded we need another level of indirection
107//
108// #define PASTE(x,y) x ## y
109// #define EXPAND_Y_AND_THEN_PASTE(x,y) PASTE(x,y)
110// #define UNIQUEVARIABLE int EXPAND_Y_AND_THEN_PASTE(x,__LINE__)
111
112#define KVI_TRACE_HACK_TOKENPASTE_2(x, y) x##y
113#define KVI_TRACE_HACK_TOKENPASTE_1(x, y) KVI_TRACE_HACK_TOKENPASTE_2(x, y)
114
115#ifdef __GNUC__
116#define KVI_TRACE_FUNCTION \
117 KviDebugContext KVI_TRACE_HACK_TOKENPASTE_1(ctx, __LINE__)(__PRETTY_FUNCTION__)
118
119#define KVI_TRACE_BLOCK(_szBlockDescription) \
120 KviDebugContext KVI_TRACE_HACK_TOKENPASTE_1(ctx, __LINE__)("%s - %s", __PRETTY_FUNCTION__, _szBlockDescription)
121
122#define KVI_TRACE(_szFmt, arg...) KviDebugContext::trace(_szFmt, ##arg)
123
124#else
125#define KVI_TRACE_FUNCTION \
126 KviDebugContext KVI_TRACE_HACK_TOKENPASTE_1(ctx, __LINE__)(__FUNCTION__)
127
128#define KVI_TRACE_BLOCK(_szBlockDescription) \
129 KviDebugContext KVI_TRACE_HACK_TOKENPASTE_1(ctx, __LINE__)("%s - %s", __FUNCTION__, _szBlockDescription)
130
131#define KVI_TRACE(_szFmt, ...) KviDebugContext::trace(_szFmt, __VA_ARGS__)
132
133#endif
134
135#else
136
137#define KVI_ASSERT(condition) \
138 do \
139 { \
140 } while(0)
141#define KVI_ASSERT_MSG(condition, message) \
142 do \
143 { \
144 } while(0)
145
146#define KVI_TRACE_FUNCTION \
147 do \
148 { \
149 } while(0)
150
151#define KVI_TRACE_BLOCK(_szBlockDescription) \
152 do \
153 { \
154 } while(0)
155
156#ifdef __GNUC__
157#define KVI_TRACE(_szFmt, arg...) \
158 do \
159 { \
160 } while(0)
161#else
162#define KVI_TRACE(_szFmt, ...) \
163 do \
164 { \
165 } while(0)
166#endif
167
168#endif
169
170#endif //_KVI_DEBUG_H_
Debug context.