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