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

« back to all changes in this revision

Viewing changes to storage/ndb/include/logger/Logger.hpp

  • 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
#ifndef Logger_H
 
17
#define Logger_H
 
18
 
 
19
#include <ndb_global.h>
 
20
#include <BaseString.hpp>
 
21
 
 
22
#define MAX_LOG_MESSAGE_SIZE 1024
 
23
 
 
24
class LogHandler;
 
25
class LogHandlerList;
 
26
 
 
27
/**
 
28
 * Logger should be used whenver you need to log a message like
 
29
 * general information or debug messages. By creating/adding different
 
30
 * log handlers, a single log message can be sent to 
 
31
 * different outputs (stdout, file or syslog).
 
32
 * 
 
33
 * Each log entry is created with a log level (or severity) which is 
 
34
 * used to identity the type of the entry, e.g., if it is a debug 
 
35
 * or an error message.
 
36
 * 
 
37
 * Example of a log entry:
 
38
 *
 
39
 *  09:17:39 2002-03-13 [myLogger] INFO -- Local checkpoint started.
 
40
 *
 
41
 * HOW TO USE
 
42
 *
 
43
 * 1) Create a new instance of the Logger.
 
44
 *
 
45
 *    Logger myLogger = new Logger();
 
46
 *
 
47
 * 2) Add the log handlers that you want, i.e., where the log entries 
 
48
 *    should be written/shown.
 
49
 *
 
50
 *    myLogger->createConsoleHandler();  // Output to console/stdout
 
51
 *    myLogger->addHandler(new FileLogHandler("mylog.txt")); // use mylog.txt
 
52
 *
 
53
 *  3) Tag each log entry with a category/name.
 
54
 *
 
55
 *    myLogger->setCategory("myLogger");
 
56
 *
 
57
 * 4) Start log messages.
 
58
 *    
 
59
 *     myLogger->alert("T-9 to lift off");
 
60
 *     myLogger->info("Here comes the sun, la la"); 
 
61
 *     myLogger->debug("Why does this not work!!!, We should not be here...")
 
62
 *
 
63
 * 5) Log only debug messages.
 
64
 *
 
65
 *    myLogger->enable(Logger::LL_DEBUG);
 
66
 *
 
67
 * 6) Log only ALERTS and ERRORS.
 
68
 *
 
69
 *    myLogger->enable(Logger::LL_ERROR, Logger::LL_ALERT);
 
70
 * 
 
71
 * 7) Do not log any messages.
 
72
 *
 
73
 *    myLogger->disable(Logger::LL_ALL);
 
74
 *
 
75
 *
 
76
 * LOG LEVELS (Matches the severity levels of syslog)
 
77
 * <pre>
 
78
 *
 
79
 *  ALERT           A condition  that  should  be  corrected
 
80
 *                  immediately,  such as a corrupted system
 
81
 *                  database.
 
82
 *
 
83
 *  CRITICAL        Critical conditions, such as hard device
 
84
 *                  errors.
 
85
 *
 
86
 *  ERROR           Errors.
 
87
 *
 
88
 *  WARNING         Warning messages.
 
89
 *
 
90
 *  INFO            Informational messages.
 
91
 *
 
92
 *  DEBUG           Messages that contain  information  nor-
 
93
 *                  mally  of use only when debugging a pro-
 
94
 *                  gram.
 
95
 * </pre>
 
96
 *
 
97
 * @version #@ $Id: Logger.hpp,v 1.7 2003/09/01 10:15:53 innpeno Exp $
 
98
 */
 
99
class Logger
 
100
{
 
101
public:
 
102
  /** The log levels. NOTE: Could not use the name LogLevel since 
 
103
   * it caused conflicts with another class.
 
104
   */
 
105
  enum LoggerLevel {LL_ON, LL_DEBUG, LL_INFO, LL_WARNING, LL_ERROR,
 
106
                    LL_CRITICAL, LL_ALERT, LL_ALL};
 
107
  
 
108
  /**
 
109
   * String representation of the the log levels.
 
110
   */
 
111
  static const char* LoggerLevelNames[];
 
112
 
 
113
  /**
 
114
   * Default constructor.
 
115
   */
 
116
  Logger();
 
117
 
 
118
  /**
 
119
   * Destructor.
 
120
   */
 
121
  virtual ~Logger();
 
122
  
 
123
  /**
 
124
   * Set a category/name that each log entry will have.
 
125
   *
 
126
   * @param pCategory the category.
 
127
   */
 
128
  void setCategory(const char* pCategory);
 
129
 
 
130
  /**
 
131
   * Create a default handler that logs to the console/stdout.
 
132
   *
 
133
   * @return true if successful.
 
134
   */
 
135
  bool createConsoleHandler();
 
136
 
 
137
  /**
 
138
   * Remove the default console handler.
 
139
   */
 
140
  void removeConsoleHandler();
 
141
 
 
142
  /**
 
143
   * Create a default handler that logs to a file called logger.log.
 
144
   *
 
145
   * @return true if successful.
 
146
   */
 
147
  bool createFileHandler();
 
148
 
 
149
  /**
 
150
   * Remove the default file handler.
 
151
   */
 
152
  void removeFileHandler();
 
153
 
 
154
  /**
 
155
   * Create a default handler that logs to the syslog.
 
156
   *
 
157
   * @return true if successful.
 
158
   */
 
159
  bool createSyslogHandler();   
 
160
 
 
161
  /**
 
162
   * Remove the default syslog handler.
 
163
   */
 
164
  void removeSyslogHandler();
 
165
 
 
166
  /**
 
167
   * Add a new log handler.
 
168
   *
 
169
   * @param pHandler a log handler.
 
170
   * @return true if successful.
 
171
   */
 
172
  bool addHandler(LogHandler* pHandler);
 
173
 
 
174
  /**
 
175
   * Add a new handler
 
176
   *
 
177
   * @param logstring string describing the handler to add
 
178
   * @param err OS errno in event of error
 
179
   * @param len max length of errStr buffer
 
180
   * @param errStr logger error string in event of error
 
181
   */
 
182
  bool addHandler(const BaseString &logstring, int *err, int len, char* errStr);
 
183
 
 
184
  /**
 
185
   * Remove a log handler.
 
186
   *
 
187
   * @param pHandler log handler to remove.
 
188
   * @return true if successful.
 
189
   */
 
190
  bool removeHandler(LogHandler* pHandler);
 
191
 
 
192
  /**
 
193
   * Remove all log handlers.
 
194
   */
 
195
  void removeAllHandlers();
 
196
 
 
197
  /**
 
198
   * Returns true if the specified log level is enabled.
 
199
   *
 
200
   * @return true if enabled.
 
201
   */
 
202
  bool isEnable(LoggerLevel logLevel) const; 
 
203
 
 
204
  /**
 
205
   * Enable the specified log level.
 
206
   *
 
207
   * @param logLevel the loglevel to enable.
 
208
   */
 
209
  void enable(LoggerLevel logLevel);
 
210
 
 
211
  /**
 
212
   * Enable log levels.
 
213
   *
 
214
   * @param fromLogLevel enable from log level.
 
215
   * @param toLogLevel enable to log level.
 
216
   */
 
217
  void enable (LoggerLevel fromLogLevel, LoggerLevel toLogLevel);
 
218
 
 
219
  /**
 
220
   * Disable log level.
 
221
   *
 
222
   * @param logLevel disable log level.
 
223
   */
 
224
  void disable(LoggerLevel logLevel);
 
225
 
 
226
  /**
 
227
   * Log an alert message.
 
228
   *
 
229
   * @param pMsg the message.
 
230
   */
 
231
  virtual void alert(const char* pMsg, ...) const;
 
232
  virtual void alert(BaseString &pMsg) const { alert(pMsg.c_str()); };
 
233
  
 
234
  /**
 
235
   * Log a critical message.
 
236
   *
 
237
   * @param pMsg the message.
 
238
   */
 
239
  virtual void critical(const char* pMsg, ...) const;
 
240
  virtual void critical(BaseString &pMsg) const { critical(pMsg.c_str()); };
 
241
 
 
242
  /**
 
243
   * Log an error message.
 
244
   *
 
245
   * @param pMsg the message.
 
246
   */
 
247
  virtual void error(const char* pMsg, ...) const;
 
248
  virtual void error(BaseString &pMsg) const { error(pMsg.c_str()); };
 
249
 
 
250
  /**
 
251
   * Log a warning message.
 
252
   *
 
253
   * @param pMsg the message.
 
254
   */
 
255
  virtual void warning(const char* pMsg, ...) const;
 
256
  virtual void warning(BaseString &pMsg) const { warning(pMsg.c_str()); };
 
257
 
 
258
  /**
 
259
   * Log an info message.
 
260
   *
 
261
   * @param pMsg the message.
 
262
   */
 
263
  virtual void info(const char* pMsg, ...) const;
 
264
  virtual void info(BaseString &pMsg) const { info(pMsg.c_str()); };
 
265
 
 
266
  /**
 
267
   * Log a debug message.
 
268
   *
 
269
   * @param pMsg the message.
 
270
   */
 
271
  virtual void debug(const char* pMsg, ...) const;
 
272
  virtual void debug(BaseString &pMsg) const { debug(pMsg.c_str()); };
 
273
 
 
274
protected:
 
275
 
 
276
  NdbMutex *m_mutex;
 
277
 
 
278
  void log(LoggerLevel logLevel, const char* msg, va_list ap) const;
 
279
 
 
280
private:
 
281
  /** Prohibit */
 
282
  Logger(const Logger&);
 
283
  Logger operator = (const Logger&);
 
284
  bool operator == (const Logger&);
 
285
 
 
286
  STATIC_CONST( MAX_LOG_LEVELS = 8 );
 
287
 
 
288
  bool m_logLevels[MAX_LOG_LEVELS];
 
289
  
 
290
  LogHandlerList* m_pHandlerList;
 
291
  const char* m_pCategory;
 
292
 
 
293
  /* Default handlers */
 
294
  NdbMutex *m_handler_mutex;
 
295
  LogHandler* m_pConsoleHandler;
 
296
  LogHandler* m_pFileHandler;
 
297
  LogHandler* m_pSyslogHandler;
 
298
};
 
299
 
 
300
#endif