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