]> git.ipfire.org Git - thirdparty/squid.git/blob - test-suite/test_tools.cc
Merge from trunk
[thirdparty/squid.git] / test-suite / test_tools.cc
1 /*
2 * $Id: test_tools.cc,v 1.11 2008/02/26 18:52:54 rousskov Exp $
3 */
4
5 // XXX: This file is made of large pieces of src/debug.cc and src/tools.cc
6 // with only a few minor modifications. TODO: redesign or delete.
7
8 #define _SQUID_EXTERNNEW_
9 #include "squid.h"
10 #include <iostream>
11 #include <sstream>
12
13 /* AYJ: the debug stuff here should really be in a stub_debug.cc file for tests to link */
14
15 /* for correct pre-definitions of debug objects */
16 #include "Debug.h"
17
18 FILE *debug_log = NULL;
19
20 void
21 xassert(const char *msg, const char *file, int line)
22 {
23 std::cout << "Assertion failed: (" << msg << ") at " << file << ":" << line << std::endl;
24 exit (1);
25 }
26
27 int Debug::Levels[MAX_DEBUG_SECTIONS];
28 int Debug::level;
29
30 static void
31 _db_print_stderr(const char *format, va_list args);
32
33 void
34 #if STDC_HEADERS
35 _db_print(const char *format,...)
36 {
37 #else
38 _db_print(va_alist)
39 va_dcl {
40 const char *format = NULL;
41 #endif
42
43 LOCAL_ARRAY(char, f, BUFSIZ);
44 va_list args1;
45 #if STDC_HEADERS
46
47 va_list args2;
48 va_list args3;
49 #else
50 #define args2 args1
51 #define args3 args1
52 #endif
53
54 #if STDC_HEADERS
55
56 va_start(args1, format);
57
58 va_start(args2, format);
59
60 va_start(args3, format);
61
62 #else
63
64 format = va_arg(args1, const char *);
65
66 #endif
67
68 snprintf(f, BUFSIZ, "%s| %s",
69 "stub time", //debugLogTime(squid_curtime),
70 format);
71
72 _db_print_stderr(f, args2);
73
74 va_end(args1);
75
76 #if STDC_HEADERS
77
78 va_end(args2);
79
80 va_end(args3);
81
82 #endif
83 }
84
85 static void
86 _db_print_stderr(const char *format, va_list args) {
87 /* FIXME? */
88 // if (opt_debug_stderr < Debug::level)
89
90 if (1 < Debug::level)
91 return;
92
93 vfprintf(stderr, format, args);
94 }
95
96 void
97 fatal_dump(const char *message) {
98 debug (0,0) ("Fatal: %s",message);
99 exit (1);
100 }
101
102 void
103 fatal(const char *message) {
104 debug (0,0) ("Fatal: %s",message);
105 exit (1);
106 }
107
108 /* used by fatalf */
109 static void
110 fatalvf(const char *fmt, va_list args) {
111 static char fatal_str[BUFSIZ];
112 vsnprintf(fatal_str, sizeof(fatal_str), fmt, args);
113 fatal(fatal_str);
114 }
115
116 /* printf-style interface for fatal */
117 #if STDC_HEADERS
118 void
119 fatalf(const char *fmt,...) {
120 va_list args;
121 va_start(args, fmt);
122 #else
123 void
124 fatalf(va_alist)
125 va_dcl {
126 va_list args;
127 const char *fmt = NULL;
128 va_start(args);
129 fmt = va_arg(args, char *);
130 #endif
131
132 fatalvf(fmt, args);
133 va_end(args);
134 }
135
136 void
137 debug_trap(const char *message) {
138 fatal(message);
139 }
140
141 int Debug::TheDepth = 0;
142
143 std::ostream &
144 Debug::getDebugOut() {
145 assert(TheDepth >= 0);
146 ++TheDepth;
147 if (TheDepth > 1) {
148 assert(CurrentDebug);
149 *CurrentDebug << std::endl << "reentrant debuging " << TheDepth << "-{";
150 } else {
151 assert(!CurrentDebug);
152 CurrentDebug = new std::ostringstream();
153 // set default formatting flags
154 CurrentDebug->setf(std::ios::fixed);
155 CurrentDebug->precision(2);
156 }
157 return *CurrentDebug;
158 }
159
160 void
161 Debug::finishDebug() {
162 assert(TheDepth >= 0);
163 assert(CurrentDebug);
164 if (TheDepth > 1) {
165 *CurrentDebug << "}-" << TheDepth << std::endl;
166 } else {
167 assert(TheDepth == 1);
168 _db_print("%s\n", CurrentDebug->str().c_str());
169 delete CurrentDebug;
170 CurrentDebug = NULL;
171 }
172 --TheDepth;
173 }
174
175 void
176 Debug::xassert(const char *msg, const char *file, int line) {
177
178 if (CurrentDebug) {
179 *CurrentDebug << "assertion failed: " << file << ":" << line <<
180 ": \"" << msg << "\"";
181 }
182 abort();
183 }
184
185 std::ostringstream *Debug::CurrentDebug (NULL);
186
187 MemAllocator *dlink_node_pool = NULL;
188
189 dlink_node *
190 dlinkNodeNew() {
191 if (dlink_node_pool == NULL)
192 dlink_node_pool = memPoolCreate("Dlink list nodes", sizeof(dlink_node));
193
194 /* where should we call memPoolDestroy(dlink_node_pool); */
195 return static_cast<dlink_node *>(dlink_node_pool->alloc());
196 }
197
198 /* the node needs to be unlinked FIRST */
199 void
200 dlinkNodeDelete(dlink_node * m) {
201 if (m == NULL)
202 return;
203
204 dlink_node_pool->free(m);
205 }
206
207 void
208 dlinkAdd(void *data, dlink_node * m, dlink_list * list) {
209 m->data = data;
210 m->prev = NULL;
211 m->next = list->head;
212
213 if (list->head)
214 list->head->prev = m;
215
216 list->head = m;
217
218 if (list->tail == NULL)
219 list->tail = m;
220 }
221
222 void
223 dlinkAddAfter(void *data, dlink_node * m, dlink_node * n, dlink_list * list) {
224 m->data = data;
225 m->prev = n;
226 m->next = n->next;
227
228 if (n->next)
229 n->next->prev = m;
230 else {
231 assert(list->tail == n);
232 list->tail = m;
233 }
234
235 n->next = m;
236 }
237
238 void
239 dlinkAddTail(void *data, dlink_node * m, dlink_list * list) {
240 m->data = data;
241 m->next = NULL;
242 m->prev = list->tail;
243
244 if (list->tail)
245 list->tail->next = m;
246
247 list->tail = m;
248
249 if (list->head == NULL)
250 list->head = m;
251 }
252
253 void
254 dlinkDelete(dlink_node * m, dlink_list * list) {
255 if (m->next)
256 m->next->prev = m->prev;
257
258 if (m->prev)
259 m->prev->next = m->next;
260
261 if (m == list->head)
262 list->head = m->next;
263
264 if (m == list->tail)
265 list->tail = m->prev;
266
267 m->next = m->prev = NULL;
268 }
269
270 Ctx
271 ctx_enter(const char *descr) {
272 return 0;
273 }
274
275 void
276 ctx_exit(Ctx ctx) {}