From fda777ea902cf521ad9139d4bde76896ed366050 Mon Sep 17 00:00:00 2001 From: Christian Herdtweck Date: Thu, 30 Apr 2015 16:13:08 +0200 Subject: [PATCH] added option log-file and option FILE to log-output; use in main and adjusted unit tests --- src/CMakeLists.txt | 1 + src/config/configuration.cpp | 13 ++++ src/config/configuration.h | 4 + src/config/configurationoptions.cpp | 4 + src/config/option/logfileoption.cpp | 71 +++++++++++++++++++++ src/config/option/logfileoption.h | 48 ++++++++++++++ src/host/logoutput.cpp | 1 + src/host/logoutput.h | 3 +- src/main.cpp | 11 +++ test/CMakeLists.test_configurationcommandline.txt | 1 + test/CMakeLists.test_configurationfile.txt | 1 + test/CMakeLists.test_configurationoptions.txt | 1 + test/test_configurationoptions.cpp | 9 +++- test/test_logoutput.cpp | 3 + 14 files changed, 169 insertions(+), 2 deletions(-) create mode 100644 src/config/option/logfileoption.cpp create mode 100644 src/config/option/logfileoption.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 992c537..68ab5db 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -49,6 +49,7 @@ set(SOURCES config/option/linkupintervaloption.cpp config/option/logleveloption.cpp config/option/logoutputoption.cpp + config/option/logfileoption.cpp config/option/nameserveroption.cpp config/option/pingfaillimitoption.cpp config/option/sourcenetworkinterfaceoption.cpp diff --git a/src/config/configuration.cpp b/src/config/configuration.cpp index 4efd663..48971fd 100644 --- a/src/config/configuration.cpp +++ b/src/config/configuration.cpp @@ -44,6 +44,7 @@ Configuration::Configuration() : Daemon( false ), LoggingLevel( LogLevel::Error ), LoggingOutput( LogOutput_SYSLOG ), + LogFileName( "" ), ConfigFileName( "" ), SourceNetworkInterface( "" ), NameServer( "" ), @@ -107,6 +108,18 @@ void Configuration::set_log_output( const LogOutput &log_output ) this->LoggingOutput = log_output; } +string Configuration::get_log_file() const +{ + return LogFileName; +} + +void Configuration::set_log_file( const std::string &log_file ) +{ + BOOST_ASSERT( !log_file.empty() ); + + this->LogFileName = log_file; +} + string Configuration::get_config_file_name() const { return ConfigFileName; diff --git a/src/config/configuration.h b/src/config/configuration.h index 1bb4895..ca0c5a7 100644 --- a/src/config/configuration.h +++ b/src/config/configuration.h @@ -61,6 +61,9 @@ public: LogOutput get_log_output() const; void set_log_output( const LogOutput &log_output ); + std::string get_log_file() const; + void set_log_file( const std::string &log_file ); + std::string get_nameserver() const; void set_nameserver( const std::string &nameserver ); @@ -115,6 +118,7 @@ private: bool Daemon; I2n::Logger::LogLevel LoggingLevel; LogOutput LoggingOutput; + std::string LogFileName; std::string ConfigFileName; std::string SourceNetworkInterface; std::string NameServer; diff --git a/src/config/configurationoptions.cpp b/src/config/configurationoptions.cpp index 5010921..3c975eb 100644 --- a/src/config/configurationoptions.cpp +++ b/src/config/configurationoptions.cpp @@ -39,6 +39,7 @@ #include "config/option/linkupintervaloption.h" #include "config/option/logleveloption.h" #include "config/option/logoutputoption.h" +#include "config/option/logfileoption.h" #include "config/option/nameserveroption.h" #include "config/option/pingfaillimitoption.h" #include "config/option/sourcenetworkinterfaceoption.h" @@ -87,6 +88,9 @@ ConfigurationOptions::ConfigurationOptions() : ConfigurationOptionItem log_output( new LogOutputOption ); GenericOptions.push_back( log_output ); + ConfigurationOptionItem log_file( new LogFileOption ); + GenericOptions.push_back( log_file ); + ConfigurationOptionItem hosts_down_limit( new HostsDownLimitOption ); ConfigOptions.push_back( hosts_down_limit ); diff --git a/src/config/option/logfileoption.cpp b/src/config/option/logfileoption.cpp new file mode 100644 index 0000000..1283c48 --- /dev/null +++ b/src/config/option/logfileoption.cpp @@ -0,0 +1,71 @@ +/* + 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/logfileoption.h" + +#include + +#include + +using namespace std; +using boost::program_options::value; +using boost::program_options::variables_map; +using I2n::Logger::GlobalLogger; + +//----------------------------------------------------------------------------- +// LogFileOption +//----------------------------------------------------------------------------- + +/** + * @brief Default constructor. + */ +LogFileOption::LogFileOption() : + ConfigurationOption( + "log-file", + value()->default_value( "pingcheck.log" ), + "File to log messages to if option log-output = FILE." + ) +{ +} + +/** + * @brief Destructor. + */ +LogFileOption::~LogFileOption() +{ +} + +bool LogFileOption::parse( + const variables_map& vm, + Configuration *configuration +) +{ + // log-file + if ( 1 <= vm.count( get_command_string() ) ) + { + string log_file = vm[ get_command_string() ].as (); + configuration->set_log_file( log_file ); + + GlobalLogger.info() << get_command_string() << "=" << log_file << endl; + return true; + } + + return false; +} diff --git a/src/config/option/logfileoption.h b/src/config/option/logfileoption.h new file mode 100644 index 0000000..195e73b --- /dev/null +++ b/src/config/option/logfileoption.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_FILE_OPTION_H +#define LOG_FILE_OPTION_H + +#include + +#include "config/option/configurationoption.h" + +//----------------------------------------------------------------------------- +// LogFileOption +//----------------------------------------------------------------------------- + +/** + * @brief The log file option. + */ +class LogFileOption : public ConfigurationOption +{ +public: + LogFileOption(); + virtual ~LogFileOption(); + + virtual bool parse( + const boost::program_options::variables_map &vm, + Configuration *configuration + ); + +}; + +#endif // LOG_FILE_OPTION_H diff --git a/src/host/logoutput.cpp b/src/host/logoutput.cpp index 8104d0e..a26392e 100644 --- a/src/host/logoutput.cpp +++ b/src/host/logoutput.cpp @@ -54,6 +54,7 @@ LogOutput get_log_output_from_string( const string &log_output_string ) log_output_string_map[ "SYSLOG" ] = LogOutput_SYSLOG; log_output_string_map[ "TERMINAL" ] = LogOutput_TERMINAL; log_output_string_map[ "CONSOLE" ] = LogOutput_TERMINAL; + log_output_string_map[ "FILE" ] = LogOutput_FILE; LogOutput protocol = log_output_string_map[ log_output_uppercase_string ]; diff --git a/src/host/logoutput.h b/src/host/logoutput.h index 991e421..4fe2209 100644 --- a/src/host/logoutput.h +++ b/src/host/logoutput.h @@ -32,7 +32,8 @@ enum LogOutput LogOutput_First = 0, LogOutput_SYSLOG = LogOutput_First, LogOutput_TERMINAL, - LogOutput_Last = LogOutput_TERMINAL + LogOutput_FILE, + LogOutput_Last = LogOutput_FILE }; LogOutput get_log_output_from_string( const std::string &output_string ); diff --git a/src/main.cpp b/src/main.cpp index 972c0f7..ecfdeae 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -163,6 +163,7 @@ void set_log_output( ) { LogOutput log_output = configuration->get_log_output(); + string log_file_name = configuration->get_log_file(); switch (log_output) { case LogOutput_SYSLOG: @@ -180,6 +181,16 @@ void set_log_output( GlobalLogger.info() << "Set log output target to terminal" << endl; GlobalLogger.info() << "(check syslog for earlier messages)" << endl; break; + case LogOutput_FILE: + GlobalLogger.info() << "Setting log output target to file " + << log_file_name << endl; + I2n::Logger::enable_syslog(false); + I2n::Logger::enable_stderr_log(false); + I2n::Logger::enable_log_file(log_file_name); + GlobalLogger.info() << "Set log output target to file " + << log_file_name << endl; + GlobalLogger.info() << "(check syslog for earlier messages)" << endl; + break; default: GlobalLogger.error() << "Unknown log output target!" << endl; break; diff --git a/test/CMakeLists.test_configurationcommandline.txt b/test/CMakeLists.test_configurationcommandline.txt index 0425095..589996c 100644 --- a/test/CMakeLists.test_configurationcommandline.txt +++ b/test/CMakeLists.test_configurationcommandline.txt @@ -21,6 +21,7 @@ add_executable(test_configurationcommandline ${CMAKE_SOURCE_DIR}/src/config/option/linkupintervaloption.cpp ${CMAKE_SOURCE_DIR}/src/config/option/logleveloption.cpp ${CMAKE_SOURCE_DIR}/src/config/option/logoutputoption.cpp + ${CMAKE_SOURCE_DIR}/src/config/option/logfileoption.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 diff --git a/test/CMakeLists.test_configurationfile.txt b/test/CMakeLists.test_configurationfile.txt index da1dcad..a3fa1f7 100644 --- a/test/CMakeLists.test_configurationfile.txt +++ b/test/CMakeLists.test_configurationfile.txt @@ -21,6 +21,7 @@ add_executable(test_configurationfile ${CMAKE_SOURCE_DIR}/src/config/option/linkupintervaloption.cpp ${CMAKE_SOURCE_DIR}/src/config/option/logleveloption.cpp ${CMAKE_SOURCE_DIR}/src/config/option/logoutputoption.cpp + ${CMAKE_SOURCE_DIR}/src/config/option/logfileoption.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 diff --git a/test/CMakeLists.test_configurationoptions.txt b/test/CMakeLists.test_configurationoptions.txt index 65ac76c..b96956c 100644 --- a/test/CMakeLists.test_configurationoptions.txt +++ b/test/CMakeLists.test_configurationoptions.txt @@ -19,6 +19,7 @@ add_executable(test_configurationoptions ${CMAKE_SOURCE_DIR}/src/config/option/linkupintervaloption.cpp ${CMAKE_SOURCE_DIR}/src/config/option/logleveloption.cpp ${CMAKE_SOURCE_DIR}/src/config/option/logoutputoption.cpp + ${CMAKE_SOURCE_DIR}/src/config/option/logfileoption.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 diff --git a/test/test_configurationoptions.cpp b/test/test_configurationoptions.cpp index 5551f6b..86463bc 100644 --- a/test/test_configurationoptions.cpp +++ b/test/test_configurationoptions.cpp @@ -104,7 +104,8 @@ 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(), 6 ); // help, version, daemon, config-file, log-level and log-output + BOOST_CHECK_EQUAL( options.size(), 7 ); + // help, version, daemon, config-file, log-level, log-output, log-file BOOST_CHECK_EQUAL( option_present( options, "help" ), true ); BOOST_CHECK_EQUAL( option_present( options, "config-file" ), true ); @@ -112,6 +113,7 @@ BOOST_AUTO_TEST_CASE( get_generic_options ) BOOST_CHECK_EQUAL( option_present( options, "version" ), true ); BOOST_CHECK_EQUAL( option_present( options, "log-level" ), true ); BOOST_CHECK_EQUAL( option_present( options, "log-output" ), true ); + BOOST_CHECK_EQUAL( option_present( options, "log-file" ), true ); } BOOST_AUTO_TEST_CASE( get_configuration_options ) @@ -151,6 +153,7 @@ BOOST_AUTO_TEST_CASE( parse_generic_options ) option_insert( "daemon", boost::any(), vm ); option_insert( "log-level", boost::any( std::string("EMERGENCY") ), vm ); option_insert( "log-output", boost::any( std::string("TERMINAL") ), vm ); + option_insert( "log-file", boost::any( std::string("pingcheck_test.log") ), vm ); ConfigurationOptions config_options; Configuration configuration; @@ -160,6 +163,7 @@ BOOST_AUTO_TEST_CASE( parse_generic_options ) BOOST_CHECK_EQUAL( configuration.get_daemon(), true ); BOOST_CHECK_EQUAL( static_cast(configuration.get_log_level()), static_cast(I2n::Logger::LogLevel::Emergency) ); BOOST_CHECK_EQUAL( static_cast(configuration.get_log_output()), static_cast(LogOutput_TERMINAL) ); + BOOST_CHECK_EQUAL( configuration.get_log_file(), "pingcheck_test.log" ); } BOOST_AUTO_TEST_CASE( parse_configuration_options ) @@ -314,6 +318,9 @@ BOOST_AUTO_TEST_CASE( halt_on_generic_options ) option_clear_and_insert( "log-output", value, vm ); BOOST_CHECK_EQUAL( config_options.halt_on_generic_options( vm ), false ); + option_clear_and_insert( "log-file", 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 ); diff --git a/test/test_logoutput.cpp b/test/test_logoutput.cpp index 6f90f04..ddedbd1 100644 --- a/test/test_logoutput.cpp +++ b/test/test_logoutput.cpp @@ -34,6 +34,7 @@ BOOST_AUTO_TEST_CASE( lowercase ) BOOST_CHECK_EQUAL( static_cast(get_log_output_from_string( "syslog" )), static_cast(LogOutput_SYSLOG) ); BOOST_CHECK_EQUAL( static_cast(get_log_output_from_string( "terminal" )), static_cast(LogOutput_TERMINAL) ); BOOST_CHECK_EQUAL( static_cast(get_log_output_from_string( "console" )), static_cast(LogOutput_TERMINAL) ); + BOOST_CHECK_EQUAL( static_cast(get_log_output_from_string( "file" )), static_cast(LogOutput_FILE) ); } BOOST_AUTO_TEST_CASE( uppercase ) @@ -41,6 +42,7 @@ BOOST_AUTO_TEST_CASE( uppercase ) BOOST_CHECK_EQUAL( static_cast(get_log_output_from_string( "SYSLOG" )), static_cast(LogOutput_SYSLOG) ); BOOST_CHECK_EQUAL( static_cast(get_log_output_from_string( "TERMINAL" )), static_cast(LogOutput_TERMINAL) ); BOOST_CHECK_EQUAL( static_cast(get_log_output_from_string( "CONSOLE" )), static_cast(LogOutput_TERMINAL) ); + BOOST_CHECK_EQUAL( static_cast(get_log_output_from_string( "FILE" )), static_cast(LogOutput_FILE) ); } BOOST_AUTO_TEST_CASE( mixed_case ) @@ -48,6 +50,7 @@ BOOST_AUTO_TEST_CASE( mixed_case ) BOOST_CHECK_EQUAL( static_cast(get_log_output_from_string( "SysLoG" )), static_cast(LogOutput_SYSLOG) ); BOOST_CHECK_EQUAL( static_cast(get_log_output_from_string( "teRmINAl" )), static_cast(LogOutput_TERMINAL) ); BOOST_CHECK_EQUAL( static_cast(get_log_output_from_string( "ConSOlE" )), static_cast(LogOutput_TERMINAL) ); + BOOST_CHECK_EQUAL( static_cast(get_log_output_from_string( "File" )), static_cast(LogOutput_FILE) ); } BOOST_AUTO_TEST_CASE( misspelled ) -- 1.7.1