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