ReactOS  0.4.14-dev-614-gbfd8a84
trace.c
Go to the documentation of this file.
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  */
18 char gTraceLBuf[256];
19 int gDebug = 0;
20 
21 extern FTPLibraryInfo gLib;
23 extern char gVersion[], gOS[];
24 extern char gOurDirectoryPath[];
25 
26 
27 
28 /*VARARGS*/
29 void
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
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
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
88 SetDebug(int i)
89 {
90  gDebug = i;
91 } /* SetDebug */
92 
93 
94 
95 
96 void
97 UseTrace(void)
98 {
100  gConn.debugLog = NULL;
102  gConn.errLog = NULL;
103 } /* UseTrace */
104 
105 
106 
107 
108 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 */
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
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));
175 
176  (void) unlink(pathName2);
177  (void) rename(pathName, pathName2);
178 } /* CloseTrace */
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble * u
Definition: glfuncs.h:240
#define vsnprintf
Definition: tif_win32.c:406
_In_ ULONG_PTR _In_ ULONG _Out_ ULONG_PTR * pid
Definition: winddi.h:3835
GLint level
Definition: gl.h:1546
int tm_min
Definition: time.h:78
void CloseTrace(void)
Definition: trace.c:159
int gDebug
Definition: trace.c:19
struct png_info_def **typedef void(__cdecl typeof(png_destroy_read_struct))(struct png_struct_def **
Definition: typeof.h:49
void SetDebug(int i)
Definition: trace.c:88
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
const char * fmt
Definition: wsprintf.c:30
char ourHostName[64]
Definition: ncftp.h:118
#define unlink
Definition: syshdrs.h:54
char * OurDirectoryPath(char *const dst, const size_t siz, const char *const fname)
Definition: util.c:486
#define FOPEN_WRITE_TEXT
Definition: syshdrs.h:83
FTPConnectionInfo gConn
Definition: main.c:37
__u16 time
Definition: mkdosfs.c:366
#define LIBNCFTP_USE_VAR(a)
Definition: ncftp.h:521
FILE * stdout
char gVersion[]
Definition: init.c:22
FTPLogProc errLogProc
Definition: ncftp.h:148
#define sprintf(buf, format,...)
Definition: sprintf.c:55
void FTPInitializeOurHostName(const FTPLIPtr)
Definition: open.c:63
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
#define kTraceFileName
Definition: trace.h:11
#define va_end(ap)
Definition: acmsvcex.h:90
_Check_return_ int __cdecl rename(_In_z_ const char *_OldFilename, _In_z_ const char *_NewFilename)
_Check_return_opt_ _CRTIMP int __cdecl fprintf(_Inout_ FILE *_File, _In_z_ _Printf_format_string_ const char *_Format,...)
FILE * errLog
Definition: ncftp.h:146
smooth NULL
Definition: ftsmooth.c:416
char * va_list
Definition: acmsvcex.h:78
#define UNUSED(x)
Definition: btrfs_drv.h:86
_Check_return_ _CRTIMP int __cdecl _chmod(_In_z_ const char *_Filename, _In_ int _Mode)
__u16 ctime
Definition: mkdosfs.c:362
_CRTIMP int __cdecl getpid(void)
char gTraceLBuf[256]
Definition: trace.c:18
FTPLibraryInfo gLib
Definition: main.c:36
char gOS[]
Definition: version.c:12
FTPLogProc debugLogProc
Definition: ncftp.h:147
_Check_return_opt_ _CRTIMP int __cdecl fclose(_Inout_ FILE *_File)
_Check_return_ _CRTIMP FILE *__cdecl fopen(_In_z_ const char *_Filename, _In_z_ const char *_Mode)
Definition: time.h:76
FILE * debugLog
Definition: ncftp.h:145
_CRTIMP struct tm *__cdecl localtime(const time_t *_Time)
Definition: time.h:424
FILE * gTraceFile
Definition: trace.c:17
_Check_return_ char *__cdecl getenv(_In_z_ const char *_VarName)
int hresult
Definition: ncftp.h:119
char gLibNcFTPVersion[64]
Definition: ftp.c:11
__kernel_time_t time_t
Definition: linux.h:252
int __cdecl vsprintf(char *_Dest, const char *_Format, va_list _Args)
Definition: sprintf.c:733
char gOurDirectoryPath[]
Definition: util.c:17
#define va_start(ap, A)
Definition: acmsvcex.h:91
time_t gTraceTime
Definition: trace.c:16
int tm_sec
Definition: time.h:77
#define msg(x)
Definition: auth_time.c:54
POINT cp
Definition: magnifier.c:59
void int int ULONGLONG int va_list * ap
Definition: winesup.h:32
void UseTrace(void)
Definition: trace.c:97
int tm_hour
Definition: time.h:79
void ErrorHook(const FTPCIPtr UNUSED(cipUnused), char *msg)
Definition: trace.c:66
void Trace(const int level, const char *const fmt,...)
Definition: trace.c:30
_Check_return_opt_ _CRTIMP int __cdecl setvbuf(_Inout_ FILE *_File, _Inout_updates_opt_z_(_Size) char *_Buf, _In_ int _Mode, _In_ size_t _Size)
void OpenTrace(void)
Definition: trace.c:109
void DebugHook(const FTPCIPtr UNUSED(cipUnused), char *msg)
Definition: trace.c:78
Definition: dsound.c:943
#define _IOLBF
Definition: stdio.h:129