Line data Source code
1 : /* 2 : * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved. 3 : */ 4 : 5 : #include "bgp/bgp_log.h" 6 : 7 : #include <string> 8 : 9 : #include "bgp/routing-instance/routing_instance.h" 10 : 11 : SandeshTraceBufferPtr BgpTraceBuf(SandeshTraceBufferCreate(BGP_TRACE_BUF, 12 : 1000)); 13 : SandeshTraceBufferPtr BgpPeerObjectTraceBuf(SandeshTraceBufferCreate( 14 : BGP_PEER_OBJECT_TRACE_BUF, 1000)); 15 : 16 : namespace bgp_log_test { 17 : 18 : bool unit_test_; 19 : 20 266 : static void init_common() { 21 266 : unit_test_ = true; 22 266 : bool log_disable = getenv("LOG_DISABLE") != NULL; 23 : 24 : // By default, we log all messages from all categories. 25 266 : Sandesh::SetLoggingParams(!log_disable, "", Sandesh::LoggingUtLevel()); 26 : 27 : // Have ability to filter messages via environment variables. 28 266 : const char *category = getenv("BGP_UT_LOG_CATEGORY"); 29 266 : if (category) Sandesh::SetLoggingCategory(category); 30 : 31 266 : const char *level = getenv("BGP_UT_LOG_LEVEL"); 32 266 : if (level) Sandesh::SetLoggingLevel(level); 33 : 34 266 : SetLoggingDisabled(log_disable); 35 266 : } 36 : 37 266 : void init() { 38 266 : LoggingInit(); 39 266 : init_common(); 40 266 : } 41 : 42 0 : void init(std::string log_file, unsigned long log_file_size, 43 : unsigned long log_file_index, bool enable_syslog, 44 : std::string syslog_facility, std::string ident, 45 : std::string log_level) { 46 0 : LoggingInit(log_file, log_file_size, log_file_index, 47 : enable_syslog, syslog_facility, ident, 48 : SandeshLevelTolog4Level( 49 : Sandesh::StringToLevel(log_level))); 50 0 : init_common(); 51 0 : } 52 : 53 2378798 : bool unit_test() { 54 2378798 : return unit_test_; 55 : } 56 : 57 7049082 : void LogServerName(const BgpServer *server) { 58 7049082 : if (!unit_test_ || !server) return; 59 : 60 6067842 : if (Sandesh::LoggingLevel() >= SandeshLevel::SYS_DEBUG) { 61 6067712 : LOG(DEBUG, "BgpServer: " << server->ToString()); 62 : } 63 : } 64 : 65 2413716 : void LogServerName(const IPeer *ipeer, const BgpTable *table) { 66 2413716 : if (!unit_test_) return; 67 : 68 2413716 : BgpServer *server = ipeer ? const_cast<IPeer *>(ipeer)->server() : NULL; 69 2413708 : if (!server && table && table->routing_instance()) { 70 : server = const_cast<RoutingInstance *>( 71 266171 : table->routing_instance())->server(); 72 : } 73 : 74 2413700 : LogServerName(server); 75 : } 76 : 77 : } // namespace bgp_log_test