summaryrefslogtreecommitdiff
path: root/ndb/include/logger
diff options
context:
space:
mode:
authorunknown <magnus@neptunus.(none)>2004-04-14 10:53:21 +0200
committerunknown <magnus@neptunus.(none)>2004-04-14 10:53:21 +0200
commit6386c55cee50bad6a9979d1fab28e03bb8612ca7 (patch)
tree3fbbacf704304b69228474b9f03549ccd585a017 /ndb/include/logger
parent0ba6cb48d84f1ff951d09871a96be6cdef3f2c3c (diff)
downloadmariadb-git-6386c55cee50bad6a9979d1fab28e03bb8612ca7.tar.gz
Initial revision of NDB Cluster files
BitKeeper/etc/logging_ok: Logging to logging@openlogging.org accepted
Diffstat (limited to 'ndb/include/logger')
-rw-r--r--ndb/include/logger/ConsoleLogHandler.hpp57
-rw-r--r--ndb/include/logger/FileLogHandler.hpp110
-rw-r--r--ndb/include/logger/LogHandler.hpp198
-rw-r--r--ndb/include/logger/Logger.hpp294
-rw-r--r--ndb/include/logger/SysLogHandler.hpp97
5 files changed, 756 insertions, 0 deletions
diff --git a/ndb/include/logger/ConsoleLogHandler.hpp b/ndb/include/logger/ConsoleLogHandler.hpp
new file mode 100644
index 00000000000..ae77b13d3b7
--- /dev/null
+++ b/ndb/include/logger/ConsoleLogHandler.hpp
@@ -0,0 +1,57 @@
+/* Copyright (C) 2003 MySQL AB
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
+
+#ifndef CONSOLELOGHANDLER_H
+#define CONSOLELOGHANDLER_H
+
+#include "LogHandler.hpp"
+
+/**
+ * Logs messages to the console/stdout.
+ *
+ * @see LogHandler
+ * @version #@ $Id: ConsoleLogHandler.hpp,v 1.2 2003/09/01 10:15:53 innpeno Exp $
+ */
+class ConsoleLogHandler : public LogHandler
+{
+public:
+ /**
+ * Default constructor.
+ */
+ ConsoleLogHandler();
+ /**
+ * Destructor.
+ */
+ virtual ~ConsoleLogHandler();
+
+ virtual bool open();
+ virtual bool close();
+
+ virtual bool setParam(const BaseString &param, const BaseString &value);
+
+protected:
+ virtual void writeHeader(const char* pCategory, Logger::LoggerLevel level);
+ virtual void writeMessage(const char* pMsg);
+ virtual void writeFooter();
+
+private:
+ /** Prohibit*/
+ ConsoleLogHandler(const ConsoleLogHandler&);
+ ConsoleLogHandler operator = (const ConsoleLogHandler&);
+ bool operator == (const ConsoleLogHandler&);
+
+};
+#endif
diff --git a/ndb/include/logger/FileLogHandler.hpp b/ndb/include/logger/FileLogHandler.hpp
new file mode 100644
index 00000000000..ae69a2f5418
--- /dev/null
+++ b/ndb/include/logger/FileLogHandler.hpp
@@ -0,0 +1,110 @@
+/* Copyright (C) 2003 MySQL AB
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
+
+#ifndef FILELOGHANDLER_H
+#define FILELOGHANDLER_H
+
+#include "LogHandler.hpp"
+
+class File;
+
+/**
+ * Logs messages to a file. The log file will be archived depending on
+ * the file's size or after N number of log entries.
+ * There will be only a specified number of archived logs
+ * which will be "recycled".
+ *
+ * The archived log file will be named as <filename>.1..N.
+ *
+ *
+ * @see LogHandler
+ * @version #@ $Id: FileLogHandler.hpp,v 1.2 2003/09/01 10:15:53 innpeno Exp $
+ */
+class FileLogHandler : public LogHandler
+{
+public:
+ /** Max number of log files to archive. */
+ static const int MAX_NO_FILES = 6;
+ /** Max file size of the log before archiving. */
+ static const long MAX_FILE_SIZE = 1024000;
+ /** Max number of log entries before archiving. */
+ static const unsigned int MAX_LOG_ENTRIES = 10000;
+
+ /**
+ * Default constructor.
+ */
+ FileLogHandler();
+
+ /**
+ * Creates a new file handler with the specified filename,
+ * max number of archived log files and max log size for each log.
+ *
+ * @param aFileName the log filename.
+ * @param maxNoFiles the maximum number of archived log files.
+ * @param maxFileSize the maximum log file size before archiving.
+ * @param maxLogEntries the maximum number of log entries before checking time to archive.
+ */
+ FileLogHandler(const char* aFileName,
+ int maxNoFiles = MAX_NO_FILES,
+ long maxFileSize = MAX_FILE_SIZE,
+ unsigned int maxLogEntries = MAX_LOG_ENTRIES);
+
+ /**
+ * Destructor.
+ */
+ virtual ~FileLogHandler();
+
+ virtual bool open();
+ virtual bool close();
+
+ virtual bool setParam(const BaseString &param, const BaseString &value);
+ virtual bool checkParams();
+
+protected:
+ virtual void writeHeader(const char* pCategory, Logger::LoggerLevel level);
+ virtual void writeMessage(const char* pMsg);
+ virtual void writeFooter();
+
+private:
+ /** Prohibit */
+ FileLogHandler(const FileLogHandler&);
+ FileLogHandler operator = (const FileLogHandler&);
+ bool operator == (const FileLogHandler&);
+
+ /**
+ * Returns true if it is time to create a new log file.
+ */
+ bool isTimeForNewFile();
+
+ /**
+ * Archives the current log file and creates a new one.
+ * The archived log filename will be in the format of <filename>.N
+ *
+ * @return true if successful.
+ */
+ bool createNewFile();
+
+ bool setFilename(const BaseString &filename);
+ bool setMaxSize(const BaseString &size);
+ bool setMaxFiles(const BaseString &files);
+
+ int m_maxNoFiles;
+ long m_maxFileSize;
+ unsigned int m_maxLogEntries;
+ File* m_pLogFile;
+};
+
+#endif
diff --git a/ndb/include/logger/LogHandler.hpp b/ndb/include/logger/LogHandler.hpp
new file mode 100644
index 00000000000..8c5c9298f69
--- /dev/null
+++ b/ndb/include/logger/LogHandler.hpp
@@ -0,0 +1,198 @@
+/* Copyright (C) 2003 MySQL AB
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
+
+#ifndef LOGHANDLER_H
+#define LOGHANDLER_H
+
+#include "Logger.hpp"
+
+#include <NdbStdio.h> // Defines NULL
+
+/**
+ * This class is the base class for all log handlers. A log handler is
+ * responsible for formatting and writing log messages to a specific output.
+ *
+ * A log entry consists of three parts: a header, <body/log message and a footer.
+ * <pre>
+ * 09:17:37 2002-03-13 [MgmSrv] INFO -- Local checkpoint 13344 started.
+ * </pre>
+ *
+ * Header format: TIME&DATE CATEGORY LEVEL --
+ * TIME&DATE = ctime() format.
+ * CATEGORY = Any string.
+ * LEVEL = ALERT to DEBUG (Log levels)
+ *
+ * Footer format: \n (currently only newline)
+ *
+ * @version #@ $Id: LogHandler.hpp,v 1.7 2003/09/01 10:15:53 innpeno Exp $
+ */
+class LogHandler
+{
+public:
+ /**
+ * Default constructor.
+ */
+ LogHandler();
+
+ /**
+ * Destructor.
+ */
+ virtual ~LogHandler();
+
+ /**
+ * Opens/initializes the log handler.
+ *
+ * @return true if successful.
+ */
+ virtual bool open() = 0;
+
+ /**
+ * Closes/free any allocated resources used by the log handler.
+ *
+ * @return true if successful.
+ */
+ virtual bool close() = 0;
+
+ /**
+ * Append a log message to the output stream/file whatever.
+ * append() will call writeHeader(), writeMessage() and writeFooter() for
+ * a child class and in that order.
+ *
+ * @param pCategory the category/name to tag the log entry with.
+ * @param level the log level.
+ * @param pMsg the log message.
+ */
+ void append(const char* pCategory, Logger::LoggerLevel level,
+ const char* pMsg);
+
+ /**
+ * Returns a default formatted header. It currently has the
+ * follwing default format: '%H:%M:%S %Y-%m-%d [CATEGORY] LOGLEVEL --'
+ *
+ * @param pStr the header string to format.
+ * @param pCategory a category/name to tag the log entry with.
+ * @param level the log level.
+ * @return the header.
+ */
+ const char* getDefaultHeader(char* pStr, const char* pCategory,
+ Logger::LoggerLevel level) const;
+
+ /**
+ * Returns a default formatted footer. Currently only returns a newline.
+ *
+ * @return the footer.
+ */
+ const char* getDefaultFooter() const;
+
+ /**
+ * Returns the date and time format used by ctime().
+ *
+ * @return the date and time format.
+ */
+ const char* getDateTimeFormat() const;
+
+ /**
+ * Sets the date and time format. It needs to have the same arguments
+ * a ctime().
+ *
+ * @param pFormat the date and time format.
+ */
+ void setDateTimeFormat(const char* pFormat);
+
+ /**
+ * Returns a string date and time string.
+ *
+ * @param pStr a string.
+ * @return a string with date and time.
+ */
+ char* getTimeAsString(char* pStr) const;
+
+ /**
+ * Returns the error code.
+ */
+ int getErrorCode() const;
+
+ /**
+ * Sets the error code.
+ *
+ * @param code the error code.
+ */
+ void setErrorCode(int code);
+
+ /**
+ * Parse logstring parameters
+ *
+ * @param params list of parameters, formatted as "param=value",
+ * entries separated by ","
+ * @return true on success, false on failure
+ */
+ bool parseParams(const BaseString &params);
+
+ /**
+ * Sets a parameters. What parameters are accepted depends on the subclass.
+ *
+ * @param param name of parameter
+ * @param value value of parameter
+ */
+ virtual bool setParam(const BaseString &param, const BaseString &value) = 0;
+
+ /**
+ * Checks that all necessary parameters have been set.
+ *
+ * @return true if all parameters are correctly set, false otherwise
+ */
+ virtual bool checkParams();
+
+protected:
+ /** Max length of the date and time header in the log. */
+ static const int MAX_DATE_TIME_HEADER_LENGTH = 64;
+ /** Max length of the header the log. */
+ static const int MAX_HEADER_LENGTH = 128;
+ /** Max lenght of footer in the log. */
+ static const int MAX_FOOTER_LENGTH = 128;
+
+ /**
+ * Write the header to the log.
+ *
+ * @param pCategory the category to tag the log with.
+ * @param level the log level.
+ */
+ virtual void writeHeader(const char* category, Logger::LoggerLevel level) = 0;
+
+ /**
+ * Write the message to the log.
+ *
+ * @param pMsg the message to log.
+ */
+ virtual void writeMessage(const char* pMsg) = 0;
+
+ /**
+ * Write the footer to the log.
+ *
+ */
+ virtual void writeFooter() = 0;
+
+private:
+ /** Prohibit */
+ LogHandler(const LogHandler&);
+ LogHandler* operator = (const LogHandler&);
+ bool operator == (const LogHandler&);
+
+ const char* m_pDateTimeFormat;
+ int m_errorCode;
+};
+
+#endif
diff --git a/ndb/include/logger/Logger.hpp b/ndb/include/logger/Logger.hpp
new file mode 100644
index 00000000000..2d12a5b8a6e
--- /dev/null
+++ b/ndb/include/logger/Logger.hpp
@@ -0,0 +1,294 @@
+/* Copyright (C) 2003 MySQL AB
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
+
+#ifndef Logger_H
+#define Logger_H
+
+#include <BaseString.hpp>
+#include <stdarg.h>
+
+class LogHandler;
+class LogHandlerList;
+
+/**
+ * Logger should be used whenver you need to log a message like
+ * general information or debug messages. By creating/adding different
+ * log handlers, a single log message can be sent to
+ * different outputs (stdout, file or syslog).
+ *
+ * Each log entry is created with a log level (or severity) which is
+ * used to identity the type of the entry, e.g., if it is a debug
+ * or an error message.
+ *
+ * Example of a log entry:
+ *
+ * 09:17:39 2002-03-13 [myLogger] INFO -- Local checkpoint started.
+ *
+ * HOW TO USE
+ *
+ * 1) Create a new instance of the Logger.
+ *
+ * Logger myLogger = new Logger();
+ *
+ * 2) Add the log handlers that you want, i.e., where the log entries
+ * should be written/shown.
+ *
+ * myLogger->createConsoleHandler(); // Output to console/stdout
+ * myLogger->addHandler(new FileLogHandler("mylog.txt")); // use mylog.txt
+ *
+ * 3) Tag each log entry with a category/name.
+ *
+ * myLogger->setCategory("myLogger");
+ *
+ * 4) Start log messages.
+ *
+ * myLogger->alert("T-9 to lift off");
+ * myLogger->info("Here comes the sun, la la");
+ * myLogger->debug("Why does this not work!!!, We should not be here...")
+ *
+ * 5) Log only debug messages.
+ *
+ * myLogger->enable(Logger::LL_DEBUG);
+ *
+ * 6) Log only ALERTS and ERRORS.
+ *
+ * myLogger->enable(Logger::LL_ERROR, Logger::LL_ALERT);
+ *
+ * 7) Do not log any messages.
+ *
+ * myLogger->disable(Logger::LL_ALL);
+ *
+ *
+ * LOG LEVELS (Matches the severity levels of syslog)
+ * <pre>
+ *
+ * ALERT A condition that should be corrected
+ * immediately, such as a corrupted system
+ * database.
+ *
+ * CRITICAL Critical conditions, such as hard device
+ * errors.
+ *
+ * ERROR Errors.
+ *
+ * WARNING Warning messages.
+ *
+ * INFO Informational messages.
+ *
+ * DEBUG Messages that contain information nor-
+ * mally of use only when debugging a pro-
+ * gram.
+ * </pre>
+ *
+ * @version #@ $Id: Logger.hpp,v 1.7 2003/09/01 10:15:53 innpeno Exp $
+ */
+class Logger
+{
+public:
+ /** The log levels. NOTE: Could not use the name LogLevel since
+ * it caused conflicts with another class.
+ */
+ enum LoggerLevel {LL_OFF, LL_DEBUG, LL_INFO, LL_WARNING, LL_ERROR,
+ LL_CRITICAL, LL_ALERT, LL_ALL};
+
+ /**
+ * String representation of the the log levels.
+ */
+ static const char* LoggerLevelNames[];
+
+ /**
+ * Default constructor.
+ */
+ Logger();
+
+ /**
+ * Destructor.
+ */
+ virtual ~Logger();
+
+ /**
+ * Set a category/name that each log entry will have.
+ *
+ * @param pCategory the category.
+ */
+ void setCategory(const char* pCategory);
+
+ /**
+ * Create a default handler that logs to the console/stdout.
+ *
+ * @return true if successful.
+ */
+ bool createConsoleHandler();
+
+ /**
+ * Remove the default console handler.
+ */
+ void removeConsoleHandler();
+
+ /**
+ * Create a default handler that logs to a file called logger.log.
+ *
+ * @return true if successful.
+ */
+ bool createFileHandler();
+
+ /**
+ * Remove the default file handler.
+ */
+ void removeFileHandler();
+
+ /**
+ * Create a default handler that logs to the syslog.
+ *
+ * On OSE a ConsoleHandler will be created since there is no syslog support.
+ *
+ * @return true if successful.
+ */
+ bool createSyslogHandler();
+
+ /**
+ * Remove the default syslog handler.
+ */
+ void removeSyslogHandler();
+
+ /**
+ * Add a new log handler.
+ *
+ * @param pHandler a log handler.
+ * @return true if successful.
+ */
+ bool addHandler(LogHandler* pHandler);
+
+ /**
+ * Add a new handler
+ *
+ * @param logstring string describing the handler to add
+ */
+ bool addHandler(const BaseString &logstring);
+
+ /**
+ * Remove a log handler.
+ *
+ * @param pHandler log handler to remove.
+ * @return true if successful.
+ */
+ bool removeHandler(LogHandler* pHandler);
+
+ /**
+ * Remove all log handlers.
+ */
+ void removeAllHandlers();
+
+ /**
+ * Returns true if the specified log level is enabled.
+ *
+ * @return true if enabled.
+ */
+ bool isEnable(LoggerLevel logLevel) const;
+
+ /**
+ * Enable the specified log level.
+ *
+ * @param logLevel the loglevel to enable.
+ */
+ void enable(LoggerLevel logLevel);
+
+ /**
+ * Enable log levels.
+ *
+ * @param fromLogLevel enable from log level.
+ * @param toLogLevel enable to log level.
+ */
+ void enable (LoggerLevel fromLogLevel, LoggerLevel toLogLevel);
+
+ /**
+ * Disable log level.
+ *
+ * @param logLevel disable log level.
+ */
+ void disable(LoggerLevel logLevel);
+
+ /**
+ * Log an alert message.
+ *
+ * @param pMsg the message.
+ */
+ virtual void alert(const char* pMsg, ...) const;
+ virtual void alert(BaseString &pMsg) const { alert(pMsg.c_str()); };
+
+ /**
+ * Log a critical message.
+ *
+ * @param pMsg the message.
+ */
+ virtual void critical(const char* pMsg, ...) const;
+ virtual void critical(BaseString &pMsg) const { critical(pMsg.c_str()); };
+
+ /**
+ * Log an error message.
+ *
+ * @param pMsg the message.
+ */
+ virtual void error(const char* pMsg, ...) const;
+ virtual void error(BaseString &pMsg) const { error(pMsg.c_str()); };
+
+ /**
+ * Log a warning message.
+ *
+ * @param pMsg the message.
+ */
+ virtual void warning(const char* pMsg, ...) const;
+ virtual void warning(BaseString &pMsg) const { warning(pMsg.c_str()); };
+
+ /**
+ * Log an info message.
+ *
+ * @param pMsg the message.
+ */
+ virtual void info(const char* pMsg, ...) const;
+ virtual void info(BaseString &pMsg) const { info(pMsg.c_str()); };
+
+ /**
+ * Log a debug message.
+ *
+ * @param pMsg the message.
+ */
+ virtual void debug(const char* pMsg, ...) const;
+ virtual void debug(BaseString &pMsg) const { debug(pMsg.c_str()); };
+
+protected:
+
+ void log(LoggerLevel logLevel, const char* msg, va_list ap) const;
+
+private:
+ /** Prohibit */
+ Logger(const Logger&);
+ Logger operator = (const Logger&);
+ bool operator == (const Logger&);
+
+ static const int MAX_LOG_LEVELS = 8;
+
+ bool m_logLevels[MAX_LOG_LEVELS];
+
+ LogHandlerList* m_pHandlerList;
+ const char* m_pCategory;
+ /* Default handlers */
+ LogHandler* m_pConsoleHandler;
+ LogHandler* m_pFileHandler;
+ LogHandler* m_pSyslogHandler;
+};
+
+#endif
diff --git a/ndb/include/logger/SysLogHandler.hpp b/ndb/include/logger/SysLogHandler.hpp
new file mode 100644
index 00000000000..4f13308d61b
--- /dev/null
+++ b/ndb/include/logger/SysLogHandler.hpp
@@ -0,0 +1,97 @@
+/* Copyright (C) 2003 MySQL AB
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
+
+#ifndef SYSLOGHANDLER_H
+#define SYSLOGHANDLER_H
+
+#include "LogHandler.hpp"
+#include <syslog.h>
+
+/**
+ * Logs messages to syslog. The default identity is 'NDB'.
+ * See 'man 3 syslog'.
+ *
+ * It logs the following severity levels.
+ * <pre>
+ *
+ * LOG_ALERT A condition that should be corrected
+ * immediately, such as a corrupted system
+ * database.
+ *
+ * LOG_CRIT Critical conditions, such as hard device
+ * errors.
+ *
+ * LOG_ERR Errors.
+ *
+ * LOG_WARNING Warning messages.
+ *
+ * LOG_INFO Informational messages.
+ *
+ * LOG_DEBUG Messages that contain information nor-
+ * mally of use only when debugging a pro-
+ * gram.
+ * </pre>
+ *
+ * @see LogHandler
+ * @version #@ $Id: SysLogHandler.hpp,v 1.2 2003/09/01 10:15:53 innpeno Exp $
+ */
+class SysLogHandler : public LogHandler
+{
+public:
+ /**
+ * Default constructor.
+ */
+ SysLogHandler();
+
+ /**
+ * Create a new syslog handler with the specified identity.
+ *
+ * @param pIdentity a syslog identity.
+ * @param facility syslog facility, defaults to LOG_USER
+ */
+ SysLogHandler(const char* pIdentity, int facility = LOG_USER);
+
+ /**
+ * Destructor.
+ */
+ virtual ~SysLogHandler();
+
+ virtual bool open();
+ virtual bool close();
+
+ virtual bool setParam(const BaseString &param, const BaseString &value);
+ bool setFacility(const BaseString &facility);
+
+protected:
+ virtual void writeHeader(const char* pCategory, Logger::LoggerLevel level);
+ virtual void writeMessage(const char* pMsg);
+ virtual void writeFooter();
+
+private:
+ /** Prohibit*/
+ SysLogHandler(const SysLogHandler&);
+ SysLogHandler operator = (const SysLogHandler&);
+ bool operator == (const SysLogHandler&);
+
+ int m_severity;
+ const char* m_pCategory;
+
+ /** Syslog identity for all log entries. */
+ const char* m_pIdentity;
+ int m_facility;
+};
+
+#endif