1 /* trace.c
2 *
3 * Copyright (c) 1992-2001 by Mike Gleason.
4 * All rights reserved.
5 *
6 */
7
8 #include "syshdrs.h"
9
10 #include "trace.h"
11 #include "util.h"
12
13 /* Saves their session in a ~/.ncftp/trace file.
14 * This is nice for me when I need to diagnose problems.
15 */
16 time_t gTraceTime;
17 FILE *gTraceFile = NULL;
18 char gTraceLBuf[256];
19 int gDebug = 0;
20
21 extern FTPLibraryInfo gLib;
22 extern FTPConnectionInfo gConn;
23 extern char gVersion[], gOS[];
24 extern char gOurDirectoryPath[];
25
26
27
28 /*VARARGS*/
29 void
Trace(const int level,const char * const fmt,...)30 Trace(const int level, const char *const fmt, ...)
31 {
32 va_list ap;
33 char buf[512];
34 struct tm *ltp;
35
36 if ((gDebug >= level) || (level > 8)) {
37 va_start(ap, fmt);
38 #ifdef HAVE_VSNPRINTF
39 (void) vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
40 buf[sizeof(buf) - 1] = '\0';
41 #else
42 (void) vsprintf(buf, fmt, ap);
43 #endif
44 va_end(ap);
45
46 (void) time(&gTraceTime);
47 ltp = localtime(&gTraceTime);
48 if ((gTraceFile != NULL) && (ltp != NULL)) {
49 (void) fprintf(gTraceFile , "%02d:%02d:%02d %s",
50 ltp->tm_hour,
51 ltp->tm_min,
52 ltp->tm_sec,
53 buf
54 );
55 }
56 if (gDebug > level) {
57 (void) fprintf(stdout, "%s", buf);
58 }
59 }
60 } /* Trace */
61
62
63
64
65 void
ErrorHook(const FTPCIPtr UNUSED (cipUnused),char * msg)66 ErrorHook(const FTPCIPtr UNUSED(cipUnused), char *msg)
67 {
68 LIBNCFTP_USE_VAR(cipUnused); /* shut up gcc */
69
70 /* Will also get Trace'd. */
71 (void) fprintf(stdout, "%s", msg);
72 } /* ErrorHook */
73
74
75
76
77 void
DebugHook(const FTPCIPtr UNUSED (cipUnused),char * msg)78 DebugHook(const FTPCIPtr UNUSED(cipUnused), char *msg)
79 {
80 LIBNCFTP_USE_VAR(cipUnused); /* shut up gcc */
81 Trace(0, "%s", msg);
82 } /* DebugHook */
83
84
85
86
87 void
SetDebug(int i)88 SetDebug(int i)
89 {
90 gDebug = i;
91 } /* SetDebug */
92
93
94
95
96 void
UseTrace(void)97 UseTrace(void)
98 {
99 gConn.debugLogProc = DebugHook;
100 gConn.debugLog = NULL;
101 gConn.errLogProc = ErrorHook;
102 gConn.errLog = NULL;
103 } /* UseTrace */
104
105
106
107
108 void
OpenTrace(void)109 OpenTrace(void)
110 {
111 FILE *fp;
112 char pathName[256];
113 char tName[32];
114 int pid;
115 const char *cp;
116 #if defined(HAVE_SYS_UTSNAME_H) && defined(HAVE_UNAME)
117 struct utsname u;
118 #endif
119
120 if (gOurDirectoryPath[0] == '\0')
121 return; /* Don't create in root directory. */
122
123 (void) sprintf(tName, "trace.%u", (unsigned int) (pid = getpid()));
124 (void) OurDirectoryPath(pathName, sizeof(pathName), tName);
125
126 fp = fopen(pathName, FOPEN_WRITE_TEXT);
127 if (fp != NULL) {
128 (void) _chmod(pathName, 00600);
129 #ifdef HAVE_SETVBUF
130 (void) setvbuf(fp, gTraceLBuf, _IOLBF, sizeof(gTraceLBuf));
131 #endif /* HAVE_SETVBUF */
132 /* Opened the trace file. */
133 (void) time(&gTraceTime);
134 (void) fprintf(fp, "SESSION STARTED at: %s", ctime(&gTraceTime));
135 (void) fprintf(fp, " Program Version: %s\n", gVersion + 5);
136 (void) fprintf(fp, " Library Version: %s\n", gLibNcFTPVersion + 5);
137 (void) fprintf(fp, " Process ID: %u\n", pid);
138 if (gOS[0] != '\0')
139 (void) fprintf(fp, " Platform: %s\n", gOS);
140 #if defined(HAVE_SYS_UTSNAME_H) && defined(HAVE_UNAME)
141 if (uname(&u) == 0) {
142 (void) fprintf(fp, " Uname: %.63s|%.63s|%.63s|%.63s|%.63s\r\n", u.sysname, u.nodename, u.release, u.version, u.machine);
143 }
144 #endif /* UNAME */
145 FTPInitializeOurHostName(&gLib);
146 (void) fprintf(fp, " Hostname: %s (rc=%d)\n", gLib.ourHostName, gLib.hresult);
147 cp = (const char *) getenv("TERM");
148 if (cp == NULL)
149 cp = "unknown?";
150 (void) fprintf(fp, " Terminal: %s\n", cp);
151 gTraceFile = fp;
152 }
153 } /* OpenTrace */
154
155
156
157
158 void
CloseTrace(void)159 CloseTrace(void)
160 {
161 char pathName[256];
162 char pathName2[256];
163 char tName[32];
164
165 if ((gOurDirectoryPath[0] == '\0') || (gTraceFile == NULL))
166 return;
167
168 (void) sprintf(tName, "trace.%u", (unsigned int) getpid());
169 (void) OurDirectoryPath(pathName, sizeof(pathName), tName);
170 (void) OurDirectoryPath(pathName2, sizeof(pathName2), kTraceFileName);
171
172 (void) time(&gTraceTime);
173 (void) fprintf(gTraceFile, "SESSION ENDED at: %s", ctime(&gTraceTime));
174 (void) fclose(gTraceFile);
175
176 (void) unlink(pathName2);
177 (void) rename(pathName, pathName2);
178 } /* CloseTrace */
179