/[cvs]/eggdrop1.9/src/logfile.c
ViewVC logotype

Contents of /eggdrop1.9/src/logfile.c

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.24 - (show annotations) (download) (as text)
Thu Jan 2 21:33:16 2003 UTC (16 years, 9 months ago) by wcc
Branch: MAIN
Changes since 1.23: +2 -2 lines
File MIME type: text/x-chdr
* Updated copyright info for 2003.

1 /*
2 * logfile.c --
3 *
4 */
5 /*
6 * Copyright (C) 2002, 2003 Eggheads Development Team
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 */
22
23 #ifndef lint
24 static const char rcsid[] = "$Id: logfile.c,v 1.23 2002/10/07 22:36:37 stdarg Exp $";
25 #endif
26
27 #include "main.h"
28 //#include "chanprog.h" /* logmodes */
29 #include "modules.h" /* add_hook() */
30 #include "lib/egglib/msprintf.h"
31 #include "logfile.h" /* prototypes */
32 #include "dccutil.h" /* dprintf_eggdrop */
33 #include "irccmp.h" /* irccmp */
34
35 typedef struct log_b {
36 struct log_b *next;
37 char *filename;
38 int mask;
39 char *chname;
40 char *last_msg;
41 int repeats;
42 int flags;
43 FILE *fp;
44 } log_t;
45
46 extern int use_stderr; /* From main.c, while we're starting eggdrop. */
47 extern int term_z;
48 extern int backgrd;
49 extern int con_chan;
50 extern time_t now;
51
52 extern int dcc_total;
53 extern struct dcc_t *dcc;
54
55 #ifndef MAKING_MODS
56 extern struct dcc_table DCC_CHAT;
57 #endif /* MAKING_MODS */
58
59 static int cycle_at = 300; /* Military time where we cycle logfiles. */
60 static int keep_all_logs = 0; /* Keep all logs? */
61 static int max_logsize = 0; /* Max log size in kilobytes. */
62 static int quick_logs = 0; /* Check size more often? */
63 static char *logfile_suffix = NULL; /* Suffix for old logfiles. */
64
65 static log_t *log_list_head = NULL; /* Linked list of logfiles. */
66
67 static int logfile_minutely();
68 static int logfile_5minutely();
69 static int logfile_cycle();
70 static void check_logsizes();
71 static void flushlog(log_t *log, char *timestamp);
72
73 /* Functions for accessing the logfiles via scripts. */
74 static int script_putlog(void *cdata, char *text);
75 static int script_putloglev(char *level, char *chan, char *text);
76
77 static script_command_t log_script_cmds[] = {
78 {"", "putlog", script_putlog, (void *)LOG_MISC, 1, "s", "text", SCRIPT_INTEGER, SCRIPT_PASS_CDATA},
79 {"", "putcmdlog", script_putlog, (void *)LOG_CMDS, 1, "s", "text", SCRIPT_INTEGER, SCRIPT_PASS_CDATA},
80 {"", "putxferlog", script_putlog, (void *)LOG_FILES, 1, "s", "text", SCRIPT_INTEGER, SCRIPT_PASS_CDATA},
81 {"", "putloglev", script_putloglev, NULL, 3, "sss", "level channel text", SCRIPT_INTEGER, 0},
82 {"", "logfile", (Function) logfile_add, NULL, 3, "sss", "modes channel filename", SCRIPT_STRING, 0},
83 {"", "stoplog", logfile_del, NULL, 1, "s", "filename", SCRIPT_INTEGER, 0},
84 {0}
85 };
86
87 static script_linked_var_t log_script_vars[] = {
88 {"", "logfile_suffix", &logfile_suffix, SCRIPT_STRING, NULL},
89 {"", "max_logsize", &max_logsize, SCRIPT_INTEGER, NULL},
90 {"", "switch_logfiles_at", &cycle_at, SCRIPT_INTEGER, NULL},
91 {"", "keep_all_logs", &keep_all_logs, SCRIPT_INTEGER, NULL},
92 {"", "quick_logs", &quick_logs, SCRIPT_INTEGER, NULL},
93 {0}
94 };
95
96 void logfile_init()
97 {
98 logfile_suffix = strdup(".%d%b%Y");
99 script_create_commands(log_script_cmds);
100 script_link_vars(log_script_vars);
101 add_hook(HOOK_MINUTELY, logfile_minutely);
102 add_hook(HOOK_5MINUTELY, logfile_5minutely);
103 }
104
105 static int get_timestamp(char *t)
106 {
107 /* Calculate timestamp. */
108 strftime(t, 32, "[%H:%M] ", localtime(&now));
109 return(0);
110 }
111
112 static int logfile_minutely()
113 {
114 struct tm *nowtm;
115 int miltime;
116
117 if (quick_logs) {
118 flushlogs();
119 check_logsizes();
120 }
121
122 nowtm = localtime(&now);
123 miltime = 100 * nowtm->tm_hour + nowtm->tm_min;
124
125 if (miltime == cycle_at) logfile_cycle();
126
127 return(0);
128 }
129
130 static int logfile_5minutely()
131 {
132 if (!quick_logs) {
133 flushlogs();
134 check_logsizes();
135 }
136 return(0);
137 }
138
139 static int logfile_cycle()
140 {
141 log_t *log, *prev;
142 char suffix[32];
143 char *newfname;
144
145 putlog(LOG_MISC, "*", _("Cycling logfiles"));
146 flushlogs();
147
148 /* Determine suffix for cycled logfiles. */
149 if (keep_all_logs) {
150 strftime(suffix, 32, logfile_suffix, localtime(&now));
151 }
152
153 prev = NULL;
154 for (log = log_list_head; log; log = log->next) {
155 fclose(log->fp);
156
157 if (keep_all_logs) newfname = msprintf("%s%s", log->filename, suffix);
158 else newfname = msprintf("%s.yesterday", log->filename);
159
160 unlink(newfname);
161 movefile(log->filename, newfname);
162 free(newfname);
163
164 log->fp = fopen(log->filename, "a");
165 if (!log->fp) {
166 logfile_del(log->filename);
167 if (prev) log = prev;
168 else log = log_list_head;
169 }
170 else prev = log;
171 }
172 return(0);
173 }
174
175 static int script_putlog(void *cdata, char *text)
176 {
177 return putlog((int) cdata, "*", "%s", text);
178 }
179
180 static int script_putloglev(char *level, char *chan, char *text)
181 {
182 int lev = 0;
183
184 //lev = logmodes(level);
185 //if (!lev) return(-1);
186 lev = LOG_MISC;
187 return putlog(lev, chan, "%s", text);
188 }
189
190 char *logfile_add(char *modes, char *chan, char *fname)
191 {
192 FILE *fp;
193 log_t *log;
194
195 /* Get rid of any duplicates. */
196 logfile_del(fname);
197
198 /* Test the filename. */
199 fp = fopen(fname, "a");
200 if (!fp) return("");
201
202 log = (log_t *)calloc(1, sizeof(*log));
203 log->filename = strdup(fname);
204 log->chname = strdup(chan);
205 log->last_msg = strdup("");
206 log->mask = LOG_MISC;
207 //log->mask = logmodes(modes);
208 log->fp = fp;
209
210 log->next = log_list_head;
211 log_list_head = log;
212
213 return(log->filename);
214 }
215
216 int logfile_del(char *filename)
217 {
218 log_t *log, *prev;
219 char timestamp[32];
220
221 prev = NULL;
222 for (log = log_list_head; log; log = log->next) {
223 if (!strcmp(log->filename, filename)) break;
224 prev = log;
225 }
226 if (!log) return(1);
227 if (prev) prev->next = log->next;
228 else log_list_head = log->next;
229 if (log->fp) {
230 get_timestamp(timestamp);
231 flushlog(log, timestamp);
232 fclose(log->fp);
233 }
234 free(log->last_msg);
235 free(log->filename);
236 free(log);
237 return(0);
238 }
239
240 /* Log something
241 * putlog(level,channel_name,format,...);
242 */
243 int putlog EGG_VARARGS_DEF(int, arg1)
244 {
245 int i, type, len;
246 log_t *log;
247 char *format, *chname, *out;
248 char timestamp[32];
249 va_list va;
250
251
252 len = 128;
253 out = (char *)malloc(len);
254 while (1) {
255 type = EGG_VARARGS_START(int, arg1, va);
256 chname = va_arg(va, char *);
257 format = va_arg(va, char *);
258 i = vsnprintf(out, len, format, va);
259 if (i > -1 && i < len) break; /* Done. */
260 if (i > len) len = i+1; /* Exact amount. */
261 else len *= 2; /* Just guessing. */
262 out = (char *)realloc(out, len);
263 }
264 len = i;
265
266 va_end(va);
267
268 get_timestamp(timestamp);
269 for (log = log_list_head; log; log = log->next) {
270 /* If this log doesn't match, skip it. */
271 if (!(log->mask & type)) continue;
272 if (chname[0] != '*' && log->chname[0] != '*' && irccmp(chname, log->chname)) continue;
273
274 /* If it's a repeat message, don't write it again. */
275 if (!strcasecmp(out, log->last_msg)) {
276 log->repeats++;
277 continue;
278 }
279
280 /* If there was a repeated message, write the count. */
281 if (log->repeats) {
282 fprintf(log->fp, "%s", timestamp);
283 fprintf(log->fp, _("Last message repeated %d time(s).\n"), log->repeats);
284 log->repeats = 0;
285 }
286
287 /* Save this msg to check for repeats next time. */
288 realloc_strcpy(log->last_msg, out);
289
290 /* Now output to the file. */
291 fprintf(log->fp, "%s%s\n", timestamp, out);
292 }
293
294 for (i = 0; i < dcc_total; i++)
295 if ((dcc[i].type == &DCC_CHAT) && (dcc[i].u.chat->con_flags & type)) {
296 if ((chname[0] == '*') || (dcc[i].u.chat->con_chan[0] == '*') ||
297 (!irccmp(chname, dcc[i].u.chat->con_chan)))
298 dprintf(i, "%s%s\n", timestamp, out);
299 }
300 if ((!backgrd) && (!con_chan) && (!term_z))
301 dprintf(DP_STDOUT, "%s%s\n", timestamp, out);
302 else if ((type & LOG_MISC) && use_stderr) {
303 dprintf(DP_STDERR, "%s%s\n", timestamp, out);
304 }
305
306 free(out);
307 return(len);
308 }
309
310 static void check_logsizes()
311 {
312 int size;
313 char *newfname;
314 log_t *log;
315
316 if (keep_all_logs || max_logsize <= 0) return;
317
318 for (log = log_list_head; log; log = log->next) {
319 size = ftell(log->fp) / 1024; /* Size in kilobytes. */
320 if (size < max_logsize) continue;
321
322 /* It's too big. */
323 putlog(LOG_MISC, "*", _("Cycling logfile %s, over max-logsize (%d kilobytes)"), log->filename, size);
324 fflush(log->fp);
325 fclose(log->fp);
326
327 newfname = msprintf("%s.yesterday", log->filename);
328 unlink(newfname);
329 movefile(log->filename, newfname);
330 free(newfname);
331 }
332 }
333
334 static void flushlog(log_t *log, char *timestamp)
335 {
336 if (log->repeats) {
337 fprintf(log->fp, "%s", timestamp);
338 fprintf(log->fp, _("Last message repeated %d time(s).\n"), log->repeats);
339 log->repeats = 0;
340 realloc_strcpy(log->last_msg, "");
341 }
342 fflush(log->fp);
343 }
344
345 /* Flush the logfiles to disk
346 */
347 void flushlogs()
348 {
349 char timestamp[32];
350 log_t *log;
351
352 get_timestamp(timestamp);
353 for (log = log_list_head; log; log = log->next) {
354 flushlog(log, timestamp);
355 }
356 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23