OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
bgp_log.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  */
4 
5 #ifndef ctrlplane_bgp_log_h
6 
7 #define ctrlplane_bgp_log_h
8 
9 #include "sandesh/sandesh_trace.h"
10 #include "sandesh/common/vns_types.h"
11 #include "base/logging.h"
12 #include "bgp/bgp_log_types.h"
13 #include "bgp/bgp_server.h"
14 #include "bgp/ipeer.h"
15 #include "bgp/bgp_table.h"
16 
17 namespace bgp_log_test {
18 
19 void init();
20 void init(std::string log_file, unsigned long log_file_size,
21  unsigned long log_file_index, bool enable_syslog,
22  std::string syslog_facility, std::string ident,
23  std::string log_level);
24 bool unit_test();
25 void LogServerName(const BgpServer *server);
26 void LogServerName(const IPeer *ipeer, const BgpTable *table);
27 
28 // Bgp Unit Test specific logging macros
29 #define BGP_DEBUG_UT(str) \
30  BGP_LOG_STR(BgpMessage, Sandesh::LoggingUtLevel(), BGP_LOG_FLAG_ALL, str)
31 #define BGP_WARN_UT(str) \
32  BGP_LOG_STR(BgpMessage, SandeshLevel::UT_WARN, BGP_LOG_FLAG_ALL, str)
33 
34 }
35 
36 #define BGP_TRACE_BUF "BgpTraceBuf"
37 #define BGP_PEER_OBJECT_TRACE_BUF "BgpPeerObjectTraceBuf"
38 
41 
42 #define BGP_LOG_FLAG_SYSLOG 1
43 #define BGP_LOG_FLAG_TRACE 2
44 #define BGP_LOG_FLAG_ALL (BGP_LOG_FLAG_SYSLOG | BGP_LOG_FLAG_TRACE)
45 
46 #define BGP_UVE_SEND(type, object) \
47 do { \
48  if (LoggingDisabled()) break; \
49  type::Send(object); \
50 } while (false) \
51 
52 #define BGP_UVE_SEND2(type, object, arg) \
53 do { \
54  if (LoggingDisabled()) break; \
55  type::Send(object, arg); \
56 } while (false) \
57 
58 // Base macro to log and/or trace BGP messages
59 #define BGP_LOG(obj, level, flags, ...) \
60 do { \
61  if (LoggingDisabled()) break; \
62  if ((flags) & BGP_LOG_FLAG_SYSLOG) { \
63  obj##Log::Send("BGP", level, __FILE__, __LINE__, ##__VA_ARGS__); \
64  if (bgp_log_test::unit_test()) break; \
65  } \
66  if ((flags) & BGP_LOG_FLAG_TRACE) { \
67  obj##Trace::TraceMsg(BgpTraceBuf, __FILE__, __LINE__, \
68  ##__VA_ARGS__); \
69  } \
70 } while (false)
71 
72 // Base macro to log and/or trace BGP messages
73 #define BGP_LOG_NOTICE(obj, flags, ...) \
74 do { \
75  if (LoggingDisabled()) break; \
76  if ((flags) & BGP_LOG_FLAG_SYSLOG) { \
77  obj##Notice##Log::Send("BGP", SandeshLevel::SYS_NOTICE, __FILE__, \
78  __LINE__, ##__VA_ARGS__); \
79  if (bgp_log_test::unit_test()) break; \
80  } \
81  if ((flags) & BGP_LOG_FLAG_TRACE) { \
82  const std::string __trace_buf(BGP_TRACE_BUF); \
83  obj##Notice##Trace::TraceMsg(BgpTraceBuf, __FILE__, __LINE__, \
84  ##__VA_ARGS__); \
85  } \
86 } while (false)
87 
88 // Base macro to log and/or trace BGP messages with C++ string as last arg
89 #define BGP_LOG_STR(obj, level, flags, arg) \
90 do { \
91  if (LoggingDisabled()) break; \
92  std::ostringstream _os; \
93  _os << arg; \
94  BGP_LOG(obj, level, flags, _os.str()); \
95 } while (false)
96 
97 // Base macro to log and/or trace BGP error messages with C++ string as last arg
98 #define BGP_LOG_ERROR_STR(obj, level, flags, arg) \
99 do { \
100  if (LoggingDisabled()) break; \
101  std::ostringstream _os; \
102  _os << arg; \
103  BGP_LOG(obj##Error, SandeshLevel::SYS_ERR, flags, _os.str()); \
104 } while (false)
105 
106 // Base macro to log and/or trace BGP warn messages with C++ string as last arg
107 #define BGP_LOG_WARNING_STR(obj, flags, arg) \
108 do { \
109  if (LoggingDisabled()) break; \
110  std::ostringstream _os; \
111  _os << arg; \
112  BGP_LOG(obj##Warning, SandeshLevel::SYS_WARN, flags, _os.str()); \
113 } while (false)
114 
115 // Base macro to log and/or trace BGP warn messages with C++ string as last arg
116 #define BGP_LOG_NOTICE_STR(obj, flags, arg) \
117 do { \
118  if (LoggingDisabled()) break; \
119  std::ostringstream _os; \
120  _os << arg; \
121  BGP_LOG(obj##Notice, SandeshLevel::SYS_NOTICE, flags, _os.str()); \
122 } while (false)
123 
124 // Log BgpServer information if available
125 //
126 // XXX Only used in unit tests. In production, there is only one BgpServer per
127 // control-node daemon
128 #define BGP_LOG_SERVER(peer, table) \
129  if (LoggingDisabled()) break; \
130  if (dynamic_cast<const IPeer *>(peer) && \
131  dynamic_cast<const IPeer *>(peer)->server() && \
132  dynamic_cast<const IPeer *>(peer)->server()->logging_disabled()) \
133  break; \
134  bgp_log_test::LogServerName(dynamic_cast<const IPeer *>(peer), \
135  dynamic_cast<const BgpTable *>(table)); \
136 
137 // BgpPeer specific logging macros
138 #define BGP_PEER_DIR_OUT "SEND"
139 #define BGP_PEER_DIR_IN "RECV"
140 #define BGP_PEER_DIR_NA ""
141 
142 #define BGP_LOG_PEER_INTERNAL(type, peer, level, flags, ...) \
143 do { \
144  const IPeer *_peer = dynamic_cast<const IPeer *>(peer); \
145  std::string _peer_name; \
146  if (_peer) { \
147  _peer_name = _peer->ToUVEKey(); \
148  } else { \
149  _peer_name = "Unknown"; \
150  } \
151  if (_peer && _peer->IsXmppPeer()) { \
152  BGP_LOG(XmppPeer ## type, level, flags, _peer_name, ##__VA_ARGS__);\
153  } else { \
154  BGP_LOG(BgpPeer ## type, level, flags, _peer_name, ##__VA_ARGS__); \
155  } \
156 } while (false)
157 
158 // Grab all the macro arguments
159 #define BGP_LOG_PEER(type, peer, level, flags, dir, arg) \
160 do { \
161  if (LoggingDisabled()) break; \
162  \
163  BGP_LOG_SERVER(peer, (BgpTable *) 0); \
164  std::ostringstream _os; \
165  _os << arg; \
166  BGP_LOG_PEER_INTERNAL(type, peer, level, flags, dir, _os.str()); \
167 } while (false)
168 
169 // Grab all the macro arguments
170 #define BGP_LOG_PEER_NOTICE(type, peer, flags, dir, arg) \
171 do { \
172  if (LoggingDisabled()) break; \
173  \
174  BGP_LOG_SERVER(peer, (BgpTable *) 0); \
175  std::ostringstream _os; \
176  _os << arg; \
177  BGP_LOG_PEER_INTERNAL(type##Notice, peer, SandeshLevel::SYS_NOTICE, \
178  flags, dir, _os.str()); \
179 } while (false)
180 
181 // Grab all the macro arguments
182 #define BGP_LOG_PEER_WARNING(type, peer, flags, dir, arg) \
183 do { \
184  if (LoggingDisabled()) break; \
185  \
186  BGP_LOG_SERVER(peer, (BgpTable *) 0); \
187  std::ostringstream _os; \
188  _os << arg; \
189  BGP_LOG_PEER_INTERNAL(type##Warning, peer, SandeshLevel::SYS_WARN, \
190  flags, dir, _os.str()); \
191 } while (false)
192 
193 // Grab all the macro arguments
194 #define BGP_LOG_PEER_CRITICAL(type, peer, flags, dir, arg) \
195 do { \
196  if (LoggingDisabled()) break; \
197  \
198  BGP_LOG_SERVER(peer, (BgpTable *) 0); \
199  std::ostringstream _os; \
200  _os << arg; \
201  BGP_LOG_PEER_INTERNAL(type##Critical, peer, SandeshLevel::SYS_CRIT, \
202  flags, dir, _os.str()); \
203 } while (false)
204 
205 #define BGP_LOG_PEER_TABLE(peer, level, flags, tbl, arg) \
206 do { \
207  if (LoggingDisabled()) break; \
208  BGP_LOG_SERVER(peer, tbl); \
209  std::ostringstream _os; \
210  _os << arg; \
211  BGP_LOG_PEER_INTERNAL(Table, peer, level, flags, BGP_PEER_DIR_NA, \
212  (tbl) ? (tbl)->name() : "", \
213  ((tbl) && (tbl)->routing_instance()) ? \
214  (tbl)->routing_instance()->name() : "", \
215  _os.str()); \
216 } while (false)
217 
218 #define BGP_LOG_PEER_INSTANCE(peer, instance, level, flags, arg) \
219 do { \
220  if (LoggingDisabled()) break; \
221  std::ostringstream _os; \
222  _os << arg; \
223  BGP_LOG_PEER_INTERNAL(Instance, peer, level, flags, BGP_PEER_DIR_NA, \
224  instance, _os.str()); \
225 } while (false)
226 
227 #define BGP_LOG_PEER_INSTANCE_WARNING(peer, instance, flags, arg) \
228 do { \
229  if (LoggingDisabled()) break; \
230  std::ostringstream _os; \
231  _os << arg; \
232  BGP_LOG_PEER_INTERNAL(Instance, peer, SandeshLevel::SYS_WARN, flags, \
233  BGP_PEER_DIR_NA, \
234  instance, _os.str()); \
235 } while (false)
236 
237 #define BGP_LOG_PEER_INSTANCE_CRITICAL(peer, instance, dir, flags, arg) \
238 do { \
239  if (LoggingDisabled()) break; \
240  std::ostringstream _os; \
241  _os << arg; \
242  BGP_LOG_PEER_INTERNAL(Instance, peer, SandeshLevel::SYS_CRIT, flags, \
243  dir, instance, _os.str()); \
244 } while (false)
245 
246 // Bgp Route specific logging macro
247 #define BGP_LOG_ROUTE(table, peer, route, arg) \
248 do { \
249  if (LoggingDisabled()) break; \
250  BGP_LOG_SERVER(peer, table); \
251  std::ostringstream _os; \
252  _os << arg; \
253  BGP_LOG_PEER_INTERNAL(Route, peer, SandeshLevel::SYS_DEBUG, \
254  BGP_LOG_FLAG_TRACE, BGP_PEER_DIR_NA, _os.str(), \
255  (route) ? (route)->ToString() : "", \
256  (table) ? (table)->name() : ""); \
257 } while (false)
258 
259 // Bgp Table specific logging macro
260 #define BGP_LOG_TABLE(table, level, flags, arg) \
261 do { \
262  if (LoggingDisabled()) break; \
263  BGP_LOG_SERVER((const IPeer *) 0, table); \
264  std::ostringstream _os; \
265  _os << arg; \
266  BGP_LOG(BgpTable, level, flags, (table) ? (table)->name() : "", \
267  _os.str()); \
268 } while (false)
269 
270 #define BGP_CONFIG_LOG_INTERNAL(type, server, level, flags, ...) \
271 do { \
272  if (LoggingDisabled()) break; \
273  if ((flags) & BGP_LOG_FLAG_SYSLOG) { \
274  bgp_log_test::LogServerName(server); \
275  BgpConfig##type##Log::Send("BGPConfig", level, \
276  __FILE__, __LINE__, ##__VA_ARGS__); \
277  } \
278  if ((flags) & BGP_LOG_FLAG_TRACE) { \
279  BgpConfig##type##Trace::TraceMsg(BgpTraceBuf, \
280  __FILE__, __LINE__, ##__VA_ARGS__); \
281  } \
282 } while (false)
283 
284 #define BGP_CONFIG_LOG_INSTANCE(type, server, rtinstance, level, flags, ...) \
285  BGP_CONFIG_LOG_INTERNAL(Instance##type, server, level, flags, \
286  (rtinstance)->name(), ##__VA_ARGS__);
287 
288 #define BGP_CONFIG_LOG_NEIGHBOR(type, server, neighbor, level, flags, ...) \
289  BGP_CONFIG_LOG_INTERNAL(Neighbor##type, server, level, flags, \
290  (neighbor)->name(), ##__VA_ARGS__);
291 
292 #define BGP_CONFIG_LOG_PEERING(type, server, peering, level, flags, ...) \
293  BGP_CONFIG_LOG_INTERNAL(Peering##type, server, level, flags, \
294  (peering)->name(), ##__VA_ARGS__);
295 
296 #define BGP_CONFIG_LOG_PROTOCOL(type, server, protocol, level, flags, ...) \
297  BGP_CONFIG_LOG_INTERNAL(Protocol##type, server, level, flags, \
298  (protocol)->instance()->name(), ##__VA_ARGS__);
299 
300 // BGP Trace macros.
301 
302 #define BGP_TRACE_PEER_OBJECT(peer, peer_info) \
303 do { \
304  if (LoggingDisabled()) break; \
305  BGP_LOG_SERVER(peer, (BgpTable *) 0); \
306  BgpPeerObjectTrace::TraceMsg(BgpPeerObjectTraceBuf, __FILE__, __LINE__,\
307  peer_info); \
308 } while (false)
309 
310 #define BGP_TRACE_PEER_PACKET(peer, msg, size, level) \
311 do { \
312  if (LoggingDisabled()) break; \
313  if ((level) > Sandesh::LoggingLevel()) break; \
314  BgpPeerInfo peer_info; \
315  \
316  BGP_LOG_SERVER(peer, (BgpTable *) 0); \
317  (peer)->state_machine()->SetDataCollectionKey(&peer_info); \
318  peer_info.set_packet_data((peer)->BytesToHexString(msg, size)); \
319  BGP_TRACE_PEER_OBJECT(peer, peer_info); \
320 } while (false)
321 
322 #endif // ctrlplane_bgp_log_h
void init()
Definition: bgp_log.cc:37
SandeshTraceBufferPtr BgpTraceBuf
void LogServerName(const BgpServer *server)
Definition: bgp_log.cc:57
boost::shared_ptr< TraceBuffer< SandeshTrace > > SandeshTraceBufferPtr
Definition: sandesh_trace.h:18
Definition: ipeer.h:186
bool unit_test()
Definition: bgp_log.cc:53
SandeshTraceBufferPtr BgpPeerObjectTraceBuf