Skip to content

Commit

Permalink
debug: hold trace file open while trace is enabled
Browse files Browse the repository at this point in the history
this gives a substantial increase in responsiveness during
traceon sessions compared to opening and closing the file
for every log line
  • Loading branch information
flrl committed Oct 22, 2017
1 parent 19e2ef1 commit d4f10cb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 21 deletions.
4 changes: 2 additions & 2 deletions curspriv.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ typedef struct /* structure for ripped off lines */
#define _DLCHAR 0x15 /* Delete Line char (^U) */

extern WINDOW *pdc_lastscr;
extern bool pdc_trace_on; /* tracing flag */
extern FILE *pdc_dbfp; /* tracing file pointer (NULL = off) */
extern bool pdc_color_started;
extern unsigned long pdc_key_modifiers;
extern MOUSE_STATUS pdc_mouse_status;
Expand Down Expand Up @@ -106,7 +106,7 @@ size_t PDC_wcstombs(char *, const wchar_t *, size_t);
#endif

#ifdef PDCDEBUG
# define PDC_LOG(x) if (pdc_trace_on) PDC_debug x
# define PDC_LOG(x) if (pdc_dbfp) PDC_debug x
#else
# define PDC_LOG(x)
#endif
Expand Down
39 changes: 20 additions & 19 deletions pdcurses/debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,49 +35,50 @@ debug
#include <sys/types.h>
#include <time.h>

bool pdc_trace_on = FALSE;
FILE *pdc_dbfp = NULL;

void PDC_debug(const char *fmt, ...)
{
va_list args;
FILE *dbfp;
char hms[9];
time_t now;

if (!pdc_trace_on)
if (!pdc_dbfp)
return;

/* open debug log file append */

dbfp = fopen("trace", "a");
if (!dbfp)
{
fprintf(stderr,
"PDC_debug(): Unable to open debug log file\n");
return;
}

time(&now);
strftime(hms, 9, "%H:%M:%S", localtime(&now));
fprintf(dbfp, "At: %8.8ld - %s ", (long) clock(), hms);
fprintf(pdc_dbfp, "At: %8.8ld - %s ", (long) clock(), hms);

va_start(args, fmt);
vfprintf(dbfp, fmt, args);
vfprintf(pdc_dbfp, fmt, args);
va_end(args);

fclose(dbfp);
}

void traceon(void)
{
pdc_trace_on = TRUE;
if (pdc_dbfp)
fclose(pdc_dbfp);

/* open debug log file append */
pdc_dbfp = fopen("trace", "a");
if (!pdc_dbfp)
{
fprintf(stderr,
"PDC_debug(): Unable to open debug log file\n");
return;
}

PDC_LOG(("traceon() - called\n"));
}

void traceoff(void)
{
if (!pdc_dbfp)
return;

PDC_LOG(("traceoff() - called\n"));

pdc_trace_on = FALSE;
fclose(pdc_dbfp);
pdc_dbfp = NULL;
}

0 comments on commit d4f10cb

Please sign in to comment.