Drizzled Public API Documentation

wrap.cc
00001 /* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2010 Mark Atwood
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; version 2 of the License.
00009  *
00010  *  This program is distributed in the hope that it will be useful,
00011  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *  GNU General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018  */
00019 
00020 #include <config.h>
00021 
00022 #include "wrap.h"
00023 
00024 #include <cassert>
00025 #include <cstdarg>
00026 #include <cstring>
00027 
00028 #ifdef __sun
00029 # include <syslog.h>
00030 # include "names.h"
00031 #else
00032 # define SYSLOG_NAMES 1
00033 # include <syslog.h>
00034 #endif
00035 
00036 namespace drizzle_plugin {
00037 
00038 WrapSyslog::WrapSyslog () :
00039   _check(false)
00040 {
00041 }
00042 
00043 WrapSyslog::~WrapSyslog ()
00044 {
00045   ::closelog();
00046 }
00047 
00048 int WrapSyslog::getFacilityByName(const char *facility_name)
00049 {
00050   for (int ndx= 0; facilitynames[ndx].c_name; ndx++)
00051   {
00052     if (strcasecmp(facilitynames[ndx].c_name, facility_name) == 0)
00053     {
00054       return facilitynames[ndx].c_val;
00055     }
00056   }
00057   // no matching facility found
00058   return -1;
00059 }
00060 
00061 /* 
00062   TODO, for the sake of performance, scan through all the priority
00063    and facility names, and construct a stl hash, minimal perfect hash,
00064    or some other high performance read data structure.  This can even
00065    be done at compile time. 
00066  */
00067 int WrapSyslog::getPriorityByName(const char *priority_name)
00068 {
00069   for (int ndx= 0; prioritynames[ndx].c_name; ndx++)
00070   {
00071     if (strcasecmp(prioritynames[ndx].c_name, priority_name) == 0)
00072     {
00073       return prioritynames[ndx].c_val;
00074     }
00075   }
00076   // no matching priority found
00077   return -1;
00078 }
00079 
00080 void WrapSyslog::openlog(const std::string &ident)
00081 {
00082   if (_check == false)
00083   {
00084     ::openlog(ident.c_str(), LOG_PID, LOG_USER);
00085     _check= true;
00086   }
00087 }
00088 
00089 void WrapSyslog::vlog(int facility, const drizzled::error::priority_t priority, const char *format, va_list ap)
00090 {
00091   assert(_check == true);
00092   vsyslog(facility | int(priority), format, ap);
00093 }
00094 
00095 void WrapSyslog::log (int facility, const drizzled::error::priority_t priority, const char *format, ...)
00096 {
00097   assert(_check == true);
00098   va_list ap;
00099   va_start(ap, format);
00100   vsyslog(facility | int(priority), format, ap);
00101   va_end(ap);
00102 }
00103 
00104 } /* namespace drizzle_plugin */