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 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 
68 enum LogLevel {
69  LOG_NONE = 0x00000000, // no logging
70 
71  LOG_ERROR = 0x00000001, // serious error messages only
72  LOG_LEVEL_ERROR = 0x00000001,
73 
74  LOG_WARN = 0x00000002, // warning messages
75  LOG_LEVEL_WARN = 0x00000003,
76 
77  LOG_DEBUG = 0x00000004, // rare ad-hoc debug messages
78  LOG_LEVEL_DEBUG = 0x00000007,
79 
80  LOG_INFO = 0x00000008, // informational messages (e.g., banners)
81  LOG_LEVEL_INFO = 0x0000000f,
82 
83  LOG_FUNCTION = 0x00000010, // function tracing
84  LOG_LEVEL_FUNCTION = 0x0000001f,
85 
86  LOG_LOGIC = 0x00000020, // control flow tracing within functions
87  LOG_LEVEL_LOGIC = 0x0000003f,
88 
89  LOG_ALL = 0x0fffffff, // print everything
91 
92  LOG_PREFIX_FUNC = 0x80000000, // prefix all trace prints with function
93  LOG_PREFIX_TIME = 0x40000000, // prefix all trace prints with simulation time
94  LOG_PREFIX_NODE = 0x20000000, // prefix all trace prints with simulation node
95  LOG_PREFIX_LEVEL = 0x10000000, // prefix all trace prints with log level (severity)
96  LOG_PREFIX_ALL = 0xf0000000 // all prefixes
97 };
98 
111 void LogComponentEnable (char const *name, enum LogLevel level);
112 
122 void LogComponentEnableAll (enum LogLevel level);
123 
124 
134 void LogComponentDisable (char const *name, enum LogLevel level);
135 
142 void LogComponentDisableAll (enum LogLevel level);
143 
144 
145 } // namespace ns3
146 
171 #define NS_LOG_COMPONENT_DEFINE(name) \
172  static ns3::LogComponent g_log = ns3::LogComponent (name)
173 
178 #define NS_LOG_APPEND_TIME_PREFIX \
179  if (g_log.IsEnabled (ns3::LOG_PREFIX_TIME)) \
180  { \
181  ns3::LogTimePrinter printer = ns3::LogGetTimePrinter (); \
182  if (printer != 0) \
183  { \
184  (*printer)(std::clog); \
185  std::clog << " "; \
186  } \
187  }
188 
193 #define NS_LOG_APPEND_NODE_PREFIX \
194  if (g_log.IsEnabled (ns3::LOG_PREFIX_NODE)) \
195  { \
196  ns3::LogNodePrinter printer = ns3::LogGetNodePrinter (); \
197  if (printer != 0) \
198  { \
199  (*printer)(std::clog); \
200  std::clog << " "; \
201  } \
202  }
203 
208 #define NS_LOG_APPEND_FUNC_PREFIX \
209  if (g_log.IsEnabled (ns3::LOG_PREFIX_FUNC)) \
210  { \
211  std::clog << g_log.Name () << ":" << \
212  __FUNCTION__ << "(): "; \
213  } \
214 
215 
219 #define NS_LOG_APPEND_LEVEL_PREFIX(level) \
220  if (g_log.IsEnabled (ns3::LOG_PREFIX_LEVEL)) \
221  { \
222  std::clog << "[" << g_log.GetLevelLabel (level) << "] "; \
223  } \
224 
225 
226 #ifndef NS_LOG_APPEND_CONTEXT
227 #define NS_LOG_APPEND_CONTEXT
228 #endif /* NS_LOG_APPEND_CONTEXT */
229 
230 
231 
232 #ifdef NS3_LOG_ENABLE
233 
234 
235 
250 #define NS_LOG(level, msg) \
251  do \
252  { \
253  if (g_log.IsEnabled (level)) \
254  { \
255  NS_LOG_APPEND_TIME_PREFIX; \
256  NS_LOG_APPEND_NODE_PREFIX; \
257  NS_LOG_APPEND_CONTEXT; \
258  NS_LOG_APPEND_FUNC_PREFIX; \
259  NS_LOG_APPEND_LEVEL_PREFIX (level); \
260  std::clog << msg << std::endl; \
261  } \
262  } \
263  while (false)
264 
271 #define NS_LOG_ERROR(msg) \
272  NS_LOG (ns3::LOG_ERROR, msg)
273 
280 #define NS_LOG_WARN(msg) \
281  NS_LOG (ns3::LOG_WARN, msg)
282 
289 #define NS_LOG_DEBUG(msg) \
290  NS_LOG (ns3::LOG_DEBUG, msg)
291 
298 #define NS_LOG_INFO(msg) \
299  NS_LOG (ns3::LOG_INFO, msg)
300 
309 #define NS_LOG_FUNCTION_NOARGS() \
310  do \
311  { \
312  if (g_log.IsEnabled (ns3::LOG_FUNCTION)) \
313  { \
314  NS_LOG_APPEND_TIME_PREFIX; \
315  NS_LOG_APPEND_NODE_PREFIX; \
316  NS_LOG_APPEND_CONTEXT; \
317  std::clog << g_log.Name () << ":" \
318  << __FUNCTION__ << "()" << std::endl; \
319  } \
320  } \
321  while (false)
322 
323 
345 #define NS_LOG_FUNCTION(parameters) \
346  do \
347  { \
348  if (g_log.IsEnabled (ns3::LOG_FUNCTION)) \
349  { \
350  NS_LOG_APPEND_TIME_PREFIX; \
351  NS_LOG_APPEND_NODE_PREFIX; \
352  NS_LOG_APPEND_CONTEXT; \
353  std::clog << g_log.Name () << ":" \
354  << __FUNCTION__ << "("; \
355  ns3::ParameterLogger (std::clog) << parameters; \
356  std::clog << ")" << std::endl; \
357  } \
358  } \
359  while (false)
360 
361 
368 #define NS_LOG_LOGIC(msg) \
369  NS_LOG (ns3::LOG_LOGIC, msg)
370 
377 #define NS_LOG_UNCOND(msg) \
378  do \
379  { \
380  std::clog << msg << std::endl; \
381  } \
382  while (false)
383 
384 #else /* LOG_ENABLE */
385 
386 #define NS_LOG(level, msg)
387 #define NS_LOG_ERROR(msg)
388 #define NS_LOG_WARN(msg)
389 #define NS_LOG_DEBUG(msg)
390 #define NS_LOG_INFO(msg)
391 #define NS_LOG_FUNCTION_NOARGS()
392 #define NS_LOG_FUNCTION(msg)
393 #define NS_LOG_LOGIC(msg)
394 #define NS_LOG_UNCOND(msg)
395 
396 #endif /* LOG_ENABLE */
397 
398 namespace ns3 {
399 
407 void LogComponentPrintList (void);
408 
409 typedef void (*LogTimePrinter)(std::ostream &os);
410 typedef void (*LogNodePrinter)(std::ostream &os);
411 
414 
417 
418 
420 public:
421  LogComponent (const std::string & name);
422  void EnvVarCheck (const std::string & name);
423  bool IsEnabled (enum LogLevel level) const;
424  bool IsNoneEnabled (void) const;
425  void Enable (enum LogLevel level);
426  void Disable (enum LogLevel level);
427  char const *Name (void) const;
428  std::string GetLevelLabel(const enum LogLevel level) const;
429 private:
430  int32_t m_levels;
431  std::string m_name;
432 };
433 
434 class ParameterLogger : public std::ostream
435 {
437  std::ostream &m_os;
438 public:
439  ParameterLogger (std::ostream &os);
440 
441  template<typename T>
443  {
444  switch (m_itemNumber)
445  {
446  case 0: // first parameter
447  m_os << param;
448  break;
449  default: // parameter following a previous parameter
450  m_os << ", " << param;
451  break;
452  }
453  m_itemNumber++;
454  return *this;
455  }
456 };
457 
458 } // namespace ns3
459 
460 
461 #endif /* NS3_LOG_H */
void LogComponentEnableAll(enum LogLevel level)
Definition: log.cc:335
void LogComponentDisableAll(enum LogLevel level)
Definition: log.cc:363
void LogSetNodePrinter(LogNodePrinter printer)
Definition: log.cc:572
LogComponent(const std::string &name)
Definition: log.cc:87
std::ostream & m_os
Definition: log.h:437
ParameterLogger & operator<<(T param)
Definition: log.h:442
bool IsEnabled(enum LogLevel level) const
Definition: log.cc:245
ParameterLogger(std::ostream &os)
Definition: log.cc:582
void Disable(enum LogLevel level)
Definition: log.cc:264
void LogSetTimePrinter(LogTimePrinter printer)
Definition: log.cc:558
LogNodePrinter LogGetNodePrinter(void)
Definition: log.cc:576
std::string GetLevelLabel(const enum LogLevel level) const
Definition: log.cc:276
void LogComponentDisable(char const *name, enum LogLevel level)
Definition: log.cc:347
int32_t m_levels
Definition: log.h:430
void EnvVarCheck(const std::string &name)
Definition: log.cc:106
void(* LogTimePrinter)(std::ostream &os)
Definition: log.h:409
bool IsNoneEnabled(void) const
Definition: log.cc:252
void(* LogNodePrinter)(std::ostream &os)
Definition: log.h:410
LogLevel
Logging severity classes and levels.
Definition: log.h:68
char const * Name(void) const
Definition: log.cc:270
LogTimePrinter LogGetTimePrinter(void)
Definition: log.cc:567
void LogComponentPrintList(void)
Print the list of logging messages available.
Definition: log.cc:375
void Enable(enum LogLevel level)
Definition: log.cc:258
std::string m_name
Definition: log.h:431
void LogComponentEnable(char const *name, enum LogLevel level)
Definition: log.cc:311