4 Copyright (C) 1999-2000 Timo Sirainen
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include "write-buffer.h"
30 #include "lib-config/iconfig.h"
33 #define DEFAULT_LOG_FILE_CREATE_MODE 600
36 static struct flock lock;
41 static const char *log_item_types[] = {
48 const char *log_timestamp;
49 static int log_file_create_mode;
50 static int rotate_tag;
52 static int log_item_str2type(const char *type)
56 for (n = 0; log_item_types[n] != NULL; n++) {
57 if (g_strcasecmp(log_item_types[n], type) == 0)
64 static void log_write_timestamp(int handle, const char *format,
65 const char *text, time_t stamp)
70 g_return_if_fail(format != NULL);
71 if (*format == '\0') return;
73 tm = localtime(&stamp);
74 if (strftime(str, sizeof(str), format, tm) > 0)
75 write_buffer(handle, str, strlen(str));
76 if (text != NULL) write_buffer(handle, text, strlen(text));
79 static char *log_filename(LOG_REC *log)
81 char *str, fname[1024];
89 str = convert_home(log->fname);
90 ret = strftime(fname, sizeof(fname), str, tm);
94 g_warning("log_filename() : strftime() failed");
98 return g_strdup(fname);
101 int log_start_logging(LOG_REC *log)
105 g_return_val_if_fail(log != NULL, FALSE);
107 if (log->handle != -1)
110 /* Append/create log file */
111 g_free_not_null(log->real_fname);
112 log->real_fname = log_filename(log);
114 if (log->real_fname != NULL &&
115 strcmp(log->real_fname, log->fname) != 0) {
116 /* path may contain variables (%time, $vars),
117 make sure the directory is created */
118 dir = g_dirname(log->real_fname);
119 mkpath(dir, LOG_DIR_CREATE_MODE);
123 log->handle = log->real_fname == NULL ? -1 :
124 open(log->real_fname, O_WRONLY | O_APPEND | O_CREAT,
125 log_file_create_mode);
126 if (log->handle == -1) {
127 signal_emit("log create failed", 1, log);
132 memset(&lock, 0, sizeof(lock));
133 lock.l_type = F_WRLCK;
134 if (fcntl(log->handle, F_SETLK, &lock) == -1 && errno == EACCES) {
137 signal_emit("log locked", 1, log);
142 lseek(log->handle, 0, SEEK_END);
144 log->opened = log->last = time(NULL);
145 log_write_timestamp(log->handle,
146 settings_get_str("log_open_string"),
149 signal_emit("log started", 1, log);
154 void log_stop_logging(LOG_REC *log)
156 g_return_if_fail(log != NULL);
158 if (log->handle == -1)
161 signal_emit("log stopped", 1, log);
163 log_write_timestamp(log->handle,
164 settings_get_str("log_close_string"),
168 memset(&lock, 0, sizeof(lock));
169 lock.l_type = F_UNLCK;
170 fcntl(log->handle, F_SETLK, &lock);
173 write_buffer_flush();
178 static void log_rotate_check(LOG_REC *log)
182 g_return_if_fail(log != NULL);
184 if (log->handle == -1 || log->real_fname == NULL)
187 new_fname = log_filename(log);
188 if (strcmp(new_fname, log->real_fname) != 0) {
190 log_stop_logging(log);
191 signal_emit("log rotated", 1, log);
193 log_start_logging(log);
198 void log_write_rec(LOG_REC *log, const char *str, int level)
205 g_return_if_fail(log != NULL);
206 g_return_if_fail(str != NULL);
208 if (log->handle == -1)
212 tm = localtime(&now);
216 tm = localtime(&log->last);
217 day -= tm->tm_mday; /* tm breaks in log_rotate_check() .. */
218 if (tm->tm_hour != hour) {
219 /* hour changed, check if we need to rotate log file */
220 log_rotate_check(log);
225 log_write_timestamp(log->handle,
226 settings_get_str("log_day_changed"),
232 if (log->colorizer == NULL)
235 str = colorstr = log->colorizer(str);
237 if ((level & MSGLEVEL_LASTLOG) == 0)
238 log_write_timestamp(log->handle, log_timestamp, str, now);
240 write_buffer(log->handle, str, strlen(str));
241 write_buffer(log->handle, "\n", 1);
243 signal_emit("log written", 2, log, str);
245 g_free_not_null(colorstr);
248 LOG_ITEM_REC *log_item_find(LOG_REC *log, int type, const char *item,
249 const char *servertag)
253 g_return_val_if_fail(log != NULL, NULL);
254 g_return_val_if_fail(item != NULL, NULL);
256 for (tmp = log->items; tmp != NULL; tmp = tmp->next) {
257 LOG_ITEM_REC *rec = tmp->data;
259 if (rec->type == type && g_strcasecmp(rec->name, item) == 0 &&
260 (rec->servertag == NULL || (servertag != NULL &&
261 g_strcasecmp(rec->servertag, servertag) == 0)))
268 void log_file_write(const char *server_tag, const char *item, int level,
269 const char *str, int no_fallbacks)
271 GSList *tmp, *fallbacks;
275 g_return_if_fail(str != NULL);
280 fallbacks = NULL; found = FALSE;
282 for (tmp = logs; tmp != NULL; tmp = tmp->next) {
283 LOG_REC *rec = tmp->data;
285 if (rec->handle == -1)
286 continue; /* log not opened yet */
288 if ((level & rec->level) == 0)
291 if (rec->items == NULL)
292 fallbacks = g_slist_append(fallbacks, rec);
293 else if (item != NULL &&
294 log_item_find(rec, LOG_ITEM_TARGET, item,
296 log_write_rec(rec, str, level);
299 if (!found && !no_fallbacks && fallbacks != NULL) {
300 /* not found from any items, so write it to all main logs */
301 tmpstr = (level & MSGLEVEL_PUBLIC) && item != NULL ?
302 g_strconcat(item, ": ", str, NULL) :
305 for (tmp = fallbacks; tmp != NULL; tmp = tmp->next)
306 log_write_rec(tmp->data, tmpstr, level);
310 g_slist_free(fallbacks);
313 LOG_REC *log_find(const char *fname)
317 for (tmp = logs; tmp != NULL; tmp = tmp->next) {
318 LOG_REC *rec = tmp->data;
320 if (strcmp(rec->fname, fname) == 0)
327 static void log_items_update_config(LOG_REC *log, CONFIG_NODE *parent)
332 parent = config_node_section(parent, "items", NODE_TYPE_LIST);
333 for (tmp = log->items; tmp != NULL; tmp = tmp->next) {
334 LOG_ITEM_REC *rec = tmp->data;
336 node = config_node_section(parent, NULL, NODE_TYPE_BLOCK);
337 iconfig_node_set_str(node, "type", log_item_types[rec->type]);
338 iconfig_node_set_str(node, "name", rec->name);
339 iconfig_node_set_str(node, "server", rec->servertag);
343 static void log_update_config(LOG_REC *log)
351 node = iconfig_node_traverse("logs", TRUE);
352 node = config_node_section(node, log->fname, NODE_TYPE_BLOCK);
355 iconfig_node_set_bool(node, "auto_open", TRUE);
357 iconfig_node_set_str(node, "auto_open", NULL);
359 levelstr = bits2level(log->level);
360 iconfig_node_set_str(node, "level", levelstr);
363 iconfig_node_set_str(node, "items", NULL);
365 if (log->items != NULL)
366 log_items_update_config(log, node);
368 signal_emit("log config save", 2, log, node);
371 static void log_remove_config(LOG_REC *log)
373 iconfig_set_str("logs", log->fname, NULL);
376 LOG_REC *log_create_rec(const char *fname, int level)
380 g_return_val_if_fail(fname != NULL, NULL);
382 rec = log_find(fname);
384 rec = g_new0(LOG_REC, 1);
385 rec->fname = g_strdup(fname);
386 rec->real_fname = log_filename(rec);
394 void log_item_add(LOG_REC *log, int type, const char *name,
395 const char *servertag)
399 g_return_if_fail(log != NULL);
400 g_return_if_fail(name != NULL);
402 if (log_item_find(log, type, name, servertag))
405 rec = g_new0(LOG_ITEM_REC, 1);
407 rec->name = g_strdup(name);
408 rec->servertag = g_strdup(servertag);
410 log->items = g_slist_append(log->items, rec);
413 void log_update(LOG_REC *log)
415 g_return_if_fail(log != NULL);
417 if (log_find(log->fname) == NULL) {
418 logs = g_slist_append(logs, log);
422 log_update_config(log);
423 signal_emit("log new", 1, log);
426 void log_item_destroy(LOG_REC *log, LOG_ITEM_REC *item)
428 log->items = g_slist_remove(log->items, item);
431 g_free_not_null(item->servertag);
435 static void log_destroy(LOG_REC *log)
437 g_return_if_fail(log != NULL);
439 if (log->handle != -1)
440 log_stop_logging(log);
442 logs = g_slist_remove(logs, log);
443 signal_emit("log remove", 1, log);
445 while (log->items != NULL)
446 log_item_destroy(log, log->items->data);
448 g_free_not_null(log->real_fname);
452 void log_close(LOG_REC *log)
454 g_return_if_fail(log != NULL);
456 log_remove_config(log);
460 static int sig_rotate_check(void)
462 static int last_hour = -1;
466 /* don't do anything until hour is changed */
468 memcpy(&tm, localtime(&now), sizeof(tm));
469 if (tm.tm_hour != last_hour) {
470 last_hour = tm.tm_hour;
471 g_slist_foreach(logs, (GFunc) log_rotate_check, NULL);
476 static void log_items_read_config(CONFIG_NODE *node, LOG_REC *log)
483 tmp = config_node_first(node->value);
484 for (; tmp != NULL; tmp = config_node_next(tmp)) {
487 if (node->type != NODE_TYPE_BLOCK)
490 item = config_node_get_str(node, "name", NULL);
491 type = log_item_str2type(config_node_get_str(node, "type", NULL));
492 if (item == NULL || type == -1)
495 rec = g_new0(LOG_ITEM_REC, 1);
497 rec->name = g_strdup(item);
498 rec->servertag = g_strdup(config_node_get_str(node, "server", NULL));
500 log->items = g_slist_append(log->items, rec);
504 static void log_read_config(void)
508 GSList *tmp, *next, *fnames;
510 /* close old logs, save list of open logs */
512 for (tmp = logs; tmp != NULL; tmp = next) {
519 if (log->handle != -1)
520 fnames = g_slist_append(fnames, g_strdup(log->fname));
524 node = iconfig_node_traverse("logs", FALSE);
525 if (node == NULL) return;
527 tmp = config_node_first(node->value);
528 for (; tmp != NULL; tmp = config_node_next(tmp)) {
531 if (node->type != NODE_TYPE_BLOCK)
534 log = g_new0(LOG_REC, 1);
535 logs = g_slist_append(logs, log);
538 log->fname = g_strdup(node->key);
539 log->autoopen = config_node_get_bool(node, "auto_open", FALSE);
540 log->level = level2bits(config_node_get_str(node, "level", 0));
542 signal_emit("log config read", 2, log, node);
544 node = config_node_section(node, "items", -1);
546 log_items_read_config(node, log);
548 if (log->autoopen || gslist_find_string(fnames, log->fname))
549 log_start_logging(log);
552 g_slist_foreach(fnames, (GFunc) g_free, NULL);
553 g_slist_free(fnames);
556 static void read_settings(void)
558 log_timestamp = settings_get_str("log_timestamp");
559 log_file_create_mode = octal2dec(settings_get_int("log_create_mode"));
564 rotate_tag = g_timeout_add(60000, (GSourceFunc) sig_rotate_check, NULL);
567 settings_add_int("log", "log_create_mode",
568 DEFAULT_LOG_FILE_CREATE_MODE);
569 settings_add_str("log", "log_timestamp", "%H:%M ");
570 settings_add_str("log", "log_open_string",
571 "--- Log opened %a %b %d %H:%M:%S %Y");
572 settings_add_str("log", "log_close_string",
573 "--- Log closed %a %b %d %H:%M:%S %Y");
574 settings_add_str("log", "log_day_changed",
575 "--- Day changed %a %b %d %Y");
578 signal_add("setup changed", (SIGNAL_FUNC) read_settings);
579 signal_add("setup reread", (SIGNAL_FUNC) log_read_config);
580 signal_add("irssi init finished", (SIGNAL_FUNC) log_read_config);
583 void log_deinit(void)
585 g_source_remove(rotate_tag);
588 log_close(logs->data);
590 signal_remove("setup changed", (SIGNAL_FUNC) read_settings);
591 signal_remove("setup reread", (SIGNAL_FUNC) log_read_config);
592 signal_remove("irssi init finished", (SIGNAL_FUNC) log_read_config);