/[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.3 - (show annotations) (download) (as text)
Thu Oct 18 11:18:44 2001 UTC (18 years ago) by stdarg
Branch: MAIN
Changes since 1.2: +1 -0 lines
File MIME type: text/x-chdr
Forgot to initialize a variable in logfile_cycle().

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

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23