Skip to content

Commit

Permalink
Support logging customization for OpenContrail c++ modules
Browse files Browse the repository at this point in the history
This patch provides the option to customize the
logging parameters in Open Contrail for c++ modules.

1. The user can define the log4cplus.properties file with
the desired loggers and appenders and
2. pass the path of the properties file in the
'DEFAULT.log_property_file' parameter in the respective
configuration file of each of the OpenContrail c++ modules.

Change-Id: I79a8445d4e1345e89cf25e7514361f3561d7b68f
Partially-Implements: bp support-log-configuration
Partial-Bug: #1460585
Partial-Bug: #1460570
  • Loading branch information
numansiddique committed Jun 1, 2015
1 parent 90b6cbd commit 5a893d7
Show file tree
Hide file tree
Showing 19 changed files with 159 additions and 89 deletions.
20 changes: 12 additions & 8 deletions src/analytics/main.cc
Expand Up @@ -44,7 +44,7 @@ using process::GetProcessStateCb;

static TaskTrigger *collector_info_trigger;
static Timer *collector_info_log_timer;
static EventManager * a_evm = NULL;
static EventManager * a_evm = NULL;

bool CollectorInfoLogTimer() {
collector_info_trigger->Set();
Expand Down Expand Up @@ -180,7 +180,7 @@ void CollectorShutdown() {

static void terminate(int param) {
// Shutdown can result in a malloc-detected error
// Taking a stack trace during this error can result in
// Taking a stack trace during this error can result in
// the process not terminating correctly
// using mallopt in this way ensures that we get a core,
// but we don't print a stack trace
Expand Down Expand Up @@ -251,10 +251,15 @@ int main(int argc, char *argv[])
NodeType::type node_type =
g_vns_constants.Module2NodeType.find(module)->second;
std::string instance_id(g_vns_constants.INSTANCE_ID_DEFAULT);
LoggingInit(options.log_file(), options.log_file_size(),
options.log_files_count(), options.use_syslog(),
options.syslog_facility(), module_id);

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_id);
}
vector<string> cassandra_servers(options.cassandra_server_list());
vector<string> cassandra_ips;
vector<int> cassandra_ports;
Expand Down Expand Up @@ -358,7 +363,7 @@ int main(int argc, char *argv[])
module_id,
analytics.name(),
g_vns_constants.NodeTypeNames.find(node_type)->second,
instance_id,
instance_id,
a_evm, "127.0.0.1", options.collector_port(),
options.http_server_port(), &vsc);

Expand Down Expand Up @@ -395,7 +400,6 @@ int main(int argc, char *argv[])
pub_msg = pub_ss.str();
ds_client->Publish(service_name, pub_msg);
}

CpuLoadData::Init();
collector_info_trigger =
new TaskTrigger(boost::bind(&CollectorInfoLogger, vsc),
Expand Down
5 changes: 4 additions & 1 deletion src/analytics/options.cc
Expand Up @@ -111,6 +111,8 @@ void Options::Initialize(EventManager &evm,
"Disable sandesh logging")
("DEFAULT.log_file", opt::value<string>()->default_value("<stdout>"),
"Filename for the logs to be written to")
("DEFAULT.log_property_file", opt::value<string>()->default_value(""),
"log4cplus property file name")
("DEFAULT.log_files_count",
opt::value<int>()->default_value(10),
"Maximum log file roll over index")
Expand Down Expand Up @@ -196,7 +198,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 @@ -275,6 +277,7 @@ 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/analytics/options.h
Expand Up @@ -36,6 +36,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 @@ -93,6 +94,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
7 changes: 7 additions & 0 deletions src/base/logging.cc
Expand Up @@ -81,3 +81,10 @@ void LoggingInit(const std::string &filename, long maxFileSize, int maxBackupInd

CheckEnvironmentAndUpdate();
}


void LoggingInit(const std::string &propertyFile) {
PropertyConfigurator::doConfigure(propertyFile);
CheckEnvironmentAndUpdate();
}

2 changes: 2 additions & 0 deletions src/base/logging.h
Expand Up @@ -45,6 +45,8 @@ void LoggingInit(const std::string &filename,
const std::string &syslogFacility,
const std::string &ident);

void LoggingInit(const std::string &propertyFile);

//
// Disable logging - For testing purposes only
//
Expand Down
12 changes: 9 additions & 3 deletions src/control-node/main.cc
Expand Up @@ -402,9 +402,15 @@ int main(int argc, char *argv[]) {
ControlNode::SetProgramName(argv[0]);
Module::type module = Module::CONTROL_NODE;
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);
}

TaskScheduler::Initialize();
ControlNode::SetDefaultSchedulingPolicy();
Expand Down
3 changes: 3 additions & 0 deletions src/control-node/options.cc
Expand Up @@ -100,6 +100,8 @@ void Options::Initialize(EventManager &evm,
"Disable sandesh logging")
("DEFAULT.log_file", opt::value<string>()->default_value("<stdout>"),
"Filename for the logs to be written to")
("DEFAULT.log_property_file", opt::value<string>()->default_value(""),
"log4cplus property file name")
("DEFAULT.log_files_count",
opt::value<int>()->default_value(10),
"Maximum log file roll over index")
Expand Down Expand Up @@ -241,6 +243,7 @@ bool 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/control-node/options.h
Expand Up @@ -25,6 +25,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 @@ -69,6 +70,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
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 @@ -170,7 +172,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 @@ -241,6 +243,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 @@ -80,6 +81,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 @@ -106,9 +106,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 @@ -172,7 +178,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 @@ -207,9 +213,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 @@ -241,7 +247,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");
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
3 changes: 3 additions & 0 deletions src/query_engine/options.cc
Expand Up @@ -93,6 +93,8 @@ void Options::Initialize(EventManager &evm,
"Disable sandesh logging")
("DEFAULT.log_file", opt::value<string>()->default_value("<stdout>"),
"Filename for the logs to be written to")
("DEFAULT.log_property_file", opt::value<string>()->default_value(""),
"log4cplus property file name")
("DEFAULT.log_files_count",
opt::value<int>()->default_value(10),
"Maximum log file roll over index")
Expand Down Expand Up @@ -217,6 +219,7 @@ 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/query_engine/options.h
Expand Up @@ -31,6 +31,7 @@ class Options {
const int max_tasks() const { return max_tasks_; }
const int max_slice() const { return max_slice_; }
const std::string log_category() const { return log_category_; }
const std::string log_property_file() const { return log_property_file_; }
const bool log_disable() const { return log_disable_; }
const std::string log_file() const { return log_file_; }
const int log_files_count() const { return log_files_count_; }
Expand Down Expand Up @@ -69,6 +70,7 @@ class Options {
std::string host_ip_;
uint16_t http_server_port_;
std::string log_category_;
std::string log_property_file_;
bool log_disable_;
std::string log_file_;
int log_files_count_;
Expand Down
13 changes: 9 additions & 4 deletions src/query_engine/qed.cc
Expand Up @@ -153,10 +153,15 @@ main(int argc, char *argv[]) {
Module::type module = Module::QUERY_ENGINE;
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);
}
error_code error;
DiscoveryServiceClient *ds_client = NULL;
ip::tcp::endpoint dss_ep;
Expand Down

0 comments on commit 5a893d7

Please sign in to comment.