log/log_init.c

Go to the documentation of this file.
00001 // Copyright (C) 2006-2007 Benedikt Böhm <hollow@gentoo.org>
00002 //
00003 // This program is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU General Public License
00005 // as published by the Free Software Foundation; either version 2
00006 // of the License, or (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 Free Software
00015 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
00016 
00017 #include <unistd.h>
00018 #include <syslog.h>
00019 #include <sys/stat.h>
00020 
00021 #include "log.h"
00022 #include "mem.h"
00023 #include "str.h"
00024 
00025 log_options_t *_log_options = 0;
00026 
00027 #define MASK_PRIO(p) (1 << (p))
00028 
00029 static
00030 int mask_to_syslog(int mask)
00031 {
00032         int newmask = 0;
00033 
00034         if (mask & MASK_PRIO(LOGP_ALERT))
00035                 newmask |= MASK_PRIO(LOG_ALERT);
00036         if (mask & MASK_PRIO(LOGP_ERROR))
00037                 newmask |= MASK_PRIO(LOG_ERR);
00038         if (mask & MASK_PRIO(LOGP_WARN))
00039                 newmask |= MASK_PRIO(LOG_WARNING);
00040         if (mask & MASK_PRIO(LOGP_NOTE))
00041                 newmask |= MASK_PRIO(LOG_NOTICE);
00042         if (mask & MASK_PRIO(LOGP_INFO))
00043                 newmask |= MASK_PRIO(LOG_INFO);
00044         if (mask & MASK_PRIO(LOGP_DEBUG))
00045                 newmask |= MASK_PRIO(LOG_DEBUG);
00046 
00047         return newmask;
00048 }
00049 
00050 void log_init(log_options_t *options)
00051 {
00052         struct stat sb;
00053 
00054         /* check file destination */
00055         if (options->log_dest & LOGD_FILE)
00056                 if (options->log_fd < 0 || fstat(options->log_fd, &sb) == -1)
00057                         options->log_dest &= ~LOGD_FILE;
00058 
00059         /* check if STDERR is available */
00060         if (options->log_dest & LOGD_STDERR)
00061                 if (fstat(STDERR_FILENO, &sb) == -1)
00062                         options->log_dest &= ~LOGD_STDERR;
00063 
00064         /* log up to LOGP_INFO if not specified */
00065         if (options->log_mask == 0)
00066                 options->log_mask = ((1 << ((LOGP_INFO) + 1)) - 1);
00067 
00068         /* sanitize ident string */
00069         if (str_isempty(options->log_ident))
00070                 options->log_ident = "(none)";
00071 
00072         if (options->log_dest & LOGD_SYSLOG) {
00073                 openlog(options->log_ident,
00074                                 options->log_opts & LOGO_PID ? LOG_PID : 0,
00075                                 options->log_facility);
00076                 setlogmask(mask_to_syslog(options->log_mask));
00077         }
00078 
00079         _log_options = (log_options_t *) mem_alloc(sizeof(log_options_t));
00080 
00081         mem_cpy(_log_options, options, sizeof(log_options_t));
00082 }

Generated on Tue Jun 19 20:38:26 2007 for lucid by  doxygen 1.5.2