comparison place.c @ 199:1d2bad7151f9

Add a -debuglog option to send an execution trace to a file. Intended to be used when debugging imake templates and other complex input, not for debugging tradcpp itself.
author David A. Holland
date Sun, 04 Sep 2016 17:14:42 -0400
parents 16b4451e34b8
children 3a25180d3a5c
comparison
equal deleted inserted replaced
198:4158b974e23f 199:1d2bad7151f9
30 #include <assert.h> 30 #include <assert.h>
31 #include <stdarg.h> 31 #include <stdarg.h>
32 #include <stdio.h> 32 #include <stdio.h>
33 #include <stdlib.h> 33 #include <stdlib.h>
34 #include <string.h> 34 #include <string.h>
35 #include <errno.h>
35 36
36 #include "utils.h" 37 #include "utils.h"
37 #include "array.h" 38 #include "array.h"
38 #include "place.h" 39 #include "place.h"
39 40
50 static struct placefilearray placefiles; 51 static struct placefilearray placefiles;
51 static bool overall_failure; 52 static bool overall_failure;
52 53
53 static const char *myprogname; 54 static const char *myprogname;
54 55
56 static FILE *debuglogfile;
57
55 //////////////////////////////////////////////////////////// 58 ////////////////////////////////////////////////////////////
56 // placefiles 59 // placefiles
57 60
58 static 61 static
59 struct placefile * 62 struct placefile *
282 { 285 {
283 return overall_failure; 286 return overall_failure;
284 } 287 }
285 288
286 //////////////////////////////////////////////////////////// 289 ////////////////////////////////////////////////////////////
290 // debug logging
291
292 void
293 debuglog_open(const struct place *p, /*const*/ char *file)
294 {
295 assert(debuglogfile == NULL);
296 debuglogfile = fopen(file, "w");
297 if (debuglogfile == NULL) {
298 complain(p, "%s: %s", file, strerror(errno));
299 die();
300 }
301 }
302
303 void
304 debuglog_close(void)
305 {
306 if (debuglogfile != NULL) {
307 fclose(debuglogfile);
308 debuglogfile = NULL;
309 }
310 }
311
312 PF(2, 3) void
313 debuglog(const struct place *p, const char *fmt, ...)
314 {
315 va_list ap;
316
317 if (debuglogfile == NULL) {
318 return;
319 }
320
321 fprintf(debuglogfile, "%s:%u: ", place_getname(p), p->line);
322 va_start(ap, fmt);
323 vfprintf(debuglogfile, fmt, ap);
324 va_end(ap);
325 fprintf(debuglogfile, "\n");
326 fflush(debuglogfile);
327 }
328
329 PF(3, 4) void
330 debuglog2(const struct place *p, const struct place *p2, const char *fmt, ...)
331 {
332 va_list ap;
333
334 if (debuglogfile == NULL) {
335 return;
336 }
337
338 fprintf(debuglogfile, "%s:%u: ", place_getname(p), p->line);
339 if (place_samefile(p, p2)) {
340 fprintf(debuglogfile, "(block began at line %u) ",
341 p2->line);
342 } else {
343 fprintf(debuglogfile, "(block began at %s:%u)",
344 place_getname(p2), p2->line);
345 }
346 va_start(ap, fmt);
347 vfprintf(debuglogfile, fmt, ap);
348 va_end(ap);
349 fprintf(debuglogfile, "\n");
350 fflush(debuglogfile);
351 }
352
353 ////////////////////////////////////////////////////////////
287 // module init and cleanup 354 // module init and cleanup
288 355
289 void 356 void
290 place_init(void) 357 place_init(void)
291 { 358 {