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