/[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.43 - (show annotations) (download) (as text)
Sun Jun 20 13:33:48 2004 UTC (15 years, 3 months ago) by wingman
Branch: MAIN
Changes since 1.42: +15 -5 lines
File MIME type: text/x-chdr
* Moved terminal mode out of telnetparty into core. No more need to
  load telnetparty in order to get a console.

* Started the beginning (heh) of '.restart'. Currently only the framework
  works, meaning if you do '.restart' the old stuff isn't cleared since
  currently everything leaks a <section>_shutdown() method :-/.

  At least we can test memory leaks (and i guess there are a lot) this way
  by doing a restart and check if there's a single byte of memory allocated
  (there _shouldn't_ be any).

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.42 2004/06/19 16:11:53 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 char timestamp[32];
185
186 prev = NULL;
187 for (log = log_list_head; log; log = log->next) {
188 if (!strcmp(log->filename, filename)) break;
189 prev = log;
190 }
191 if (!log) return(1);
192 if (prev) prev->next = log->next;
193 else log_list_head = log->next;
194 if (log->fp) {
195 timer_get_timestamp(timestamp, sizeof (timestamp));
196 flushlog(log, timestamp);
197 fclose(log->fp);
198 }
199 free(log->last_msg);
200 free(log->filename);
201 free(log);
202 return(0);
203 }
204
205 static int on_putlog(int flags, const char *chan, const char *text, int len)
206 {
207 log_t *log;
208 char timestamp[32];
209
210 timer_get_timestamp(timestamp, sizeof (timestamp));
211 for (log = log_list_head; log; log = log->next) {
212 /* If this log doesn't match, skip it. */
213 if (!(log->mask & flags)) continue;
214 if (chan[0] != '*' && log->chname[0] != '*' && irccmp(chan, log->chname)) continue;
215
216
217 /* If it's a repeat message, don't write it again. */
218 if (!strcasecmp(text, log->last_msg)) {
219 log->repeats++;
220 continue;
221 }
222
223 /* If there was a repeated message, write the count. */
224 if (log->repeats) {
225 fprintf(log->fp, "%s", timestamp);
226 fprintf(log->fp, _("Last message repeated %d time(s).\n"), log->repeats);
227 log->repeats = 0;
228 }
229
230 /* Save this msg to check for repeats next time. */
231 str_redup(&log->last_msg, text);
232
233 /* Now output to the file. */
234 fprintf(log->fp, "%s%s\n", timestamp, text);
235 }
236
237 if (!backgrd || use_stderr) {
238
239 if (terminal_mode) {
240 /* check if HQ is on console. If yes we disable
241 * output to stdout since otherwise everything would
242 * be printed out twice. */
243 if (!terminal_enabled) {
244 terminal_enabled = (
245 partymember_lookup_nick (TERMINAL_NICK) != NULL);
246 }
247 if (terminal_enabled)
248 return 0;
249
250 }
251
252 fprintf (stdout, "%s %s%s\n", chan, timestamp, text);
253 }
254
255 return(0);
256 }
257
258 static void check_logsizes()
259 {
260 int size;
261 char *newfname;
262 log_t *log;
263
264 if (core_config.keep_all_logs || core_config.max_logsize <= 0) return;
265
266 for (log = log_list_head; log; log = log->next) {
267 size = ftell(log->fp) / 1024; /* Size in kilobytes. */
268 if (size < core_config.max_logsize) continue;
269
270 /* It's too big. */
271 putlog(LOG_MISC, "*", _("Cycling logfile %s: over max-logsize (%d kilobytes)."), log->filename, size);
272 fclose(log->fp);
273
274 newfname = egg_mprintf("%s.yesterday", log->filename);
275 unlink(newfname);
276 movefile(log->filename, newfname);
277 free(newfname);
278 }
279 }
280
281 static void flushlog(log_t *log, char *timestamp)
282 {
283 if (log->repeats) {
284 fprintf(log->fp, "%s", timestamp);
285 fprintf(log->fp, _("Last message repeated %d time(s).\n"), log->repeats);
286 log->repeats = 0;
287 realloc_strcpy(log->last_msg, "");
288 }
289 fflush(log->fp);
290 }
291
292 /* Flush the logfiles to disk
293 */
294 void flushlogs()
295 {
296 char timestamp[32];
297 log_t *log;
298
299 timer_get_timestamp(timestamp, sizeof (timestamp));
300 for (log = log_list_head; log; log = log->next) {
301 flushlog(log, timestamp);
302 }
303 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23