OpenSDN source code
bgp_debug.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_debug.h"
7 
8 #if defined(__BGP_DEBUG__)
9 
10 
11 bool BgpDebug::enable_ = (getenv("BGP_DEBUG") != NULL);
12 bool BgpDebug::enable_stack_trace_ = (getenv("BGP_DEBUG_STACK_TRACE") != NULL);
13 
14 #define COLOR_RESET "\e[m"
15 #define COLOR_BLACK "\e[0;30m"
16 #define COLOR_RED "\e[0;31m"
17 #define COLOR_GREEN "\e[0;32m"
18 #define COLOR_BROWN "\e[0;33m"
19 #define COLOR_BLUE "\e[0;34m"
20 #define COLOR_MAGENTA "\e[0;35m"
21 #define COLOR_CYAN "\e[0;36m"
22 #define COLOR_GRAY "\e[0;37m"
23 #define COLOR_DARKGRAY "\e[1;30m"
24 #define COLOR_LIGHTBLUE "\e[1;34m"
25 #define COLOR_LIGHTGREEN "\e[1;32m"
26 #define COLOR_LIGHTCYAN "\e[1;36m"
27 #define COLOR_LIGHTRED "\e[1;31m"
28 #define COLOR_LIGHTPURPLE "\e[1;35m"
29 #define COLOR_YELLOW "\e[1;33m"
30 #define COLOR_WHITE "\e[1;37m"
31 
32 void BgpDebug::Debug(DBTable *t, DBEntry *r, IPeer *peer, const char *file,
33  const char *function, int line, char *msg) {
34  if (!enable_) return;
35  if (enable_stack_trace_) BackTrace::Log();
36 
37  char *buf;
38  char *str;
39 
40  BgpTable *table = dynamic_cast<BgpTable *>(t);
41  BgpRoute *rt = dynamic_cast<BgpRoute *>(r);
42 
43  size_t buf_size = 1024 * 1024;
44  buf = static_cast<char *>(malloc(buf_size));
45  str = buf;
46 
47  const char *server_name = (peer && peer->server()) ?
48  peer->server()->ToString().c_str() : "Unknown";
49  const char *instance_name =
50  (table && table->routing_instance()) ?
51  table->routing_instance()->name().c_str() : "Unknown";
52  const char *table_name = table ?
53  Address::FamilyToString(table->family()).c_str() : "Unknown";
54  const char *peer_name = peer ? peer->ToString().c_str() : "Unknown";
55  const char *route_name = rt ? rt->ToString().c_str() : "Unknown";
56 
57  str += snprintf(str, buf_size - (str - buf), "\n%s:%s:%d: ",
58  file, function, line); // class: typeid(*this).name()
59  if (strcmp(server_name, "Unknown")) {
60  str += snprintf(str, buf_size - (str - buf),
61  COLOR_BROWN "Serv %s(%p); ",
62  server_name, peer->server());
63  }
64  if (strcmp(instance_name, "Unknown")) {
65  str += snprintf(str, buf_size - (str - buf),
66  COLOR_RED "Inst %s(%p); ", instance_name,
67  table->routing_instance());
68  }
69  if (strcmp(table_name, "Unknown")) {
70  if (strcmp(table_name, "inet"))
71  str += snprintf(str, buf_size - (str - buf),
72  COLOR_LIGHTBLUE "Tabl %s(%p); ",
73  table_name, table);
74  else
75  str += snprintf(str, buf_size - (str - buf),
76  COLOR_BLUE "Tabl %s(%p); ",
77  table_name, table);
78  }
79  if (strcmp(peer_name, "Unknown")) {
80  str += snprintf(str, buf_size - (str - buf),
81  COLOR_GREEN "Peer %s(%s)(%p), ", peer_name,
82  peer->GetStateName().c_str(), peer);
83  }
84  if (strcmp(route_name, "Unknown")) {
85  str += snprintf(str, buf_size - (str - buf),
86  COLOR_MAGENTA "Route %s(%p)", route_name, rt);
87  }
88  str += snprintf(str, buf_size - (str - buf), COLOR_CYAN "\n %s", msg);
89  str += snprintf(str, buf_size - (str - buf), "\n"
90  "------------------------------------------"
91  "------------------------------------------");
92  str += snprintf(str, buf_size - (str - buf), COLOR_RESET);
93 
94  BGP_LOG_STR(BgpMessage, SandeshLevel::SYS_DEBUG, BGP_LOG_FLAG_ALL, buf);
95  free(buf);
96 }
97 
98 #endif // __BGP_DEBUG__
#define BGP_LOG_FLAG_ALL
Definition: bgp_log.h:45
#define BGP_LOG_STR(obj, level, flags, arg)
Definition: bgp_log.h:90
static std::string FamilyToString(Family fmly)
Definition: address.cc:64
static void Log(const std::string &msg)
Definition: backtrace.cc:78
virtual std::string ToString() const
Definition: bgp_server.cc:565
RoutingInstance * routing_instance()
Definition: bgp_table.h:148
virtual Address::Family family() const =0
virtual std::string ToString() const =0
Definition: ipeer.h:187
virtual const std::string & ToString() const =0
virtual const std::string GetStateName() const =0
virtual BgpServer * server()=0
const std::string & name() const