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 log_dir_create_mode;
51 static int rotate_tag;
53 static int log_item_str2type(const char *type)
57 for (n = 0; log_item_types[n] != NULL; n++) {
58 if (g_strcasecmp(log_item_types[n], type) == 0)
65 static void log_write_timestamp(int handle, const char *format,
66 const char *text, time_t stamp)
71 g_return_if_fail(format != NULL);
72 if (*format == '\0') return;
74 tm = localtime(&stamp);
75 if (strftime(str, sizeof(str), format, tm) > 0)
76 write_buffer(handle, str, strlen(str));
77 if (text != NULL) write_buffer(handle, text, strlen(text));
80 static char *log_filename(LOG_REC *log)
82 char *str, fname[1024];
90 str = convert_home(log->fname);
91 ret = strftime(fname, sizeof(fname), str, tm);
95 g_warning("log_filename() : strftime() failed");
99 return g_strdup(fname);
102 int log_start_logging(LOG_REC *log)
106 g_return_val_if_fail(log != NULL, FALSE);
108 if (log->handle != -1)
111 /* Append/create log file */
112 g_free_not_null(log->real_fname);
113 log->real_fname = log_filename(log);
115 if (log->real_fname != NULL &&
116 strcmp(log->real_fname, log->fname) != 0) {
117 /* path may contain variables (%time, $vars),
118 make sure the directory is created */
119 dir = g_dirname(log->real_fname);
120 mkpath(dir, log_dir_create_mode);
124 log->handle = log->real_fname == NULL ? -1 :
125 open(log->real_fname, O_WRONLY | O_APPEND | O_CREAT,
126 log_file_create_mode);
127 if (log->handle == -1) {
128 signal_emit("log create failed", 1, log);
133 memset(&lock, 0, sizeof(lock));
134 lock.l_type = F_WRLCK;
135 if (fcntl(log->handle, F_SETLK, &lock) == -1 && errno == EACCES) {
138 signal_emit("log locked", 1, log);
143 lseek(log->handle, 0, SEEK_END);
145 log->opened = log->last = time(NULL);
146 log_write_timestamp(log->handle,
147 settings_get_str("log_open_string"),
150 signal_emit("log started", 1, log);
155 void log_stop_logging(LOG_REC *log)
157 g_return_if_fail(log != NULL);
159 if (log->handle == -1)
162 signal_emit("log stopped", 1, log);
164 log_write_timestamp(log->handle,
165 settings_get_str("log_close_string"),
169 memset(&lock, 0, sizeof(lock));
170 lock.l_type = F_UNLCK;
171 fcntl(log->handle, F_SETLK, &lock);
174 write_buffer_flush();
179 static void log_rotate_check(LOG_REC *log)
183 g_return_if_fail(log != NULL);
185 if (log->handle == -1 || log->real_fname == NULL)
188 new_fname = log_filename(log);
189 if (strcmp(new_fname, log->real_fname) != 0) {
191 log_stop_logging(log);
192 signal_emit("log rotated", 1, log);
194 log_start_logging(log);
199 void log_write_rec(LOG_REC *log, const char *str, int level)
206 g_return_if_fail(log != NULL);
207 g_return_if_fail(str != NULL);
209 if (log->handle == -1)
213 tm = localtime(&now);
217 tm = localtime(&log->last);
218 day -= tm->tm_mday; /* tm breaks in log_rotate_check() .. */
219 if (tm->tm_hour != hour) {
220 /* hour changed, check if we need to rotate log file */
221 log_rotate_check(log);
226 log_write_timestamp(log->handle,
227 settings_get_str("log_day_changed"),
233 if (log->colorizer == NULL)
236 str = colorstr = log->colorizer(str);
238 if ((level & MSGLEVEL_LASTLOG) == 0)
239 log_write_timestamp(log->handle, log_timestamp, str, now);
241 write_buffer(log->handle, str, strlen(str));
242 write_buffer(log->handle, "\n", 1);
244 signal_emit("log written", 2, log, str);
246 g_free_not_null(colorstr);
249 LOG_ITEM_REC *log_item_find(LOG_REC *log, int type, const char *item,
250 const char *servertag)
254 g_return_val_if_fail(log != NULL, NULL);
255 g_return_val_if_fail(item != NULL, NULL);
257 for (tmp = log->items; tmp != NULL; tmp = tmp->next) {
258 LOG_ITEM_REC *rec = tmp->data;
260 if (rec->type == type && g_strcasecmp(rec->name, item) == 0 &&
261 (rec->servertag == NULL || (servertag != NULL &&
262 g_strcasecmp(rec->servertag, servertag) == 0)))
269 void log_file_write(const char *server_tag, const char *item, int level,
270 const char *str, int no_fallbacks)
272 GSList *tmp, *fallbacks;
276 g_return_if_fail(str != NULL);
281 fallbacks = NULL; found = FALSE;
283 for (tmp = logs; tmp != NULL; tmp = tmp->next) {
284 LOG_REC *rec = tmp->data;
286 if (rec->handle == -1)
287 continue; /* log not opened yet */
289 if ((level & rec->level) == 0)
292 if (rec->items == NULL)
293 fallbacks = g_slist_append(fallbacks, rec);
294 else if (item != NULL &&
295 log_item_find(rec, LOG_ITEM_TARGET, item,
297 log_write_rec(rec, str, level);
300 if (!found && !no_fallbacks && fallbacks != NULL) {
301 /* not found from any items, so write it to all main logs */
302 tmpstr = (level & MSGLEVEL_PUBLIC) && item != NULL ?
303 g_strconcat(item, ": ", str, NULL) :
306 for (tmp = fallbacks; tmp != NULL; tmp = tmp->next)
307 log_write_rec(tmp->data, tmpstr, level);
311 g_slist_free(fallbacks);
314 LOG_REC *log_find(const char *fname)
318 for (tmp = logs; tmp != NULL; tmp = tmp->next) {
319 LOG_REC *rec = tmp->data;
321 if (strcmp(rec->fname, fname) == 0)
328 static void log_items_update_config(LOG_REC *log, CONFIG_NODE *parent)
333 parent = config_node_section(parent, "items", NODE_TYPE_LIST);
334 for (tmp = log->items; tmp != NULL; tmp = tmp->next) {
335 LOG_ITEM_REC *rec = tmp->data;
337 node = config_node_section(parent, NULL, NODE_TYPE_BLOCK);
338 iconfig_node_set_str(node, "type", log_item_types[rec->type]);
339 iconfig_node_set_str(node, "name", rec->name);
340 iconfig_node_set_str(node, "server", rec->servertag);
344 static void log_update_config(LOG_REC *log)
352 node = iconfig_node_traverse("logs", TRUE);
353 node = config_node_section(node, log->fname, NODE_TYPE_BLOCK);
356 iconfig_node_set_bool(node, "auto_open", TRUE);
358 iconfig_node_set_str(node, "auto_open", NULL);
360 levelstr = bits2level(log->level);
361 iconfig_node_set_str(node, "level", levelstr);
364 iconfig_node_set_str(node, "items", NULL);
366 if (log->items != NULL)
367 log_items_update_config(log, node);
369 signal_emit("log config save", 2, log, node);
372 static void log_remove_config(LOG_REC *log)
374 iconfig_set_str("logs", log->fname, NULL);
377 LOG_REC *log_create_rec(const char *fname, int level)
381 g_return_val_if_fail(fname != NULL, NULL);
383 rec = log_find(fname);
385 rec = g_new0(LOG_REC, 1);
386 rec->fname = g_strdup(fname);
387 rec->real_fname = log_filename(rec);
395 void log_item_add(LOG_REC *log, int type, const char *name,
396 const char *servertag)
400 g_return_if_fail(log != NULL);
401 g_return_if_fail(name != NULL);
403 if (log_item_find(log, type, name, servertag))
406 rec = g_new0(LOG_ITEM_REC, 1);
408 rec->name = g_strdup(name);
409 rec->servertag = g_strdup(servertag);
411 log->items = g_slist_append(log->items, rec);
414 void log_update(LOG_REC *log)
416 g_return_if_fail(log != NULL);
418 if (log_find(log->fname) == NULL) {
419 logs = g_slist_append(logs, log);
423 log_update_config(log);
424 signal_emit("log new", 1, log);
427 void log_item_destroy(LOG_REC *log, LOG_ITEM_REC *item)
429 log->items = g_slist_remove(log->items, item);
432 g_free_not_null(item->servertag);
436 static void log_destroy(LOG_REC *log)
438 g_return_if_fail(log != NULL);
440 if (log->handle != -1)
441 log_stop_logging(log);
443 logs = g_slist_remove(logs, log);
444 signal_emit("log remove", 1, log);
446 while (log->items != NULL)
447 log_item_destroy(log, log->items->data);
449 g_free_not_null(log->real_fname);
453 void log_close(LOG_REC *log)
455 g_return_if_fail(log != NULL);
457 log_remove_config(log);
461 static int sig_rotate_check(void)
463 static int last_hour = -1;
467 /* don't do anything until hour is changed */
469 memcpy(&tm, localtime(&now), sizeof(tm));
470 if (tm.tm_hour != last_hour) {
471 last_hour = tm.tm_hour;
472 g_slist_foreach(logs, (GFunc) log_rotate_check, NULL);
477 static void log_items_read_config(CONFIG_NODE *node, LOG_REC *log)
484 tmp = config_node_first(node->value);
485 for (; tmp != NULL; tmp = config_node_next(tmp)) {
488 if (node->type != NODE_TYPE_BLOCK)
491 item = config_node_get_str(node, "name", NULL);
492 type = log_item_str2type(config_node_get_str(node, "type", NULL));
493 if (item == NULL || type == -1)
496 rec = g_new0(LOG_ITEM_REC, 1);
498 rec->name = g_strdup(item);
499 rec->servertag = g_strdup(config_node_get_str(node, "server", NULL));
501 log->items = g_slist_append(log->items, rec);
505 static void log_read_config(void)
509 GSList *tmp, *next, *fnames;
511 /* close old logs, save list of open logs */
513 for (tmp = logs; tmp != NULL; tmp = next) {
520 if (log->handle != -1)
521 fnames = g_slist_append(fnames, g_strdup(log->fname));
525 node = iconfig_node_traverse("logs", FALSE);
526 if (node == NULL) return;
528 tmp = config_node_first(node->value);
529 for (; tmp != NULL; tmp = config_node_next(tmp)) {
532 if (node->type != NODE_TYPE_BLOCK)
535 log = g_new0(LOG_REC, 1);
536 logs = g_slist_append(logs, log);
539 log->fname = g_strdup(node->key);
540 log->autoopen = config_node_get_bool(node, "auto_open", FALSE);
541 log->level = level2bits(config_node_get_str(node, "level", 0));
543 signal_emit("log config read", 2, log, node);
545 node = config_node_section(node, "items", -1);
547 log_items_read_config(node, log);
549 if (log->autoopen || gslist_find_string(fnames, log->fname))
550 log_start_logging(log);
553 g_slist_foreach(fnames, (GFunc) g_free, NULL);
554 g_slist_free(fnames);
557 static void read_settings(void)
559 log_timestamp = settings_get_str("log_timestamp");
560 log_file_create_mode = octal2dec(settings_get_int("log_create_mode"));
562 log_dir_create_mode = log_file_create_mode;
563 if (log_file_create_mode & 0400) log_dir_create_mode |= 0100;
564 if (log_file_create_mode & 0040) log_dir_create_mode |= 0010;
565 if (log_file_create_mode & 0004) log_dir_create_mode |= 0001;
570 rotate_tag = g_timeout_add(60000, (GSourceFunc) sig_rotate_check, NULL);
573 settings_add_int("log", "log_create_mode",
574 DEFAULT_LOG_FILE_CREATE_MODE);
575 settings_add_str("log", "log_timestamp", "%H:%M ");
576 settings_add_str("log", "log_open_string",
577 "--- Log opened %a %b %d %H:%M:%S %Y");
578 settings_add_str("log", "log_close_string",
579 "--- Log closed %a %b %d %H:%M:%S %Y");
580 settings_add_str("log", "log_day_changed",
581 "--- Day changed %a %b %d %Y");
584 signal_add("setup changed", (SIGNAL_FUNC) read_settings);
585 signal_add("setup reread", (SIGNAL_FUNC) log_read_config);
586 signal_add("irssi init finished", (SIGNAL_FUNC) log_read_config);
589 void log_deinit(void)
591 g_source_remove(rotate_tag);
594 log_close(logs->data);
596 signal_remove("setup changed", (SIGNAL_FUNC) read_settings);
597 signal_remove("setup reread", (SIGNAL_FUNC) log_read_config);
598 signal_remove("irssi init finished", (SIGNAL_FUNC) log_read_config);