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
Daemon( false ),
LoggingLevel( LogLevel::Error ),
LoggingOutput( LogOutput_SYSLOG ),
+ LogFileName( "" ),
ConfigFileName( "" ),
SourceNetworkInterface( "" ),
NameServer( "" ),
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;
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 );
bool Daemon;
I2n::Logger::LogLevel LoggingLevel;
LogOutput LoggingOutput;
+ std::string LogFileName;
std::string ConfigFileName;
std::string SourceNetworkInterface;
std::string NameServer;
#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"
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 );
--- /dev/null
+/*
+ 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;
+}
--- /dev/null
+/*
+ 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
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 ];
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 );
)
{
LogOutput log_output = configuration->get_log_output();
+ string log_file_name = configuration->get_log_file();
switch (log_output)
{
case LogOutput_SYSLOG:
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;
${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
${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
${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
// 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 );
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 )
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;
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 )
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 );
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 )
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 )
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 )