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