added option log-file and option FILE to log-output; use in main and adjusted unit...
authorChristian Herdtweck <christian.herdtweck@intra2net.com>
Thu, 30 Apr 2015 14:13:08 +0000 (16:13 +0200)
committerChristian Herdtweck <christian.herdtweck@intra2net.com>
Mon, 4 May 2015 14:57:59 +0000 (16:57 +0200)
14 files changed:
src/CMakeLists.txt
src/config/configuration.cpp
src/config/configuration.h
src/config/configurationoptions.cpp
src/config/option/logfileoption.cpp [new file with mode: 0644]
src/config/option/logfileoption.h [new file with mode: 0644]
src/host/logoutput.cpp
src/host/logoutput.h
src/main.cpp
test/CMakeLists.test_configurationcommandline.txt
test/CMakeLists.test_configurationfile.txt
test/CMakeLists.test_configurationoptions.txt
test/test_configurationoptions.cpp
test/test_logoutput.cpp

index 992c537..68ab5db 100644 (file)
@@ -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
index 4efd663..48971fd 100644 (file)
@@ -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;
index 1bb4895..ca0c5a7 100644 (file)
@@ -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;
index 5010921..3c975eb 100644 (file)
@@ -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 (file)
index 0000000..1283c48
--- /dev/null
@@ -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 <iostream>
+
+#include <logfunc.hpp>
+
+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<string>()->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<string> ();
+        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 (file)
index 0000000..195e73b
--- /dev/null
@@ -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 <boost/program_options.hpp>
+
+#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
index 8104d0e..a26392e 100644 (file)
@@ -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 ];
 
index 991e421..4fe2209 100644 (file)
@@ -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 );
index 972c0f7..ecfdeae 100644 (file)
@@ -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;
index 0425095..589996c 100644 (file)
@@ -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
index da1dcad..a3fa1f7 100644 (file)
@@ -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
index 65ac76c..b96956c 100644 (file)
@@ -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
index 5551f6b..86463bc 100644 (file)
@@ -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<int>(configuration.get_log_level()), static_cast<int>(I2n::Logger::LogLevel::Emergency) );
     BOOST_CHECK_EQUAL( static_cast<int>(configuration.get_log_output()), static_cast<int>(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 );
 
index 6f90f04..ddedbd1 100644 (file)
@@ -34,6 +34,7 @@ BOOST_AUTO_TEST_CASE( lowercase )
     BOOST_CHECK_EQUAL( static_cast<int>(get_log_output_from_string( "syslog" )), static_cast<int>(LogOutput_SYSLOG) );
     BOOST_CHECK_EQUAL( static_cast<int>(get_log_output_from_string( "terminal" )), static_cast<int>(LogOutput_TERMINAL) );
     BOOST_CHECK_EQUAL( static_cast<int>(get_log_output_from_string( "console" )), static_cast<int>(LogOutput_TERMINAL) );
+    BOOST_CHECK_EQUAL( static_cast<int>(get_log_output_from_string( "file" )), static_cast<int>(LogOutput_FILE) );
 }
 
 BOOST_AUTO_TEST_CASE( uppercase )
@@ -41,6 +42,7 @@ BOOST_AUTO_TEST_CASE( uppercase )
     BOOST_CHECK_EQUAL( static_cast<int>(get_log_output_from_string( "SYSLOG" )), static_cast<int>(LogOutput_SYSLOG) );
     BOOST_CHECK_EQUAL( static_cast<int>(get_log_output_from_string( "TERMINAL" )), static_cast<int>(LogOutput_TERMINAL) );
     BOOST_CHECK_EQUAL( static_cast<int>(get_log_output_from_string( "CONSOLE" )), static_cast<int>(LogOutput_TERMINAL) );
+    BOOST_CHECK_EQUAL( static_cast<int>(get_log_output_from_string( "FILE" )), static_cast<int>(LogOutput_FILE) );
 }
 
 BOOST_AUTO_TEST_CASE( mixed_case )
@@ -48,6 +50,7 @@ BOOST_AUTO_TEST_CASE( mixed_case )
     BOOST_CHECK_EQUAL( static_cast<int>(get_log_output_from_string( "SysLoG" )), static_cast<int>(LogOutput_SYSLOG) );
     BOOST_CHECK_EQUAL( static_cast<int>(get_log_output_from_string( "teRmINAl" )), static_cast<int>(LogOutput_TERMINAL) );
     BOOST_CHECK_EQUAL( static_cast<int>(get_log_output_from_string( "ConSOlE" )), static_cast<int>(LogOutput_TERMINAL) );
+    BOOST_CHECK_EQUAL( static_cast<int>(get_log_output_from_string( "File" )), static_cast<int>(LogOutput_FILE) );
 }
 
 BOOST_AUTO_TEST_CASE( misspelled )