1 /********************************************************************\
2   * BitlBee -- An IRC to other IM-networks gateway                     *
3   *                                                                    *
4   * Copyright 2002-2005 Wilmer van der Gaast and others                *
5   \********************************************************************/
6 
7 /* Logging services for the bee                         */
8 
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.
14 
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.
19 
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., 51 Franklin St.,
23   Fifth Floor, Boston, MA  02110-1301  USA
24 */
25 
26 #define BITLBEE_CORE
27 #include "bitlbee.h"
28 #include <syslog.h>
29 
30 static log_t logoutput;
31 
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);
36 
log_init(void)37 void log_init(void)
38 {
39 	openlog("bitlbee", LOG_PID, LOG_DAEMON);
40 
41 	logoutput.informational = &log_null;
42 	logoutput.warning = &log_null;
43 	logoutput.error = &log_null;
44 #ifdef DEBUG
45 	logoutput.debug = &log_null;
46 #endif
47 
48 	return;
49 }
50 
log_link(int level,int output)51 void log_link(int level, int output)
52 {
53 	void (*output_function)(int level, const char *logmessage) = &log_null;
54 
55 	if (output == LOGOUTPUT_NULL) {
56 		output_function = &log_null;
57 	} else if (output == LOGOUTPUT_IRC) {
58 		output_function = &log_irc;
59 	} else if (output == LOGOUTPUT_SYSLOG) {
60 		output_function = &log_syslog;
61 	} else if (output == LOGOUTPUT_CONSOLE) {
62 		output_function = &log_console;
63 	}
64 
65 	if (level == LOGLVL_INFO) {
66 		logoutput.informational = output_function;
67 	} else if (level == LOGLVL_WARNING) {
68 		logoutput.warning = output_function;
69 	} else if (level == LOGLVL_ERROR) {
70 		logoutput.error = output_function;
71 	}
72 #ifdef DEBUG
73 	else if (level == LOGLVL_DEBUG) {
74 		logoutput.debug = output_function;
75 	}
76 #endif
77 	return;
78 
79 }
80 
log_message(int level,const char * message,...)81 void log_message(int level, const char *message, ...)
82 {
83 
84 	va_list ap;
85 	char *msgstring;
86 
87 	va_start(ap, message);
88 	msgstring = g_strdup_vprintf(message, ap);
89 	va_end(ap);
90 
91 	if (level == LOGLVL_INFO) {
92 		(*(logoutput.informational))(level, msgstring);
93 	}
94 	if (level == LOGLVL_WARNING) {
95 		(*(logoutput.warning))(level, msgstring);
96 	}
97 	if (level == LOGLVL_ERROR) {
98 		(*(logoutput.error))(level, msgstring);
99 	}
100 #ifdef DEBUG
101 	if (level == LOGLVL_DEBUG) {
102 		(*(logoutput.debug))(level, msgstring);
103 	}
104 #endif
105 
106 	g_free(msgstring);
107 
108 	return;
109 }
110 
log_error(const char * functionname)111 void log_error(const char *functionname)
112 {
113 	log_message(LOGLVL_ERROR, "%s: %s", functionname, strerror(errno));
114 
115 	return;
116 }
117 
log_null(int level,const char * message)118 static void log_null(int level, const char *message)
119 {
120 	return;
121 }
122 
log_irc(int level,const char * message)123 static void log_irc(int level, const char *message)
124 {
125 	if (level == LOGLVL_ERROR) {
126 		irc_write_all(1, "ERROR :Error: %s", message);
127 	}
128 	if (level == LOGLVL_WARNING) {
129 		irc_write_all(0, "ERROR :Warning: %s", message);
130 	}
131 	if (level == LOGLVL_INFO) {
132 		irc_write_all(0, "ERROR :Informational: %s", message);
133 	}
134 #ifdef DEBUG
135 	if (level == LOGLVL_DEBUG) {
136 		irc_write_all(0, "ERROR :Debug: %s", message);
137 	}
138 #endif
139 
140 	return;
141 }
142 
log_syslog(int level,const char * message)143 static void log_syslog(int level, const char *message)
144 {
145 	if (level == LOGLVL_ERROR) {
146 		syslog(LOG_ERR, "%s", message);
147 	}
148 	if (level == LOGLVL_WARNING) {
149 		syslog(LOG_WARNING, "%s", message);
150 	}
151 	if (level == LOGLVL_INFO) {
152 		syslog(LOG_INFO, "%s", message);
153 	}
154 #ifdef DEBUG
155 	if (level == LOGLVL_DEBUG) {
156 		syslog(LOG_DEBUG, "%s", message);
157 	}
158 #endif
159 	return;
160 }
161 
log_console(int level,const char * message)162 static void log_console(int level, const char *message)
163 {
164 	if (level == LOGLVL_ERROR) {
165 		fprintf(stderr, "Error: %s\n", message);
166 	}
167 	if (level == LOGLVL_WARNING) {
168 		fprintf(stderr, "Warning: %s\n", message);
169 	}
170 	if (level == LOGLVL_INFO) {
171 		fprintf(stdout, "Informational: %s\n", message);
172 	}
173 #ifdef DEBUG
174 	if (level == LOGLVL_DEBUG) {
175 		fprintf(stdout, "Debug: %s\n", message);
176 	}
177 #endif
178 	/* Always log stuff in syslogs too. */
179 	log_syslog(level, message);
180 	return;
181 }
182