/[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.18 - (show annotations) (download) (as text)
Thu Apr 25 04:06:40 2002 UTC (17 years, 5 months ago) by stdarg
Branch: MAIN
Changes since 1.17: +7 -12 lines
File MIME type: text/x-chdr
* Implemented string/int variable linking for the tcl module
* Started working on linking variables in the perl module
* Some minor fixes as I came across them

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23