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