From b9f639b5b6563eef44ed607b3bbf0c4865a482b7 Mon Sep 17 00:00:00 2001 From: ellie timoney Date: Mon, 23 Oct 2017 13:18:27 +1100 Subject: [PATCH] debug: add PDC_TRACE_FLUSH env var to toggle aggressive fflush() --- pdcurses/debug.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/pdcurses/debug.c b/pdcurses/debug.c index 8321cdca..14f936e3 100644 --- a/pdcurses/debug.c +++ b/pdcurses/debug.c @@ -31,11 +31,13 @@ debug **man-end****************************************************************/ +#include #include #include #include FILE *pdc_dbfp = NULL; +static bool want_fflush = FALSE; void PDC_debug(const char *fmt, ...) { @@ -53,10 +55,25 @@ void PDC_debug(const char *fmt, ...) va_start(args, fmt); vfprintf(pdc_dbfp, fmt, args); va_end(args); + + /* If you are crashing and losing debugging information, enable this + * by setting the environment variable PDC_TRACE_FLUSH to 1. This may + * impact performance. + */ + if (want_fflush) + fflush(pdc_dbfp); + + /* If with PDC_TRACE_FLUSH enabled you are still losing logging in + * crashes, you may need to add a platform-dependent mechanism to flush + * the OS buffers as well (such as fsync() on POSIX) -- but expect + * terrible performance. + */ } void traceon(void) { + char *env; + if (pdc_dbfp) fclose(pdc_dbfp); @@ -69,6 +86,9 @@ void traceon(void) return; } + if ((env = getenv("PDC_TRACE_FLUSH"))) + want_fflush = atoi(env); + PDC_LOG(("traceon() - called\n")); } @@ -81,4 +101,5 @@ void traceoff(void) fclose(pdc_dbfp); pdc_dbfp = NULL; + want_fflush = FALSE; }