1 /* Copyright (c) 2003-2006 MySQL AB
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; version 2 of the License.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 GNU General Public License for more details.
12 You should have received a copy of the GNU General Public License
13 along with this program; if not, write to the Free Software
14 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
16 #include "SysLogHandler.hpp"
24 SysLogHandler::SysLogHandler() :
31 SysLogHandler::SysLogHandler(const char* pIdentity
, int facility
) :
33 m_pIdentity(pIdentity
),
39 SysLogHandler::~SysLogHandler()
46 ::setlogmask(LOG_UPTO(LOG_DEBUG
)); // Log from EMERGENCY down to DEBUG
47 ::openlog(m_pIdentity
, LOG_PID
|LOG_CONS
|LOG_ODELAY
, m_facility
); // PID, CONSOLE delay openlog
53 SysLogHandler::close()
61 SysLogHandler::writeHeader(const char* pCategory
, Logger::LoggerLevel level
)
63 // Save category to be used by writeMessage...
64 m_pCategory
= pCategory
;
65 // Map LogLevel to syslog severity
68 case Logger::LL_ALERT
:
69 m_severity
= LOG_ALERT
;
71 case Logger::LL_CRITICAL
:
72 m_severity
= LOG_CRIT
;
74 case Logger::LL_ERROR
:
77 case Logger::LL_WARNING
:
78 m_severity
= LOG_WARNING
;
81 m_severity
= LOG_INFO
;
83 case Logger::LL_DEBUG
:
84 m_severity
= LOG_DEBUG
;
87 m_severity
= LOG_INFO
;
94 SysLogHandler::writeMessage(const char* pMsg
)
96 ::syslog(m_facility
| m_severity
, "[%s] %s", m_pCategory
, pMsg
);
100 SysLogHandler::writeFooter()
102 // Need to close it everytime? Do we run out of file descriptors?
107 SysLogHandler::setParam(const BaseString
¶m
, const BaseString
&value
) {
108 if(param
== "facility") {
109 return setFacility(value
);
114 static const struct syslog_facility
{
117 } facilitynames
[] = {
118 { "auth", LOG_AUTH
},
120 { "authpriv", LOG_AUTHPRIV
},
122 { "cron", LOG_CRON
},
123 { "daemon", LOG_DAEMON
},
127 { "kern", LOG_KERN
},
129 { "mail", LOG_MAIL
},
130 { "news", LOG_NEWS
},
131 { "syslog", LOG_SYSLOG
},
132 { "user", LOG_USER
},
133 { "uucp", LOG_UUCP
},
134 { "local0", LOG_LOCAL0
},
135 { "local1", LOG_LOCAL1
},
136 { "local2", LOG_LOCAL2
},
137 { "local3", LOG_LOCAL3
},
138 { "local4", LOG_LOCAL4
},
139 { "local5", LOG_LOCAL5
},
140 { "local6", LOG_LOCAL6
},
141 { "local7", LOG_LOCAL7
},
146 SysLogHandler::setFacility(const BaseString
&facility
) {
147 const struct syslog_facility
*c
;
148 for(c
= facilitynames
; c
->name
!= NULL
; c
++) {
149 if(facility
== c
->name
) {
150 m_facility
= c
->value
;
156 setErrorStr("Invalid syslog facility name");