OpenSDN source code
bgp_log.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 
6 #include "bgp/bgp_log.h"
7 
8 #include <string>
9 
11 
13  1000));
16 
17 namespace bgp_log_test {
18 
20 
21 static void init_common() {
22  unit_test_ = true;
23  bool log_disable = getenv("LOG_DISABLE") != NULL;
24 
25  // By default, we log all messages from all categories.
27 
28  // Have ability to filter messages via environment variables.
29  const char *category = getenv("BGP_UT_LOG_CATEGORY");
30  if (category) Sandesh::SetLoggingCategory(category);
31 
32  const char *level = getenv("BGP_UT_LOG_LEVEL");
33  if (level) Sandesh::SetLoggingLevel(level);
34 
35  SetLoggingDisabled(log_disable);
36 }
37 
38 void init() {
39  LoggingInit();
40  init_common();
41 }
42 
43 void init(std::string log_file, unsigned long log_file_size,
44  unsigned long log_file_index, bool enable_syslog,
45  std::string syslog_facility, std::string ident,
46  std::string log_level) {
47  LoggingInit(log_file, log_file_size, log_file_index,
48  enable_syslog, syslog_facility, ident,
50  Sandesh::StringToLevel(log_level)));
51  init_common();
52 }
53 
54 bool unit_test() {
55  return unit_test_;
56 }
57 
58 void LogServerName(const BgpServer *server) {
59  if (!unit_test_ || !server) return;
60 
61  if (Sandesh::LoggingLevel() >= SandeshLevel::SYS_DEBUG) {
62  LOG(DEBUG, "BgpServer: " << server->ToString());
63  }
64 }
65 
66 void LogServerName(const IPeer *ipeer, const BgpTable *table) {
67  if (!unit_test_) return;
68 
69  BgpServer *server = ipeer ? const_cast<IPeer *>(ipeer)->server() : NULL;
70  if (!server && table && table->routing_instance()) {
71  server = const_cast<RoutingInstance *>(
72  table->routing_instance())->server();
73  }
74 
75  LogServerName(server);
76 }
77 
78 } // namespace bgp_log_test
SandeshTraceBufferPtr BgpTraceBuf(SandeshTraceBufferCreate(BGP_TRACE_BUF, 1000))
SandeshTraceBufferPtr BgpPeerObjectTraceBuf(SandeshTraceBufferCreate(BGP_PEER_OBJECT_TRACE_BUF, 1000))
#define BGP_PEER_OBJECT_TRACE_BUF
Definition: bgp_log.h:38
#define BGP_TRACE_BUF
Definition: bgp_log.h:37
virtual std::string ToString() const
Definition: bgp_server.cc:565
RoutingInstance * routing_instance()
Definition: bgp_table.h:148
Definition: ipeer.h:187
static void SetLoggingLevel(std::string level)
Definition: sandesh.cc:392
static SandeshLevel::type LoggingLevel()
Definition: cpp/sandesh.h:224
static SandeshLevel::type LoggingUtLevel()
Definition: cpp/sandesh.h:225
static void SetLoggingCategory(std::string category)
Definition: sandesh.cc:440
static void SetLoggingParams(bool enable_local_log, std::string category, std::string level, bool enable_trace_print=false, bool enable_flow_log=false, bool enable_session_syslog=false)
Definition: sandesh.cc:371
static SandeshLevel::type StringToLevel(std::string level)
Definition: sandesh.cc:863
void LoggingInit()
Definition: logging.cc:83
void SetLoggingDisabled(bool flag)
Definition: logging.cc:60
#define LOG(_Level, _Msg)
Definition: logging.h:35
bool unit_test()
Definition: bgp_log.cc:54
void init()
Definition: bgp_log.cc:38
void LogServerName(const BgpServer *server)
Definition: bgp_log.cc:58
bool unit_test_
Definition: bgp_log.cc:19
static void init_common()
Definition: bgp_log.cc:21
log4cplus::LogLevel SandeshLevelTolog4Level(SandeshLevel::type slevel)
Definition: sandesh.cc:397
boost::shared_ptr< TraceBuffer< SandeshTrace > > SandeshTraceBufferPtr
Definition: sandesh_trace.h:19
SandeshTraceBufferPtr SandeshTraceBufferCreate(const std::string &buf_name, size_t buf_size, bool trace_enable=true)
Definition: sandesh_trace.h:47