A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
log.h
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 NS3_LOG_H
22 #define NS3_LOG_H
23 
24 #include <string>
25 #include <iostream>
26 #include <stdint.h>
27 #include <map>
28 
29 namespace ns3 {
30 
31 enum LogLevel {
32  LOG_NONE = 0x00000000, // no logging
33 
34  LOG_ERROR = 0x00000001, // serious error messages only
35  LOG_LEVEL_ERROR = 0x00000001,
36 
37  LOG_WARN = 0x00000002, // warning messages
38  LOG_LEVEL_WARN = 0x00000003,
39 
40  LOG_DEBUG = 0x00000004, // rare ad-hoc debug messages
41  LOG_LEVEL_DEBUG = 0x00000007,
42 
43  LOG_INFO = 0x00000008, // informational messages (e.g., banners)
44  LOG_LEVEL_INFO = 0x0000000f,
45 
46  LOG_FUNCTION = 0x00000010, // function tracing
47  LOG_LEVEL_FUNCTION = 0x0000001f,
48 
49  LOG_LOGIC = 0x00000020, // control flow tracing within functions
50  LOG_LEVEL_LOGIC = 0x0000003f,
51 
52  LOG_ALL = 0x0fffffff, // print everything
53  LOG_LEVEL_ALL = LOG_ALL,
54 
55  LOG_PREFIX_FUNC = 0x80000000, // prefix all trace prints with function
56  LOG_PREFIX_TIME = 0x40000000, // prefix all trace prints with simulation time
57  LOG_PREFIX_NODE = 0x20000000, // prefix all trace prints with simulation node
58  LOG_PREFIX_LEVEL = 0x10000000, // prefix all trace prints with log level (severity)
59  LOG_PREFIX_ALL = 0xf0000000 // all prefixes
60 };
61 
74 void LogComponentEnable (char const *name, enum LogLevel level);
75 
85 void LogComponentEnableAll (enum LogLevel level);
86 
87 
97 void LogComponentDisable (char const *name, enum LogLevel level);
98 
105 void LogComponentDisableAll (enum LogLevel level);
106 
107 
108 } // namespace ns3
109 
122 #define NS_LOG_COMPONENT_DEFINE(name) \
123  static ns3::LogComponent g_log = ns3::LogComponent (name)
124 
125 #define NS_LOG_APPEND_TIME_PREFIX \
126  if (g_log.IsEnabled (ns3::LOG_PREFIX_TIME)) \
127  { \
128  ns3::LogTimePrinter printer = ns3::LogGetTimePrinter (); \
129  if (printer != 0) \
130  { \
131  (*printer)(std::clog); \
132  std::clog << " "; \
133  } \
134  }
135 
136 #define NS_LOG_APPEND_NODE_PREFIX \
137  if (g_log.IsEnabled (ns3::LOG_PREFIX_NODE)) \
138  { \
139  ns3::LogNodePrinter printer = ns3::LogGetNodePrinter (); \
140  if (printer != 0) \
141  { \
142  (*printer)(std::clog); \
143  std::clog << " "; \
144  } \
145  }
146 
147 #define NS_LOG_APPEND_FUNC_PREFIX \
148  if (g_log.IsEnabled (ns3::LOG_PREFIX_FUNC)) \
149  { \
150  std::clog << g_log.Name () << ":" << \
151  __FUNCTION__ << "(): "; \
152  } \
153 
154 #define NS_LOG_APPEND_LEVEL_PREFIX(level) \
155  if (g_log.IsEnabled (ns3::LOG_PREFIX_LEVEL)) \
156  { \
157  std::clog << "[" << g_log.GetLevelLabel (level) << "] "; \
158  } \
159 
160 
161 #ifndef NS_LOG_APPEND_CONTEXT
162 #define NS_LOG_APPEND_CONTEXT
163 #endif /* NS_LOG_APPEND_CONTEXT */
164 
165 
166 
167 #ifdef NS3_LOG_ENABLE
168 
169 
216 #define NS_LOG(level, msg) \
217  do \
218  { \
219  if (g_log.IsEnabled (level)) \
220  { \
221  NS_LOG_APPEND_TIME_PREFIX; \
222  NS_LOG_APPEND_NODE_PREFIX; \
223  NS_LOG_APPEND_CONTEXT; \
224  NS_LOG_APPEND_FUNC_PREFIX; \
225  NS_LOG_APPEND_LEVEL_PREFIX (level); \
226  std::clog << msg << std::endl; \
227  } \
228  } \
229  while (false)
230 
237 #define NS_LOG_ERROR(msg) \
238  NS_LOG (ns3::LOG_ERROR, msg)
239 
246 #define NS_LOG_WARN(msg) \
247  NS_LOG (ns3::LOG_WARN, msg)
248 
255 #define NS_LOG_DEBUG(msg) \
256  NS_LOG (ns3::LOG_DEBUG, msg)
257 
264 #define NS_LOG_INFO(msg) \
265  NS_LOG (ns3::LOG_INFO, msg)
266 
275 #define NS_LOG_FUNCTION_NOARGS() \
276  do \
277  { \
278  if (g_log.IsEnabled (ns3::LOG_FUNCTION)) \
279  { \
280  NS_LOG_APPEND_TIME_PREFIX; \
281  NS_LOG_APPEND_NODE_PREFIX; \
282  NS_LOG_APPEND_CONTEXT; \
283  std::clog << g_log.Name () << ":" \
284  << __FUNCTION__ << "()" << std::endl; \
285  } \
286  } \
287  while (false)
288 
289 
311 #define NS_LOG_FUNCTION(parameters) \
312  do \
313  { \
314  if (g_log.IsEnabled (ns3::LOG_FUNCTION)) \
315  { \
316  NS_LOG_APPEND_TIME_PREFIX; \
317  NS_LOG_APPEND_NODE_PREFIX; \
318  NS_LOG_APPEND_CONTEXT; \
319  std::clog << g_log.Name () << ":" \
320  << __FUNCTION__ << "("; \
321  ns3::ParameterLogger (std::clog) << parameters; \
322  std::clog << ")" << std::endl; \
323  } \
324  } \
325  while (false)
326 
327 
334 #define NS_LOG_LOGIC(msg) \
335  NS_LOG (ns3::LOG_LOGIC, msg)
336 
343 #define NS_LOG_UNCOND(msg) \
344  do \
345  { \
346  std::clog << msg << std::endl; \
347  } \
348  while (false)
349 
350 #else /* LOG_ENABLE */
351 
352 #define NS_LOG(level, msg)
353 #define NS_LOG_ERROR(msg)
354 #define NS_LOG_WARN(msg)
355 #define NS_LOG_DEBUG(msg)
356 #define NS_LOG_INFO(msg)
357 #define NS_LOG_FUNCTION_NOARGS()
358 #define NS_LOG_FUNCTION(msg)
359 #define NS_LOG_LOGIC(msg)
360 #define NS_LOG_UNCOND(msg)
361 
362 #endif /* LOG_ENABLE */
363 
364 namespace ns3 {
365 
373 void LogComponentPrintList (void);
374 
375 typedef void (*LogTimePrinter)(std::ostream &os);
376 typedef void (*LogNodePrinter)(std::ostream &os);
377 
378 void LogSetTimePrinter (LogTimePrinter);
379 LogTimePrinter LogGetTimePrinter (void);
380 
381 void LogSetNodePrinter (LogNodePrinter);
382 LogNodePrinter LogGetNodePrinter (void);
383 
384 
386 public:
387  LogComponent (char const *name);
388  void EnvVarCheck (char const *name);
389  bool IsEnabled (enum LogLevel level) const;
390  bool IsNoneEnabled (void) const;
391  void Enable (enum LogLevel level);
392  void Disable (enum LogLevel level);
393  char const *Name (void) const;
394  std::string GetLevelLabel(const enum LogLevel level) const;
395 private:
396  int32_t m_levels;
397  char const *m_name;
398 };
399 
400 class ParameterLogger : public std::ostream
401 {
402  int m_itemNumber;
403  std::ostream &m_os;
404 public:
405  ParameterLogger (std::ostream &os);
406 
407  template<typename T>
408  ParameterLogger& operator<< (T param)
409  {
410  switch (m_itemNumber)
411  {
412  case 0: // first parameter
413  m_os << param;
414  break;
415  default: // parameter following a previous parameter
416  m_os << ", " << param;
417  break;
418  }
419  m_itemNumber++;
420  return *this;
421  }
422 };
423 
424 } // namespace ns3
425 
426 
427 #endif /* NS3_LOG_H */
void LogComponentEnableAll(enum LogLevel level)
Definition: log.cc:335
void LogComponentDisableAll(enum LogLevel level)
Definition: log.cc:363
void LogComponentDisable(char const *name, enum LogLevel level)
Definition: log.cc:347
void LogComponentPrintList(void)
Definition: log.cc:375
void LogComponentEnable(char const *name, enum LogLevel level)
Definition: log.cc:311