mySQL 5.0.11 sources for tomato
[tomato.git] / release / src / router / mysql / storage / ndb / src / common / logger / SysLogHandler.cpp
blobf579c24447f9156a189b63c4e7fb3001c26aaa3a
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"
18 #include <syslog.h>
21 // PUBLIC
24 SysLogHandler::SysLogHandler() :
25 m_severity(LOG_INFO),
26 m_pIdentity("NDB"),
27 m_facility(LOG_USER)
31 SysLogHandler::SysLogHandler(const char* pIdentity, int facility) :
32 m_severity(LOG_INFO),
33 m_pIdentity(pIdentity),
34 m_facility(facility)
39 SysLogHandler::~SysLogHandler()
43 bool
44 SysLogHandler::open()
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
49 return true;
52 bool
53 SysLogHandler::close()
55 ::closelog();
57 return true;
60 void
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
66 switch (level)
68 case Logger::LL_ALERT:
69 m_severity = LOG_ALERT;
70 break;
71 case Logger::LL_CRITICAL:
72 m_severity = LOG_CRIT;
73 break;
74 case Logger::LL_ERROR:
75 m_severity = LOG_ERR;
76 break;
77 case Logger::LL_WARNING:
78 m_severity = LOG_WARNING;
79 break;
80 case Logger::LL_INFO:
81 m_severity = LOG_INFO;
82 break;
83 case Logger::LL_DEBUG:
84 m_severity = LOG_DEBUG;
85 break;
86 default:
87 m_severity = LOG_INFO;
88 break;
93 void
94 SysLogHandler::writeMessage(const char* pMsg)
96 ::syslog(m_facility | m_severity, "[%s] %s", m_pCategory, pMsg);
99 void
100 SysLogHandler::writeFooter()
102 // Need to close it everytime? Do we run out of file descriptors?
103 //::closelog();
106 bool
107 SysLogHandler::setParam(const BaseString &param, const BaseString &value) {
108 if(param == "facility") {
109 return setFacility(value);
111 return false;
114 static const struct syslog_facility {
115 const char *name;
116 int value;
117 } facilitynames[] = {
118 { "auth", LOG_AUTH },
119 #ifdef LOG_AUTHPRIV
120 { "authpriv", LOG_AUTHPRIV },
121 #endif
122 { "cron", LOG_CRON },
123 { "daemon", LOG_DAEMON },
124 #ifdef LOG_FTP
125 { "ftp", LOG_FTP },
126 #endif
127 { "kern", LOG_KERN },
128 { "lpr", LOG_LPR },
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 },
142 { NULL, -1 }
145 bool
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;
151 close();
152 open();
153 return true;
156 setErrorStr("Invalid syslog facility name");
157 return false;