2
0
mirror of https://gitlab.isc.org/isc-projects/kea synced 2025-08-31 05:55:28 +00:00

[#1955] don't revert logging on initial config fail

This commit is contained in:
Andrei Pavel
2022-08-24 16:43:11 +03:00
parent 827f13461d
commit a9ac326d83
8 changed files with 87 additions and 19 deletions

View File

@@ -414,18 +414,19 @@ ControlledDhcpv4Srv::commandConfigSetHandler(const string&,
// Use new configuration.
CfgMgr::instance().commit();
} else {
} else if (CfgMgr::instance().getCurrentCfg()->getSequence() != 0) {
// Ok, we applied the logging from the upcoming configuration, but
// there were problems with the config. As such, we need to back off
// and revert to the previous logging configuration.
// and revert to the previous logging configuration. This is not done if
// sequence == 0, because that would mean always reverting to stdout by
// default, and it is arguably more helpful to have the error in a
// potential file or syslog configured in the upcoming configuration.
CfgMgr::instance().getCurrentCfg()->applyLoggingCfg();
if (CfgMgr::instance().getCurrentCfg()->getSequence() != 0) {
// Not initial configuration so someone can believe we reverted
// to the previous configuration. It is not the case so be clear
// about this.
LOG_FATAL(dhcp4_logger, DHCP4_CONFIG_UNRECOVERABLE_ERROR);
}
// Not initial configuration so someone can believe we reverted
// to the previous configuration. It is not the case so be clear
// about this.
LOG_FATAL(dhcp4_logger, DHCP4_CONFIG_UNRECOVERABLE_ERROR);
}
return (result);

View File

@@ -16,6 +16,7 @@
#include <exceptions/exceptions.h>
#include <log/logger_support.h>
#include <log/logger_manager.h>
#include <log/output_option.h>
#include <process/daemon.h>
#include <boost/lexical_cast.hpp>
@@ -240,8 +241,15 @@ main(int argc, char* argv[]) {
server.init(config_file);
} catch (const std::exception& ex) {
// Let's log out what went wrong.
try {
// Let's log out what went wrong.
// Log with the current logger, but only if it's not
// configured with console output so as to not log twice.
if (!dhcp4_logger.hasAppender(isc::log::OutputOption::DEST_CONSOLE)) {
LOG_ERROR(dhcp4_logger, DHCP4_INIT_FAIL).arg(ex.what());
}
// Log on the console as well.
isc::log::LoggerManager log_manager;
log_manager.process();
LOG_ERROR(dhcp4_logger, DHCP4_INIT_FAIL).arg(ex.what());

View File

@@ -417,18 +417,19 @@ ControlledDhcpv6Srv::commandConfigSetHandler(const string&,
// Use new configuration.
CfgMgr::instance().commit();
} else {
} else if (CfgMgr::instance().getCurrentCfg()->getSequence() != 0) {
// Ok, we applied the logging from the upcoming configuration, but
// there were problems with the config. As such, we need to back off
// and revert to the previous logging configuration.
// and revert to the previous logging configuration. This is not done if
// sequence == 0, because that would mean always reverting to stdout by
// default, and it is arguably more helpful to have the error in a
// potential file or syslog configured in the upcoming configuration.
CfgMgr::instance().getCurrentCfg()->applyLoggingCfg();
if (CfgMgr::instance().getCurrentCfg()->getSequence() != 0) {
// Not initial configuration so someone can believe we reverted
// to the previous configuration. It is not the case so be clear
// about this.
LOG_FATAL(dhcp6_logger, DHCP6_CONFIG_UNRECOVERABLE_ERROR);
}
// Not initial configuration so someone can believe we reverted
// to the previous configuration. It is not the case so be clear
// about this.
LOG_FATAL(dhcp6_logger, DHCP6_CONFIG_UNRECOVERABLE_ERROR);
}
return (result);

View File

@@ -16,6 +16,7 @@
#include <exceptions/exceptions.h>
#include <log/logger_support.h>
#include <log/logger_manager.h>
#include <log/output_option.h>
#include <process/daemon.h>
#include <boost/lexical_cast.hpp>
@@ -240,8 +241,15 @@ main(int argc, char* argv[]) {
server.init(config_file);
} catch (const std::exception& ex) {
// Let's log out what went wrong.
try {
// Let's log out what went wrong.
// Log with the current logger, but only if it's not
// configured with console output so as to not log twice.
if (!dhcp6_logger.hasAppender(isc::log::OutputOption::DEST_CONSOLE)) {
LOG_ERROR(dhcp6_logger, DHCP6_INIT_FAIL).arg(ex.what());
}
// Log on the console as well.
isc::log::LoggerManager log_manager;
log_manager.process();
LOG_ERROR(dhcp6_logger, DHCP6_INIT_FAIL).arg(ex.what());

View File

@@ -204,6 +204,11 @@ Logger::setInterprocessSync(isc::log::interprocess::InterprocessSync* sync) {
getLoggerPtr()->setInterprocessSync(sync);
}
bool
Logger::hasAppender(OutputOption::Destination const destination) {
return getLoggerPtr()->hasAppender(destination);
}
// Comparison (testing only)
bool

View File

@@ -17,6 +17,7 @@
#include <log/logger_level.h>
#include <log/message_types.h>
#include <log/log_formatter.h>
#include <log/output_option.h>
namespace isc {
namespace log {
@@ -294,6 +295,13 @@ public:
/// a BadInterprocessSync exception is thrown.
void setInterprocessSync(isc::log::interprocess::InterprocessSync* sync);
/// @brief Check if this logger has an appender of the given type.
///
/// @param destination the appender type to be checked: console, file or syslog
///
/// @return true if an appender of the given type is found, false otherwise
bool hasAppender(OutputOption::Destination const destination);
/// \brief Equality
///
/// Check if two instances of this logger refer to the same stream.

View File

@@ -20,9 +20,12 @@
#include <boost/static_assert.hpp>
#include <boost/algorithm/string.hpp>
#include <log4cplus/version.h>
#include <log4cplus/configurator.h>
#include <log4cplus/consoleappender.h>
#include <log4cplus/fileappender.h>
#include <log4cplus/loggingmacros.h>
#include <log4cplus/syslogappender.h>
#include <log4cplus/version.h>
#include <log/logger.h>
#include <log/logger_impl.h>
@@ -198,5 +201,31 @@ LoggerImpl::outputRaw(const Severity& severity, const string& message) {
}
}
bool
LoggerImpl::hasAppender(OutputOption::Destination const destination) {
// Get the appender for the name under which this logger is registered.
log4cplus::SharedAppenderPtrList appenders(
log4cplus::Logger::getInstance(name_).getAllAppenders());
// If there are no appenders, they might be under the root name.
if (appenders.size() == 0) {
appenders = log4cplus::Logger::getInstance(getRootLoggerName()).getAllAppenders();
}
for (log4cplus::helpers::SharedObjectPtr<log4cplus::Appender> logger : appenders) {
if (destination == OutputOption::DEST_CONSOLE &&
dynamic_cast<log4cplus::ConsoleAppender*>(logger.get())) {
return true;
} else if (destination == OutputOption::DEST_FILE &&
dynamic_cast<log4cplus::FileAppender*>(logger.get())) {
return true;
} else if (destination == OutputOption::DEST_SYSLOG &&
dynamic_cast<log4cplus::SysLogAppender*>(logger.get())) {
return true;
}
}
return false;
}
} // namespace log
} // namespace isc

View File

@@ -26,6 +26,7 @@
#include <log/logger_level_impl.h>
#include <log/message_types.h>
#include <log/interprocess/interprocess_sync.h>
#include <log/output_option.h>
namespace isc {
namespace log {
@@ -177,6 +178,13 @@ public:
/// If NULL is passed, a BadInterprocessSync exception is thrown.
void setInterprocessSync(isc::log::interprocess::InterprocessSync* sync);
/// @brief Check if this logger has an appender of the given type.
///
/// @param destination the appender type to be checked: console, file or syslog
///
/// @return true if an appender of the given type is found, false otherwise
bool hasAppender(OutputOption::Destination const destination);
/// \brief Equality
///
/// Check if two instances of this logger refer to the same stream.