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 2 : static void init_common() { 21 2 : unit_test_ = true; 22 2 : bool log_disable = getenv("LOG_DISABLE") != NULL; 23 : 24 : // By default, we log all messages from all categories. 25 2 : Sandesh::SetLoggingParams(!log_disable, "", Sandesh::LoggingUtLevel()); 26 : 27 : // Have ability to filter messages via environment variables. 28 2 : const char *category = getenv("BGP_UT_LOG_CATEGORY"); 29 2 : if (category) Sandesh::SetLoggingCategory(category); 30 : 31 2 : const char *level = getenv("BGP_UT_LOG_LEVEL"); 32 2 : if (level) Sandesh::SetLoggingLevel(level); 33 : 34 2 : SetLoggingDisabled(log_disable); 35 2 : } 36 : 37 2 : void init() { 38 2 : LoggingInit(); 39 2 : init_common(); 40 2 : } 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 4374 : bool unit_test() { 54 4374 : return unit_test_; 55 : } 56 : 57 0 : void LogServerName(const BgpServer *server) { 58 0 : if (!unit_test_ || !server) return; 59 : 60 0 : if (Sandesh::LoggingLevel() >= SandeshLevel::SYS_DEBUG) { 61 0 : LOG(DEBUG, "BgpServer: " << server->ToString()); 62 : } 63 : } 64 : 65 0 : void LogServerName(const IPeer *ipeer, const BgpTable *table) { 66 0 : if (!unit_test_) return; 67 : 68 0 : BgpServer *server = ipeer ? const_cast<IPeer *>(ipeer)->server() : NULL; 69 0 : if (!server && table && table->routing_instance()) { 70 : server = const_cast<RoutingInstance *>( 71 0 : table->routing_instance())->server(); 72 : } 73 : 74 0 : LogServerName(server); 75 : } 76 : 77 : } // namespace bgp_log_test