source: log.c @ f4396c4

Last change on this file since f4396c4 was 0edc2fb, checked in by dequis <dx@…>, at 2015-10-30T10:31:09Z

log: Refactor log_link() to be less redundant.

"I didn't feel like messing with pointer to function pointers",
said the comment. Well, I do, comment. It wasn't a big deal either.

  • Property mode set to 100644
File size: 4.4 KB
RevLine 
[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
30static log_t logoutput;
31
[764c7d1]32static void log_null(int level, const char *logmessage);
33static void log_irc(int level, const char *logmessage);
34static void log_syslog(int level, const char *logmessage);
35static void log_console(int level, const char *logmessage);
[b7d3cc34]36
[5ebff60]37void 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]51void log_link(int level, int output)
52{
[0edc2fb]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        }
[b7d3cc34]64
[5ebff60]65        if (level == LOGLVL_INFO) {
[0edc2fb]66                logoutput.informational = output_function;
[5ebff60]67        } else if (level == LOGLVL_WARNING) {
[0edc2fb]68                logoutput.warning = output_function;
[5ebff60]69        } else if (level == LOGLVL_ERROR) {
[0edc2fb]70                logoutput.error = output_function;
[b7d3cc34]71        }
72#ifdef DEBUG
[5ebff60]73        else if (level == LOGLVL_DEBUG) {
[0edc2fb]74                logoutput.debug = output_function;
[b7d3cc34]75        }
76#endif
[5ebff60]77        return;
[b7d3cc34]78
79}
80
[5ebff60]81void log_message(int level, const char *message, ...)
82{
[b7d3cc34]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
[5ebff60]91        if (level == LOGLVL_INFO) {
[b7d3cc34]92                (*(logoutput.informational))(level, msgstring);
[5ebff60]93        }
94        if (level == LOGLVL_WARNING) {
[b7d3cc34]95                (*(logoutput.warning))(level, msgstring);
[5ebff60]96        }
97        if (level == LOGLVL_ERROR) {
[b7d3cc34]98                (*(logoutput.error))(level, msgstring);
[5ebff60]99        }
[b7d3cc34]100#ifdef DEBUG
[5ebff60]101        if (level == LOGLVL_DEBUG) {
[b7d3cc34]102                (*(logoutput.debug))(level, msgstring);
[5ebff60]103        }
[b7d3cc34]104#endif
105
106        g_free(msgstring);
[5ebff60]107
[b7d3cc34]108        return;
109}
110
[5ebff60]111void log_error(const char *functionname)
112{
[b7d3cc34]113        log_message(LOGLVL_ERROR, "%s: %s", functionname, strerror(errno));
[5ebff60]114
[b7d3cc34]115        return;
116}
117
[5ebff60]118static void log_null(int level, const char *message)
119{
[b7d3cc34]120        return;
121}
122
[5ebff60]123static void log_irc(int level, const char *message)
124{
125        if (level == LOGLVL_ERROR) {
[22d41a2]126                irc_write_all(1, "ERROR :Error: %s", message);
[5ebff60]127        }
128        if (level == LOGLVL_WARNING) {
[22d41a2]129                irc_write_all(0, "ERROR :Warning: %s", message);
[5ebff60]130        }
131        if (level == LOGLVL_INFO) {
132                irc_write_all(0, "ERROR :Informational: %s", message);
133        }
[b7d3cc34]134#ifdef DEBUG
[5ebff60]135        if (level == LOGLVL_DEBUG) {
136                irc_write_all(0, "ERROR :Debug: %s", message);
137        }
138#endif
[b7d3cc34]139
140        return;
141}
142
[5ebff60]143static void log_syslog(int level, const char *message)
144{
145        if (level == LOGLVL_ERROR) {
[b7d3cc34]146                syslog(LOG_ERR, "%s", message);
[5ebff60]147        }
148        if (level == LOGLVL_WARNING) {
[b7d3cc34]149                syslog(LOG_WARNING, "%s", message);
[5ebff60]150        }
151        if (level == LOGLVL_INFO) {
[b7d3cc34]152                syslog(LOG_INFO, "%s", message);
[5ebff60]153        }
[b7d3cc34]154#ifdef DEBUG
[5ebff60]155        if (level == LOGLVL_DEBUG) {
[b7d3cc34]156                syslog(LOG_DEBUG, "%s", message);
[5ebff60]157        }
[b7d3cc34]158#endif
159        return;
160}
161
[5ebff60]162static void log_console(int level, const char *message)
163{
164        if (level == LOGLVL_ERROR) {
[b7d3cc34]165                fprintf(stderr, "Error: %s\n", message);
[5ebff60]166        }
167        if (level == LOGLVL_WARNING) {
[b7d3cc34]168                fprintf(stderr, "Warning: %s\n", message);
[5ebff60]169        }
170        if (level == LOGLVL_INFO) {
[b7d3cc34]171                fprintf(stdout, "Informational: %s\n", message);
[5ebff60]172        }
[b7d3cc34]173#ifdef DEBUG
[5ebff60]174        if (level == LOGLVL_DEBUG) {
[b7d3cc34]175                fprintf(stdout, "Debug: %s\n", message);
[5ebff60]176        }
[b7d3cc34]177#endif
[156bbd7]178        /* Always log stuff in syslogs too. */
179        log_syslog(level, message);
[b7d3cc34]180        return;
181}
Note: See TracBrowser for help on using the repository browser.