From: Guilherme Maciel Ferreira Date: Thu, 28 Apr 2011 12:29:04 +0000 (+0200) Subject: Log messages via I2n::Logger instead of cout/cerr. X-Git-Tag: v1.0~63 X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=301610ca7658b61e6941a5c4aa996982f4fdecb4;p=pingcheck Log messages via I2n::Logger instead of cout/cerr. --- diff --git a/src/config/configurationreader.cpp b/src/config/configurationreader.cpp index b767709..5dffe35 100644 --- a/src/config/configurationreader.cpp +++ b/src/config/configurationreader.cpp @@ -6,6 +6,8 @@ #include #include +#include + using namespace std; using boost::program_options::command_line_parser; using boost::program_options::options_description; @@ -13,6 +15,7 @@ using boost::program_options::parsed_options; using boost::program_options::positional_options_description; using boost::program_options::value; using boost::program_options::variables_map; +using I2n::Logger::GlobalLogger; //----------------------------------------------------------------------------- // ConfigurationReader @@ -145,7 +148,8 @@ bool ConfigurationReader::parse_generic_options( // daemon bool have_daemon = ( vm.count( DaemonCmdStr ) > 0 ); Config.set_daemon( have_daemon ); - cout << DaemonCmdStr << "=" << have_daemon << endl; + GlobalLogger.info() << DaemonCmdStr << "=" + << have_daemon << endl; // config-file if ( vm.count( ConfigFileCmdStr ) > 0 ) @@ -153,7 +157,8 @@ bool ConfigurationReader::parse_generic_options( string config_file_name = vm[ ConfigFileCmdStr ].as (); Config.set_config_file_name( config_file_name ); - cout << ConfigFileCmdStr << "=" << config_file_name << endl; + GlobalLogger.info() << ConfigFileCmdStr << "=" + << config_file_name << endl; } return false; @@ -185,8 +190,8 @@ bool ConfigurationReader::parse_configuration_options( const variables_map &vm ) vm[ SourceNetworkInterfaceCmdStr ].as (); Config.set_source_network_interface( source_network_interface ); - cout << SourceNetworkInterfaceCmdStr << "=" << source_network_interface - << endl; + GlobalLogger.info() << SourceNetworkInterfaceCmdStr << "=" + << source_network_interface << endl; } // nameserver @@ -195,7 +200,8 @@ bool ConfigurationReader::parse_configuration_options( const variables_map &vm ) string nameserver = vm[ NameServerCmdStr ].as (); Config.set_nameserver( nameserver ); - cout << NameServerCmdStr << "=" << nameserver << endl; + GlobalLogger.info() << NameServerCmdStr << "=" + << nameserver << endl; } // hosts-down-limit @@ -205,7 +211,8 @@ bool ConfigurationReader::parse_configuration_options( const variables_map &vm ) host_down_limit = vm[ HostsDownLimitCmdStr ].as (); Config.set_hosts_down_limit( host_down_limit ); - cout << HostsDownLimitCmdStr << "=" << host_down_limit << endl; + GlobalLogger.info() << HostsDownLimitCmdStr << "=" + << host_down_limit << endl; } // ping-fail-limit @@ -214,7 +221,8 @@ bool ConfigurationReader::parse_configuration_options( const variables_map &vm ) int ping_fail_limit = vm[ PingFailLimitCmdStr ].as (); Config.set_ping_fail_limit( ping_fail_limit ); - cout << PingFailLimitCmdStr << "=" << ping_fail_limit << endl; + GlobalLogger.info() << PingFailLimitCmdStr << "=" + << ping_fail_limit << endl; } // status-notifier-cmd @@ -223,7 +231,8 @@ bool ConfigurationReader::parse_configuration_options( const variables_map &vm ) string status_notifier_cmd = vm[ StatusNotifierCmdCmdStr ].as(); Config.set_status_notifier_cmd( status_notifier_cmd ); - cout << StatusNotifierCmdCmdStr << "=" << status_notifier_cmd << endl; + GlobalLogger.info() << StatusNotifierCmdCmdStr << "=" + << status_notifier_cmd << endl; } // link-up-interval @@ -232,8 +241,8 @@ bool ConfigurationReader::parse_configuration_options( const variables_map &vm ) int link_up_interval_in_min = vm[ LinkUpIntervalCmdStr ].as(); Config.set_link_up_interval_in_min( link_up_interval_in_min ); - cout << LinkUpIntervalCmdStr << "=" << link_up_interval_in_min - << endl; + GlobalLogger.info() << LinkUpIntervalCmdStr << "=" + << link_up_interval_in_min << endl; } // [host] name @@ -248,7 +257,8 @@ bool ConfigurationReader::parse_configuration_options( const variables_map &vm ) HostItem host_item( new Host( host_name ) ); hosts_list.push_back( host_item ); - cout << HostNameCmdStr << "=" << host_name << endl; + GlobalLogger.info() << HostNameCmdStr << "=" + << host_name << endl; } Config.set_hosts( hosts_list ); @@ -272,7 +282,8 @@ bool ConfigurationReader::parse_configuration_options( const variables_map &vm ) host_item->set_interval_in_sec( host_interval_in_sec ); ++hosts_it; - cout << HostIntervalCmdStr << "=" << host_interval_in_sec << endl; + GlobalLogger.info() << HostIntervalCmdStr << "=" + << host_interval_in_sec << endl; } hosts_interval_count = hosts_intervals.size(); @@ -311,7 +322,7 @@ bool ConfigurationReader::process_command_line( } catch ( const std::exception &ex ) { - cerr << ex.what() << endl; + GlobalLogger.error() << ex.what() << endl; return false; } } @@ -336,8 +347,8 @@ bool ConfigurationReader::process_configuration_file( ifstream ifs( config_file_name.c_str() ); if ( !ifs ) { - cerr << "Error: could not open " << config_file_name << " file." - << endl; + GlobalLogger.error() << "Error: could not open " + << config_file_name << " file." << endl; return false; } else diff --git a/src/dns/dnsresolver.cpp b/src/dns/dnsresolver.cpp index 2285b6f..1aba3f2 100644 --- a/src/dns/dnsresolver.cpp +++ b/src/dns/dnsresolver.cpp @@ -7,6 +7,8 @@ #include #include +#include + #include "dns/hostaddress.h" using namespace std; @@ -17,6 +19,7 @@ using boost::net::dns::resource_base_t; using boost::net::dns::message; using boost::net::dns::rr_list_t; using boost::shared_ptr; +using I2n::Logger::GlobalLogger; //----------------------------------------------------------------------------- // DnsResolver @@ -46,7 +49,8 @@ bool DnsResolver::resolve() BOOST_ASSERT( !HostDnsAddress.empty() ); BOOST_ASSERT( !NameServer.empty() ); - cout << "Resolved IP(s) for host : " << HostDnsAddress << endl; + GlobalLogger.info() << "Resolved IP(s) for host : " + << HostDnsAddress << endl; try { @@ -56,8 +60,8 @@ bool DnsResolver::resolve() int resolved_ip_count = answers_list.size(); if ( resolved_ip_count < 1 ) { - cerr << "Error: host " << HostDnsAddress << " could not be resolved." - << endl; + GlobalLogger.error() << "Error: host " + << HostDnsAddress << " could not be resolved." << endl; return false; } @@ -68,8 +72,8 @@ bool DnsResolver::resolve() } catch ( const std::exception &ex ) { - cerr << "Error: host " << HostDnsAddress << " could not be resolved. " - << ex.what() << endl; + GlobalLogger.error() << "Error: host " << HostDnsAddress + << " could not be resolved. " << ex.what() << endl; return false; } @@ -174,7 +178,8 @@ void DnsResolver::fill_resolved_ip_list( HostAddress resolved_host( ip, ttl ); resolved_host_address_list->push_back( resolved_host ); - cout << "- " << ip << " [" << ttl << "s]" << endl; + GlobalLogger.info() << "- " << ip + << " [" << ttl << "s]" << endl; } } diff --git a/src/host/boostpinger.cpp b/src/host/boostpinger.cpp index 9f7306d..6b0ef3e 100644 --- a/src/host/boostpinger.cpp +++ b/src/host/boostpinger.cpp @@ -8,6 +8,8 @@ #include #include +#include + #include "icmp/icmpchecksumcalculator.h" #include "icmp/icmpdata.h" #include "icmp/icmpheader.h" @@ -24,6 +26,7 @@ using boost::date_time::time_resolution_traits_adapted64_impl; using boost::posix_time::microsec_clock; using boost::posix_time::ptime; using boost::posix_time::seconds; +using I2n::Logger::GlobalLogger; //----------------------------------------------------------------------------- // BoostPinger @@ -49,8 +52,8 @@ BoostPinger::BoostPinger( if ( !select_source_network_interface( source_network_interface ) ) { - cerr << "Error: could not bind the socket with the local interface." - << ::strerror( errno ) << endl; + GlobalLogger.error() << "Error: could not bind the socket " + "with the local interface." << ::strerror( errno ) << endl; } } @@ -152,12 +155,14 @@ void BoostPinger::send_echo_request( const IcmpPacket &icmp_packet ) size_t bytes_sent = Socket.send_to( data, DestinationEndpoint ); if ( bytes_sent != buffer_size( data ) ) { - cerr << "Error: fail sending ping data." << endl; + GlobalLogger.error() << "Error: fail sending ping data." + << endl; } } catch ( const exception &ex ) { - cerr << "Error: fail sending ping data. " << ex.what() << endl; + GlobalLogger.error() << "Error: fail sending ping data. " + << ex.what() << endl; } schedule_timeout_echo_reply(); @@ -266,7 +271,7 @@ void BoostPinger::handle_receive_icmp_packet( const size_t &bytes_transferred ) void BoostPinger::print_request_timeout() const { - cout << "Request timed out" << endl; + GlobalLogger.info() << "Request timed out" << endl; } void BoostPinger::print_echo_reply( @@ -287,7 +292,7 @@ void BoostPinger::print_echo_reply( time_resolution_traits_adapted64_impl::int_type elapsed_time = (now - TimeSent).total_milliseconds(); - cout << bytes_received << " bytes " + GlobalLogger.info() << bytes_received << " bytes " << "from " << remote_address << ": icmp_seq=" << sequence_number << " ttl=" << ttl @@ -306,7 +311,7 @@ void BoostPinger::print_destination_unreachable( string local_address = ipv4_hdr.get_destination_address().to_string(); uint16_t sequence_number = icmp_hdr.get_sequence_number(); - cout << "From " << local_address + GlobalLogger.info() << "From " << local_address << " icmp_seq=" << sequence_number << " Destination Net Unreachable" << endl; } diff --git a/src/host/pingscheduler.cpp b/src/host/pingscheduler.cpp index 7620b33..5dca442 100644 --- a/src/host/pingscheduler.cpp +++ b/src/host/pingscheduler.cpp @@ -4,6 +4,8 @@ #include +#include + #include "dns/dnsresolver.h" #include "host/boostpinger.h" #include "link/linkstatusanalyzer.h" @@ -16,6 +18,7 @@ using boost::posix_time::microsec_clock; using boost::posix_time::ptime; using boost::posix_time::seconds; using boost::shared_ptr; +using I2n::Logger::GlobalLogger; //----------------------------------------------------------------------------- // PingScheduler @@ -94,12 +97,12 @@ void PingScheduler::setup_next_ping() // TODO this code is similar to the one at start_pinging(), make it simple! if ( IpList.expired_resolved_ip() ) { - cout << "Updating DNS ... "; // TODO + GlobalLogger.info() << "Updating DNS ... "; bool address_resolved = resolve_ping_address(); if ( !address_resolved ) { - cerr << "Error: could not update host IP, may use outdated address" - << endl; + GlobalLogger.error() << "Error: could not update host " + "IP, may use outdated address" << endl; } } @@ -138,15 +141,15 @@ void PingScheduler::update_ping_interval() { PingIntervalInSec.speed_up(); - cout << "- Speeding up ping interval to: " << PingIntervalInSec - << "s" << endl; // TODO output log + GlobalLogger.info() << "- Speeding up ping interval to: " + << PingIntervalInSec << "s" << endl; } else { PingIntervalInSec.back_to_original(); - cout << "- Stick to the original ping interval: " << PingIntervalInSec - << "s" << endl; // TODO output log + GlobalLogger.info() << "- Stick to the original ping interval: " + << PingIntervalInSec << "s" << endl; } } @@ -155,8 +158,8 @@ void PingScheduler::update_ping_elapsed_time() ptime now = microsec_clock::universal_time(); time_resolution_traits_adapted64_impl::int_type elapsed_time_in_sec = (now - TimeSentLastPing).total_seconds(); - cout << "- Time elapsed since last ping: " << elapsed_time_in_sec << "s" - << endl; // TODO output log + GlobalLogger.info() << "- Time elapsed since last ping: " + << elapsed_time_in_sec << "s" << endl; TimeSentLastPing = microsec_clock::universal_time(); } diff --git a/src/link/linkstatusanalyzer.cpp b/src/link/linkstatusanalyzer.cpp index 8b3ee8c..30e7b34 100644 --- a/src/link/linkstatusanalyzer.cpp +++ b/src/link/linkstatusanalyzer.cpp @@ -4,9 +4,12 @@ #include +#include + using namespace std; using boost::posix_time::microsec_clock; using boost::posix_time::ptime; +using I2n::Logger::GlobalLogger; //----------------------------------------------------------------------------- // LinkStatusAnalyzer @@ -38,7 +41,7 @@ void LinkStatusAnalyzer::notify_host_up( const string &host_address ) { BOOST_ASSERT( !host_address.empty() ); - cout << "- Host up: " << host_address << endl; // TODO + GlobalLogger.info() << "- Host up: " << host_address << endl; add_host_up( host_address ); @@ -55,7 +58,7 @@ void LinkStatusAnalyzer::notify_host_down( const string &host_address ) { BOOST_ASSERT( !host_address.empty() ); - cout << "- Host down: " << host_address << endl; // TODO + GlobalLogger.info() << "- Host down: " << host_address << endl; add_host_down( host_address ); diff --git a/src/link/statusnotifiercommand.cpp b/src/link/statusnotifiercommand.cpp index e1c2bfd..e894ff6 100644 --- a/src/link/statusnotifiercommand.cpp +++ b/src/link/statusnotifiercommand.cpp @@ -6,9 +6,11 @@ #include +#include #include using namespace std; +using I2n::Logger::GlobalLogger; //----------------------------------------------------------------------------- // StatusNotifierCommand @@ -61,7 +63,8 @@ bool StatusNotifierCommand::execute() const string args = get_arguments_string(); string secure_command = app + " " + args; - cout << "- Command: " << secure_command << endl; // TODO + GlobalLogger.info() << "- Command: " << secure_command + << endl; int ret = system( secure_command.c_str() ); if ( ret != 0 ) diff --git a/src/main.cpp b/src/main.cpp index 5a1a82e..af2aa0e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,7 +5,8 @@ #include #include -#include "daemonfunc.hpp" +#include +#include #include "config/configurationreader.h" #include "config/host.h" @@ -15,9 +16,18 @@ using namespace std; using boost::asio::io_service; using boost::shared_ptr; +using I2n::Logger::GlobalLogger; + +void init_log() +{ + I2n::Logger::enable_syslog( I2n::Logger::Facility::User ); + I2n::Logger::set_log_level( I2n::Logger::LogLevel::Notice ); +} int main( int argc, char* argv[] ) { + init_log(); + // TODO load_configuration() or read_configuration() ConfigurationReader config_reader; bool read_success = config_reader.parse( argc, argv ); @@ -76,7 +86,8 @@ int main( int argc, char* argv[] ) bool started = scheduler->start_pinging(); if ( !started ) { - cerr << "Error: could not start pinger." << endl; + GlobalLogger.error() << "Error: could not start pinger." + << endl; } }