Mercurial > ~dholland > hg > tradcpp > index.cgi
diff 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 (2016-09-04) |
parents | 16b4451e34b8 |
children | 3a25180d3a5c |
line wrap: on
line diff
--- a/place.c Sat Dec 05 18:08:24 2015 -0500 +++ b/place.c Sun Sep 04 17:14:42 2016 -0400 @@ -32,6 +32,7 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <errno.h> #include "utils.h" #include "array.h" @@ -52,6 +53,8 @@ static const char *myprogname; +static FILE *debuglogfile; + //////////////////////////////////////////////////////////// // placefiles @@ -284,6 +287,70 @@ } //////////////////////////////////////////////////////////// +// debug logging + +void +debuglog_open(const struct place *p, /*const*/ char *file) +{ + assert(debuglogfile == NULL); + debuglogfile = fopen(file, "w"); + if (debuglogfile == NULL) { + complain(p, "%s: %s", file, strerror(errno)); + die(); + } +} + +void +debuglog_close(void) +{ + if (debuglogfile != NULL) { + fclose(debuglogfile); + debuglogfile = NULL; + } +} + +PF(2, 3) void +debuglog(const struct place *p, const char *fmt, ...) +{ + va_list ap; + + if (debuglogfile == NULL) { + return; + } + + fprintf(debuglogfile, "%s:%u: ", place_getname(p), p->line); + va_start(ap, fmt); + vfprintf(debuglogfile, fmt, ap); + va_end(ap); + fprintf(debuglogfile, "\n"); + fflush(debuglogfile); +} + +PF(3, 4) void +debuglog2(const struct place *p, const struct place *p2, const char *fmt, ...) +{ + va_list ap; + + if (debuglogfile == NULL) { + return; + } + + fprintf(debuglogfile, "%s:%u: ", place_getname(p), p->line); + if (place_samefile(p, p2)) { + fprintf(debuglogfile, "(block began at line %u) ", + p2->line); + } else { + fprintf(debuglogfile, "(block began at %s:%u)", + place_getname(p2), p2->line); + } + va_start(ap, fmt); + vfprintf(debuglogfile, fmt, ap); + va_end(ap); + fprintf(debuglogfile, "\n"); + fflush(debuglogfile); +} + +//////////////////////////////////////////////////////////// // module init and cleanup void