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 */
19 #include <ndb_global.h>
20 #include <BaseString.hpp>
22 #define MAX_LOG_MESSAGE_SIZE 1024
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).
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.
37 * Example of a log entry:
39 * 09:17:39 2002-03-13 [myLogger] INFO -- Local checkpoint started.
43 * 1) Create a new instance of the Logger.
45 * Logger myLogger = new Logger();
47 * 2) Add the log handlers that you want, i.e., where the log entries
48 * should be written/shown.
50 * myLogger->createConsoleHandler(); // Output to console/stdout
51 * myLogger->addHandler(new FileLogHandler("mylog.txt")); // use mylog.txt
53 * 3) Tag each log entry with a category/name.
55 * myLogger->setCategory("myLogger");
57 * 4) Start log messages.
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...")
63 * 5) Log only debug messages.
65 * myLogger->enable(Logger::LL_DEBUG);
67 * 6) Log only ALERTS and ERRORS.
69 * myLogger->enable(Logger::LL_ERROR, Logger::LL_ALERT);
71 * 7) Do not log any messages.
73 * myLogger->disable(Logger::LL_ALL);
76 * LOG LEVELS (Matches the severity levels of syslog)
79 * ALERT A condition that should be corrected
80 * immediately, such as a corrupted system
83 * CRITICAL Critical conditions, such as hard device
88 * WARNING Warning messages.
90 * INFO Informational messages.
92 * DEBUG Messages that contain information nor-
93 * mally of use only when debugging a pro-
97 * @version #@ $Id: Logger.hpp,v 1.7 2003/09/01 10:15:53 innpeno Exp $
102 /** The log levels. NOTE: Could not use the name LogLevel since
103 * it caused conflicts with another class.
105 enum LoggerLevel
{LL_ON
, LL_DEBUG
, LL_INFO
, LL_WARNING
, LL_ERROR
,
106 LL_CRITICAL
, LL_ALERT
, LL_ALL
};
109 * String representation of the the log levels.
111 static const char* LoggerLevelNames
[];
114 * Default constructor.
124 * Set a category/name that each log entry will have.
126 * @param pCategory the category.
128 void setCategory(const char* pCategory
);
131 * Create a default handler that logs to the console/stdout.
133 * @return true if successful.
135 bool createConsoleHandler();
138 * Remove the default console handler.
140 void removeConsoleHandler();
143 * Create a default handler that logs to a file called logger.log.
145 * @return true if successful.
147 bool createFileHandler();
150 * Remove the default file handler.
152 void removeFileHandler();
155 * Create a default handler that logs to the syslog.
157 * @return true if successful.
159 bool createSyslogHandler();
162 * Remove the default syslog handler.
164 void removeSyslogHandler();
167 * Add a new log handler.
169 * @param pHandler a log handler.
170 * @return true if successful.
172 bool addHandler(LogHandler
* pHandler
);
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
182 bool addHandler(const BaseString
&logstring
, int *err
, int len
, char* errStr
);
185 * Remove a log handler.
187 * @param pHandler log handler to remove.
188 * @return true if successful.
190 bool removeHandler(LogHandler
* pHandler
);
193 * Remove all log handlers.
195 void removeAllHandlers();
198 * Returns true if the specified log level is enabled.
200 * @return true if enabled.
202 bool isEnable(LoggerLevel logLevel
) const;
205 * Enable the specified log level.
207 * @param logLevel the loglevel to enable.
209 void enable(LoggerLevel logLevel
);
214 * @param fromLogLevel enable from log level.
215 * @param toLogLevel enable to log level.
217 void enable (LoggerLevel fromLogLevel
, LoggerLevel toLogLevel
);
222 * @param logLevel disable log level.
224 void disable(LoggerLevel logLevel
);
227 * Log an alert message.
229 * @param pMsg the message.
231 virtual void alert(const char* pMsg
, ...) const;
232 virtual void alert(BaseString
&pMsg
) const { alert(pMsg
.c_str()); };
235 * Log a critical message.
237 * @param pMsg the message.
239 virtual void critical(const char* pMsg
, ...) const;
240 virtual void critical(BaseString
&pMsg
) const { critical(pMsg
.c_str()); };
243 * Log an error message.
245 * @param pMsg the message.
247 virtual void error(const char* pMsg
, ...) const;
248 virtual void error(BaseString
&pMsg
) const { error(pMsg
.c_str()); };
251 * Log a warning message.
253 * @param pMsg the message.
255 virtual void warning(const char* pMsg
, ...) const;
256 virtual void warning(BaseString
&pMsg
) const { warning(pMsg
.c_str()); };
259 * Log an info message.
261 * @param pMsg the message.
263 virtual void info(const char* pMsg
, ...) const;
264 virtual void info(BaseString
&pMsg
) const { info(pMsg
.c_str()); };
267 * Log a debug message.
269 * @param pMsg the message.
271 virtual void debug(const char* pMsg
, ...) const;
272 virtual void debug(BaseString
&pMsg
) const { debug(pMsg
.c_str()); };
278 void log(LoggerLevel logLevel
, const char* msg
, va_list ap
) const;
282 Logger(const Logger
&);
283 Logger
operator = (const Logger
&);
284 bool operator == (const Logger
&);
286 STATIC_CONST( MAX_LOG_LEVELS
= 8 );
288 bool m_logLevels
[MAX_LOG_LEVELS
];
290 LogHandlerList
* m_pHandlerList
;
291 const char* m_pCategory
;
293 /* Default handlers */
294 NdbMutex
*m_handler_mutex
;
295 LogHandler
* m_pConsoleHandler
;
296 LogHandler
* m_pFileHandler
;
297 LogHandler
* m_pSyslogHandler
;