2 ctrlproxy: A modular IRC proxy
3 (c) 2002-2005 Jelmer Vernooij <jelmer@nl.linux.org>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
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.
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., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include "internals.h"
27 static const char *get_date(void)
30 time_t t = time(NULL);
31 strftime(ret, sizeof(ret), "%F %H:%M:%S", localtime(&t));
35 gboolean no_log_timestamp = FALSE;
36 enum log_level current_log_level = LOG_INFO;
39 static void log_entry(enum log_level level, const struct network *n, const struct client *c, const char *data)
44 if (level > current_log_level)
48 admin_log(level, n, c, data);
50 if (!no_log_timestamp)
51 fprintf(flog, "[%s] ", get_date());
53 g_assert(strchr(data, '\n') == NULL);
54 fprintf(flog, "%s", data);
57 fprintf(flog, " (%s", n->name);
60 fprintf(flog, "/%s", c->description);
69 void log_network_line(const struct network *n, const struct line *l, gboolean incoming)
72 if (current_log_level < LOG_DATA)
75 raw = irc_line_string(l);
76 log_network(LOG_DATA, n, "%c %s", incoming?'<':'>', raw);
80 void log_client_line(const struct client *n, const struct line *l, gboolean incoming)
83 if (current_log_level < LOG_DATA)
86 raw = irc_line_string(l);
87 log_client(LOG_DATA, n, "%c %s", incoming?'<':'>', raw);
91 void log_network(enum log_level level, const struct network *n, const char *fmt, ...)
96 tmp = g_strdup_vprintf(fmt, ap);
97 log_entry(level, n, NULL, tmp);
102 void log_client(enum log_level level, const struct client *c, const char *fmt, ...)
107 tmp = g_strdup_vprintf(fmt, ap);
109 log_entry(level, c->network, c, tmp);
113 void log_global(enum log_level level, const char *fmt, ...)
118 tmp = g_strdup_vprintf(fmt, ap);
120 log_entry(level, NULL, NULL, tmp);
126 static void log_handler(const gchar *log_domain, GLogLevelFlags flags, const gchar *message, gpointer user_data) {
127 log_global(LOG_ERROR, "[%s] %s", log_domain, message);
130 static void fini_log(void)
132 log_global(LOG_INFO, "Closing log file");
133 if (flog != stderr) {
139 gboolean init_log(const char *lf)
141 g_log_set_handler ("GLib", G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL | G_LOG_FLAG_RECURSION, log_handler, NULL);
148 flog = fopen(lf, "a+");
150 perror("Opening log file");
156 log_global(LOG_INFO, "Opening log file");