ReactOS 0.4.15-dev-5666-gc548b97
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
13extern int gMaxLogSize;
14char gLogFileName[256];
15
16extern char gOurDirectoryPath[];
17
18
19void
21{
23} /* InitLog */
24
25
26
27void
28LogXfer(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
44void
45LogOpen(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
64void
65EndLog(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
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
#define NULL
Definition: types.h:112
__kernel_time_t time_t
Definition: linux.h:252
time_t now
Definition: finger.c:65
GLenum mode
Definition: glext.h:6217
_Check_return_opt_ _CRTIMP int __cdecl fputs(_In_z_ const char *_Str, _Inout_ FILE *_File)
_Check_return_opt_ _CRTIMP int __cdecl fprintf(_Inout_ FILE *_File, _In_z_ _Printf_format_string_ const char *_Format,...)
_Check_return_ _CRTIMP FILE *__cdecl fopen(_In_z_ const char *_Filename, _In_z_ const char *_Mode)
_Check_return_opt_ _CRTIMP char *__cdecl fgets(_Out_writes_z_(_MaxCount) char *_Buf, _In_ int _MaxCount, _Inout_ FILE *_File)
_Check_return_opt_ _CRTIMP int __cdecl fclose(_Inout_ FILE *_File)
#define Stat
Definition: syshdrs.h:78
#define kLogFileName
Definition: log.h:11
__u16 ctime
Definition: mkdosfs.c:4
__u16 time
Definition: mkdosfs.c:8
static unsigned char * fat
Definition: mkdosfs.c:542
void LogXfer(const char *const mode, const char *const url)
Definition: log.c:28
int gMaxLogSize
Definition: pref.c:53
char gLogFileName[256]
Definition: log.c:14
void InitLog(void)
Definition: log.c:20
void LogOpen(const char *const host)
Definition: log.c:45
char gOurDirectoryPath[]
Definition: util.c:17
void EndLog(void)
Definition: log.c:65
char * OurDirectoryPath(char *const dst, const size_t siz, const char *const fname)
Definition: util.c:486
static const WCHAR url[]
Definition: encode.c:1432
int remove
Definition: msacm.c:1366
#define FOPEN_READ_TEXT
Definition: syshdrs.h:82
#define FOPEN_WRITE_TEXT
Definition: syshdrs.h:83
#define FOPEN_APPEND_TEXT
Definition: syshdrs.h:84
#define L(x)
Definition: ntvdm.h:50
#define long
Definition: qsort.c:33
const WCHAR * str
_Check_return_ int __cdecl rename(_In_z_ const char *_OldFilename, _In_z_ const char *_NewFilename)
char * host
Definition: whois.c:55