/[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.44 - (show annotations) (download) (as text)
Mon Jun 21 10:59:39 2004 UTC (15 years, 3 months ago) by wingman
Branch: MAIN
Changes since 1.43: +10 -12 lines
File MIME type: text/x-chdr
* Some left-over timer changes

1 /* logfile.c: logging
2 *
3 * Copyright (C) 2001, 2002, 2003, 2004 Eggheads Development Team
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 *
19 */
20
21 #ifndef lint
22 static const char rcsid[] = "$Id: logfile.c,v 1.43 2004/06/20 13:33:48 wingman Exp $";
23 #endif
24
25 #include <eggdrop/eggdrop.h>
26 #include <stdio.h>
27 #include <unistd.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #include "core_config.h"
31 #include "terminal.h" /* TERMINAL_NICK */
32 #include "logfile.h"
33
34 typedef struct log_b {
35 struct log_b *next;
36 char *filename;
37 int mask;
38 char *chname;
39 char *last_msg;
40 int repeats;
41 int flags;
42 FILE *fp;
43 } log_t;
44
45 extern int backgrd, use_stderr, terminal_mode;
46 static int terminal_enabled = 0;
47 extern time_t now;
48
49 static log_t *log_list_head = NULL; /* Linked list of logfiles. */
50
51 static int logfile_minutely();
52 static int logfile_5minutely();
53 static int logfile_cycle();
54 static void check_logsizes();
55 static void flushlog(log_t *log, char *timestamp);
56
57 /* Bind for the log table. The core does logging to files and the partyline, but
58 * modules may implement sql logging or whatever. */
59 static int on_putlog(int flags, const char *chan, const char *text, int len);
60
61 static script_command_t log_script_cmds[] = {
62 {"", "logfile", logfile_add, NULL, 3, "sss", "modes chan filename", SCRIPT_STRING, 0}, /* DDD */
63 {0}
64 };
65
66 static bind_list_t log_binds[] = {
67 {NULL, NULL, on_putlog},
68 {0}
69 };
70
71 static bind_list_t log_events[] = {
72 {NULL, "minutely", logfile_minutely}, /* DDD */
73 {NULL, "5minutely", logfile_5minutely}, /* DDD */
74 {0}
75 };
76
77 void logfile_init(void)
78 {
79 script_create_commands(log_script_cmds);
80 bind_add_list("log", log_binds);
81 bind_add_list("event", log_events);
82 }
83
84 void logfile_shutdown(void)
85 {
86 flushlogs();
87
88 script_delete_commands(log_script_cmds);
89 bind_rem_list("log", log_binds);
90 bind_rem_list("event", log_events);
91 }
92
93 static int logfile_minutely()
94 {
95 struct tm *nowtm;
96 int miltime;
97
98 if (core_config.quick_logs) {
99 flushlogs();
100 check_logsizes();
101 }
102
103 nowtm = localtime(&now);
104 miltime = 100 * nowtm->tm_hour + nowtm->tm_min;
105
106 if (miltime == core_config.switch_logfiles_at) logfile_cycle();
107
108 return(0);
109 }
110
111 static int logfile_5minutely()
112 {
113 if (!core_config.quick_logs) {
114 flushlogs();
115 check_logsizes();
116 }
117 return(0);
118 }
119
120 static int logfile_cycle()
121 {
122 log_t *log, *prev;
123 char suffix[32];
124 char *newfname;
125
126 putlog(LOG_MISC, "*", _("Cycling logfiles..."));
127 flushlogs();
128
129 /* Determine suffix for cycled logfiles. */
130 if (core_config.keep_all_logs) {
131 strftime(suffix, 32, core_config.logfile_suffix, localtime(&now));
132 }
133
134 prev = NULL;
135 for (log = log_list_head; log; log = log->next) {
136 fclose(log->fp);
137
138 if (core_config.keep_all_logs) newfname = egg_mprintf("%s%s", log->filename, suffix);
139 else newfname = egg_mprintf("%s.yesterday", log->filename);
140
141 unlink(newfname);
142 movefile(log->filename, newfname);
143 free(newfname);
144
145 log->fp = fopen(log->filename, "a");
146 if (!log->fp) {
147 logfile_del(log->filename);
148 if (prev) log = prev;
149 else log = log_list_head;
150 }
151 else prev = log;
152 }
153 return(0);
154 }
155
156 char *logfile_add(char *modes, char *chan, char *fname)
157 {
158 FILE *fp;
159 log_t *log;
160
161 /* Get rid of any duplicates. */
162 logfile_del(fname);
163
164 /* Test the filename. */
165 fp = fopen(fname, "a");
166 if (!fp) return("");
167
168 log = (log_t *)calloc(1, sizeof(*log));
169 log->filename = strdup(fname);
170 log->chname = strdup(chan);
171 log->last_msg = strdup("");
172 log->mask = LOG_ALL;
173 log->fp = fp;
174
175 log->next = log_list_head;
176 log_list_head = log;
177
178 return(log->filename);
179 }
180
181 int logfile_del(char *filename)
182 {
183 log_t *log, *prev;
184
185 prev = NULL;
186 for (log = log_list_head; log; log = log->next) {
187 if (!strcmp(log->filename, filename)) break;
188 prev = log;
189 }
190 if (!log) return(1);
191 if (prev) prev->next = log->next;
192 else log_list_head = log->next;
193 if (log->fp) {
194 flushlog(log, timer_get_timestamp());
195 fclose(log->fp);
196 }
197 free(log->last_msg);
198 free(log->filename);
199 free(log);
200 return(0);
201 }
202
203 static int on_putlog(int flags, const char *chan, const char *text, int len)
204 {
205 log_t *log;
206 char *ts;
207
208 ts = timer_get_timestamp();
209 for (log = log_list_head; log; log = log->next) {
210 /* If this log doesn't match, skip it. */
211 if (!(log->mask & flags)) continue;
212 if (chan[0] != '*' && log->chname[0] != '*' && irccmp(chan, log->chname)) continue;
213
214
215 /* If it's a repeat message, don't write it again. */
216 if (!strcasecmp(text, log->last_msg)) {
217 log->repeats++;
218 continue;
219 }
220
221 /* If there was a repeated message, write the count. */
222 if (log->repeats) {
223 fprintf(log->fp, "%s", ts);
224 fprintf(log->fp, _("Last message repeated %d time(s).\n"), log->repeats);
225 log->repeats = 0;
226 }
227
228 /* Save this msg to check for repeats next time. */
229 str_redup(&log->last_msg, text);
230
231 /* Now output to the file. */
232 fprintf(log->fp, "%s%s\n", ts, text);
233 }
234
235 if (!backgrd || use_stderr) {
236
237 if (terminal_mode) {
238 /* check if HQ is on console. If yes we disable
239 * output to stdout since otherwise everything would
240 * be printed out twice. */
241 if (!terminal_enabled) {
242 terminal_enabled = (
243 partymember_lookup_nick (TERMINAL_NICK) != NULL);
244 }
245 if (terminal_enabled)
246 return 0;
247
248 }
249
250 fprintf (stdout, "%s %s%s\n", chan, ts, text);
251 }
252
253 return(0);
254 }
255
256 static void check_logsizes()
257 {
258 int size;
259 char *newfname;
260 log_t *log;
261
262 if (core_config.keep_all_logs || core_config.max_logsize <= 0) return;
263
264 for (log = log_list_head; log; log = log->next) {
265 size = ftell(log->fp) / 1024; /* Size in kilobytes. */
266 if (size < core_config.max_logsize) continue;
267
268 /* It's too big. */
269 putlog(LOG_MISC, "*", _("Cycling logfile %s: over max-logsize (%d kilobytes)."), log->filename, size);
270 fclose(log->fp);
271
272 newfname = egg_mprintf("%s.yesterday", log->filename);
273 unlink(newfname);
274 movefile(log->filename, newfname);
275 free(newfname);
276 }
277 }
278
279 static void flushlog(log_t *log, char *timestamp)
280 {
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 realloc_strcpy(log->last_msg, "");
286 }
287 fflush(log->fp);
288 }
289
290 /* Flush the logfiles to disk
291 */
292 void flushlogs()
293 {
294 char *ts;
295 log_t *log;
296
297 ts = timer_get_timestamp();
298 for (log = log_list_head; log; log = log->next) {
299 flushlog(log, ts);
300 }
301 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23