1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
|
/* Logging routines.
*
* (C) 2003-2013 Anope Team
* Contact us at team@anope.org
*
* Please read COPYING and README for further details.
*
* Based on the original code of Epona by Lara.
* Based on the original code of Services by Andy Church.
*
*
*/
#include "services.h"
#include "pseudo.h"
static FILE *logfile;
static int curday = 0;
/*************************************************************************/
static int get_logname(char *name, int count, struct tm *tm)
{
char timestamp[32];
time_t t;
if (!tm) {
time(&t);
tm = localtime(&t);
}
/* fix bug 577 */
strftime(timestamp, sizeof(timestamp), "%Y%m%d", tm);
snprintf(name, count, "logs/%s.%s", log_filename, timestamp);
curday = tm->tm_yday;
return 1;
}
/*************************************************************************/
static void remove_log(void)
{
time_t t;
struct tm tm;
char name[PATH_MAX];
if (!KeepLogs)
return;
time(&t);
t -= (60 * 60 * 24 * KeepLogs);
tm = *localtime(&t);
/* removed if from here cause get_logchan is always 1 */
get_logname(name, sizeof(name), &tm);
#ifndef _WIN32
unlink(name);
#else
DeleteFile(name);
#endif
}
/*************************************************************************/
static void checkday(void)
{
time_t t;
struct tm tm;
time(&t);
tm = *localtime(&t);
if (curday != tm.tm_yday) {
close_log();
remove_log();
open_log();
}
}
/*************************************************************************/
/* Open the log file. Return -1 if the log file could not be opened, else
* return 0. */
int open_log(void)
{
char name[PATH_MAX];
if (logfile)
return 0;
/* if removed again.. get_logname is always 1 */
get_logname(name, sizeof(name), NULL);
logfile = fopen(name, "a");
if (logfile)
setbuf(logfile, NULL);
return logfile != NULL ? 0 : -1;
}
/*************************************************************************/
/* Close the log file. */
void close_log(void)
{
if (!logfile)
return;
fclose(logfile);
logfile = NULL;
}
/*************************************************************************/
/* added cause this is used over and over in the code */
char *log_gettimestamp(void)
{
time_t t;
struct tm tm;
static char tbuf[256];
time(&t);
tm = *localtime(&t);
#if HAVE_GETTIMEOFDAY
if (debug) {
char *s;
struct timeval tv;
gettimeofday(&tv, NULL);
strftime(tbuf, sizeof(tbuf) - 1, "[%b %d %H:%M:%S", &tm);
s = tbuf + strlen(tbuf);
s += snprintf(s, sizeof(tbuf) - (s - tbuf), ".%06d",
(int) tv.tv_usec);
strftime(s, sizeof(tbuf) - (s - tbuf) - 1, " %Y]", &tm);
} else {
#endif
strftime(tbuf, sizeof(tbuf) - 1, "[%b %d %H:%M:%S %Y]", &tm);
#if HAVE_GETTIMEOFDAY
}
#endif
return tbuf;
}
/*************************************************************************/
/* Log stuff to the log file with a datestamp. Note that errno is
* preserved by this routine and log_perror().
*/
void alog(const char *fmt, ...)
{
va_list args;
char *buf;
int errno_save = errno;
char str[BUFSIZE];
checkday();
if (!fmt) {
return;
}
va_start(args, fmt);
vsnprintf(str, sizeof(str), fmt, args);
va_end(args);
buf = log_gettimestamp();
if (logfile) {
fprintf(logfile, "%s %s\n", buf, str);
}
if (nofork) {
fprintf(stderr, "%s %s\n", buf, str);
}
if (LogChannel && logchan && !debug && findchan(LogChannel)) {
privmsg(s_GlobalNoticer, LogChannel, "%s", str);
}
errno = errno_save;
}
/*************************************************************************/
/* Like alog(), but tack a ": " and a system error message (as returned by
* strerror()) onto the end.
*/
void log_perror(const char *fmt, ...)
{
va_list args;
char *buf;
int errno_save = errno;
char str[BUFSIZE];
checkday();
if (!fmt) {
return;
}
va_start(args, fmt);
vsnprintf(str, sizeof(str), fmt, args);
va_end(args);
buf = log_gettimestamp();
if (logfile) {
fprintf(logfile, "%s %s : %s\n", buf, str, strerror(errno_save));
}
if (nofork) {
fprintf(stderr, "%s %s : %s\n", buf, str, strerror(errno_save));
}
errno = errno_save;
}
/*************************************************************************/
/* We've hit something we can't recover from. Let people know what
* happened, then go down.
*/
void fatal(const char *fmt, ...)
{
va_list args;
char *buf;
char buf2[4096];
checkday();
if (!fmt) {
return;
}
va_start(args, fmt);
vsnprintf(buf2, sizeof(buf2), fmt, args);
va_end(args);
buf = log_gettimestamp();
if (logfile)
fprintf(logfile, "%s FATAL: %s\n", buf, buf2);
if (nofork)
fprintf(stderr, "%s FATAL: %s\n", buf, buf2);
if (servsock >= 0)
anope_cmd_global(NULL, "FATAL ERROR! %s", buf2);
/* one of the many places this needs to be called from */
ModuleRunTimeDirCleanUp();
exit(1);
}
/*************************************************************************/
/* Same thing, but do it like perror(). */
void fatal_perror(const char *fmt, ...)
{
va_list args;
char *buf;
char buf2[4096];
int errno_save = errno;
checkday();
if (!fmt) {
return;
}
va_start(args, fmt);
vsnprintf(buf2, sizeof(buf2), fmt, args);
va_end(args);
buf = log_gettimestamp();
if (logfile)
fprintf(logfile, "%s FATAL: %s: %s\n", buf, buf2,
strerror(errno_save));
if (nofork)
fprintf(stderr, "%s FATAL: %s: %s\n", buf, buf2,
strerror(errno_save));
if (servsock >= 0)
anope_cmd_global(NULL, "FATAL ERROR! %s: %s", buf2,
strerror(errno_save));
/* one of the many places this needs to be called from */
ModuleRunTimeDirCleanUp();
exit(1);
}
/*************************************************************************/
/* Same thing, but do it like perror().
* This is for socket errors. On *nix, it works just like fatal_perror,
* on Win32, it uses the socket error code and formatting functions.
*/
void fatal_sockerror(const char *fmt, ...)
{
va_list args;
char *buf;
char buf2[4096];
int errno_save = ano_sockgeterr();
if (!fmt) {
return;
}
checkday();
/* this will fix 581 */
va_start(args, fmt);
vsnprintf(buf2, sizeof(buf2), fmt, args);
va_end(args);
buf = log_gettimestamp();
if (logfile)
fprintf(logfile, "%s FATAL: %s: %s\n", buf, buf2,
ano_sockstrerror(errno_save));
if (stderr)
fprintf(stderr, "%s FATAL: %s: %s\n", buf, buf2,
ano_sockstrerror(errno_save));
if (servsock >= 0)
anope_cmd_global(NULL, "FATAL ERROR! %s: %s", buf2,
strerror(errno_save));
/* one of the many places this needs to be called from */
ModuleRunTimeDirCleanUp();
exit(1);
}
|