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