~ubuntu-branches/ubuntu/precise/mysql-5.1/precise

« back to all changes in this revision

Viewing changes to storage/ndb/src/common/logger/SysLogHandler.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Tretkowski
  • Date: 2010-03-17 14:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20100317145602-x7e30l1b2sb5s6w6
Tags: upstream-5.1.45
ImportĀ upstreamĀ versionĀ 5.1.45

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2003 MySQL AB
 
2
 
 
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.
 
6
 
 
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.
 
11
 
 
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
15
 
 
16
#include "SysLogHandler.hpp"
 
17
 
 
18
#include <syslog.h>
 
19
 
 
20
//
 
21
// PUBLIC
 
22
//
 
23
 
 
24
SysLogHandler::SysLogHandler() :
 
25
  m_severity(LOG_INFO),
 
26
  m_pIdentity("NDB"),
 
27
  m_facility(LOG_USER)
 
28
{
 
29
}
 
30
 
 
31
SysLogHandler::SysLogHandler(const char* pIdentity, int facility) : 
 
32
  m_severity(LOG_INFO), 
 
33
  m_pIdentity(pIdentity),
 
34
  m_facility(facility)
 
35
{
 
36
 
 
37
}
 
38
 
 
39
SysLogHandler::~SysLogHandler()
 
40
{
 
41
}
 
42
 
 
43
bool
 
44
SysLogHandler::open()
 
45
{
 
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
 
48
 
 
49
  return true;
 
50
}
 
51
 
 
52
bool
 
53
SysLogHandler::close()
 
54
{
 
55
  ::closelog();
 
56
 
 
57
  return true;
 
58
}
 
59
 
 
60
void 
 
61
SysLogHandler::writeHeader(const char* pCategory, Logger::LoggerLevel level)
 
62
{
 
63
  // Save category to be used by writeMessage...
 
64
  m_pCategory = pCategory;
 
65
  // Map LogLevel to syslog severity
 
66
  switch (level)
 
67
  {
 
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;
 
89
  }
 
90
 
 
91
}
 
92
 
 
93
void 
 
94
SysLogHandler::writeMessage(const char* pMsg)
 
95
{
 
96
  ::syslog(m_facility | m_severity, "[%s] %s", m_pCategory, pMsg); 
 
97
}
 
98
 
 
99
void 
 
100
SysLogHandler::writeFooter()
 
101
{
 
102
  // Need to close it everytime? Do we run out of file descriptors?
 
103
  //::closelog();
 
104
}
 
105
 
 
106
bool
 
107
SysLogHandler::setParam(const BaseString &param, const BaseString &value) {
 
108
  if(param == "facility") {
 
109
    return setFacility(value);
 
110
  }
 
111
  return false;
 
112
}
 
113
 
 
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 }
 
143
};
 
144
 
 
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;
 
154
    }
 
155
  }
 
156
  setErrorStr("Invalid syslog facility name");
 
157
  return false;
 
158
}