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 namespace syslog {
38 
39 WrapSyslog::WrapSyslog () :
40  _check(false)
41 {
42 }
43 
44 WrapSyslog::~WrapSyslog ()
45 {
46  ::closelog();
47 }
48 
49 int WrapSyslog::getFacilityByName(const char *facility_name)
50 {
51  for (int ndx= 0; facilitynames[ndx].c_name; ndx++)
52  {
53  if (strcasecmp(facilitynames[ndx].c_name, facility_name) == 0)
54  {
55  return facilitynames[ndx].c_val;
56  }
57  }
58  // no matching facility found
59  return -1;
60 }
61 
62 /*
63  TODO, for the sake of performance, scan through all the priority
64  and facility names, and construct a stl hash, minimal perfect hash,
65  or some other high performance read data structure. This can even
66  be done at compile time.
67  */
68 int WrapSyslog::getPriorityByName(const char *priority_name)
69 {
70  for (int ndx= 0; prioritynames[ndx].c_name; ndx++)
71  {
72  if (strcasecmp(prioritynames[ndx].c_name, priority_name) == 0)
73  {
74  return prioritynames[ndx].c_val;
75  }
76  }
77  // no matching priority found
78  return -1;
79 }
80 
81 void WrapSyslog::openlog(const std::string &ident)
82 {
83  if (_check == false)
84  {
85  ::openlog(ident.c_str(), LOG_PID, LOG_USER);
86  _check= true;
87  }
88 }
89 
90 void WrapSyslog::vlog(int facility, const drizzled::error::priority_t priority, const char *format, va_list ap)
91 {
92  assert(_check == true);
93  vsyslog(facility | int(priority), format, ap);
94 }
95 
96 void WrapSyslog::log (int facility, const drizzled::error::priority_t priority, const char *format, ...)
97 {
98  assert(_check == true);
99  va_list ap;
100  va_start(ap, format);
101  vsyslog(facility | int(priority), format, ap);
102  va_end(ap);
103 }
104 
105 } /* namespace syslog */
106 } /* namespace drizzle_plugin */