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