]> git.ipfire.org Git - thirdparty/squid.git/blob - test-suite/test_tools.cc
Author: Francesco Chemolli <kinkie@squid-cache.org>
[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 #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 _db_print(const char *format,...)
35 {
36 LOCAL_ARRAY(char, f, BUFSIZ);
37 va_list args1;
38 va_list args2;
39 va_list args3;
40
41 va_start(args1, format);
42 va_start(args2, format);
43 va_start(args3, format);
44
45 snprintf(f, BUFSIZ, "%s| %s",
46 "stub time", //debugLogTime(squid_curtime),
47 format);
48
49 _db_print_stderr(f, args2);
50
51 va_end(args1);
52 va_end(args2);
53 va_end(args3);
54 }
55
56 static void
57 _db_print_stderr(const char *format, va_list args) {
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 debug (0,0) ("Fatal: %s",message);
70 exit (1);
71 }
72
73 void
74 fatal(const char *message) {
75 debug (0,0) ("Fatal: %s",message);
76 exit (1);
77 }
78
79 /* used by fatalf */
80 static void
81 fatalvf(const char *fmt, va_list args) {
82 static char fatal_str[BUFSIZ];
83 vsnprintf(fatal_str, sizeof(fatal_str), fmt, args);
84 fatal(fatal_str);
85 }
86
87 /* printf-style interface for fatal */
88 void
89 fatalf(const char *fmt,...) {
90 va_list args;
91 va_start(args, fmt);
92 fatalvf(fmt, args);
93 va_end(args);
94 }
95
96 void
97 debug_trap(const char *message) {
98 fatal(message);
99 }
100
101 int Debug::TheDepth = 0;
102
103 std::ostream &
104 Debug::getDebugOut() {
105 assert(TheDepth >= 0);
106 ++TheDepth;
107 if (TheDepth > 1) {
108 assert(CurrentDebug);
109 *CurrentDebug << std::endl << "reentrant debuging " << TheDepth << "-{";
110 } else {
111 assert(!CurrentDebug);
112 CurrentDebug = new std::ostringstream();
113 // set default formatting flags
114 CurrentDebug->setf(std::ios::fixed);
115 CurrentDebug->precision(2);
116 }
117 return *CurrentDebug;
118 }
119
120 void
121 Debug::finishDebug() {
122 assert(TheDepth >= 0);
123 assert(CurrentDebug);
124 if (TheDepth > 1) {
125 *CurrentDebug << "}-" << TheDepth << std::endl;
126 } else {
127 assert(TheDepth == 1);
128 _db_print("%s\n", CurrentDebug->str().c_str());
129 delete CurrentDebug;
130 CurrentDebug = NULL;
131 }
132 --TheDepth;
133 }
134
135 void
136 Debug::xassert(const char *msg, const char *file, int line) {
137
138 if (CurrentDebug) {
139 *CurrentDebug << "assertion failed: " << file << ":" << line <<
140 ": \"" << msg << "\"";
141 }
142 abort();
143 }
144
145 std::ostringstream *Debug::CurrentDebug (NULL);
146
147 MemAllocator *dlink_node_pool = NULL;
148
149 dlink_node *
150 dlinkNodeNew() {
151 if (dlink_node_pool == NULL)
152 dlink_node_pool = memPoolCreate("Dlink list nodes", sizeof(dlink_node));
153
154 /* where should we call memPoolDestroy(dlink_node_pool); */
155 return static_cast<dlink_node *>(dlink_node_pool->alloc());
156 }
157
158 /* the node needs to be unlinked FIRST */
159 void
160 dlinkNodeDelete(dlink_node * m) {
161 if (m == NULL)
162 return;
163
164 dlink_node_pool->free(m);
165 }
166
167 void
168 dlinkAdd(void *data, dlink_node * m, dlink_list * list) {
169 m->data = data;
170 m->prev = NULL;
171 m->next = list->head;
172
173 if (list->head)
174 list->head->prev = m;
175
176 list->head = m;
177
178 if (list->tail == NULL)
179 list->tail = m;
180 }
181
182 void
183 dlinkAddAfter(void *data, dlink_node * m, dlink_node * n, dlink_list * list) {
184 m->data = data;
185 m->prev = n;
186 m->next = n->next;
187
188 if (n->next)
189 n->next->prev = m;
190 else {
191 assert(list->tail == n);
192 list->tail = m;
193 }
194
195 n->next = m;
196 }
197
198 void
199 dlinkAddTail(void *data, dlink_node * m, dlink_list * list) {
200 m->data = data;
201 m->next = NULL;
202 m->prev = list->tail;
203
204 if (list->tail)
205 list->tail->next = m;
206
207 list->tail = m;
208
209 if (list->head == NULL)
210 list->head = m;
211 }
212
213 void
214 dlinkDelete(dlink_node * m, dlink_list * list) {
215 if (m->next)
216 m->next->prev = m->prev;
217
218 if (m->prev)
219 m->prev->next = m->next;
220
221 if (m == list->head)
222 list->head = m->next;
223
224 if (m == list->tail)
225 list->tail = m->prev;
226
227 m->next = m->prev = NULL;
228 }
229
230 Ctx
231 ctx_enter(const char *descr) {
232 return 0;
233 }
234
235 void
236 ctx_exit(Ctx ctx) {}