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 "wrap.h"
00021 
00022 #include <assert.h>
00023 #include <stdarg.h>
00024 #include <string.h>
00025 
00026 #ifdef __sun
00027 # include <syslog.h>
00028 # include "names.h"
00029 #else
00030 # define SYSLOG_NAMES 1
00031 # include <syslog.h>
00032 #endif
00033 
00034 namespace drizzle_plugin
00035 {
00036 
00037 WrapSyslog::WrapSyslog () :
00038   _check(false)
00039 { }
00040 
00041 WrapSyslog::~WrapSyslog ()
00042 {
00043   ::closelog();
00044 }
00045 
00046 
00047 /* TODO, for the sake of performance, scan through all the priority
00048    and facility names, and construct a stl hash, minimal perfect hash,
00049    or some other high performance read data structure.  This can even
00050    be done at compile time. */
00051 
00052 int WrapSyslog::getPriorityByName(const char *priority_name)
00053 {
00054   for (int ndx= 0; prioritynames[ndx].c_name; ndx++)
00055   {
00056     if (strcasecmp(prioritynames[ndx].c_name, priority_name) == 0)
00057     {
00058       return prioritynames[ndx].c_val;
00059     }
00060   }
00061   // no matching priority found
00062   return -1;
00063 }
00064 
00065 int WrapSyslog::getFacilityByName(const char *facility_name)
00066 {
00067   for (int ndx= 0; facilitynames[ndx].c_name; ndx++)
00068   {
00069     if (strcasecmp(facilitynames[ndx].c_name, facility_name) == 0)
00070     {
00071       return facilitynames[ndx].c_val;
00072     }
00073   }
00074   // no matching facility found
00075   return -1;
00076 }
00077 
00078 void WrapSyslog::openlog(const std::string &ident)
00079 {
00080   if (_check == false)
00081   {
00082     ::openlog(ident.c_str(), LOG_PID, LOG_USER);
00083     _check= true;
00084   }
00085 }
00086 
00087 void WrapSyslog::vlog(int facility, int priority, const char *format, va_list ap)
00088 {
00089   assert(_check == true);
00090   vsyslog(facility | priority, format, ap);
00091 }
00092 
00093 void WrapSyslog::log (int facility, int priority, const char *format, ...)
00094 {
00095   assert(_check == true);
00096   va_list ap;
00097   va_start(ap, format);
00098   vsyslog(facility | priority, format, ap);
00099   va_end(ap);
00100 }
00101 
00102 } /* namespace drizzle_plugin */