log/log_internal.c

Go to the documentation of this file.
00001 // Copyright 2006 Benedikt Böhm <hollow@gentoo.org>
00002 //
00003 // This program is free software; you can redistribute it and/or modify
00004 // it under the terms of the GNU General Public License as published by
00005 // the Free Software Foundation; either version 2 of the License, or
00006 // (at your option) any later version.
00007 //
00008 // This program is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 // GNU General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU General Public License
00014 // along with this program; if not, write to the
00015 // Free Software Foundation, Inc.,
00016 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00017 
00018 #include <unistd.h>
00019 #include <stdlib.h>
00020 #include <errno.h>
00021 #include <stdarg.h>
00022 #include <syslog.h>
00023 #include <string.h>
00024 #include <time.h>
00025 
00026 #include "log.h"
00027 #include "printf.h"
00028 #include "str.h"
00029 
00030 extern log_options_t *_log_options;
00031 
00032 static int errno_orig = 0;
00033 
00034 static
00035 void log_fd(int fd, int level, const char *msg)
00036 {
00037         char timebuf[17];
00038         time_t curtime = time(NULL);
00039         
00040         if (!(_log_options->mask & level))
00041                 return;
00042         
00043         switch (level) {
00044                 case LOG_EMERG:   _lucid_dprintf(fd, "[emrge]"); break;
00045                 case LOG_ALERT:   _lucid_dprintf(fd, "[alert]"); break;
00046                 case LOG_CRIT:    _lucid_dprintf(fd, "[crit ]"); break;
00047                 case LOG_ERR:     _lucid_dprintf(fd, "[error]"); break;
00048                 case LOG_WARNING: _lucid_dprintf(fd, "[warn ]"); break;
00049                 case LOG_NOTICE:  _lucid_dprintf(fd, "[note ]"); break;
00050                 case LOG_INFO:    _lucid_dprintf(fd, "[info ]"); break;
00051                 case LOG_DEBUG:   _lucid_dprintf(fd, "[debug]"); break;
00052                 default:          _lucid_dprintf(fd, "[none ]"); break;
00053         }
00054         
00055         if (_log_options->time) {
00056                 str_zero(timebuf, 17);
00057                 strftime(timebuf, 17, "%b %d %T", localtime(&curtime));
00058                 _lucid_dprintf(fd, " %s", timebuf);
00059         }
00060         
00061         _lucid_dprintf(fd, " %s", _log_options->ident);
00062         
00063         if (_log_options->flags & LOG_PID)
00064                 _lucid_dprintf(fd, "[%d]", getpid());
00065         
00066         _lucid_dprintf(fd, ": %s\n", msg);
00067 }
00068 
00069 void log_internal(int level, int strerr, const char *fmt, va_list ap)
00070 {
00071         char *buf, *msg;
00072         
00073         if (!_log_options)
00074                 return;
00075         
00076         _lucid_vasprintf(&msg, fmt, ap);
00077         
00078         if (strerr) {
00079                 buf = msg;
00080                 _lucid_asprintf(&msg, "%s: %s", buf, strerror(errno_orig));
00081                 free(buf);
00082         }
00083         
00084         if (_log_options->syslog)
00085                 syslog(_log_options->facility|level, "%s", msg);
00086         
00087         if (_log_options->stderr)
00088                 log_fd(STDERR_FILENO, level, msg);
00089         
00090         if (_log_options->file)
00091                 log_fd(_log_options->fd, level, msg);
00092         
00093         free(msg);
00094 }
00095 
00096 #define LOGFUNC(name, level, rc) \
00097 int log_ ## name (const char *fmt, ...) { \
00098         va_list ap; va_start(ap, fmt); \
00099         log_internal(level, 0, fmt, ap); \
00100         va_end(ap); \
00101         return rc; \
00102 }
00103 
00104 LOGFUNC(emerg,  LOG_EMERG,   EXIT_FAILURE)
00105 LOGFUNC(alert,  LOG_ALERT,   EXIT_FAILURE)
00106 LOGFUNC(crit,   LOG_CRIT,    EXIT_FAILURE)
00107 LOGFUNC(error,  LOG_ERR,     EXIT_FAILURE)
00108 LOGFUNC(warn,   LOG_WARNING, EXIT_SUCCESS)
00109 LOGFUNC(notice, LOG_NOTICE,  EXIT_SUCCESS)
00110 LOGFUNC(info,   LOG_INFO,    EXIT_SUCCESS)
00111 LOGFUNC(debug,  LOG_DEBUG,   EXIT_SUCCESS)
00112 
00113 #define LOGFUNCDIE(name, level) \
00114 void log_ ## name ## _and_die(const char *fmt, ...) { \
00115         va_list ap; va_start(ap, fmt); \
00116         log_internal(level, 0, fmt, ap); \
00117         va_end(ap); \
00118         exit(EXIT_FAILURE); \
00119 }
00120 
00121 LOGFUNCDIE(emerg, LOG_EMERG)
00122 LOGFUNCDIE(alert, LOG_ALERT)
00123 LOGFUNCDIE(crit,  LOG_CRIT)
00124 LOGFUNCDIE(error, LOG_ERR)
00125 
00126 #define LOGPFUNC(name, level, rc) \
00127 int log_p ## name (const char *fmt, ...) { \
00128         errno_orig = errno; \
00129         va_list ap; va_start(ap, fmt); \
00130         log_internal(level, 1, fmt, ap); \
00131         va_end(ap); \
00132         return rc; \
00133 }
00134 
00135 LOGPFUNC(emerg,  LOG_EMERG,   EXIT_FAILURE)
00136 LOGPFUNC(alert,  LOG_ALERT,   EXIT_FAILURE)
00137 LOGPFUNC(crit,   LOG_CRIT,    EXIT_FAILURE)
00138 LOGPFUNC(error,  LOG_ERR,     EXIT_FAILURE)
00139 LOGPFUNC(warn,   LOG_WARNING, EXIT_SUCCESS)
00140 LOGPFUNC(notice, LOG_NOTICE,  EXIT_SUCCESS)
00141 LOGPFUNC(info,   LOG_INFO,    EXIT_SUCCESS)
00142 LOGPFUNC(debug,  LOG_DEBUG,   EXIT_SUCCESS)
00143 
00144 #define LOGPFUNCDIE(name, level) \
00145 void log_p ## name ## _and_die(const char *fmt, ...) { \
00146         errno_orig = errno; \
00147         va_list ap; va_start(ap, fmt); \
00148         log_internal(level, 1, fmt, ap); \
00149         va_end(ap); \
00150         exit(EXIT_FAILURE); \
00151 }
00152 
00153 LOGPFUNCDIE(emerg, LOG_EMERG)
00154 LOGPFUNCDIE(alert, LOG_ALERT)
00155 LOGPFUNCDIE(crit,  LOG_CRIT)
00156 LOGPFUNCDIE(error, LOG_ERR)
00157 

Generated on Sun Dec 3 17:45:53 2006 for lucid by  doxygen 1.5.1