Try to show the user_split info for a protocol in "help purple $PROTOCOL".
[bitlbee.git] / log.c
blobf6f9ab2b24f7a4137d8f098e46ecee5dd67e7a21
1 /********************************************************************\
2 * BitlBee -- An IRC to other IM-networks gateway *
3 * *
4 * Copyright 2002-2005 Wilmer van der Gaast and others *
5 \********************************************************************/
7 /* Logging services for the bee */
9 /*
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License with
21 the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL;
22 if not, write to the Free Software Foundation, Inc., 59 Temple Place,
23 Suite 330, Boston, MA 02111-1307 USA
26 #define BITLBEE_CORE
27 #include "bitlbee.h"
28 #include <syslog.h>
30 static log_t logoutput;
32 static void log_null(int level, const char *logmessage);
33 static void log_irc(int level, const char *logmessage);
34 static void log_syslog(int level, const char *logmessage);
35 static void log_console(int level, const char *logmessage);
37 void log_init(void) {
38 openlog("bitlbee", LOG_PID, LOG_DAEMON);
40 logoutput.informational = &log_null;
41 logoutput.warning = &log_null;
42 logoutput.error = &log_null;
43 #ifdef DEBUG
44 logoutput.debug = &log_null;
45 #endif
47 return;
50 void log_link(int level, int output) {
51 /* I know it's ugly, but it works and I didn't feel like messing with pointer to function pointers */
53 if(level == LOGLVL_INFO) {
54 if(output == LOGOUTPUT_NULL)
55 logoutput.informational = &log_null;
56 else if(output == LOGOUTPUT_IRC)
57 logoutput.informational = &log_irc;
58 else if(output == LOGOUTPUT_SYSLOG)
59 logoutput.informational = &log_syslog;
60 else if(output == LOGOUTPUT_CONSOLE)
61 logoutput.informational = &log_console;
63 else if(level == LOGLVL_WARNING) {
64 if(output == LOGOUTPUT_NULL)
65 logoutput.warning = &log_null;
66 else if(output == LOGOUTPUT_IRC)
67 logoutput.warning = &log_irc;
68 else if(output == LOGOUTPUT_SYSLOG)
69 logoutput.warning = &log_syslog;
70 else if(output == LOGOUTPUT_CONSOLE)
71 logoutput.warning = &log_console;
73 else if(level == LOGLVL_ERROR) {
74 if(output == LOGOUTPUT_NULL)
75 logoutput.error = &log_null;
76 else if(output == LOGOUTPUT_IRC)
77 logoutput.error = &log_irc;
78 else if(output == LOGOUTPUT_SYSLOG)
79 logoutput.error = &log_syslog;
80 else if(output == LOGOUTPUT_CONSOLE)
81 logoutput.error = &log_console;
83 #ifdef DEBUG
84 else if(level == LOGLVL_DEBUG) {
85 if(output == LOGOUTPUT_NULL)
86 logoutput.debug = &log_null;
87 else if(output == LOGOUTPUT_IRC)
88 logoutput.debug = &log_irc;
89 else if(output == LOGOUTPUT_SYSLOG)
90 logoutput.debug = &log_syslog;
91 else if(output == LOGOUTPUT_CONSOLE)
92 logoutput.debug = &log_console;
94 #endif
95 return;
99 void log_message(int level, const char *message, ... ) {
101 va_list ap;
102 char *msgstring;
104 va_start(ap, message);
105 msgstring = g_strdup_vprintf(message, ap);
106 va_end(ap);
108 if(level == LOGLVL_INFO)
109 (*(logoutput.informational))(level, msgstring);
110 if(level == LOGLVL_WARNING)
111 (*(logoutput.warning))(level, msgstring);
112 if(level == LOGLVL_ERROR)
113 (*(logoutput.error))(level, msgstring);
114 #ifdef DEBUG
115 if(level == LOGLVL_DEBUG)
116 (*(logoutput.debug))(level, msgstring);
117 #endif
119 g_free(msgstring);
121 return;
124 void log_error(const char *functionname) {
125 log_message(LOGLVL_ERROR, "%s: %s", functionname, strerror(errno));
127 return;
130 static void log_null(int level, const char *message) {
131 return;
134 static void log_irc(int level, const char *message) {
135 if(level == LOGLVL_ERROR)
136 irc_write_all(1, "ERROR :Error: %s", message);
137 if(level == LOGLVL_WARNING)
138 irc_write_all(0, "ERROR :Warning: %s", message);
139 if(level == LOGLVL_INFO)
140 irc_write_all(0, "ERROR :Informational: %s", message);
141 #ifdef DEBUG
142 if(level == LOGLVL_DEBUG)
143 irc_write_all(0, "ERROR :Debug: %s", message);
144 #endif
146 return;
149 static void log_syslog(int level, const char *message) {
150 if(level == LOGLVL_ERROR)
151 syslog(LOG_ERR, "%s", message);
152 if(level == LOGLVL_WARNING)
153 syslog(LOG_WARNING, "%s", message);
154 if(level == LOGLVL_INFO)
155 syslog(LOG_INFO, "%s", message);
156 #ifdef DEBUG
157 if(level == LOGLVL_DEBUG)
158 syslog(LOG_DEBUG, "%s", message);
159 #endif
160 return;
163 static void log_console(int level, const char *message) {
164 if(level == LOGLVL_ERROR)
165 fprintf(stderr, "Error: %s\n", message);
166 if(level == LOGLVL_WARNING)
167 fprintf(stderr, "Warning: %s\n", message);
168 if(level == LOGLVL_INFO)
169 fprintf(stdout, "Informational: %s\n", message);
170 #ifdef DEBUG
171 if(level == LOGLVL_DEBUG)
172 fprintf(stdout, "Debug: %s\n", message);
173 #endif
174 /* Always log stuff in syslogs too. */
175 log_syslog(level, message);
176 return;