|
@@ -19,10 +19,14 @@
|
|
|
#include <stdarg.h>
|
|
|
#include <stdio.h>
|
|
|
#include <boost/lexical_cast.hpp>
|
|
|
+#include <boost/static_assert.hpp>
|
|
|
+
|
|
|
+#include <log4cplus/configurator.h>
|
|
|
|
|
|
#include <log/debug_levels.h>
|
|
|
#include <log/root_logger_name.h>
|
|
|
#include <log/logger.h>
|
|
|
+#include <log/logger_levels.h>
|
|
|
#include <log/logger_impl.h>
|
|
|
#include <log/message_dictionary.h>
|
|
|
#include <log/message_types.h>
|
|
@@ -37,8 +41,6 @@ namespace log {
|
|
|
|
|
|
// Static initializations
|
|
|
|
|
|
-LoggerImpl::LoggerInfoMap LoggerImpl::logger_info_;
|
|
|
-LoggerImpl::LoggerInfo LoggerImpl::root_logger_info_(isc::log::INFO, 0);
|
|
|
|
|
|
// Constructor
|
|
|
LoggerImpl::LoggerImpl(const std::string& name, bool)
|
|
@@ -51,6 +53,13 @@ LoggerImpl::LoggerImpl(const std::string& name, bool)
|
|
|
is_root_ = false;
|
|
|
name_ = getRootLoggerName() + "." + name;
|
|
|
}
|
|
|
+ fmt_name_ = std::string("[") + name_ + std::string("] ");
|
|
|
+
|
|
|
+ // Initialize log4cplus if not already done
|
|
|
+ initLog4cplus();
|
|
|
+
|
|
|
+ // Return existing instance of logger, creating it if it does not exist.
|
|
|
+ logger_ = log4cplus::Logger::getInstance(name_);
|
|
|
}
|
|
|
|
|
|
// Destructor. (Here because of virtual declaration.)
|
|
@@ -59,54 +68,22 @@ LoggerImpl::~LoggerImpl() {
|
|
|
}
|
|
|
|
|
|
// Set the severity for logging.
|
|
|
+// TODO IGNORE DEBUG LEVEL FOR NOW
|
|
|
|
|
|
void
|
|
|
-LoggerImpl::setSeverity(isc::log::Severity severity, int dbglevel) {
|
|
|
+LoggerImpl::setSeverity(isc::log::Severity severity, int /*dbglevel*/) {
|
|
|
|
|
|
// Silently coerce the debug level into the valid range of 0 to 99
|
|
|
|
|
|
- int debug_level = max(MIN_DEBUG_LEVEL, min(MAX_DEBUG_LEVEL, dbglevel));
|
|
|
- if (is_root_) {
|
|
|
-
|
|
|
- // Can only set severity for the root logger, you can't disable it.
|
|
|
- // Any attempt to do so is silently ignored.
|
|
|
- if (severity != isc::log::DEFAULT) {
|
|
|
- root_logger_info_ = LoggerInfo(severity, debug_level);
|
|
|
- }
|
|
|
-
|
|
|
- } else if (severity == isc::log::DEFAULT) {
|
|
|
-
|
|
|
- // Want to set to default; this means removing the information
|
|
|
- // about this logger from the logger_info_ if it is set.
|
|
|
- LoggerInfoMap::iterator i = logger_info_.find(name_);
|
|
|
- if (i != logger_info_.end()) {
|
|
|
- logger_info_.erase(i);
|
|
|
- }
|
|
|
-
|
|
|
- } else {
|
|
|
-
|
|
|
- // Want to set this information
|
|
|
- logger_info_[name_] = LoggerInfo(severity, debug_level);
|
|
|
- }
|
|
|
+ //int debug_level = max(MIN_DEBUG_LEVEL, min(MAX_DEBUG_LEVEL, dbglevel));
|
|
|
+ logger_.setLogLevel(convertFromBindSeverity(severity));
|
|
|
}
|
|
|
|
|
|
// Return severity level
|
|
|
|
|
|
isc::log::Severity
|
|
|
LoggerImpl::getSeverity() {
|
|
|
-
|
|
|
- if (is_root_) {
|
|
|
- return (root_logger_info_.severity);
|
|
|
- }
|
|
|
- else {
|
|
|
- LoggerInfoMap::iterator i = logger_info_.find(name_);
|
|
|
- if (i != logger_info_.end()) {
|
|
|
- return ((i->second).severity);
|
|
|
- }
|
|
|
- else {
|
|
|
- return (isc::log::DEFAULT);
|
|
|
- }
|
|
|
- }
|
|
|
+ return convertToBindSeverity(logger_.getLogLevel());
|
|
|
}
|
|
|
|
|
|
// Get effective severity. Either the current severity or, if not set, the
|
|
@@ -114,86 +91,29 @@ LoggerImpl::getSeverity() {
|
|
|
|
|
|
isc::log::Severity
|
|
|
LoggerImpl::getEffectiveSeverity() {
|
|
|
-
|
|
|
- if (!is_root_ && !logger_info_.empty()) {
|
|
|
-
|
|
|
- // Not root logger and there is at least one item in the info map for a
|
|
|
- // logger.
|
|
|
- LoggerInfoMap::iterator i = logger_info_.find(name_);
|
|
|
- if (i != logger_info_.end()) {
|
|
|
-
|
|
|
- // Found, so return the severity.
|
|
|
- return ((i->second).severity);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // Must be the root logger, or this logger is defaulting to the root logger
|
|
|
- // settings.
|
|
|
- return (root_logger_info_.severity);
|
|
|
+ return convertToBindSeverity(logger_.getChainedLogLevel());
|
|
|
}
|
|
|
|
|
|
+
|
|
|
// Get the debug level. This returns 0 unless the severity is DEBUG.
|
|
|
+// TODO: DEBUG LEVEL IGNORED FOR NOW
|
|
|
|
|
|
int
|
|
|
LoggerImpl::getDebugLevel() {
|
|
|
-
|
|
|
- if (!is_root_ && !logger_info_.empty()) {
|
|
|
-
|
|
|
- // Not root logger and there is something in the map, check if there
|
|
|
- // is a setting for this one.
|
|
|
- LoggerInfoMap::iterator i = logger_info_.find(name_);
|
|
|
- if (i != logger_info_.end()) {
|
|
|
-
|
|
|
- // Found, so return the debug level.
|
|
|
- if ((i->second).severity == isc::log::DEBUG) {
|
|
|
- return ((i->second).dbglevel);
|
|
|
- } else {
|
|
|
- return (0);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // Must be the root logger, or this logger is defaulting to the root logger
|
|
|
- // settings.
|
|
|
- if (root_logger_info_.severity == isc::log::DEBUG) {
|
|
|
- return (root_logger_info_.dbglevel);
|
|
|
- } else {
|
|
|
- return (0);
|
|
|
- }
|
|
|
+ return (0);
|
|
|
}
|
|
|
|
|
|
// The code for isXxxEnabled is quite simple and is in the header. The only
|
|
|
// exception is isDebugEnabled() where we have the complication of the debug
|
|
|
// levels.
|
|
|
+// TODO: DEBUG LEVEL IGNORED FOR NOW
|
|
|
|
|
|
bool
|
|
|
LoggerImpl::isDebugEnabled(int dbglevel) {
|
|
|
-
|
|
|
- if (!is_root_ && !logger_info_.empty()) {
|
|
|
-
|
|
|
- // Not root logger and there is something in the map, check if there
|
|
|
- // is a setting for this one.
|
|
|
- LoggerInfoMap::iterator i = logger_info_.find(name_);
|
|
|
- if (i != logger_info_.end()) {
|
|
|
-
|
|
|
- // Found, so return the debug level.
|
|
|
- if ((i->second).severity <= isc::log::DEBUG) {
|
|
|
- return ((i->second).dbglevel >= dbglevel);
|
|
|
- } else {
|
|
|
- return (false); // Nothing lower than debug
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- // Must be the root logger, or this logger is defaulting to the root logger
|
|
|
- // settings.
|
|
|
- if (root_logger_info_.severity <= isc::log::DEBUG) {
|
|
|
- return (root_logger_info_.dbglevel >= dbglevel);
|
|
|
- } else {
|
|
|
- return (false);
|
|
|
- }
|
|
|
+ return logger_.isEnabledFor(log4cplus::DEBUG_LOG_LEVEL);
|
|
|
}
|
|
|
|
|
|
+
|
|
|
// Output a general message
|
|
|
string*
|
|
|
LoggerImpl::lookupMessage(const MessageID& ident) {
|
|
@@ -202,18 +122,103 @@ LoggerImpl::lookupMessage(const MessageID& ident) {
|
|
|
}
|
|
|
|
|
|
void
|
|
|
-LoggerImpl::outputRaw(const char* sevText, const string& message) {
|
|
|
- // Get the time in a struct tm format, and convert to text
|
|
|
- time_t t_time;
|
|
|
- time(&t_time);
|
|
|
- struct tm* tm_time = localtime(&t_time);
|
|
|
-
|
|
|
- char chr_time[32];
|
|
|
- (void) strftime(chr_time, sizeof(chr_time), "%Y-%m-%d %H:%M:%S", tm_time);
|
|
|
-
|
|
|
- // Now output.
|
|
|
- cout << chr_time << " " << sevText << " [" << getName() << "] " <<
|
|
|
- message << endl;
|
|
|
+LoggerImpl::outputRaw(const Severity& severity, const string& message) {
|
|
|
+ switch (severity) {
|
|
|
+ case DEBUG:
|
|
|
+ LOG4CPLUS_DEBUG(logger_, fmt_name_ << message);
|
|
|
+ break;
|
|
|
+
|
|
|
+ case INFO:
|
|
|
+ LOG4CPLUS_INFO(logger_, fmt_name_ << message);
|
|
|
+ break;
|
|
|
+
|
|
|
+ case WARN:
|
|
|
+ LOG4CPLUS_WARN(logger_, fmt_name_ << message);
|
|
|
+ break;
|
|
|
+
|
|
|
+ case ERROR:
|
|
|
+ LOG4CPLUS_ERROR(logger_, fmt_name_ << message);
|
|
|
+ break;
|
|
|
+
|
|
|
+ case FATAL:
|
|
|
+ LOG4CPLUS_FATAL(logger_, fmt_name_ << message);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// Convert levels - from BIND 10 level to log4cplus level.
|
|
|
+// Namespaces explicitly used to clarify what level we are talking about
|
|
|
+log4cplus::LogLevel
|
|
|
+LoggerImpl::convertFromBindSeverity(const isc::log::Severity& inlevel) {
|
|
|
+
|
|
|
+ // BIND 10 logging levels are small integers so we can do a table lookup
|
|
|
+ static const log4cplus::LogLevel level[] = {
|
|
|
+ log4cplus::NOT_SET_LOG_LEVEL,
|
|
|
+ log4cplus::DEBUG_LOG_LEVEL,
|
|
|
+ log4cplus::INFO_LOG_LEVEL,
|
|
|
+ log4cplus::WARN_LOG_LEVEL,
|
|
|
+ log4cplus::ERROR_LOG_LEVEL,
|
|
|
+ log4cplus::FATAL_LOG_LEVEL,
|
|
|
+ log4cplus::OFF_LOG_LEVEL
|
|
|
+ };
|
|
|
+
|
|
|
+ // ... with compile-time checks to ensure that table indexes are correct.
|
|
|
+ BOOST_STATIC_ASSERT(static_cast<int>(isc::log::DEFAULT) == 0);
|
|
|
+ BOOST_STATIC_ASSERT(static_cast<int>(isc::log::DEBUG) == 1);
|
|
|
+ BOOST_STATIC_ASSERT(static_cast<int>(isc::log::INFO) == 2);
|
|
|
+ BOOST_STATIC_ASSERT(static_cast<int>(isc::log::WARN) == 3);
|
|
|
+ BOOST_STATIC_ASSERT(static_cast<int>(isc::log::ERROR) == 4);
|
|
|
+ BOOST_STATIC_ASSERT(static_cast<int>(isc::log::FATAL) == 5);
|
|
|
+ BOOST_STATIC_ASSERT(static_cast<int>(isc::log::NONE) == 6);
|
|
|
+
|
|
|
+ // No need to check that the iundex is out of range. Setting the type of
|
|
|
+ // the argument to isc::log::Severity ensures that it must be one of the
|
|
|
+ // Severity enum members - conversion of a numeric value to an enum is not
|
|
|
+ // permitted.
|
|
|
+ return (level[inlevel]);
|
|
|
+}
|
|
|
+
|
|
|
+// Convert levels - from log4plus level to BIND 10 level
|
|
|
+// Namespaces explicitly used to clarify what level we are talking about
|
|
|
+isc::log::Severity
|
|
|
+LoggerImpl::convertToBindSeverity(const log4cplus::LogLevel& inlevel) {
|
|
|
+
|
|
|
+ // Not easy to do a table lookup as the numerical values of log4cplus levels
|
|
|
+ // are quite high.
|
|
|
+ switch (inlevel) {
|
|
|
+ case log4cplus::NOT_SET_LOG_LEVEL:
|
|
|
+ return (isc::log::DEFAULT);
|
|
|
+
|
|
|
+ case log4cplus::DEBUG_LOG_LEVEL:
|
|
|
+ return (isc::log::DEBUG);
|
|
|
+
|
|
|
+ case log4cplus::INFO_LOG_LEVEL:
|
|
|
+ return (isc::log::INFO);
|
|
|
+
|
|
|
+ case log4cplus::WARN_LOG_LEVEL:
|
|
|
+ return (isc::log::WARN);
|
|
|
+
|
|
|
+ case log4cplus::ERROR_LOG_LEVEL:
|
|
|
+ return (isc::log::ERROR);
|
|
|
+
|
|
|
+ case log4cplus::FATAL_LOG_LEVEL:
|
|
|
+ return (isc::log::FATAL);
|
|
|
+ }
|
|
|
+
|
|
|
+ return (isc::log::NONE);
|
|
|
+}
|
|
|
+
|
|
|
+// One-time initialization of log4cplus
|
|
|
+
|
|
|
+
|
|
|
+void
|
|
|
+LoggerImpl::initLog4cplus() {
|
|
|
+ static bool not_initialized = true;
|
|
|
+
|
|
|
+ if (not_initialized) {
|
|
|
+ log4cplus::BasicConfigurator config;
|
|
|
+ config.configure();
|
|
|
+ not_initialized = false;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
} // namespace log
|