Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Kitware
GitHub Repository: Kitware/CMake
Path: blob/master/Utilities/cmpdcurses/pdcurses/debug.c
3153 views
1
/* PDCurses */
2
3
#include <curspriv.h>
4
5
/*man-start**************************************************************
6
7
debug
8
-----
9
10
### Synopsis
11
12
void traceon(void);
13
void traceoff(void);
14
void PDC_debug(const char *, ...);
15
16
### Description
17
18
traceon() and traceoff() toggle the recording of debugging
19
information to the file "trace". Although not standard, similar
20
functions are in some other curses implementations.
21
22
PDC_debug() is the function that writes to the file, based on whether
23
traceon() has been called. It's used from the PDC_LOG() macro.
24
25
The environment variable PDC_TRACE_FLUSH controls whether the trace
26
file contents are fflushed after each write. The default is not. Set
27
it to enable this (may affect performance).
28
29
### Portability
30
X/Open ncurses NetBSD
31
traceon - - -
32
traceoff - - -
33
PDC_debug - - -
34
35
**man-end****************************************************************/
36
37
#include <stdlib.h>
38
#include <string.h>
39
#include <sys/types.h>
40
#include <time.h>
41
42
static bool want_fflush = FALSE;
43
44
void PDC_debug(const char *fmt, ...)
45
{
46
va_list args;
47
char hms[9];
48
time_t now;
49
50
if (!SP || !SP->dbfp)
51
return;
52
53
time(&now);
54
strftime(hms, 9, "%H:%M:%S", localtime(&now));
55
fprintf(SP->dbfp, "At: %8.8ld - %s ", (long) clock(), hms);
56
57
va_start(args, fmt);
58
vfprintf(SP->dbfp, fmt, args);
59
va_end(args);
60
61
/* If you are crashing and losing debugging information, enable this
62
by setting the environment variable PDC_TRACE_FLUSH. This may
63
impact performance. */
64
65
if (want_fflush)
66
fflush(SP->dbfp);
67
68
/* If with PDC_TRACE_FLUSH enabled you are still losing logging in
69
crashes, you may need to add a platform-dependent mechanism to
70
flush the OS buffers as well (such as fsync() on POSIX) -- but
71
expect terrible performance. */
72
}
73
74
void traceon(void)
75
{
76
if (!SP)
77
return;
78
79
if (SP->dbfp)
80
fclose(SP->dbfp);
81
82
/* open debug log file append */
83
SP->dbfp = fopen("trace", "a");
84
if (!SP->dbfp)
85
{
86
fprintf(stderr, "PDC_debug(): Unable to open debug log file\n");
87
return;
88
}
89
90
if (getenv("PDC_TRACE_FLUSH"))
91
want_fflush = TRUE;
92
93
PDC_LOG(("traceon() - called\n"));
94
}
95
96
void traceoff(void)
97
{
98
if (!SP || !SP->dbfp)
99
return;
100
101
PDC_LOG(("traceoff() - called\n"));
102
103
fclose(SP->dbfp);
104
SP->dbfp = NULL;
105
want_fflush = FALSE;
106
}
107
108