A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
log.h
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2006,2007 INRIA
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  */
20 
21 #ifndef LOG_H
22 #define LOG_H
23 
24 #include <string>
25 #include <iostream>
26 #include <stdint.h>
27 
28 namespace ns3 {
29 
30 enum LogLevel {
31  LOG_NONE = 0x00000000, // no logging
32 
33  LOG_ERROR = 0x00000001, // serious error messages only
34  LOG_LEVEL_ERROR = 0x00000001,
35 
36  LOG_WARN = 0x00000002, // warning messages
37  LOG_LEVEL_WARN = 0x00000003,
38 
39  LOG_DEBUG = 0x00000004, // rare ad-hoc debug messages
40  LOG_LEVEL_DEBUG = 0x00000007,
41 
42  LOG_INFO = 0x00000008, // informational messages (e.g., banners)
43  LOG_LEVEL_INFO = 0x0000000f,
44 
45  LOG_FUNCTION = 0x00000010, // function tracing
46  LOG_LEVEL_FUNCTION = 0x0000001f,
47 
48  LOG_LOGIC = 0x00000020, // control flow tracing within functions
49  LOG_LEVEL_LOGIC = 0x0000003f,
50 
51  LOG_ALL = 0x1fffffff, // print everything
53 
54  LOG_PREFIX_FUNC = 0x80000000, // prefix all trace prints with function
55  LOG_PREFIX_TIME = 0x40000000, // prefix all trace prints with simulation time
56  LOG_PREFIX_NODE = 0x20000000 // prefix all trace prints with simulation node
57 };
58 
71 void LogComponentEnable (char const *name, enum LogLevel level);
72 
82 void LogComponentEnableAll (enum LogLevel level);
83 
84 
94 void LogComponentDisable (char const *name, enum LogLevel level);
95 
102 void LogComponentDisableAll (enum LogLevel level);
103 
104 
105 } // namespace ns3
106 
119 #define NS_LOG_COMPONENT_DEFINE(name) \
120  static ns3::LogComponent g_log = ns3::LogComponent (name)
121 
122 #define NS_LOG_APPEND_TIME_PREFIX \
123  if (g_log.IsEnabled (ns3::LOG_PREFIX_TIME)) \
124  { \
125  ns3::LogTimePrinter printer = ns3::LogGetTimePrinter (); \
126  if (printer != 0) \
127  { \
128  (*printer)(std::clog); \
129  std::clog << " "; \
130  } \
131  }
132 
133 #define NS_LOG_APPEND_NODE_PREFIX \
134  if (g_log.IsEnabled (ns3::LOG_PREFIX_NODE)) \
135  { \
136  ns3::LogNodePrinter printer = ns3::LogGetNodePrinter (); \
137  if (printer != 0) \
138  { \
139  (*printer)(std::clog); \
140  std::clog << " "; \
141  } \
142  }
143 
144 #define NS_LOG_APPEND_FUNC_PREFIX \
145  if (g_log.IsEnabled (ns3::LOG_PREFIX_FUNC)) \
146  { \
147  std::clog << g_log.Name () << ":" << \
148  __FUNCTION__ << "(): "; \
149  } \
150 
151 #ifndef NS_LOG_APPEND_CONTEXT
152 #define NS_LOG_APPEND_CONTEXT
153 #endif /* NS_LOG_APPEND_CONTEXT */
154 
155 
156 
157 #ifdef NS3_LOG_ENABLE
158 
159 
206 #define NS_LOG(level, msg) \
207  do \
208  { \
209  if (g_log.IsEnabled (level)) \
210  { \
211  NS_LOG_APPEND_TIME_PREFIX; \
212  NS_LOG_APPEND_NODE_PREFIX; \
213  NS_LOG_APPEND_CONTEXT; \
214  NS_LOG_APPEND_FUNC_PREFIX; \
215  std::clog << msg << std::endl; \
216  } \
217  } \
218  while (false)
219 
226 #define NS_LOG_ERROR(msg) \
227  NS_LOG (ns3::LOG_ERROR, msg)
228 
235 #define NS_LOG_WARN(msg) \
236  NS_LOG (ns3::LOG_WARN, msg)
237 
244 #define NS_LOG_DEBUG(msg) \
245  NS_LOG (ns3::LOG_DEBUG, msg)
246 
253 #define NS_LOG_INFO(msg) \
254  NS_LOG (ns3::LOG_INFO, msg)
255 
264 #define NS_LOG_FUNCTION_NOARGS() \
265  do \
266  { \
267  if (g_log.IsEnabled (ns3::LOG_FUNCTION)) \
268  { \
269  NS_LOG_APPEND_TIME_PREFIX; \
270  NS_LOG_APPEND_NODE_PREFIX; \
271  NS_LOG_APPEND_CONTEXT; \
272  std::clog << g_log.Name () << ":" \
273  << __FUNCTION__ << "()" << std::endl; \
274  } \
275  } \
276  while (false)
277 
278 
300 #define NS_LOG_FUNCTION(parameters) \
301  do \
302  { \
303  if (g_log.IsEnabled (ns3::LOG_FUNCTION)) \
304  { \
305  NS_LOG_APPEND_TIME_PREFIX; \
306  NS_LOG_APPEND_NODE_PREFIX; \
307  NS_LOG_APPEND_CONTEXT; \
308  std::clog << g_log.Name () << ":" \
309  << __FUNCTION__ << "("; \
310  ns3::ParameterLogger (std::clog) << parameters; \
311  std::clog << ")" << std::endl; \
312  } \
313  } \
314  while (false)
315 
316 
323 #define NS_LOG_LOGIC(msg) \
324  NS_LOG (ns3::LOG_LOGIC, msg)
325 
332 #define NS_LOG_UNCOND(msg) \
333  do \
334  { \
335  std::clog << msg << std::endl; \
336  } \
337  while (false)
338 
339 #else /* LOG_ENABLE */
340 
341 #define NS_LOG(level, msg)
342 #define NS_LOG_ERROR(msg)
343 #define NS_LOG_WARN(msg)
344 #define NS_LOG_DEBUG(msg)
345 #define NS_LOG_INFO(msg)
346 #define NS_LOG_FUNCTION_NOARGS()
347 #define NS_LOG_FUNCTION(msg)
348 #define NS_LOG_LOGIC(msg)
349 #define NS_LOG_UNCOND(msg)
350 
351 #endif /* LOG_ENABLE */
352 
353 namespace ns3 {
354 
362 void LogComponentPrintList (void);
363 
364 typedef void (*LogTimePrinter)(std::ostream &os);
365 typedef void (*LogNodePrinter)(std::ostream &os);
366 
369 
372 
373 
375 public:
376  LogComponent (char const *name);
377  void EnvVarCheck (char const *name);
378  bool IsEnabled (enum LogLevel level) const;
379  bool IsNoneEnabled (void) const;
380  void Enable (enum LogLevel level);
381  void Disable (enum LogLevel level);
382  char const *Name (void) const;
383 private:
384  int32_t m_levels;
385  char const *m_name;
386 };
387 
388 class ParameterLogger : public std::ostream
389 {
391  std::ostream &m_os;
392 public:
393  ParameterLogger (std::ostream &os);
394 
395  template<typename T>
397  {
398  switch (m_itemNumber)
399  {
400  case 0: // first parameter
401  m_os << param;
402  break;
403  default: // parameter following a previous parameter
404  m_os << ", " << param;
405  break;
406  }
407  m_itemNumber++;
408  return *this;
409  }
410 };
411 
412 } // namespace ns3
413 
414 
415 #endif /* LOG_H */