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