Merged with Irssi 0.8.6.
[silc.git] / apps / irssi / src / core / log.c
1 /*
2  log.c : irssi
3
4     Copyright (C) 1999-2000 Timo Sirainen
5
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.
10
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.
15
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
19 */
20
21 #include "module.h"
22 #include "signals.h"
23 #include "commands.h"
24 #include "levels.h"
25 #include "misc.h"
26 #include "servers.h"
27 #include "log.h"
28 #include "write-buffer.h"
29
30 #include "lib-config/iconfig.h"
31 #include "settings.h"
32
33 #define DEFAULT_LOG_FILE_CREATE_MODE 600
34
35 #ifdef HAVE_FCNTL
36 static struct flock lock;
37 #endif
38
39 GSList *logs;
40
41 static const char *log_item_types[] = {
42         "target",
43         "window",
44
45         NULL
46 };
47
48 const char *log_timestamp;
49 static int log_file_create_mode;
50 static int log_dir_create_mode;
51 static int rotate_tag;
52
53 static int log_item_str2type(const char *type)
54 {
55         int n;
56
57         for (n = 0; log_item_types[n] != NULL; n++) {
58                 if (g_strcasecmp(log_item_types[n], type) == 0)
59                         return n;
60         }
61
62         return -1;
63 }
64
65 static void log_write_timestamp(int handle, const char *format,
66                                 const char *text, time_t stamp)
67 {
68         struct tm *tm;
69         char str[256];
70
71         g_return_if_fail(format != NULL);
72         if (*format == '\0') return;
73
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));
78 }
79
80 static char *log_filename(LOG_REC *log)
81 {
82         char *str, fname[1024];
83         struct tm *tm;
84         size_t ret;
85         time_t now;
86
87         now = time(NULL);
88         tm = localtime(&now);
89
90         str = convert_home(log->fname);
91         ret = strftime(fname, sizeof(fname), str, tm);
92         g_free(str);
93
94         if (ret <= 0) {
95                 g_warning("log_filename() : strftime() failed");
96                 return NULL;
97         }
98
99         return g_strdup(fname);
100 }
101
102 int log_start_logging(LOG_REC *log)
103 {
104         char *dir;
105
106         g_return_val_if_fail(log != NULL, FALSE);
107
108         if (log->handle != -1)
109                 return TRUE;
110
111         /* Append/create log file */
112         g_free_not_null(log->real_fname);
113         log->real_fname = log_filename(log);
114
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);
121                 g_free(dir);
122         }
123
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);
129                 log->failed = TRUE;
130                 return FALSE;
131         }
132 #ifdef HAVE_FCNTL
133         memset(&lock, 0, sizeof(lock));
134         lock.l_type = F_WRLCK;
135         if (fcntl(log->handle, F_SETLK, &lock) == -1 && errno == EACCES) {
136                 close(log->handle);
137                 log->handle = -1;
138                 signal_emit("log locked", 1, log);
139                 log->failed = TRUE;
140                 return FALSE;
141         }
142 #endif
143         lseek(log->handle, 0, SEEK_END);
144
145         log->opened = log->last = time(NULL);
146         log_write_timestamp(log->handle,
147                             settings_get_str("log_open_string"),
148                             "\n", log->last);
149
150         signal_emit("log started", 1, log);
151         log->failed = FALSE;
152         return TRUE;
153 }
154
155 void log_stop_logging(LOG_REC *log)
156 {
157         g_return_if_fail(log != NULL);
158
159         if (log->handle == -1)
160                 return;
161
162         signal_emit("log stopped", 1, log);
163
164         log_write_timestamp(log->handle,
165                             settings_get_str("log_close_string"),
166                             "\n", time(NULL));
167
168 #ifdef HAVE_FCNTL
169         memset(&lock, 0, sizeof(lock));
170         lock.l_type = F_UNLCK;
171         fcntl(log->handle, F_SETLK, &lock);
172 #endif
173
174         write_buffer_flush();
175         close(log->handle);
176         log->handle = -1;
177 }
178
179 static void log_rotate_check(LOG_REC *log)
180 {
181         char *new_fname;
182
183         g_return_if_fail(log != NULL);
184
185         if (log->handle == -1 || log->real_fname == NULL)
186                 return;
187
188         new_fname = log_filename(log);
189         if (strcmp(new_fname, log->real_fname) != 0) {
190                 /* rotate log */
191                 log_stop_logging(log);
192                 signal_emit("log rotated", 1, log);
193
194                 log_start_logging(log);
195         }
196         g_free(new_fname);
197 }
198
199 void log_write_rec(LOG_REC *log, const char *str, int level)
200 {
201         char *colorstr;
202         struct tm *tm;
203         time_t now;
204         int hour, day;
205
206         g_return_if_fail(log != NULL);
207         g_return_if_fail(str != NULL);
208
209         if (log->handle == -1)
210                 return;
211
212         now = time(NULL);
213         tm = localtime(&now);
214         hour = tm->tm_hour;
215         day = tm->tm_mday;
216
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);
222         }
223
224         if (day != 0) {
225                 /* day changed */
226                 log_write_timestamp(log->handle,
227                                     settings_get_str("log_day_changed"),
228                                     "\n", now);
229         }
230
231         log->last = now;
232
233         if (log->colorizer == NULL)
234                 colorstr = NULL;
235         else
236                 str = colorstr = log->colorizer(str);
237
238         if ((level & MSGLEVEL_LASTLOG) == 0)
239                 log_write_timestamp(log->handle, log_timestamp, str, now);
240         else
241                 write_buffer(log->handle, str, strlen(str));
242         write_buffer(log->handle, "\n", 1);
243
244         signal_emit("log written", 2, log, str);
245
246         g_free_not_null(colorstr);
247 }
248
249 LOG_ITEM_REC *log_item_find(LOG_REC *log, int type, const char *item,
250                             const char *servertag)
251 {
252         GSList *tmp;
253
254         g_return_val_if_fail(log != NULL, NULL);
255         g_return_val_if_fail(item != NULL, NULL);
256
257         for (tmp = log->items; tmp != NULL; tmp = tmp->next) {
258                 LOG_ITEM_REC *rec = tmp->data;
259
260                 if (rec->type == type && g_strcasecmp(rec->name, item) == 0 &&
261                     (rec->servertag == NULL || (servertag != NULL &&
262                         g_strcasecmp(rec->servertag, servertag) == 0)))
263                         return rec;
264         }
265
266         return NULL;
267 }
268
269 void log_file_write(const char *server_tag, const char *item, int level,
270                     const char *str, int no_fallbacks)
271 {
272         GSList *tmp, *fallbacks;
273         char *tmpstr;
274         int found;
275
276         g_return_if_fail(str != NULL);
277
278         if (logs == NULL)
279                 return;
280
281         fallbacks = NULL; found = FALSE;
282
283         for (tmp = logs; tmp != NULL; tmp = tmp->next) {
284                 LOG_REC *rec = tmp->data;
285
286                 if (rec->handle == -1)
287                         continue; /* log not opened yet */
288
289                 if ((level & rec->level) == 0)
290                         continue;
291
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,
296                                        server_tag) != NULL)
297                         log_write_rec(rec, str, level);
298         }
299
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) :
304                         g_strdup(str);
305
306                 for (tmp = fallbacks; tmp != NULL; tmp = tmp->next)
307                         log_write_rec(tmp->data, tmpstr, level);
308
309                 g_free(tmpstr);
310         }
311         g_slist_free(fallbacks);
312 }
313
314 LOG_REC *log_find(const char *fname)
315 {
316         GSList *tmp;
317
318         for (tmp = logs; tmp != NULL; tmp = tmp->next) {
319                 LOG_REC *rec = tmp->data;
320
321                 if (strcmp(rec->fname, fname) == 0)
322                         return rec;
323         }
324
325         return NULL;
326 }
327
328 static void log_items_update_config(LOG_REC *log, CONFIG_NODE *parent)
329 {
330         GSList *tmp;
331         CONFIG_NODE *node;
332
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;
336
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);
341         }
342 }
343
344 static void log_update_config(LOG_REC *log)
345 {
346         CONFIG_NODE *node;
347         char *levelstr;
348
349         if (log->temp)
350                 return;
351
352         node = iconfig_node_traverse("logs", TRUE);
353         node = config_node_section(node, log->fname, NODE_TYPE_BLOCK);
354
355         if (log->autoopen)
356                 iconfig_node_set_bool(node, "auto_open", TRUE);
357         else
358                 iconfig_node_set_str(node, "auto_open", NULL);
359
360         levelstr = bits2level(log->level);
361         iconfig_node_set_str(node, "level", levelstr);
362         g_free(levelstr);
363
364         iconfig_node_set_str(node, "items", NULL);
365
366         if (log->items != NULL)
367                 log_items_update_config(log, node);
368
369         signal_emit("log config save", 2, log, node);
370 }
371
372 static void log_remove_config(LOG_REC *log)
373 {
374         iconfig_set_str("logs", log->fname, NULL);
375 }
376
377 LOG_REC *log_create_rec(const char *fname, int level)
378 {
379         LOG_REC *rec;
380
381         g_return_val_if_fail(fname != NULL, NULL);
382
383         rec = log_find(fname);
384         if (rec == NULL) {
385                 rec = g_new0(LOG_REC, 1);
386                 rec->fname = g_strdup(fname);
387                 rec->real_fname = log_filename(rec);
388                 rec->handle = -1;
389         }
390
391         rec->level = level;
392         return rec;
393 }
394
395 void log_item_add(LOG_REC *log, int type, const char *name,
396                   const char *servertag)
397 {
398         LOG_ITEM_REC *rec;
399
400         g_return_if_fail(log != NULL);
401         g_return_if_fail(name != NULL);
402
403         if (log_item_find(log, type, name, servertag))
404                 return;
405
406         rec = g_new0(LOG_ITEM_REC, 1);
407         rec->type = type;
408         rec->name = g_strdup(name);
409         rec->servertag = g_strdup(servertag);
410
411         log->items = g_slist_append(log->items, rec);
412 }
413
414 void log_update(LOG_REC *log)
415 {
416         g_return_if_fail(log != NULL);
417
418         if (log_find(log->fname) == NULL) {
419                 logs = g_slist_append(logs, log);
420                 log->handle = -1;
421         }
422
423         log_update_config(log);
424         signal_emit("log new", 1, log);
425 }
426
427 void log_item_destroy(LOG_REC *log, LOG_ITEM_REC *item)
428 {
429         log->items = g_slist_remove(log->items, item);
430
431         g_free(item->name);
432         g_free_not_null(item->servertag);
433         g_free(item);
434 }
435
436 static void log_destroy(LOG_REC *log)
437 {
438         g_return_if_fail(log != NULL);
439
440         if (log->handle != -1)
441                 log_stop_logging(log);
442
443         logs = g_slist_remove(logs, log);
444         signal_emit("log remove", 1, log);
445
446         while (log->items != NULL)
447                 log_item_destroy(log, log->items->data);
448         g_free(log->fname);
449         g_free_not_null(log->real_fname);
450         g_free(log);
451 }
452
453 void log_close(LOG_REC *log)
454 {
455         g_return_if_fail(log != NULL);
456
457         log_remove_config(log);
458         log_destroy(log);
459 }
460
461 static int sig_rotate_check(void)
462 {
463         static int last_hour = -1;
464         struct tm tm;
465         time_t now;
466
467         /* don't do anything until hour is changed */
468         now = time(NULL);
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);
473         }
474         return 1;
475 }
476
477 static void log_items_read_config(CONFIG_NODE *node, LOG_REC *log)
478 {
479         LOG_ITEM_REC *rec;
480         GSList *tmp;
481         char *item;
482         int type;
483
484         tmp = config_node_first(node->value);
485         for (; tmp != NULL; tmp = config_node_next(tmp)) {
486                 node = tmp->data;
487
488                 if (node->type != NODE_TYPE_BLOCK)
489                         continue;
490
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)
494                         continue;
495
496                 rec = g_new0(LOG_ITEM_REC, 1);
497                 rec->type = type;
498                 rec->name = g_strdup(item);
499                 rec->servertag = g_strdup(config_node_get_str(node, "server", NULL));
500
501                 log->items = g_slist_append(log->items, rec);
502         }
503 }
504
505 static void log_read_config(void)
506 {
507         CONFIG_NODE *node;
508         LOG_REC *log;
509         GSList *tmp, *next, *fnames;
510
511         /* close old logs, save list of open logs */
512         fnames = NULL;
513         for (tmp = logs; tmp != NULL; tmp = next) {
514                 log = tmp->data;
515
516                 next = tmp->next;
517                 if (log->temp)
518                         continue;
519
520                 if (log->handle != -1)
521                         fnames = g_slist_append(fnames, g_strdup(log->fname));
522                 log_destroy(log);
523         }
524
525         node = iconfig_node_traverse("logs", FALSE);
526         if (node == NULL) return;
527
528         tmp = config_node_first(node->value);
529         for (; tmp != NULL; tmp = config_node_next(tmp)) {
530                 node = tmp->data;
531
532                 if (node->type != NODE_TYPE_BLOCK)
533                         continue;
534
535                 log = g_new0(LOG_REC, 1);
536                 logs = g_slist_append(logs, log);
537
538                 log->handle = -1;
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));
542
543                 signal_emit("log config read", 2, log, node);
544
545                 node = config_node_section(node, "items", -1);
546                 if (node != NULL)
547                         log_items_read_config(node, log);
548
549                 if (log->autoopen || gslist_find_string(fnames, log->fname))
550                         log_start_logging(log);
551         }
552
553         g_slist_foreach(fnames, (GFunc) g_free, NULL);
554         g_slist_free(fnames);
555 }
556
557 static void read_settings(void)
558 {
559         log_timestamp = settings_get_str("log_timestamp");
560         log_file_create_mode = octal2dec(settings_get_int("log_create_mode"));
561
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;
566 }
567
568 void log_init(void)
569 {
570         rotate_tag = g_timeout_add(60000, (GSourceFunc) sig_rotate_check, NULL);
571         logs = NULL;
572
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");
582
583         read_settings();
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);
587 }
588
589 void log_deinit(void)
590 {
591         g_source_remove(rotate_tag);
592
593         while (logs != NULL)
594                 log_close(logs->data);
595
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);
599 }