/[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.22 - (show annotations) (download) (as text)
Sun May 12 05:59:52 2002 UTC (17 years, 5 months ago) by stdarg
Branch: MAIN
Changes since 1.21: +3 -3 lines
File MIME type: text/x-chdr
* Converting the scripting modules to using a well-defined interface rather than the general registry.
* Converted the 'traffic' command.
* Got rid of the registry since it wasn't needed anymore.

1 /*
2 * logfile.c --
3 *
4 */
5 /*
6 * Copyright (C) 2002 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.21 2002/05/05 16:40:38 tothwolf 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 return putlog(lev, chan, "%s", text);
187 }
188
189 char *logfile_add(char *modes, char *chan, char *fname)
190 {
191 FILE *fp;
192 log_t *log;
193
194 /* Get rid of any duplicates. */
195 logfile_del(fname);
196
197 /* Test the filename. */
198 fp = fopen(fname, "a");
199 if (!fp) return("");
200
201 log = (log_t *)calloc(1, sizeof(*log));
202 log->filename = strdup(fname);
203 log->chname = strdup(chan);
204 log->last_msg = strdup("");
205 log->mask = logmodes(modes);
206 log->fp = fp;
207
208 log->next = log_list_head;
209 log_list_head = log;
210
211 return(log->filename);
212 }
213
214 int logfile_del(char *filename)
215 {
216 log_t *log, *prev;
217 char timestamp[32];
218
219 prev = NULL;
220 for (log = log_list_head; log; log = log->next) {
221 if (!strcmp(log->filename, filename)) break;
222 prev = log;
223 }
224 if (!log) return(1);
225 if (prev) prev->next = log->next;
226 else log_list_head = log->next;
227 if (log->fp) {
228 get_timestamp(timestamp);
229 flushlog(log, timestamp);
230 fclose(log->fp);
231 }
232 free(log->last_msg);
233 free(log->filename);
234 free(log);
235 return(0);
236 }
237
238 /* Log something
239 * putlog(level,channel_name,format,...);
240 */
241 int putlog EGG_VARARGS_DEF(int, arg1)
242 {
243 int i, type, len;
244 log_t *log;
245 char *format, *chname, *out;
246 char timestamp[32];
247 va_list va;
248
249
250 len = 128;
251 out = (char *)malloc(len);
252 while (1) {
253 type = EGG_VARARGS_START(int, arg1, va);
254 chname = va_arg(va, char *);
255 format = va_arg(va, char *);
256 i = vsnprintf(out, len, format, va);
257 if (i > -1 && i < len) break; /* Done. */
258 if (i > len) len = i+1; /* Exact amount. */
259 else len *= 2; /* Just guessing. */
260 out = (char *)realloc(out, len);
261 }
262 len = i;
263
264 va_end(va);
265
266 get_timestamp(timestamp);
267 for (log = log_list_head; log; log = log->next) {
268 /* If this log doesn't match, skip it. */
269 if (!(log->mask & type)) continue;
270 if (chname[0] != '*' && log->chname[0] != '*' && irccmp(chname, log->chname)) continue;
271
272 /* If it's a repeat message, don't write it again. */
273 if (!strcasecmp(out, log->last_msg)) {
274 log->repeats++;
275 continue;
276 }
277
278 /* If there was a repeated message, write the count. */
279 if (log->repeats) {
280 fprintf(log->fp, "%s", timestamp);
281 fprintf(log->fp, _("Last message repeated %d time(s).\n"), log->repeats);
282 log->repeats = 0;
283 }
284
285 /* Save this msg to check for repeats next time. */
286 realloc_strcpy(log->last_msg, out);
287
288 /* Now output to the file. */
289 fprintf(log->fp, "%s%s\n", timestamp, out);
290 }
291
292 for (i = 0; i < dcc_total; i++)
293 if ((dcc[i].type == &DCC_CHAT) && (dcc[i].u.chat->con_flags & type)) {
294 if ((chname[0] == '*') || (dcc[i].u.chat->con_chan[0] == '*') ||
295 (!irccmp(chname, dcc[i].u.chat->con_chan)))
296 dprintf(i, "%s%s\n", timestamp, out);
297 }
298 if ((!backgrd) && (!con_chan) && (!term_z))
299 dprintf(DP_STDOUT, "%s%s\n", timestamp, out);
300 else if ((type & LOG_MISC) && use_stderr) {
301 dprintf(DP_STDERR, "%s%s\n", timestamp, out);
302 }
303
304 free(out);
305 return(len);
306 }
307
308 static void check_logsizes()
309 {
310 int size;
311 char *newfname;
312 log_t *log;
313
314 if (keep_all_logs || max_logsize <= 0) return;
315
316 for (log = log_list_head; log; log = log->next) {
317 size = ftell(log->fp) / 1024; /* Size in kilobytes. */
318 if (size < max_logsize) continue;
319
320 /* It's too big. */
321 putlog(LOG_MISC, "*", _("Cycling logfile %s, over max-logsize (%d kilobytes)"), log->filename, size);
322 fflush(log->fp);
323 fclose(log->fp);
324
325 newfname = msprintf("%s.yesterday", log->filename);
326 unlink(newfname);
327 movefile(log->filename, newfname);
328 free(newfname);
329 }
330 }
331
332 static void flushlog(log_t *log, char *timestamp)
333 {
334 if (log->repeats) {
335 fprintf(log->fp, "%s", timestamp);
336 fprintf(log->fp, _("Last message repeated %d time(s).\n"), log->repeats);
337 log->repeats = 0;
338 realloc_strcpy(log->last_msg, "");
339 }
340 fflush(log->fp);
341 }
342
343 /* Flush the logfiles to disk
344 */
345 void flushlogs()
346 {
347 char timestamp[32];
348 log_t *log;
349
350 get_timestamp(timestamp);
351 for (log = log_list_head; log; log = log->next) {
352 flushlog(log, timestamp);
353 }
354 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23