diff --git a/compiler/generate/t_cpp_generator.cc b/compiler/generate/t_cpp_generator.cc index 9d6b88fb..6938f86d 100755 --- a/compiler/generate/t_cpp_generator.cc +++ b/compiler/generate/t_cpp_generator.cc @@ -151,6 +151,8 @@ class t_cpp_generator : public t_oop_generator { void generate_sandesh_async_create_fn(ofstream &out, t_sandesh *tsandesh); void generate_sandesh_async_create_macro(ofstream &out, t_sandesh *tsandesh); void generate_sandesh_session_log_unrolled_fn(ofstream &out, t_sandesh *tsandesh); + void generate_sandesh_session_adjust_session_end_point_objects_fn(ofstream &out, + t_sandesh *tsandesh); void generate_sandesh_flow_send_fn(ofstream &out, t_sandesh *tsandesh); void generate_sandesh_systemlog_creators(ofstream &out, t_sandesh *tsandesh); void generate_sandesh_objectlog_creators(ofstream &out, t_sandesh *tsandesh); @@ -1569,10 +1571,18 @@ void t_cpp_generator::generate_sandesh_objectlog_creators(ofstream &out, void t_cpp_generator::generate_sandesh_session_log_unrolled_fn(ofstream &out, t_sandesh *tsandesh) { + //To be removed out << indent() << "static void LogUnrolled"; out << generate_sandesh_async_creator(tsandesh, true, false, false, "", "", true, false, false, false); - out << ";"; + out << ";" << endl; +} + +void t_cpp_generator::generate_sandesh_session_adjust_session_end_point_objects_fn(ofstream &out, + t_sandesh *tsandesh) { + out << "static void adjust_session_end_point_objects(std::vector" + " & session_data);" << endl; + out << endl; } void t_cpp_generator::generate_sandesh_flow_send_fn(ofstream &out, @@ -1589,23 +1599,6 @@ void t_cpp_generator::generate_sandesh_flow_send_fn(ofstream &out, indent_up(); out << indent() << "return;" << endl; scope_down(out); - out << indent() << - "if (IsFlowLoggingEnabled() && LoggingUseSyslog()) {" << endl; - indent_up(); - out << indent() << "UpdateTxMsgFailStats(\"" << tsandesh->get_name() << - "\", 0, SandeshTxDropReason::SendingToSyslog);" << endl; - const t_type *t = tsandesh->get_type(); - if (((t_base_type *)t)->is_sandesh_session()) { - out << indent() << "LogUnrolled" << - generate_sandesh_async_creator(tsandesh, false, - false, false, "", "", false, false, false) << ";" << endl; - } else { - out << indent() << "Log" << - generate_sandesh_async_creator(tsandesh, false, - false, false, "", "", false, false, false) << ";" << endl; - } - out << indent() << "return;" << endl; - scope_down(out); out << indent() << "if (IsSendingAllMessagesDisabled() ||" << " IsSendingFlowsDisabled()) {" << endl; indent_up(); @@ -1620,6 +1613,16 @@ void t_cpp_generator::generate_sandesh_flow_send_fn(ofstream &out, scope_down(out); out << indent() << "return;" << endl; scope_down(out); + + out << indent() << + "if (is_send_slo_to_logger_enabled() || is_send_sampled_to_logger_enabled()) { " << + endl; + indent_up(); + const t_type *t = tsandesh->get_type(); + if (((t_base_type *)t)->is_sandesh_session()) { + out << indent() << "LogUnrolled(category, level, session_data);" << endl; + } + scope_down(out); out << indent() << "if (level >= SendingLevel()) {" << endl; indent_up(); out << indent() << "UpdateTxMsgFailStats(\"" << tsandesh->get_name() << @@ -1639,6 +1642,15 @@ void t_cpp_generator::generate_sandesh_flow_send_fn(ofstream &out, " * snh = new " << tsandesh->get_name() << generate_sandesh_no_static_const_string_function(tsandesh, false, false, false, false) << ";" << endl; + out << indent() << "if (!is_send_sampled_to_collector_enabled() && !is_send_slo_to_collector_enabled()) {" << endl; + indent_up(); + out << indent() << "return;" << endl; + scope_down(out); + out << indent() << " if (is_send_sampled_to_collector_enabled() != is_send_slo_to_collector_enabled()) {" << endl; + indent_up(); + //out << indent() << "adjust_session_end_point_objects(snh->session_data);"; + scope_down(out); + out << endl; out << indent() << "snh->set_level(level);" << endl; out << indent() << "snh->set_category(category);" << endl; out << indent() << "snh->Dispatch();" << endl; @@ -1653,6 +1665,7 @@ void t_cpp_generator::generate_sandesh_flow_creators(ofstream &out, // Generate LogUnrolled declaration only for Session message if (((t_base_type *)t)->is_sandesh_session()) { generate_sandesh_session_log_unrolled_fn(out, tsandesh); + generate_sandesh_session_adjust_session_end_point_objects_fn(out, tsandesh); } // Generate send function and macros generate_sandesh_flow_send_fn(out, tsandesh); diff --git a/library/cpp/sandesh.cc b/library/cpp/sandesh.cc index 576e0493..b3d78c58 100644 --- a/library/cpp/sandesh.cc +++ b/library/cpp/sandesh.cc @@ -10,6 +10,8 @@ #include #include +#include +#include #include #include #include @@ -33,11 +35,17 @@ #include "sandesh_connection.h" #include "sandesh_state_machine.h" +#include +#include +#include +#include + using boost::asio::ip::tcp; using boost::asio::ip::address; using namespace contrail::sandesh::protocol; using namespace contrail::sandesh::transport; +using namespace log4cplus; // Statics Sandesh::SandeshRole::type Sandesh::role_ = SandeshRole::Invalid; @@ -51,6 +59,10 @@ bool Sandesh::disable_flow_collection_ = false; bool Sandesh::disable_sending_all_ = false; bool Sandesh::disable_sending_object_logs_ = false; bool Sandesh::disable_sending_flows_ = false; +bool Sandesh::slo_to_collector_ = false; +bool Sandesh::sampled_to_collector_ = false; +bool Sandesh::slo_to_logger_ = false; +bool Sandesh::sampled_to_logger_ = false; SandeshClient *Sandesh::client_ = NULL; SandeshConfig Sandesh::config_; std::auto_ptr Sandesh::recv_queue_; @@ -70,10 +82,17 @@ SandeshMessageStatistics Sandesh::msg_stats_; tbb::mutex Sandesh::stats_mutex_; log4cplus::Logger Sandesh::logger_ = log4cplus::Logger::getInstance(LOG4CPLUS_TEXT("SANDESH")); +log4cplus::Logger Sandesh::slo_logger_ = + log4cplus::Logger::getInstance(LOG4CPLUS_TEXT("SLO_SESSION")); +log4cplus::Logger Sandesh::sampled_logger_ = + log4cplus::Logger::getInstance(LOG4CPLUS_TEXT("SAMPLED_SESSION")); Sandesh::ModuleContextMap Sandesh::module_context_; tbb::atomic Sandesh::sandesh_send_ratelimit_; +const char *loggingPattern = "%D{%Y-%m-%d %a %H:%M:%S:%Q %Z} " + " %h [Thread %t, Pid %i]: %m%n"; + const char * Sandesh::SandeshRoleToString(SandeshRole::type role) { switch (role) { case SandeshRole::Generator: @@ -962,3 +981,72 @@ size_t Sandesh::SandeshQueue::AtomicDecrementQueueCount( size_t sandesh_size = element->GetSize(); return count_.fetch_and_add((size_t)(0-sandesh_size)) - sandesh_size; } + +/* + * Add the configured appenders for the sample logger + */ +void Sandesh::set_logger_appender(const std::string &file_name, long max_file_size, + int max_backup_index, + const std::string &syslog_facility, + const std::vector &destn, + const std::string &ident, + bool is_sampled_logger) { + log4cplus::Logger logger; + if (is_sampled_logger) { + logger = Sandesh::sampled_logger(); + } else { + logger = Sandesh::slo_logger(); + } + logger.setAdditivity(false); + // Session messages log level is + logger.setLogLevel(SandeshLevel::SYS_NOTICE); + // Local logging for SLO logger + if (std::find(destn.begin(), destn.end(), "file") != + destn.end()) { + // Append file appender to SLO logger + SharedAppenderPtr fileappender(new RollingFileAppender(file_name, + max_file_size, max_backup_index)); + logger.addAppender(fileappender); + if (is_sampled_logger) { + Sandesh::sampled_to_logger_ = true; + } else { + Sandesh::slo_to_logger_ = true; + } + } + // SYSLOG appender for SLO logger + if (std::find(destn.begin(), destn.end(), "syslog") != + destn.end()) { + helpers::Properties props; + std::string syslogident = boost::str( + boost::format("%1%[%2%]") % ident % getpid()); + props.setProperty(LOG4CPLUS_TEXT("facility"), + boost::starts_with(syslog_facility, "LOG_") + ? syslog_facility.substr(4) + : syslog_facility); + props.setProperty(LOG4CPLUS_TEXT("ident"), syslogident); + props.setProperty(LOG4CPLUS_TEXT("additivity"), "false"); + SharedAppenderPtr syslogappender(new SysLogAppender(props)); + std::auto_ptr syslog_layout_ptr(new PatternLayout( + loggingPattern)); + syslogappender->setLayout(syslog_layout_ptr); + logger.addAppender(syslogappender); + if (is_sampled_logger) { + Sandesh::sampled_to_logger_ = true; + } else { + Sandesh::slo_to_logger_ = true; + } + } +} + +void Sandesh::set_send_to_collector_flags( + const std::vector &sampled_destn, + const std::vector &slo_destn) { + if (std::find(slo_destn.begin(), slo_destn.end(), "collector") != + slo_destn.end()) { + Sandesh::slo_to_collector_ = true; + } + if (std::find(sampled_destn.begin(), sampled_destn.end(), "collector") != + sampled_destn.end()) { + Sandesh::sampled_to_collector_ = true; + } +} diff --git a/library/cpp/sandesh.h b/library/cpp/sandesh.h index ec85392c..fd3cc1d4 100644 --- a/library/cpp/sandesh.h +++ b/library/cpp/sandesh.h @@ -319,6 +319,22 @@ class Sandesh { static const char* LevelToString(SandeshLevel::type level); static SandeshLevel::type StringToLevel(std::string level); static log4cplus::Logger& logger() { return logger_; } + static log4cplus::Logger& slo_logger() { return slo_logger_; } + static log4cplus::Logger& sampled_logger() { return sampled_logger_; } + static void set_logger_appender(const std::string &file_name, + long max_file_size, + int max_backup_index, + const std::string &syslog_facility, + const std::vector &destn, + const std::string &ident, + bool is_sampled_logger); + static void set_send_to_collector_flags( + const std::vector &sampled_destination, + const std::vector &slo_destination); + static bool is_send_slo_to_collector_enabled() { return slo_to_collector_; } + static bool is_send_sampled_to_collector_enabled() { return sampled_to_collector_; } + static bool is_send_slo_to_logger_enabled() { return slo_to_logger_; } + static bool is_send_sampled_to_logger_enabled() { return sampled_to_logger_; } protected: void set_timestamp(time_t timestamp) { timestamp_ = timestamp; } @@ -407,6 +423,8 @@ class Sandesh { static SandeshMessageStatistics msg_stats_; static tbb::mutex stats_mutex_; static log4cplus::Logger logger_; + static log4cplus::Logger slo_logger_; + static log4cplus::Logger sampled_logger_; static bool disable_flow_collection_; // disable flow collection static SandeshConfig config_; static bool disable_sending_all_; @@ -423,6 +441,10 @@ class Sandesh { std::string category_; std::string name_; static tbb::atomic sandesh_send_ratelimit_; + static bool slo_to_collector_; + static bool sampled_to_collector_; + static bool slo_to_logger_; + static bool sampled_to_logger_; }; struct SandeshElement {