2000-04-26 04:03:38 -04:00
|
|
|
/*
|
|
|
|
log.c : irssi
|
|
|
|
|
|
|
|
Copyright (C) 1999-2000 Timo Sirainen
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "module.h"
|
|
|
|
#include "signals.h"
|
|
|
|
#include "commands.h"
|
|
|
|
#include "levels.h"
|
|
|
|
#include "misc.h"
|
2000-10-17 19:37:21 -04:00
|
|
|
#include "servers.h"
|
2000-04-26 04:03:38 -04:00
|
|
|
#include "log.h"
|
2001-02-10 00:54:35 -05:00
|
|
|
#include "write-buffer.h"
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
#include "lib-config/iconfig.h"
|
|
|
|
#include "settings.h"
|
|
|
|
|
2000-06-01 21:04:42 -04:00
|
|
|
#define DEFAULT_LOG_FILE_CREATE_MODE 644
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
#ifdef HAVE_FCNTL
|
|
|
|
static struct flock lock;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
GSList *logs;
|
|
|
|
|
2000-10-17 19:37:21 -04:00
|
|
|
static const char *log_item_types[] = {
|
|
|
|
"target",
|
|
|
|
"window",
|
|
|
|
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
const char *log_timestamp;
|
|
|
|
static int log_file_create_mode;
|
|
|
|
static int rotate_tag;
|
|
|
|
|
2000-10-17 19:37:21 -04:00
|
|
|
static int log_item_str2type(const char *type)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
|
|
|
|
for (n = 0; log_item_types[n] != NULL; n++) {
|
|
|
|
if (g_strcasecmp(log_item_types[n], type) == 0)
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2000-07-16 16:18:05 -04:00
|
|
|
static void log_write_timestamp(int handle, const char *format,
|
2001-02-17 07:08:31 -05:00
|
|
|
const char *text, time_t stamp)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
|
|
|
struct tm *tm;
|
|
|
|
char str[256];
|
|
|
|
|
2000-05-18 04:46:56 -04:00
|
|
|
g_return_if_fail(format != NULL);
|
|
|
|
if (*format == '\0') return;
|
|
|
|
|
2000-07-16 16:18:05 -04:00
|
|
|
tm = localtime(&stamp);
|
2001-02-17 07:08:31 -05:00
|
|
|
if (strftime(str, sizeof(str), format, tm) > 0)
|
2001-02-10 00:54:35 -05:00
|
|
|
write_buffer(handle, str, strlen(str));
|
2001-02-17 07:08:31 -05:00
|
|
|
if (text != NULL) write_buffer(handle, text, strlen(text));
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
2000-08-11 18:07:42 -04:00
|
|
|
static char *log_filename(LOG_REC *log)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
|
|
|
char *str, fname[1024];
|
|
|
|
struct tm *tm;
|
2001-01-14 13:02:03 -05:00
|
|
|
size_t ret;
|
2000-07-16 16:18:05 -04:00
|
|
|
time_t now;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-07-16 16:18:05 -04:00
|
|
|
now = time(NULL);
|
|
|
|
tm = localtime(&now);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
str = convert_home(log->fname);
|
2001-01-14 13:02:03 -05:00
|
|
|
ret = strftime(fname, sizeof(fname), str, tm);
|
2000-04-26 04:03:38 -04:00
|
|
|
g_free(str);
|
|
|
|
|
2001-01-14 13:02:03 -05:00
|
|
|
if (ret <= 0) {
|
|
|
|
g_warning("log_filename() : strftime() failed");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2000-08-11 18:07:42 -04:00
|
|
|
return g_strdup(fname);
|
|
|
|
}
|
|
|
|
|
|
|
|
int log_start_logging(LOG_REC *log)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail(log != NULL, FALSE);
|
|
|
|
|
|
|
|
if (log->handle != -1)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
/* Append/create log file */
|
|
|
|
g_free_not_null(log->real_fname);
|
|
|
|
log->real_fname = log_filename(log);
|
2001-01-14 13:02:03 -05:00
|
|
|
log->handle = log->real_fname == NULL ? -1 :
|
|
|
|
open(log->real_fname, O_WRONLY | O_APPEND | O_CREAT,
|
|
|
|
log_file_create_mode);
|
2000-05-18 04:46:56 -04:00
|
|
|
if (log->handle == -1) {
|
|
|
|
signal_emit("log create failed", 1, log);
|
2000-10-13 17:53:25 -04:00
|
|
|
log->failed = TRUE;
|
2000-05-18 04:46:56 -04:00
|
|
|
return FALSE;
|
|
|
|
}
|
2000-04-26 04:03:38 -04:00
|
|
|
#ifdef HAVE_FCNTL
|
|
|
|
memset(&lock, 0, sizeof(lock));
|
|
|
|
lock.l_type = F_WRLCK;
|
2001-02-16 15:21:18 -05:00
|
|
|
if (fcntl(log->handle, F_SETLK, &lock) == -1 && errno == EACCES) {
|
2000-04-26 04:03:38 -04:00
|
|
|
close(log->handle);
|
|
|
|
log->handle = -1;
|
|
|
|
signal_emit("log locked", 1, log);
|
2000-10-13 17:53:25 -04:00
|
|
|
log->failed = TRUE;
|
2000-04-26 04:03:38 -04:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
lseek(log->handle, 0, SEEK_END);
|
|
|
|
|
|
|
|
log->opened = log->last = time(NULL);
|
2000-07-16 16:18:05 -04:00
|
|
|
log_write_timestamp(log->handle,
|
|
|
|
settings_get_str("log_open_string"),
|
|
|
|
"\n", log->last);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
signal_emit("log started", 1, log);
|
2000-10-13 17:53:25 -04:00
|
|
|
log->failed = FALSE;
|
2000-04-26 04:03:38 -04:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void log_stop_logging(LOG_REC *log)
|
|
|
|
{
|
|
|
|
g_return_if_fail(log != NULL);
|
|
|
|
|
|
|
|
if (log->handle == -1)
|
|
|
|
return;
|
|
|
|
|
|
|
|
signal_emit("log stopped", 1, log);
|
|
|
|
|
2000-07-16 16:18:05 -04:00
|
|
|
log_write_timestamp(log->handle,
|
|
|
|
settings_get_str("log_close_string"),
|
|
|
|
"\n", time(NULL));
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
#ifdef HAVE_FCNTL
|
|
|
|
memset(&lock, 0, sizeof(lock));
|
|
|
|
lock.l_type = F_UNLCK;
|
|
|
|
fcntl(log->handle, F_SETLK, &lock);
|
|
|
|
#endif
|
|
|
|
|
2001-02-10 00:54:35 -05:00
|
|
|
write_buffer_flush();
|
2000-04-26 04:03:38 -04:00
|
|
|
close(log->handle);
|
|
|
|
log->handle = -1;
|
|
|
|
}
|
|
|
|
|
2000-08-11 18:07:42 -04:00
|
|
|
static void log_rotate_check(LOG_REC *log)
|
|
|
|
{
|
|
|
|
char *new_fname;
|
|
|
|
|
|
|
|
g_return_if_fail(log != NULL);
|
|
|
|
|
|
|
|
if (log->handle == -1 || log->real_fname == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
new_fname = log_filename(log);
|
|
|
|
if (strcmp(new_fname, log->real_fname) != 0) {
|
|
|
|
/* rotate log */
|
|
|
|
log_stop_logging(log);
|
|
|
|
log_start_logging(log);
|
|
|
|
}
|
|
|
|
g_free(new_fname);
|
|
|
|
}
|
|
|
|
|
2001-02-17 07:08:31 -05:00
|
|
|
void log_write_rec(LOG_REC *log, const char *str, int level)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
|
|
|
struct tm *tm;
|
2000-07-16 16:18:05 -04:00
|
|
|
time_t now;
|
2000-08-11 18:07:42 -04:00
|
|
|
int hour, day;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
g_return_if_fail(log != NULL);
|
|
|
|
g_return_if_fail(str != NULL);
|
|
|
|
|
|
|
|
if (log->handle == -1)
|
|
|
|
return;
|
|
|
|
|
2000-07-16 16:18:05 -04:00
|
|
|
now = time(NULL);
|
|
|
|
tm = localtime(&now);
|
2000-08-11 18:07:42 -04:00
|
|
|
hour = tm->tm_hour;
|
2000-04-26 04:03:38 -04:00
|
|
|
day = tm->tm_mday;
|
|
|
|
|
|
|
|
tm = localtime(&log->last);
|
2000-08-25 17:10:05 -04:00
|
|
|
day -= tm->tm_mday; /* tm breaks in log_rotate_check() .. */
|
2000-08-11 18:07:42 -04:00
|
|
|
if (tm->tm_hour != hour) {
|
|
|
|
/* hour changed, check if we need to rotate log file */
|
|
|
|
log_rotate_check(log);
|
|
|
|
}
|
|
|
|
|
2000-08-25 17:10:05 -04:00
|
|
|
if (day != 0) {
|
2000-04-26 04:03:38 -04:00
|
|
|
/* day changed */
|
2000-07-16 16:18:05 -04:00
|
|
|
log_write_timestamp(log->handle,
|
|
|
|
settings_get_str("log_day_changed"),
|
|
|
|
"\n", now);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
2000-08-25 17:10:05 -04:00
|
|
|
|
2000-07-16 16:18:05 -04:00
|
|
|
log->last = now;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2001-02-17 07:08:31 -05:00
|
|
|
if ((level & MSGLEVEL_LASTLOG) == 0)
|
|
|
|
log_write_timestamp(log->handle, log_timestamp, str, now);
|
|
|
|
else
|
|
|
|
write_buffer(log->handle, str, strlen(str));
|
2001-02-10 00:54:35 -05:00
|
|
|
write_buffer(log->handle, "\n", 1);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
signal_emit("log written", 2, log, str);
|
|
|
|
}
|
|
|
|
|
2000-10-17 19:37:21 -04:00
|
|
|
LOG_ITEM_REC *log_item_find(LOG_REC *log, int type, const char *item,
|
2001-02-17 14:44:22 -05:00
|
|
|
const char *servertag)
|
2000-10-17 19:37:21 -04:00
|
|
|
{
|
|
|
|
GSList *tmp;
|
|
|
|
|
|
|
|
g_return_val_if_fail(log != NULL, NULL);
|
|
|
|
g_return_val_if_fail(item != NULL, NULL);
|
|
|
|
|
|
|
|
for (tmp = log->items; tmp != NULL; tmp = tmp->next) {
|
|
|
|
LOG_ITEM_REC *rec = tmp->data;
|
|
|
|
|
|
|
|
if (rec->type == type && g_strcasecmp(rec->name, item) == 0 &&
|
2001-02-17 14:44:22 -05:00
|
|
|
(rec->servertag == NULL || (servertag != NULL &&
|
|
|
|
g_strcasecmp(rec->servertag, servertag) == 0)))
|
2000-10-17 19:37:21 -04:00
|
|
|
return rec;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2000-10-27 23:01:11 -04:00
|
|
|
void log_file_write(SERVER_REC *server, const char *item, int level,
|
|
|
|
const char *str, int no_fallbacks)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
|
|
|
GSList *tmp, *fallbacks;
|
2001-02-17 19:11:23 -05:00
|
|
|
char *tmpstr, *servertag;
|
2000-04-26 04:03:38 -04:00
|
|
|
int found;
|
|
|
|
|
|
|
|
g_return_if_fail(str != NULL);
|
|
|
|
|
2000-10-28 17:04:01 -04:00
|
|
|
if (logs == NULL)
|
|
|
|
return;
|
|
|
|
|
2001-02-17 19:11:23 -05:00
|
|
|
servertag = server == NULL ? NULL : server->tag;
|
2000-04-26 04:03:38 -04:00
|
|
|
fallbacks = NULL; found = FALSE;
|
|
|
|
|
|
|
|
for (tmp = logs; tmp != NULL; tmp = tmp->next) {
|
|
|
|
LOG_REC *rec = tmp->data;
|
|
|
|
|
|
|
|
if (rec->handle == -1)
|
|
|
|
continue; /* log not opened yet */
|
|
|
|
|
|
|
|
if ((level & rec->level) == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (rec->items == NULL)
|
|
|
|
fallbacks = g_slist_append(fallbacks, rec);
|
2001-02-17 14:44:22 -05:00
|
|
|
else if (item != NULL &&
|
|
|
|
log_item_find(rec, LOG_ITEM_TARGET, item,
|
2001-02-17 19:11:23 -05:00
|
|
|
servertag) != NULL)
|
2001-02-17 07:08:31 -05:00
|
|
|
log_write_rec(rec, str, level);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!found && !no_fallbacks && fallbacks != NULL) {
|
|
|
|
/* not found from any items, so write it to all main logs */
|
2001-02-17 07:08:31 -05:00
|
|
|
tmpstr = (level & MSGLEVEL_PUBLIC) ?
|
|
|
|
g_strconcat(item, ": ", str, NULL) :
|
|
|
|
g_strdup(str);
|
|
|
|
|
|
|
|
for (tmp = fallbacks; tmp != NULL; tmp = tmp->next)
|
|
|
|
log_write_rec(tmp->data, tmpstr, level);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2001-02-17 07:08:31 -05:00
|
|
|
g_free(tmpstr);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
g_slist_free(fallbacks);
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG_REC *log_find(const char *fname)
|
|
|
|
{
|
|
|
|
GSList *tmp;
|
|
|
|
|
|
|
|
for (tmp = logs; tmp != NULL; tmp = tmp->next) {
|
|
|
|
LOG_REC *rec = tmp->data;
|
|
|
|
|
|
|
|
if (strcmp(rec->fname, fname) == 0)
|
|
|
|
return rec;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2000-10-17 19:37:21 -04:00
|
|
|
static void log_items_update_config(LOG_REC *log, CONFIG_NODE *parent)
|
|
|
|
{
|
|
|
|
GSList *tmp;
|
|
|
|
CONFIG_NODE *node;
|
|
|
|
|
|
|
|
parent = config_node_section(parent, "items", NODE_TYPE_LIST);
|
|
|
|
for (tmp = log->items; tmp != NULL; tmp = tmp->next) {
|
|
|
|
LOG_ITEM_REC *rec = tmp->data;
|
|
|
|
|
|
|
|
node = config_node_section(parent, NULL, NODE_TYPE_BLOCK);
|
|
|
|
iconfig_node_set_str(node, "type", log_item_types[rec->type]);
|
|
|
|
iconfig_node_set_str(node, "name", rec->name);
|
|
|
|
iconfig_node_set_str(node, "server", rec->servertag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void log_update_config(LOG_REC *log)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
|
|
|
CONFIG_NODE *node;
|
|
|
|
char *levelstr;
|
|
|
|
|
2000-07-21 18:55:20 -04:00
|
|
|
if (log->temp)
|
|
|
|
return;
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
node = iconfig_node_traverse("logs", TRUE);
|
|
|
|
node = config_node_section(node, log->fname, NODE_TYPE_BLOCK);
|
|
|
|
|
|
|
|
if (log->autoopen)
|
2000-11-26 05:24:30 -05:00
|
|
|
iconfig_node_set_bool(node, "auto_open", TRUE);
|
2000-04-26 04:03:38 -04:00
|
|
|
else
|
2000-05-10 09:57:42 -04:00
|
|
|
iconfig_node_set_str(node, "auto_open", NULL);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
levelstr = bits2level(log->level);
|
2000-05-10 09:57:42 -04:00
|
|
|
iconfig_node_set_str(node, "level", levelstr);
|
2000-04-26 04:03:38 -04:00
|
|
|
g_free(levelstr);
|
|
|
|
|
2000-05-10 09:57:42 -04:00
|
|
|
iconfig_node_set_str(node, "items", NULL);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-10-17 19:37:21 -04:00
|
|
|
if (log->items != NULL)
|
|
|
|
log_items_update_config(log, node);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void log_remove_config(LOG_REC *log)
|
|
|
|
{
|
|
|
|
iconfig_set_str("logs", log->fname, NULL);
|
|
|
|
}
|
|
|
|
|
2000-10-17 19:37:21 -04:00
|
|
|
LOG_REC *log_create_rec(const char *fname, int level)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
|
|
|
LOG_REC *rec;
|
|
|
|
|
|
|
|
g_return_val_if_fail(fname != NULL, NULL);
|
|
|
|
|
|
|
|
rec = log_find(fname);
|
|
|
|
if (rec == NULL) {
|
|
|
|
rec = g_new0(LOG_REC, 1);
|
|
|
|
rec->fname = g_strdup(fname);
|
2000-11-25 11:30:56 -05:00
|
|
|
rec->real_fname = log_filename(rec);
|
2000-04-26 04:03:38 -04:00
|
|
|
rec->handle = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
rec->level = level;
|
|
|
|
return rec;
|
|
|
|
}
|
|
|
|
|
2000-10-17 19:37:21 -04:00
|
|
|
void log_item_add(LOG_REC *log, int type, const char *name,
|
2001-02-17 14:44:22 -05:00
|
|
|
const char *servertag)
|
2000-10-17 19:37:21 -04:00
|
|
|
{
|
|
|
|
LOG_ITEM_REC *rec;
|
|
|
|
|
|
|
|
g_return_if_fail(log != NULL);
|
|
|
|
g_return_if_fail(name != NULL);
|
|
|
|
|
2001-02-17 14:44:22 -05:00
|
|
|
if (log_item_find(log, type, name, servertag))
|
2000-10-17 19:37:21 -04:00
|
|
|
return;
|
|
|
|
|
|
|
|
rec = g_new0(LOG_ITEM_REC, 1);
|
|
|
|
rec->type = type;
|
|
|
|
rec->name = g_strdup(name);
|
2001-02-17 14:44:22 -05:00
|
|
|
rec->servertag = g_strdup(servertag);
|
2000-10-17 19:37:21 -04:00
|
|
|
|
|
|
|
log->items = g_slist_append(log->items, rec);
|
|
|
|
}
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
void log_update(LOG_REC *log)
|
|
|
|
{
|
|
|
|
g_return_if_fail(log != NULL);
|
|
|
|
|
|
|
|
if (log_find(log->fname) == NULL) {
|
|
|
|
logs = g_slist_append(logs, log);
|
|
|
|
log->handle = -1;
|
|
|
|
}
|
|
|
|
|
2000-10-17 19:37:21 -04:00
|
|
|
log_update_config(log);
|
2000-04-26 04:03:38 -04:00
|
|
|
signal_emit("log new", 1, log);
|
|
|
|
}
|
|
|
|
|
2000-10-17 19:37:21 -04:00
|
|
|
void log_item_destroy(LOG_REC *log, LOG_ITEM_REC *item)
|
|
|
|
{
|
|
|
|
log->items = g_slist_remove(log->items, item);
|
|
|
|
|
|
|
|
g_free(item->name);
|
|
|
|
g_free_not_null(item->servertag);
|
|
|
|
g_free(item);
|
|
|
|
}
|
|
|
|
|
2000-07-02 14:52:15 -04:00
|
|
|
static void log_destroy(LOG_REC *log)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
|
|
|
g_return_if_fail(log != NULL);
|
|
|
|
|
|
|
|
if (log->handle != -1)
|
|
|
|
log_stop_logging(log);
|
|
|
|
|
|
|
|
logs = g_slist_remove(logs, log);
|
|
|
|
signal_emit("log remove", 1, log);
|
|
|
|
|
2000-10-17 19:37:21 -04:00
|
|
|
while (log->items != NULL)
|
|
|
|
log_item_destroy(log, log->items->data);
|
2000-04-26 04:03:38 -04:00
|
|
|
g_free(log->fname);
|
2000-08-11 18:07:42 -04:00
|
|
|
g_free_not_null(log->real_fname);
|
2000-04-26 04:03:38 -04:00
|
|
|
g_free(log);
|
|
|
|
}
|
|
|
|
|
2000-07-02 14:52:15 -04:00
|
|
|
void log_close(LOG_REC *log)
|
|
|
|
{
|
|
|
|
g_return_if_fail(log != NULL);
|
|
|
|
|
|
|
|
log_remove_config(log);
|
|
|
|
log_destroy(log);
|
|
|
|
}
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
static int sig_rotate_check(void)
|
|
|
|
{
|
|
|
|
static int last_hour = -1;
|
2000-08-11 18:07:42 -04:00
|
|
|
struct tm tm;
|
2000-04-26 04:03:38 -04:00
|
|
|
time_t now;
|
|
|
|
|
|
|
|
/* don't do anything until hour is changed */
|
|
|
|
now = time(NULL);
|
2000-08-11 18:07:42 -04:00
|
|
|
memcpy(&tm, localtime(&now), sizeof(tm));
|
|
|
|
if (tm.tm_hour != last_hour) {
|
|
|
|
last_hour = tm.tm_hour;
|
|
|
|
g_slist_foreach(logs, (GFunc) log_rotate_check, NULL);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2000-10-17 19:37:21 -04:00
|
|
|
static void log_items_read_config(CONFIG_NODE *node, LOG_REC *log)
|
|
|
|
{
|
|
|
|
LOG_ITEM_REC *rec;
|
|
|
|
GSList *tmp;
|
|
|
|
char *item;
|
2000-10-18 17:29:22 -04:00
|
|
|
int type;
|
2000-10-17 19:37:21 -04:00
|
|
|
|
|
|
|
for (tmp = node->value; tmp != NULL; tmp = tmp->next) {
|
|
|
|
node = tmp->data;
|
|
|
|
|
2000-10-18 17:29:22 -04:00
|
|
|
if (node->type != NODE_TYPE_BLOCK)
|
|
|
|
continue;
|
|
|
|
|
2000-10-17 19:37:21 -04:00
|
|
|
item = config_node_get_str(node, "name", NULL);
|
|
|
|
type = log_item_str2type(config_node_get_str(node, "type", NULL));
|
|
|
|
if (item == NULL || type == -1)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
rec = g_new0(LOG_ITEM_REC, 1);
|
|
|
|
rec->type = type;
|
|
|
|
rec->name = g_strdup(item);
|
|
|
|
rec->servertag = g_strdup(config_node_get_str(node, "server", NULL));
|
2000-10-18 17:29:22 -04:00
|
|
|
|
|
|
|
log->items = g_slist_append(log->items, rec);
|
2000-10-17 19:37:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
static void log_read_config(void)
|
|
|
|
{
|
|
|
|
CONFIG_NODE *node;
|
|
|
|
LOG_REC *log;
|
2000-07-02 14:52:15 -04:00
|
|
|
GSList *tmp, *next, *fnames;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-07-02 14:52:15 -04:00
|
|
|
/* close old logs, save list of open logs */
|
|
|
|
fnames = NULL;
|
|
|
|
for (tmp = logs; tmp != NULL; tmp = next) {
|
|
|
|
log = tmp->data;
|
|
|
|
|
|
|
|
next = tmp->next;
|
|
|
|
if (log->temp)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (log->handle != -1)
|
|
|
|
fnames = g_slist_append(fnames, g_strdup(log->fname));
|
|
|
|
log_destroy(log);
|
|
|
|
}
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
node = iconfig_node_traverse("logs", FALSE);
|
|
|
|
if (node == NULL) return;
|
|
|
|
|
|
|
|
for (tmp = node->value; tmp != NULL; tmp = tmp->next) {
|
|
|
|
node = tmp->data;
|
|
|
|
|
|
|
|
if (node->type != NODE_TYPE_BLOCK)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
log = g_new0(LOG_REC, 1);
|
|
|
|
logs = g_slist_append(logs, log);
|
|
|
|
|
|
|
|
log->handle = -1;
|
|
|
|
log->fname = g_strdup(node->key);
|
|
|
|
log->autoopen = config_node_get_bool(node, "auto_open", FALSE);
|
|
|
|
log->level = level2bits(config_node_get_str(node, "level", 0));
|
|
|
|
|
|
|
|
node = config_node_section(node, "items", -1);
|
2000-10-17 19:37:21 -04:00
|
|
|
if (node != NULL)
|
|
|
|
log_items_read_config(node, log);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-07-02 14:52:15 -04:00
|
|
|
if (log->autoopen || gslist_find_string(fnames, log->fname))
|
|
|
|
log_start_logging(log);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
2000-07-02 14:52:15 -04:00
|
|
|
|
|
|
|
g_slist_foreach(fnames, (GFunc) g_free, NULL);
|
|
|
|
g_slist_free(fnames);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void read_settings(void)
|
|
|
|
{
|
|
|
|
log_timestamp = settings_get_str("log_timestamp");
|
|
|
|
log_file_create_mode = octal2dec(settings_get_int("log_create_mode"));
|
|
|
|
}
|
|
|
|
|
|
|
|
void log_init(void)
|
|
|
|
{
|
|
|
|
rotate_tag = g_timeout_add(60000, (GSourceFunc) sig_rotate_check, NULL);
|
|
|
|
logs = NULL;
|
|
|
|
|
2000-07-16 16:18:05 -04:00
|
|
|
settings_add_int("log", "log_create_mode",
|
|
|
|
DEFAULT_LOG_FILE_CREATE_MODE);
|
2000-04-26 04:03:38 -04:00
|
|
|
settings_add_str("log", "log_timestamp", "%H:%M ");
|
2000-07-16 16:18:05 -04:00
|
|
|
settings_add_str("log", "log_open_string",
|
|
|
|
"--- Log opened %a %b %d %H:%M:%S %Y");
|
|
|
|
settings_add_str("log", "log_close_string",
|
|
|
|
"--- Log closed %a %b %d %H:%M:%S %Y");
|
|
|
|
settings_add_str("log", "log_day_changed",
|
|
|
|
"--- Day changed %a %b %d %Y");
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
read_settings();
|
|
|
|
log_read_config();
|
|
|
|
signal_add("setup changed", (SIGNAL_FUNC) read_settings);
|
|
|
|
signal_add("setup reread", (SIGNAL_FUNC) log_read_config);
|
|
|
|
}
|
|
|
|
|
|
|
|
void log_deinit(void)
|
|
|
|
{
|
|
|
|
g_source_remove(rotate_tag);
|
|
|
|
|
|
|
|
while (logs != NULL)
|
|
|
|
log_close(logs->data);
|
|
|
|
|
|
|
|
signal_remove("setup changed", (SIGNAL_FUNC) read_settings);
|
|
|
|
signal_remove("setup reread", (SIGNAL_FUNC) log_read_config);
|
|
|
|
}
|