ReactOS  0.4.14-dev-50-g13bb5e2
log.c
Go to the documentation of this file.
1 /* log.c
2  *
3  * Copyright (c) 1992-2001 by Mike Gleason.
4  * All rights reserved.
5  *
6  */
7 
8 #include "syshdrs.h"
9 
10 #include "util.h"
11 #include "log.h"
12 
13 extern int gMaxLogSize;
14 char gLogFileName[256];
15 
16 extern char gOurDirectoryPath[];
17 
18 
19 void
20 InitLog(void)
21 {
23 } /* InitLog */
24 
25 
26 
27 void
28 LogXfer(const char *const mode, const char *const url)
29 {
30  FILE *fp;
31 
32  if (gMaxLogSize == 0)
33  return; /* Don't log */
34 
36  if (fp != NULL) {
37  (void) fprintf(fp, " %s %s\n", mode, url);
38  (void) fclose(fp);
39  }
40 } /* LogOpen */
41 
42 
43 
44 void
45 LogOpen(const char *const host)
46 {
47  time_t now;
48  FILE *fp;
49 
50  if (gMaxLogSize == 0)
51  return; /* Don't log */
52 
53  time(&now);
55  if (fp != NULL) {
56  (void) fprintf(fp, "%s at %s", host, ctime(&now));
57  (void) fclose(fp);
58  }
59 } /* LogOpen */
60 
61 
62 
63 
64 void
65 EndLog(void)
66 {
67  FILE *new, *old;
68  struct Stat st;
69  long fat;
70  char str[512];
71  char tmpLog[256];
72 
73  if (gOurDirectoryPath[0] == '\0')
74  return; /* Don't create in root directory. */
75 
76  /* If the user wants to, s/he can specify the maximum size of the log file,
77  * so it doesn't waste too much disk space. If the log is too fat, trim the
78  * older lines (at the top) until we're under the limit.
79  */
80  if ((gMaxLogSize <= 0) || (Stat(gLogFileName, &st) < 0))
81  return; /* Never trim, or no log. */
82 
83  if ((size_t)st.st_size < (size_t)gMaxLogSize)
84  return; /* Log size not over limit yet. */
85 
86  if ((old = fopen(gLogFileName, FOPEN_READ_TEXT)) == NULL)
87  return;
88 
89  /* Want to make it so we're about 30% below capacity.
90  * That way we won't trim the log each time we run the program.
91  */
92  fat = (long) st.st_size - (long) gMaxLogSize + (long) (0.30 * gMaxLogSize);
93  while (fat > 0L) {
94  if (fgets(str, (int) sizeof(str), old) == NULL)
95  return;
96  fat -= (long) strlen(str);
97  }
98  /* skip lines until a new site was opened */
99  for (;;) {
100  if (fgets(str, (int) sizeof(str), old) == NULL) {
101  (void) fclose(old);
103  return; /* Nothing left, start anew next time. */
104  }
105  if (! isspace(*str))
106  break;
107  }
108 
109  /* Copy the remaining lines in "old" to "new" */
110  OurDirectoryPath(tmpLog, sizeof(tmpLog), "log.tmp");
111  if ((new = fopen(tmpLog, FOPEN_WRITE_TEXT)) == NULL) {
112  (void) fclose(old);
113  return;
114  }
115  (void) fputs(str, new);
116  while (fgets(str, (int) sizeof(str), old) != NULL)
117  (void) fputs(str, new);
118  (void) fclose(old);
119  (void) fclose(new);
120  if (remove(gLogFileName) < 0)
121  return;
122  if (rename(tmpLog, gLogFileName) < 0)
123  return;
124 } /* EndLog */
#define isspace(c)
Definition: acclib.h:69
struct png_info_def **typedef void(__cdecl typeof(png_destroy_read_struct))(struct png_struct_def **
Definition: typeof.h:49
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
char * host
Definition: whois.c:55
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
#define FOPEN_APPEND_TEXT
Definition: syshdrs.h:84
__u16 time
Definition: mkdosfs.c:366
_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,...)
time_t now
Definition: finger.c:65
const WCHAR * str
char gLogFileName[256]
Definition: log.c:14
smooth NULL
Definition: ftsmooth.c:416
#define FOPEN_READ_TEXT
Definition: syshdrs.h:82
__u16 ctime
Definition: mkdosfs.c:362
void InitLog(void)
Definition: log.c:20
static const WCHAR url[]
Definition: encode.c:1432
int gMaxLogSize
Definition: pref.c:53
static const WCHAR L[]
Definition: oid.c:1250
_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)
void EndLog(void)
Definition: log.c:65
GLenum mode
Definition: glext.h:6217
void LogOpen(const char *const host)
Definition: log.c:45
_Check_return_opt_ _CRTIMP char *__cdecl fgets(_Out_writes_z_(_MaxCount) char *_Buf, _In_ int _MaxCount, _Inout_ FILE *_File)
#define Stat
Definition: syshdrs.h:78
__kernel_time_t time_t
Definition: linux.h:252
#define long
Definition: qsort.c:33
void LogXfer(const char *const mode, const char *const url)
Definition: log.c:28
int remove
Definition: msacm.c:1353
#define kLogFileName
Definition: log.h:11
static unsigned char * fat
Definition: mkdosfs.c:542
_Check_return_opt_ _CRTIMP int __cdecl fputs(_In_z_ const char *_Str, _Inout_ FILE *_File)
char gOurDirectoryPath[]
Definition: util.c:17