~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/syslog/wrap.cc

Merged vcol stuff.

Show diffs side-by-side

added added

removed removed

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