Debian Codegraph @

src/tests/pandokia_fct.h

raw symbols SCIP
1 /*
2 * A Pandokia log file writer for FCTX
3 *
4 * See the fctx runner documentation for examples.
5 *
6 * BUG: the pipe handling deadlocks if the test writes too much to
7 * the pipe. You can reproduce this bug with the JUnit/XML reporter.
8 * Maybe fix it to use tmp files.
9 */
10
11 /*
12 * Guard against multiple inclusion; also, use this to skip if FCT has
13 * pandokia support built in someday
14 */
15 #ifndef FCT_PANDOKIA_LOGGER
16 #define FCT_PANDOKIA_LOGGER
17
18 /*
19 * Get fct.h if the user has not already included it. This means you
20 * can use a newer fctx by including it before pandokia_fct.h, but you
21 * can have the default provided by pandokia.
22 */
23 #ifndef FCT_BGN
24 #include "cextern/fct.h"
25 #endif
26
27 /*
28 * Check for one or more compatible versions of fctx
29 */
30 #if (FCT_VERSION_MAJOR == 1) && (FCT_VERSION_MINOR == 6)
31 #define PANDOKIA_FCTX_INTERFACE_OK
32 #endif
33
34 /*
35 * warn if not a known version, but continue anyway
36 */
37 #ifndef PANDOKIA_FCTX_INTERFACE_OK
38 #warning "FCTX version is not one that pandokia.h is tested with."
39 #endif
40
41 #ifdef WIN32
42 #include <time.h>
43 #else
44 #include <sys/time.h>
45 #endif
46
47 /*
48 * each logger has to have one of these.
49 */
50 struct _pandokia_logger {
51 _fct_logger_head;
52 char *pdk_log_name;
53 FILE *pdk_log;
54 char *pdk_prefix;
55 char *pdk_file;
56 char *pdk_basename;
57 };
58
59 /*
60 *
61 */
62 void
63 log_name(struct _pandokia_logger *l, fct_logger_evt_t const *e)
64 {
65 fprintf(l->pdk_log, "test_name=");
66 if (l->pdk_prefix[0]) {
67 fprintf(l->pdk_log, "%s", l->pdk_prefix);
68 }
69 fprintf(l->pdk_log, "%s.%s\n", l->pdk_basename, fct_test__name(e->test));
70 }
71
72 /*
73 * happens before the start of the test:
74 */
75 void
76 pandokia_test_start(fct_logger_i *li, fct_logger_evt_t const *e)
77 {
78 struct timeval t;
79 struct _pandokia_logger *l = (struct _pandokia_logger *) li;
80 char *d;
81 /* log test name */
82 log_name(l, e);
83 /* location of the test -- well, close enough to get started finding it anyway */
84 d = getenv("PDK_DIRECTORY");
85 if (!d) {
86 d = "";
87 }
88 fprintf(l->pdk_log, "location=%s/%s\n", d, l->pdk_file);
89 /* log start time */
90 gettimeofday(&t, NULL);
91 fprintf(l->pdk_log, "start_time=%ld.%06d\n", (long) t.tv_sec, (int) t.tv_usec);
92 /* begin capturing stdout/stderr */
93 FCT_SWITCH_STDOUT_TO_BUFFER();
94 FCT_SWITCH_STDERR_TO_BUFFER();
95 /*
96 * Flush log file so we have some clues if this test core dumps.
97 */
98 fflush(l->pdk_log);
99 }
100
101 /*
102 * happens after the end of a test:
103 */
104 void
105 pandokia_test_end(fct_logger_i *li, fct_logger_evt_t const *e)
106 {
107 struct _pandokia_logger *l = (struct _pandokia_logger *) li;
108 struct timeval end_time;
109 int read_length, x, c;
110 char std_buffer[16384];
111
112 /*
113 * the end time of the test
114 */
115 gettimeofday(&end_time, NULL);
116 fprintf(l->pdk_log, "end_time=%ld.%06d\n", (long) end_time.tv_sec, (int) end_time.tv_usec);
117
118 /*
119 * fctx only seems to understand pass/fail as test status; hard to do more in C.
120 */
121 fprintf(l->pdk_log, "status=%c\n", fct_test__is_pass(e->test) ? 'P' : 'F');
122
123 /*
124 * stop capturing stdout/stderr, gather it into the log
125 */
126 FCT_SWITCH_STDOUT_TO_STDOUT();
127 FCT_SWITCH_STDERR_TO_STDERR();
128
129 fprintf(l->pdk_log, "log:\n.");
130 while ((read_length = _fct_read(fct_stdout_pipe[0], std_buffer, sizeof(std_buffer))) > 0) {
131 for (x = 0; x < read_length; x++) {
132 c = std_buffer[x];
133 fputc(c, l->pdk_log);
134 if (c == '\n') {
135 fputc('.', l->pdk_log);
136 }
137 }
138 }
139 fprintf(l->pdk_log, "\n\n");
140
141 /*
142 * end of record
143 */
144 fprintf(l->pdk_log, "END\n\n");
145
146 /*
147 * Flush log file so it is consistent in case the next test core dumps.
148 */
149 fflush(l->pdk_log);
150 }
151
152 /*
153 * invoked when a test is skipped
154 */
155 void
156 pandokia_skip(fct_logger_i *li, fct_logger_evt_t const *e)
157 {
158 struct _pandokia_logger *l = (struct _pandokia_logger *) li;
159 log_name(l, e);
160 fprintf(l->pdk_log, "status=D\nEND\n\n");
161 }
162
163 /*
164 * pandokia_logger_object serves two purposes:
165 *
166 * - a flag that indicates we are running in pandokia
167 * - a shortcut to the actual pandokia data
168 *
169 * Otherwise, you would have to walk some fairly complicated data
170 * structures inside fctx to get to this single instance.
171 */
172 struct _pandokia_logger *pandokia_logger_object = NULL;
173
174 /*
175 * creates/initializes a logger record - once before any test runs
176 */
177
178 fct_logger_i *
179 pandokia_logger(void)
180 {
181 struct _pandokia_logger *l =
182 (struct _pandokia_logger *) calloc(1, sizeof(struct _pandokia_logger));
183 if (l == NULL) {
184 return NULL;
185 }
186 fct_logger__init((fct_logger_i *) l);
187 l->vtable.on_test_skip = pandokia_skip;
188 l->vtable.on_test_start = pandokia_test_start;
189 l->vtable.on_test_end = pandokia_test_end;
190 /*
191 * do we need to do this? I say just abandon the allocated memory at the end.
192 * l->vtable.on_delete = fct_pandokia_logger__on_delete;
193 */
194
195 /* name of pandokia log file */
196 l->pdk_log_name = getenv("PDK_LOG");
197 if (!l->pdk_log_name) {
198 l->pdk_log_name = "PDK_LOG";
199 }
200
201 /* open pandokia log file */
202 l->pdk_log = fopen(l->pdk_log_name, "a");
203 fprintf(l->pdk_log, "\n\n");
204
205 /* collect prefix to put on test names */
206 l->pdk_prefix = getenv("PDK_TESTPREFIX");
207 if (!l->pdk_prefix) {
208 l->pdk_prefix = "";
209 }
210
211 /* shortcut to the object for pandokia-aware code */
212 pandokia_logger_object = l;
213
214 /* the name of the file that pandokia ran */
215 l->pdk_file = getenv("PDK_FILE");
216 if (l->pdk_file == NULL) {
217 l->pdk_file = "";
218 }
219
220 /* basename of the file that pandokia ran. */
221 l->pdk_basename = strdup(l->pdk_file); /* leaked */
222 assert(l->pdk_basename);
223 {
224 char *s = strrchr(l->pdk_basename, '.');
225 if (s) {
226 *s = 0;
227 }
228 }
229
230 return (fct_logger_i *) l;
231 }
232
233 /*
234 * If we are running in Pandokia, arrange for the default logger to be
235 * the pandokia logger. We do this by taking over slot 0 in the
236 * FCT_LOGGER_TYPES table. So, a user who has to use the "maker" test
237 * runner does not have to remember to say "./a.out --logger pdk" -- they
238 * just say "./a.out".
239 *
240 * We also implement a standard custom logger so that "a.out --logger pdk"
241 * works when the user asks for it explicitly from outside pdkrun.
242 *
243 */
244
245 static void
246 pandokia_intercept_logger(void)
247 {
248 char *s;
249 /*
250 * presence of PDK_FILE is a proxy for running from pandokia.
251 */
252 s = getenv("PDK_FILE");
253 if (!s) {
254 return;
255 }
256
257 // FCT_LOGGER_TYPES[0].name = "pdk";
258 FCT_LOGGER_TYPES[0].logger_new_fn = (fct_logger_new_fn) pandokia_logger;
259 FCT_LOGGER_TYPES[0].desc = "Pandokia log files";
260 }
261
262 /*
263 * Here how you add a custom logger taken from the fctx examples.
264 */
265 static fct_logger_types_t custlogs[] = {
266 {"pdk", (fct_logger_new_fn) pandokia_logger, "write pandokia log files"}, {NULL, NULL, NULL}};
267
268 /*
269 * Replace FCT_BGN_FN with our own modified FCT_BGN_FN in order to install
270 * our custom logger. pandokia_intercept_logger() implicitly does
271 * --logger pdk if it sees the pandokia environment set up.
272 */
273
274 #undef FCT_BGN_FN
275 #define FCT_BGN_FN(_FNNAME_) \
276 int _FNNAME_(int argc, char *argv[]) \
277 { \
278 FCT_INIT(argc, argv) \
279 fctlog_install(custlogs); \
280 pandokia_intercept_logger();
281
282 /*
283 * These are the macros suggested when using custom loggers. I find
284 * that using different macros is not really a good idea because tests
285 * don't switch between custom and non-custom environments well. I
286 * don't recommend these any more, but they are here for backward
287 * compatibility.
288 */
289 #define CL_FCT_BGN() FCT_BGN()
290 #define CL_FCT_END() FCT_END()
291
292 /*
293 * attributes
294 */
295 void
296 pandokia_attr(char *kind, char *name, char *value)
297 {
298 if (pandokia_logger_object) {
299 char *s;
300 FILE *f;
301 f = pandokia_logger_object->pdk_log;
302 fprintf(f, "%s_%s=", kind, name);
303 for (s = value; *s; s++) {
304 if (*s != '\n') {
305 fputc(*s, f);
306 }
307 }
308 fputc('\n', f);
309 }
310 }
311
312 void
313 pandokia_attr_double(char *kind, char *name, double value)
314 {
315 if (pandokia_logger_object) {
316 fprintf(pandokia_logger_object->pdk_log, "%s_%s=%f\n", kind, name, value);
317 }
318 }
319
320 void
321 pandokia_attr_int(char *kind, char *name, int value)
322 {
323 if (pandokia_logger_object) {
324 fprintf(pandokia_logger_object->pdk_log, "%s_%s=%d\n", kind, name, value);
325 }
326 }
327
328 /*
329 *
330 * Here is our support for okfiles
331 */
332
333 FILE *pandokia_okfile_fp = NULL; /* open okfile */
334 char const *pandokia_current_test = NULL; /* name of the last test we saw */
335
336 #define pandokia_okfile(filename) pandokia_okfile_real(fctkern_ptr__, filename)
337 /*
338 * fctkern_ptr__ is a critical data structure; unfortunately,
339 * it is also a local variable hidden inside main, so we use this
340 * macro to sneak it into our function.
341 */
342
343 void
344 pandokia_okfile_real(fctkern_t *fctkern_ptr__, char *filename)
345 {
346 /*
347 * if the last test name we saw is not EQ to the one we seem to
348 * be working on now, then we are on a new test and it is time
349 * to open a new okfile.
350 */
351 if (fctkern_ptr__->ns.curr_test_name != pandokia_current_test) {
352 int n;
353 char *okfile_name;
354 pandokia_current_test = fctkern_ptr__->ns.curr_test_name;
355
356 if (pandokia_okfile_fp) {
357 fclose(pandokia_okfile_fp);
358 }
359
360 /*
361 * generate the okfile name
362 * pdk_basename is basename(PDK_FILE)
363 * pandokia_current_test is the name of the test we are currently running
364 * 20 is clearly more bytes than we need for the constant part
365 */
366 n = strlen(pandokia_logger_object->pdk_basename) + strlen(pandokia_current_test) + 20;
367 okfile_name = malloc(n); /* leaked */
368 snprintf(
369 okfile_name, n, "%s.%s.okfile", pandokia_logger_object->pdk_basename,
370 pandokia_current_test);
371
372 pandokia_okfile_fp = fopen(okfile_name, "w");
373
374 /*
375 * log the TDA about the okfile
376 */
377 pandokia_attr("tda", "_okfile", okfile_name);
378
379 /* */
380 free(okfile_name);
381 }
382
383 /*
384 * okfile is lines of the form
385 * output_file reference_file \n
386 * relative to the directory the okfile is in.
387 */
388 fprintf(pandokia_okfile_fp, "%s ref/%s\n", filename, filename);
389 fflush(pandokia_okfile_fp);
390 }
391
392 #endif