/[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.11 - (show annotations) (download) (as text)
Wed Nov 14 01:57:50 2001 UTC (17 years, 11 months ago) by stdarg
Branch: MAIN
Changes since 1.10: +0 -1 lines
File MIME type: text/x-chdr
* Re-ordered init procedures so hooks[] is initialized earlier.
* Logfiles should cycle now.

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23