~drizzle-trunk/drizzle/development

1637.3.1 by Mark Atwood
new syslog module, with plugins for query log, error message, and SYSLOG() function
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
2440.4.2 by Brian Aker
Fix level_t to be more inline with syslog
20
#include <config.h>
21
1637.3.1 by Mark Atwood
new syslog module, with plugins for query log, error message, and SYSLOG() function
22
#include "wrap.h"
23
2440.4.2 by Brian Aker
Fix level_t to be more inline with syslog
24
#include <cassert>
25
#include <cstdarg>
26
#include <cstring>
1637.3.1 by Mark Atwood
new syslog module, with plugins for query log, error message, and SYSLOG() function
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
2385.3.17 by Olaf van der Spek
Remove unnecessary constructors and destructors
36
namespace drizzle_plugin {
1964.2.7 by Monty Taylor
Refactored syslog module and changed it to use sys_var directly.
37
1637.3.1 by Mark Atwood
new syslog module, with plugins for query log, error message, and SYSLOG() function
38
WrapSyslog::WrapSyslog () :
1964.2.7 by Monty Taylor
Refactored syslog module and changed it to use sys_var directly.
39
  _check(false)
2440.4.2 by Brian Aker
Fix level_t to be more inline with syslog
40
{
41
}
1637.3.1 by Mark Atwood
new syslog module, with plugins for query log, error message, and SYSLOG() function
42
43
WrapSyslog::~WrapSyslog ()
44
{
45
  ::closelog();
46
}
47
2440.4.2 by Brian Aker
Fix level_t to be more inline with syslog
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
}
1637.3.1 by Mark Atwood
new syslog module, with plugins for query log, error message, and SYSLOG() function
60
2440.4.2 by Brian Aker
Fix level_t to be more inline with syslog
61
/* 
62
  TODO, for the sake of performance, scan through all the priority
1637.3.1 by Mark Atwood
new syslog module, with plugins for query log, error message, and SYSLOG() function
63
   and facility names, and construct a stl hash, minimal perfect hash,
64
   or some other high performance read data structure.  This can even
2440.4.2 by Brian Aker
Fix level_t to be more inline with syslog
65
   be done at compile time. 
66
 */
1637.3.1 by Mark Atwood
new syslog module, with plugins for query log, error message, and SYSLOG() function
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
1964.2.7 by Monty Taylor
Refactored syslog module and changed it to use sys_var directly.
80
void WrapSyslog::openlog(const std::string &ident)
1637.3.1 by Mark Atwood
new syslog module, with plugins for query log, error message, and SYSLOG() function
81
{
1964.2.7 by Monty Taylor
Refactored syslog module and changed it to use sys_var directly.
82
  if (_check == false)
1637.3.1 by Mark Atwood
new syslog module, with plugins for query log, error message, and SYSLOG() function
83
  {
1964.2.7 by Monty Taylor
Refactored syslog module and changed it to use sys_var directly.
84
    ::openlog(ident.c_str(), LOG_PID, LOG_USER);
85
    _check= true;
1637.3.1 by Mark Atwood
new syslog module, with plugins for query log, error message, and SYSLOG() function
86
  }
87
}
88
2440.4.3 by Brian Aker
Update naming convention for priority.
89
void WrapSyslog::vlog(int facility, const drizzled::error::priority_t priority, const char *format, va_list ap)
1637.3.1 by Mark Atwood
new syslog module, with plugins for query log, error message, and SYSLOG() function
90
{
1964.2.7 by Monty Taylor
Refactored syslog module and changed it to use sys_var directly.
91
  assert(_check == true);
2440.4.2 by Brian Aker
Fix level_t to be more inline with syslog
92
  vsyslog(facility | int(priority), format, ap);
1637.3.1 by Mark Atwood
new syslog module, with plugins for query log, error message, and SYSLOG() function
93
}
94
2440.4.3 by Brian Aker
Update naming convention for priority.
95
void WrapSyslog::log (int facility, const drizzled::error::priority_t priority, const char *format, ...)
1637.3.1 by Mark Atwood
new syslog module, with plugins for query log, error message, and SYSLOG() function
96
{
1964.2.7 by Monty Taylor
Refactored syslog module and changed it to use sys_var directly.
97
  assert(_check == true);
1637.3.1 by Mark Atwood
new syslog module, with plugins for query log, error message, and SYSLOG() function
98
  va_list ap;
99
  va_start(ap, format);
2440.4.2 by Brian Aker
Fix level_t to be more inline with syslog
100
  vsyslog(facility | int(priority), format, ap);
1637.3.1 by Mark Atwood
new syslog module, with plugins for query log, error message, and SYSLOG() function
101
  va_end(ap);
102
}
1964.2.7 by Monty Taylor
Refactored syslog module and changed it to use sys_var directly.
103
104
} /* namespace drizzle_plugin */