MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SysLogHandler.cpp
1 /*
2  Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; version 2 of the License.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17 
18 #ifndef _WIN32
19 
20 #include "SysLogHandler.hpp"
21 
22 #include <syslog.h>
23 
24 //
25 // PUBLIC
26 //
27 
29  m_severity(LOG_INFO),
30  m_pIdentity("NDB"),
31  m_facility(LOG_USER),
32  m_open(false)
33 {
34 }
35 
36 SysLogHandler::SysLogHandler(const char* pIdentity, int facility) :
37  m_severity(LOG_INFO),
38  m_pIdentity(pIdentity),
39  m_facility(facility),
40  m_open(false)
41 {
42 
43 }
44 
46 {
47 }
48 
49 bool
51 {
52  ::setlogmask(LOG_UPTO(LOG_DEBUG)); // Log from EMERGENCY down to DEBUG
53  ::openlog(m_pIdentity, LOG_PID|LOG_CONS|LOG_ODELAY, m_facility); // PID, CONSOLE delay openlog
54  m_open= true;
55  return true;
56 }
57 
58 bool
60 {
61  ::closelog();
62  m_open= false;
63  return true;
64 }
65 
66 bool
68 {
69  return m_open;
70 }
71 
72 void
74 {
75  // Save category to be used by writeMessage...
76  m_pCategory = pCategory;
77  // Map LogLevel to syslog severity
78  switch (level)
79  {
80  case Logger::LL_ALERT:
81  m_severity = LOG_ALERT;
82  break;
83  case Logger::LL_CRITICAL:
84  m_severity = LOG_CRIT;
85  break;
86  case Logger::LL_ERROR:
87  m_severity = LOG_ERR;
88  break;
89  case Logger::LL_WARNING:
90  m_severity = LOG_WARNING;
91  break;
92  case Logger::LL_INFO:
93  m_severity = LOG_INFO;
94  break;
95  case Logger::LL_DEBUG:
96  m_severity = LOG_DEBUG;
97  break;
98  default:
99  m_severity = LOG_INFO;
100  break;
101  }
102 
103 }
104 
105 void
107 {
108  ::syslog(m_facility | m_severity, "[%s] %s", m_pCategory, pMsg);
109 }
110 
111 void
113 {
114  // Need to close it everytime? Do we run out of file descriptors?
115  //::closelog();
116 }
117 
118 bool
119 SysLogHandler::setParam(const BaseString &param, const BaseString &value) {
120  if(param == "facility") {
121  return setFacility(value);
122  }
123  return false;
124 }
125 
126 static const struct syslog_facility {
127  const char *name;
128  int value;
129 } facilitynames[] = {
130  { "auth", LOG_AUTH },
131 #ifdef LOG_AUTHPRIV
132  { "authpriv", LOG_AUTHPRIV },
133 #endif
134  { "cron", LOG_CRON },
135  { "daemon", LOG_DAEMON },
136 #ifdef LOG_FTP
137  { "ftp", LOG_FTP },
138 #endif
139  { "kern", LOG_KERN },
140  { "lpr", LOG_LPR },
141  { "mail", LOG_MAIL },
142  { "news", LOG_NEWS },
143  { "syslog", LOG_SYSLOG },
144  { "user", LOG_USER },
145  { "uucp", LOG_UUCP },
146  { "local0", LOG_LOCAL0 },
147  { "local1", LOG_LOCAL1 },
148  { "local2", LOG_LOCAL2 },
149  { "local3", LOG_LOCAL3 },
150  { "local4", LOG_LOCAL4 },
151  { "local5", LOG_LOCAL5 },
152  { "local6", LOG_LOCAL6 },
153  { "local7", LOG_LOCAL7 },
154  { NULL, -1 }
155 };
156 
157 bool
158 SysLogHandler::setFacility(const BaseString &facility) {
159  const struct syslog_facility *c;
160  for(c = facilitynames; c->name != NULL; c++) {
161  if(facility == c->name) {
162  m_facility = c->value;
163  close();
164  open();
165  return true;
166  }
167  }
168  setErrorStr("Invalid syslog facility name");
169  return false;
170 }
171 
172 #endif