[b7d3cc34] | 1 | /********************************************************************\ |
---|
| 2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
| 3 | * * |
---|
[c92e6801] | 4 | * Copyright 2002-2005 Wilmer van der Gaast and others * |
---|
[b7d3cc34] | 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; |
---|
[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 | |
---|
| 37 | void log_init(void) { |
---|
| 38 | openlog("bitlbee", LOG_PID, LOG_DAEMON); |
---|
| 39 | |
---|
[ac9f0e9] | 40 | logoutput.informational = &log_null; |
---|
| 41 | logoutput.warning = &log_null; |
---|
| 42 | logoutput.error = &log_null; |
---|
[b7d3cc34] | 43 | #ifdef DEBUG |
---|
[ac9f0e9] | 44 | logoutput.debug = &log_null; |
---|
[b7d3cc34] | 45 | #endif |
---|
| 46 | |
---|
| 47 | return; |
---|
| 48 | } |
---|
| 49 | |
---|
| 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 */ |
---|
| 52 | |
---|
[ac9f0e9] | 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; |
---|
[b7d3cc34] | 62 | } |
---|
[ac9f0e9] | 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; |
---|
[b7d3cc34] | 72 | } |
---|
[ac9f0e9] | 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; |
---|
[b7d3cc34] | 82 | } |
---|
| 83 | #ifdef DEBUG |
---|
[ac9f0e9] | 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; |
---|
[b7d3cc34] | 93 | } |
---|
| 94 | #endif |
---|
| 95 | return; |
---|
| 96 | |
---|
| 97 | } |
---|
| 98 | |
---|
[764c7d1] | 99 | void log_message(int level, const char *message, ... ) { |
---|
[b7d3cc34] | 100 | |
---|
| 101 | va_list ap; |
---|
| 102 | char *msgstring; |
---|
| 103 | |
---|
| 104 | va_start(ap, message); |
---|
| 105 | msgstring = g_strdup_vprintf(message, ap); |
---|
| 106 | va_end(ap); |
---|
| 107 | |
---|
[ac9f0e9] | 108 | if(level == LOGLVL_INFO) |
---|
[b7d3cc34] | 109 | (*(logoutput.informational))(level, msgstring); |
---|
[ac9f0e9] | 110 | if(level == LOGLVL_WARNING) |
---|
[b7d3cc34] | 111 | (*(logoutput.warning))(level, msgstring); |
---|
[ac9f0e9] | 112 | if(level == LOGLVL_ERROR) |
---|
[b7d3cc34] | 113 | (*(logoutput.error))(level, msgstring); |
---|
| 114 | #ifdef DEBUG |
---|
[ac9f0e9] | 115 | if(level == LOGLVL_DEBUG) |
---|
[b7d3cc34] | 116 | (*(logoutput.debug))(level, msgstring); |
---|
| 117 | #endif |
---|
| 118 | |
---|
| 119 | g_free(msgstring); |
---|
| 120 | |
---|
| 121 | return; |
---|
| 122 | } |
---|
| 123 | |
---|
[764c7d1] | 124 | void log_error(const char *functionname) { |
---|
[b7d3cc34] | 125 | log_message(LOGLVL_ERROR, "%s: %s", functionname, strerror(errno)); |
---|
| 126 | |
---|
| 127 | return; |
---|
| 128 | } |
---|
| 129 | |
---|
[764c7d1] | 130 | static void log_null(int level, const char *message) { |
---|
[b7d3cc34] | 131 | return; |
---|
| 132 | } |
---|
| 133 | |
---|
[764c7d1] | 134 | static void log_irc(int level, const char *message) { |
---|
[ac9f0e9] | 135 | if(level == LOGLVL_ERROR) |
---|
[22d41a2] | 136 | irc_write_all(1, "ERROR :Error: %s", message); |
---|
[ac9f0e9] | 137 | if(level == LOGLVL_WARNING) |
---|
[22d41a2] | 138 | irc_write_all(0, "ERROR :Warning: %s", message); |
---|
[ac9f0e9] | 139 | if(level == LOGLVL_INFO) |
---|
[22d41a2] | 140 | irc_write_all(0, "ERROR :Informational: %s", message); |
---|
[b7d3cc34] | 141 | #ifdef DEBUG |
---|
[ac9f0e9] | 142 | if(level == LOGLVL_DEBUG) |
---|
[22d41a2] | 143 | irc_write_all(0, "ERROR :Debug: %s", message); |
---|
[b7d3cc34] | 144 | #endif |
---|
| 145 | |
---|
| 146 | return; |
---|
| 147 | } |
---|
| 148 | |
---|
[764c7d1] | 149 | static void log_syslog(int level, const char *message) { |
---|
[ac9f0e9] | 150 | if(level == LOGLVL_ERROR) |
---|
[b7d3cc34] | 151 | syslog(LOG_ERR, "%s", message); |
---|
[ac9f0e9] | 152 | if(level == LOGLVL_WARNING) |
---|
[b7d3cc34] | 153 | syslog(LOG_WARNING, "%s", message); |
---|
[ac9f0e9] | 154 | if(level == LOGLVL_INFO) |
---|
[b7d3cc34] | 155 | syslog(LOG_INFO, "%s", message); |
---|
| 156 | #ifdef DEBUG |
---|
[ac9f0e9] | 157 | if(level == LOGLVL_DEBUG) |
---|
[b7d3cc34] | 158 | syslog(LOG_DEBUG, "%s", message); |
---|
| 159 | #endif |
---|
| 160 | return; |
---|
| 161 | } |
---|
| 162 | |
---|
[764c7d1] | 163 | static void log_console(int level, const char *message) { |
---|
[ac9f0e9] | 164 | if(level == LOGLVL_ERROR) |
---|
[b7d3cc34] | 165 | fprintf(stderr, "Error: %s\n", message); |
---|
[ac9f0e9] | 166 | if(level == LOGLVL_WARNING) |
---|
[b7d3cc34] | 167 | fprintf(stderr, "Warning: %s\n", message); |
---|
[ac9f0e9] | 168 | if(level == LOGLVL_INFO) |
---|
[b7d3cc34] | 169 | fprintf(stdout, "Informational: %s\n", message); |
---|
| 170 | #ifdef DEBUG |
---|
[ac9f0e9] | 171 | if(level == LOGLVL_DEBUG) |
---|
[b7d3cc34] | 172 | fprintf(stdout, "Debug: %s\n", message); |
---|
| 173 | #endif |
---|
[156bbd7] | 174 | /* Always log stuff in syslogs too. */ |
---|
| 175 | log_syslog(level, message); |
---|
[b7d3cc34] | 176 | return; |
---|
| 177 | } |
---|