From 277988ca7879e1869d9da012b12f9be3220ecdfd Mon Sep 17 00:00:00 2001 From: Numan Siddique Date: Mon, 1 Jun 2015 13:50:23 +0530 Subject: [PATCH] Support logging customization for dns module Change-Id: I7679ffbb3619d57a3d337f3efb5bc0058a04abc8 Closes-bug: #1460570 --- src/dns/cmn/dns_options.cc | 6 +++++- src/dns/cmn/dns_options.h | 2 ++ src/dns/contrail-dns.conf | 1 + src/dns/main.cc | 24 +++++++++++++++--------- src/dns/test/dns_options_test.cc | 8 +++++++- 5 files changed, 30 insertions(+), 11 deletions(-) diff --git a/src/dns/cmn/dns_options.cc b/src/dns/cmn/dns_options.cc index b1a314bb8e7..54915428515 100644 --- a/src/dns/cmn/dns_options.cc +++ b/src/dns/cmn/dns_options.cc @@ -104,6 +104,8 @@ void Options::Initialize(EventManager &evm, "Category filter for local logging of sandesh messages") ("DEFAULT.log_disable", opt::bool_switch(&log_disable_), "Disable sandesh logging") + ("DEFAULT.log_property_file", opt::value()->default_value(""), + "log4cplus property file name") ("DEFAULT.log_file", opt::value()->default_value(""), "Filename for the logs to be written to") ("DEFAULT.log_files_count", @@ -182,7 +184,7 @@ void Options::GetOptValueImpl( std::vector tmp( var_map[val].as >()); // Now split the individual elements - for (typename std::vector::const_iterator it = + for (typename std::vector::const_iterator it = tmp.begin(); it != tmp.end(); it++) { std::stringstream ss(*it); @@ -253,6 +255,8 @@ void Options::Process(int argc, char *argv[], GetOptValue(var_map, log_category_, "DEFAULT.log_category"); GetOptValue(var_map, log_file_, "DEFAULT.log_file"); + GetOptValue(var_map, log_property_file_, + "DEFAULT.log_property_file"); GetOptValue(var_map, log_files_count_, "DEFAULT.log_files_count"); GetOptValue(var_map, log_file_size_, "DEFAULT.log_file_size"); GetOptValue(var_map, log_level_, "DEFAULT.log_level"); diff --git a/src/dns/cmn/dns_options.h b/src/dns/cmn/dns_options.h index 100f4924ac7..e0f9a927512 100644 --- a/src/dns/cmn/dns_options.h +++ b/src/dns/cmn/dns_options.h @@ -30,6 +30,7 @@ class Options { const std::string log_category() const { return log_category_; } const bool log_disable() const { return log_disable_; } const std::string log_file() const { return log_file_; } + const std::string log_property_file() const { return log_property_file_; } const int log_files_count() const { return log_files_count_; } const long log_file_size() const { return log_file_size_; } const std::string log_level() const { return log_level_; } @@ -83,6 +84,7 @@ class Options { std::string log_category_; bool log_disable_; std::string log_file_; + std::string log_property_file_; int log_files_count_; long log_file_size_; std::string log_level_; diff --git a/src/dns/contrail-dns.conf b/src/dns/contrail-dns.conf index b6b324855de..164ab31bfbb 100644 --- a/src/dns/contrail-dns.conf +++ b/src/dns/contrail-dns.conf @@ -22,6 +22,7 @@ log_file=/var/log/contrail/contrail-dns.log log_level=SYS_NOTICE log_local=1 # test_mode=0 +# log_property_file= # log4cplus property file [DISCOVERY] # port=5998 diff --git a/src/dns/main.cc b/src/dns/main.cc index 36d23640b38..9cc91b3a47d 100644 --- a/src/dns/main.cc +++ b/src/dns/main.cc @@ -107,9 +107,15 @@ int main(int argc, char *argv[]) { Module::type module = Module::DNS; string module_name = g_vns_constants.ModuleNames.find(module)->second; - LoggingInit(options.log_file(), options.log_file_size(), - options.log_files_count(), options.use_syslog(), - options.syslog_facility(), module_name); + std::string log_property_file = options.log_property_file(); + if (log_property_file.size()) { + LoggingInit(log_property_file); + } + else { + LoggingInit(options.log_file(), options.log_file_size(), + options.log_files_count(), options.use_syslog(), + options.syslog_facility(), module_name); + } string build_info_str; Dns::GetVersion(build_info_str); @@ -125,11 +131,11 @@ int main(int argc, char *argv[]) { string hostname = host_name(ec); Dns::SetHostName(hostname); if (options.discovery_server().empty()) { - NodeType::type node_type = + NodeType::type node_type = g_vns_constants.Module2NodeType.find(module)->second; Sandesh::InitGenerator( module_name, - options.hostname(), + options.hostname(), g_vns_constants.NodeTypeNames.find(node_type)->second, g_vns_constants.INSTANCE_ID_DEFAULT, Dns::GetEventManager(), @@ -182,7 +188,7 @@ int main(int argc, char *argv[]) { new TaskTrigger(boost::bind(&DnsInfoLogger), TaskScheduler::GetInstance()->GetTaskId("dns::Config"), 0); - dns_info_log_timer = + dns_info_log_timer = TimerManager::CreateTimer(*(Dns::GetEventManager()->io_service()), "Dns Info log timer"); dns_info_log_timer->Start(60*1000, boost::bind(&DnsInfoLogTimer), NULL); @@ -215,9 +221,9 @@ int main(int argc, char *argv[]) { //subscribe to collector service if not configured if (!options.collectors_configured()) { Module::type module = Module::DNS; - NodeType::type node_type = + NodeType::type node_type = g_vns_constants.Module2NodeType.find(module)->second; - string subscriber_name = + string subscriber_name = g_vns_constants.ModuleNames.find(module)->second; string node_type_name = g_vns_constants.NodeTypeNames.find(node_type)->second; @@ -253,7 +259,7 @@ int main(int argc, char *argv[]) { ifmap_server.set_ifmap_manager(ifmapmgr); Dns::GetEventManager()->Run(); - + Dns::ShutdownDiscoveryClient(ds_client); return 0; diff --git a/src/dns/test/dns_options_test.cc b/src/dns/test/dns_options_test.cc index d7bd52e49e9..1db3d215bd3 100644 --- a/src/dns/test/dns_options_test.cc +++ b/src/dns/test/dns_options_test.cc @@ -106,6 +106,7 @@ TEST_F(OptionsTest, DefaultConfFile) { EXPECT_EQ(options_.log_category(), ""); EXPECT_EQ(options_.log_disable(), false); EXPECT_EQ(options_.log_file(), "/var/log/contrail/contrail-dns.log"); + EXPECT_EQ(options_.log_property_file(), ""); EXPECT_EQ(options_.log_files_count(), 10); EXPECT_EQ(options_.log_file_size(), 1024*1024); EXPECT_EQ(options_.log_level(), "SYS_NOTICE"); @@ -118,18 +119,20 @@ TEST_F(OptionsTest, DefaultConfFile) { } TEST_F(OptionsTest, OverrideStringFromCommandLine) { - int argc = 5; + int argc = 6; char *argv[argc]; char argv_0[] = "dns_options_test"; char argv_1[] = "--conf_file=controller/src/dns/contrail-dns.conf"; char argv_2[] = "--DEFAULT.log_file=test.log"; char argv_3[] = "--DEFAULT.rndc_config_file=test.rndc"; char argv_4[] = "--DEFAULT.rndc_secret=secret123"; + char argv_5[] = "--DEFAULT.log_property_file=log4cplus.prop"; argv[0] = argv_0; argv[1] = argv_1; argv[2] = argv_2; argv[3] = argv_3; argv[4] = argv_4; + argv[5] = argv_5; options_.Parse(evm_, argc, argv); @@ -152,6 +155,7 @@ TEST_F(OptionsTest, OverrideStringFromCommandLine) { EXPECT_EQ(options_.log_category(), ""); EXPECT_EQ(options_.log_disable(), false); EXPECT_EQ(options_.log_file(), "test.log"); // Overridden from cmd line. + EXPECT_EQ(options_.log_property_file(), "log4cplus.prop"); // Overridden from cmd line. EXPECT_EQ(options_.log_files_count(), 10); EXPECT_EQ(options_.log_file_size(), 1024*1024); EXPECT_EQ(options_.log_level(), "SYS_NOTICE"); @@ -224,6 +228,7 @@ TEST_F(OptionsTest, CustomConfigFile) { "log_level=SYS_DEBUG\n" "log_local=1\n" "test_mode=1\n" + "log_property_file=log4cplus.prop\n" "\n" "[DISCOVERY]\n" "port=100\n" @@ -272,6 +277,7 @@ TEST_F(OptionsTest, CustomConfigFile) { EXPECT_EQ(options_.log_category(), "dns"); EXPECT_EQ(options_.log_disable(), true); EXPECT_EQ(options_.log_file(), "test.log"); + EXPECT_EQ(options_.log_property_file(), "log4cplus.prop"); EXPECT_EQ(options_.log_files_count(), 20); EXPECT_EQ(options_.log_file_size(), 1024); EXPECT_EQ(options_.log_level(), "SYS_DEBUG");