From: Guilherme Maciel Ferreira Date: Sun, 19 Feb 2012 22:35:16 +0000 (-0200) Subject: Bring aboard log level configuration. X-Git-Tag: v1.3~11^2~2 X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=2c40f49333916e2eb60c6309e5338e034a008348;p=pingcheck Bring aboard log level configuration. --- diff --git a/src/config/configuration.cpp b/src/config/configuration.cpp index e1844b0..7c63485 100644 --- a/src/config/configuration.cpp +++ b/src/config/configuration.cpp @@ -29,6 +29,7 @@ using namespace std; Configuration::Configuration() : Daemon( false ), + LoggingLevel( LogLevel_ERROR ), ConfigFileName( "" ), SourceNetworkInterface( "" ), NameServer( "" ), @@ -61,6 +62,18 @@ void Configuration::set_daemon( bool daemon ) Daemon = daemon; } +LogLevel Configuration::get_log_level() const +{ + return LoggingLevel; +} + +void Configuration::set_log_level( const LogLevel &log_level ) +{ + BOOST_ASSERT( (LogLevel_First <= log_level) && (log_level <= LogLevel_Last) ); + + this->LoggingLevel = log_level; +} + string Configuration::get_config_file_name() const { return ConfigFileName; diff --git a/src/config/configuration.h b/src/config/configuration.h index 1c2fd34..061dff9 100644 --- a/src/config/configuration.h +++ b/src/config/configuration.h @@ -28,6 +28,7 @@ on this file might be covered by the GNU General Public License. #include #include "config/host.h" +#include "host/loglevel.h" //----------------------------------------------------------------------------- // Configuration @@ -48,6 +49,9 @@ public: std::string get_config_file_name() const; void set_config_file_name( const std::string &config_file_name ); + LogLevel get_log_level() const; + void set_log_level( const LogLevel &log_level ); + std::string get_nameserver() const; void set_nameserver( const std::string &nameserver ); @@ -76,6 +80,7 @@ public: private: bool Daemon; + LogLevel LoggingLevel; std::string ConfigFileName; std::string SourceNetworkInterface; std::string NameServer; diff --git a/src/config/configurationoptions.cpp b/src/config/configurationoptions.cpp index f392ff1..09d455c 100644 --- a/src/config/configurationoptions.cpp +++ b/src/config/configurationoptions.cpp @@ -36,6 +36,7 @@ #include "config/option/hostpingintervaloption.h" #include "config/option/linkdownintervaloption.h" #include "config/option/linkupintervaloption.h" +#include "config/option/logleveloption.h" #include "config/option/nameserveroption.h" #include "config/option/pingfaillimitoption.h" #include "config/option/sourcenetworkinterfaceoption.h" @@ -73,6 +74,9 @@ ConfigurationOptions::ConfigurationOptions() : ConfigurationOptionItem version( new VersionOption ); GenericOptions.push_back( version ); + ConfigurationOptionItem log_level( new LogLevelOption ); + GenericOptions.push_back( log_level ); + ConfigurationOptionItem hosts_down_limit( new HostsDownLimitOption ); ConfigOptions.push_back( hosts_down_limit ); diff --git a/src/config/option/logleveloption.cpp b/src/config/option/logleveloption.cpp new file mode 100644 index 0000000..935cbe3 --- /dev/null +++ b/src/config/option/logleveloption.cpp @@ -0,0 +1,74 @@ +/* + The software in this package is distributed under the GNU General + Public License version 2 (with a special exception described below). + + A copy of GNU General Public License (GPL) is included in this distribution, + in the file COPYING.GPL. + + As a special exception, if other files instantiate templates or use macros + or inline functions from this file, or you compile this file and link it + with other works to produce a work based on this file, this file + does not by itself cause the resulting work to be covered + by the GNU General Public License. + + However the source code for this file must still be made available + in accordance with section (3) of the GNU General Public License. + + This exception does not invalidate any other reasons why a work based + on this file might be covered by the GNU General Public License. + */ + +#include "config/option/logleveloption.h" + +#include + +#include + +#include "host/loglevel.h" + +using namespace std; +using boost::program_options::value; +using boost::program_options::variables_map; +using I2n::Logger::GlobalLogger; + +//----------------------------------------------------------------------------- +// LogLevelOption +//----------------------------------------------------------------------------- + +/** + * @brief Default constructor. + */ +LogLevelOption::LogLevelOption() : + ConfigurationOption( + "log-level", + value()->default_value( "ERROR" ), + "Level of the log messages." + ) +{ +} + +/** + * @brief Destructor. + */ +LogLevelOption::~LogLevelOption() +{ +} + +bool LogLevelOption::parse( + const variables_map& vm, + Configuration *configuration +) +{ + // log-level + if ( 1 <= vm.count( get_command_string() ) ) + { + string log_level_string = vm[ get_command_string() ].as (); + LogLevel log_level = get_log_level_from_string( log_level_string ); + configuration->set_log_level( log_level ); + + GlobalLogger.info() << get_command_string() << "=" << log_level_string << endl; + return true; + } + + return false; +} diff --git a/src/config/option/logleveloption.h b/src/config/option/logleveloption.h new file mode 100644 index 0000000..1463520 --- /dev/null +++ b/src/config/option/logleveloption.h @@ -0,0 +1,48 @@ +/* + The software in this package is distributed under the GNU General + Public License version 2 (with a special exception described below). + + A copy of GNU General Public License (GPL) is included in this distribution, + in the file COPYING.GPL. + + As a special exception, if other files instantiate templates or use macros + or inline functions from this file, or you compile this file and link it + with other works to produce a work based on this file, this file + does not by itself cause the resulting work to be covered + by the GNU General Public License. + + However the source code for this file must still be made available + in accordance with section (3) of the GNU General Public License. + + This exception does not invalidate any other reasons why a work based + on this file might be covered by the GNU General Public License. + */ + +#ifndef LOG_LEVEL_OPTION_H +#define LOG_LEVEL_OPTION_H + +#include + +#include "config/option/configurationoption.h" + +//----------------------------------------------------------------------------- +// LogLevelOption +//----------------------------------------------------------------------------- + +/** + * @brief The log level option. + */ +class LogLevelOption : public ConfigurationOption +{ +public: + LogLevelOption(); + virtual ~LogLevelOption(); + + virtual bool parse( + const boost::program_options::variables_map &vm, + Configuration *configuration + ); + +}; + +#endif // LOG_LEVEL_OPTION_H diff --git a/test/CMakeLists.test_configurationcommandline.txt b/test/CMakeLists.test_configurationcommandline.txt index 9bf66b0..8b8e80c 100644 --- a/test/CMakeLists.test_configurationcommandline.txt +++ b/test/CMakeLists.test_configurationcommandline.txt @@ -17,11 +17,13 @@ add_executable(test_configurationcommandline ${CMAKE_SOURCE_DIR}/src/config/option/hostsdownlimitoption.cpp ${CMAKE_SOURCE_DIR}/src/config/option/linkdownintervaloption.cpp ${CMAKE_SOURCE_DIR}/src/config/option/linkupintervaloption.cpp + ${CMAKE_SOURCE_DIR}/src/config/option/logleveloption.cpp ${CMAKE_SOURCE_DIR}/src/config/option/nameserveroption.cpp ${CMAKE_SOURCE_DIR}/src/config/option/pingfaillimitoption.cpp ${CMAKE_SOURCE_DIR}/src/config/option/sourcenetworkinterfaceoption.cpp ${CMAKE_SOURCE_DIR}/src/config/option/statusnotifiercmdoption.cpp ${CMAKE_SOURCE_DIR}/src/config/option/versionoption.cpp + ${CMAKE_SOURCE_DIR}/src/host/loglevel.cpp ${CMAKE_SOURCE_DIR}/src/host/pingprotocol.cpp ) diff --git a/test/CMakeLists.test_configurationfile.txt b/test/CMakeLists.test_configurationfile.txt index 1c7e19b..c83ffa7 100644 --- a/test/CMakeLists.test_configurationfile.txt +++ b/test/CMakeLists.test_configurationfile.txt @@ -17,11 +17,13 @@ add_executable(test_configurationfile ${CMAKE_SOURCE_DIR}/src/config/option/hostsdownlimitoption.cpp ${CMAKE_SOURCE_DIR}/src/config/option/linkdownintervaloption.cpp ${CMAKE_SOURCE_DIR}/src/config/option/linkupintervaloption.cpp + ${CMAKE_SOURCE_DIR}/src/config/option/logleveloption.cpp ${CMAKE_SOURCE_DIR}/src/config/option/nameserveroption.cpp ${CMAKE_SOURCE_DIR}/src/config/option/pingfaillimitoption.cpp ${CMAKE_SOURCE_DIR}/src/config/option/sourcenetworkinterfaceoption.cpp ${CMAKE_SOURCE_DIR}/src/config/option/statusnotifiercmdoption.cpp ${CMAKE_SOURCE_DIR}/src/config/option/versionoption.cpp + ${CMAKE_SOURCE_DIR}/src/host/loglevel.cpp ${CMAKE_SOURCE_DIR}/src/host/pingprotocol.cpp ) diff --git a/test/CMakeLists.test_configurationoptions.txt b/test/CMakeLists.test_configurationoptions.txt index 86b7cfa..0bd9de1 100644 --- a/test/CMakeLists.test_configurationoptions.txt +++ b/test/CMakeLists.test_configurationoptions.txt @@ -15,11 +15,13 @@ add_executable(test_configurationoptions ${CMAKE_SOURCE_DIR}/src/config/option/hostsdownlimitoption.cpp ${CMAKE_SOURCE_DIR}/src/config/option/linkdownintervaloption.cpp ${CMAKE_SOURCE_DIR}/src/config/option/linkupintervaloption.cpp + ${CMAKE_SOURCE_DIR}/src/config/option/logleveloption.cpp ${CMAKE_SOURCE_DIR}/src/config/option/nameserveroption.cpp ${CMAKE_SOURCE_DIR}/src/config/option/pingfaillimitoption.cpp ${CMAKE_SOURCE_DIR}/src/config/option/sourcenetworkinterfaceoption.cpp ${CMAKE_SOURCE_DIR}/src/config/option/statusnotifiercmdoption.cpp ${CMAKE_SOURCE_DIR}/src/config/option/versionoption.cpp + ${CMAKE_SOURCE_DIR}/src/host/loglevel.cpp ${CMAKE_SOURCE_DIR}/src/host/pingprotocol.cpp ) diff --git a/test/test_configurationoptions.cpp b/test/test_configurationoptions.cpp index f4343b0..e770429 100644 --- a/test/test_configurationoptions.cpp +++ b/test/test_configurationoptions.cpp @@ -27,6 +27,7 @@ on this file might be covered by the GNU General Public License. #include #include +#include "host/loglevel.h" #include "host/pingprotocol.h" #include "config/configurationoptions.h" @@ -102,12 +103,13 @@ BOOST_AUTO_TEST_CASE( get_generic_options ) // if this assert fails, you must add or remove one of the options in the // test bellow - BOOST_CHECK_EQUAL( options.size(), 4 ); // help, version, daemon and config-file + BOOST_CHECK_EQUAL( options.size(), 5 ); // help, version, daemon, config-file and log-level BOOST_CHECK_EQUAL( option_present( options, "help" ), true ); BOOST_CHECK_EQUAL( option_present( options, "config-file" ), true ); BOOST_CHECK_EQUAL( option_present( options, "daemon" ), true ); BOOST_CHECK_EQUAL( option_present( options, "version" ), true ); + BOOST_CHECK_EQUAL( option_present( options, "log-level" ), true ); } BOOST_AUTO_TEST_CASE( get_configuration_options ) @@ -138,6 +140,7 @@ BOOST_AUTO_TEST_CASE( parse_generic_options ) option_insert( "config-file", boost::any( std::string("pingcheck.conf") ), vm ); option_insert( "daemon", boost::any(), vm ); + option_insert( "log-level", boost::any( std::string("EMERGENCY") ), vm ); ConfigurationOptions config_options; Configuration configuration; @@ -145,6 +148,7 @@ BOOST_AUTO_TEST_CASE( parse_generic_options ) BOOST_CHECK_EQUAL( configuration.get_config_file_name(), "pingcheck.conf" ); BOOST_CHECK_EQUAL( configuration.get_daemon(), true ); + BOOST_CHECK_EQUAL( configuration.get_log_level(), LogLevel_EMERGENCY ); } BOOST_AUTO_TEST_CASE( parse_configuration_options ) @@ -253,6 +257,9 @@ BOOST_AUTO_TEST_CASE( halt_on_generic_options ) option_clear_and_insert( "version", value, vm ); BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), true ); + option_clear_and_insert( "log-level", value, vm ); + BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false ); + option_clear_and_insert( "hosts-down-limit", value, vm ); BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false );