Drizzled Public API Documentation

wrap.cc
1 /* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3  *
4  * Copyright (C) 2010 Mark Atwood
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 #include <config.h>
21 
22 #include "wrap.h"
23 
24 #include <cassert>
25 #include <cstdarg>
26 #include <cstring>
27 
28 #ifdef __sun
29 # include <syslog.h>
30 # include "names.h"
31 #else
32 # define SYSLOG_NAMES 1
33 # include <syslog.h>
34 #endif
35 
36 namespace drizzle_plugin {
37 
38 WrapSyslog::WrapSyslog () :
39  _check(false)
40 {
41 }
42 
43 WrapSyslog::~WrapSyslog ()
44 {
45  ::closelog();
46 }
47 
48 int WrapSyslog::getFacilityByName(const char *facility_name)
49 {
50  for (int ndx= 0; facilitynames[ndx].c_name; ndx++)
51  {
52  if (strcasecmp(facilitynames[ndx].c_name, facility_name) == 0)
53  {
54  return facilitynames[ndx].c_val;
55  }
56  }
57  // no matching facility found
58  return -1;
59 }
60 
61 /*
62  TODO, for the sake of performance, scan through all the priority
63  and facility names, and construct a stl hash, minimal perfect hash,
64  or some other high performance read data structure. This can even
65  be done at compile time.
66  */
67 int WrapSyslog::getPriorityByName(const char *priority_name)
68 {
69  for (int ndx= 0; prioritynames[ndx].c_name; ndx++)
70  {
71  if (strcasecmp(prioritynames[ndx].c_name, priority_name) == 0)
72  {
73  return prioritynames[ndx].c_val;
74  }
75  }
76  // no matching priority found
77  return -1;
78 }
79 
80 void WrapSyslog::openlog(const std::string &ident)
81 {
82  if (_check == false)
83  {
84  ::openlog(ident.c_str(), LOG_PID, LOG_USER);
85  _check= true;
86  }
87 }
88 
89 void WrapSyslog::vlog(int facility, const drizzled::error::priority_t priority, const char *format, va_list ap)
90 {
91  assert(_check == true);
92  vsyslog(facility | int(priority), format, ap);
93 }
94 
95 void WrapSyslog::log (int facility, const drizzled::error::priority_t priority, const char *format, ...)
96 {
97  assert(_check == true);
98  va_list ap;
99  va_start(ap, format);
100  vsyslog(facility | int(priority), format, ap);
101  va_end(ap);
102 }
103 
104 } /* namespace drizzle_plugin */