Skip to content

Commit

Permalink
Merge "Support logging customization for dns module"
Browse files Browse the repository at this point in the history
  • Loading branch information
Zuul authored and opencontrail-ci-admin committed Jun 25, 2015
2 parents 0781f26 + 277988c commit 3c3a4a8
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 11 deletions.
6 changes: 5 additions & 1 deletion src/dns/cmn/dns_options.cc
Expand Up @@ -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<string>()->default_value(""),
"log4cplus property file name")
("DEFAULT.log_file", opt::value<string>()->default_value("<stdout>"),
"Filename for the logs to be written to")
("DEFAULT.log_files_count",
Expand Down Expand Up @@ -182,7 +184,7 @@ void Options::GetOptValueImpl(
std::vector<ElementType> tmp(
var_map[val].as<std::vector<ElementType> >());
// Now split the individual elements
for (typename std::vector<ElementType>::const_iterator it =
for (typename std::vector<ElementType>::const_iterator it =
tmp.begin();
it != tmp.end(); it++) {
std::stringstream ss(*it);
Expand Down Expand Up @@ -253,6 +255,8 @@ void Options::Process(int argc, char *argv[],

GetOptValue<string>(var_map, log_category_, "DEFAULT.log_category");
GetOptValue<string>(var_map, log_file_, "DEFAULT.log_file");
GetOptValue<string>(var_map, log_property_file_,
"DEFAULT.log_property_file");
GetOptValue<int>(var_map, log_files_count_, "DEFAULT.log_files_count");
GetOptValue<long>(var_map, log_file_size_, "DEFAULT.log_file_size");
GetOptValue<string>(var_map, log_level_, "DEFAULT.log_level");
Expand Down
2 changes: 2 additions & 0 deletions src/dns/cmn/dns_options.h
Expand Up @@ -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_; }
Expand Down Expand Up @@ -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_;
Expand Down
1 change: 1 addition & 0 deletions src/dns/contrail-dns.conf
Expand Up @@ -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
Expand Down
24 changes: 15 additions & 9 deletions src/dns/main.cc
Expand Up @@ -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);
Expand All @@ -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(),
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 7 additions & 1 deletion src/dns/test/dns_options_test.cc
Expand Up @@ -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");
Expand All @@ -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);

Expand All @@ -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");
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit 3c3a4a8

Please sign in to comment.