/* PDCurses */12#include <curspriv.h>34/*man-start**************************************************************56debug7-----89### Synopsis1011void traceon(void);12void traceoff(void);13void PDC_debug(const char *, ...);1415### Description1617traceon() and traceoff() toggle the recording of debugging18information to the file "trace". Although not standard, similar19functions are in some other curses implementations.2021PDC_debug() is the function that writes to the file, based on whether22traceon() has been called. It's used from the PDC_LOG() macro.2324The environment variable PDC_TRACE_FLUSH controls whether the trace25file contents are fflushed after each write. The default is not. Set26it to enable this (may affect performance).2728### Portability29X/Open ncurses NetBSD30traceon - - -31traceoff - - -32PDC_debug - - -3334**man-end****************************************************************/3536#include <stdlib.h>37#include <string.h>38#include <sys/types.h>39#include <time.h>4041static bool want_fflush = FALSE;4243void PDC_debug(const char *fmt, ...)44{45va_list args;46char hms[9];47time_t now;4849if (!SP || !SP->dbfp)50return;5152time(&now);53strftime(hms, 9, "%H:%M:%S", localtime(&now));54fprintf(SP->dbfp, "At: %8.8ld - %s ", (long) clock(), hms);5556va_start(args, fmt);57vfprintf(SP->dbfp, fmt, args);58va_end(args);5960/* If you are crashing and losing debugging information, enable this61by setting the environment variable PDC_TRACE_FLUSH. This may62impact performance. */6364if (want_fflush)65fflush(SP->dbfp);6667/* If with PDC_TRACE_FLUSH enabled you are still losing logging in68crashes, you may need to add a platform-dependent mechanism to69flush the OS buffers as well (such as fsync() on POSIX) -- but70expect terrible performance. */71}7273void traceon(void)74{75if (!SP)76return;7778if (SP->dbfp)79fclose(SP->dbfp);8081/* open debug log file append */82SP->dbfp = fopen("trace", "a");83if (!SP->dbfp)84{85fprintf(stderr, "PDC_debug(): Unable to open debug log file\n");86return;87}8889if (getenv("PDC_TRACE_FLUSH"))90want_fflush = TRUE;9192PDC_LOG(("traceon() - called\n"));93}9495void traceoff(void)96{97if (!SP || !SP->dbfp)98return;99100PDC_LOG(("traceoff() - called\n"));101102fclose(SP->dbfp);103SP->dbfp = NULL;104want_fflush = FALSE;105}106107108