A Discrete-Event Network Simulator
API
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 #include "log-macros-enabled.h"
30 #include "log-macros-disabled.h"
31 
85 namespace ns3 {
86 
90 enum LogLevel {
91  LOG_NONE = 0x00000000,
92 
93  LOG_ERROR = 0x00000001,
94  LOG_LEVEL_ERROR = 0x00000001,
95 
96  LOG_WARN = 0x00000002,
97  LOG_LEVEL_WARN = 0x00000003,
98 
99  LOG_DEBUG = 0x00000004,
100  LOG_LEVEL_DEBUG = 0x00000007,
101 
102  LOG_INFO = 0x00000008,
103  LOG_LEVEL_INFO = 0x0000000f,
104 
105  LOG_FUNCTION = 0x00000010,
106  LOG_LEVEL_FUNCTION = 0x0000001f,
107 
108  LOG_LOGIC = 0x00000020,
109  LOG_LEVEL_LOGIC = 0x0000003f,
110 
111  LOG_ALL = 0x0fffffff,
113 
114  LOG_PREFIX_FUNC = 0x80000000,
115  LOG_PREFIX_TIME = 0x40000000,
116  LOG_PREFIX_NODE = 0x20000000,
117  LOG_PREFIX_LEVEL = 0x10000000,
118  LOG_PREFIX_ALL = 0xf0000000
119 };
120 
133 void LogComponentEnable (char const *name, enum LogLevel level);
134 
143 void LogComponentEnableAll (enum LogLevel level);
144 
145 
155 void LogComponentDisable (char const *name, enum LogLevel level);
156 
162 void LogComponentDisableAll (enum LogLevel level);
163 
164 
165 } // namespace ns3
166 
167 
201 #define NS_LOG_COMPONENT_DEFINE(name) \
202  static ns3::LogComponent g_log = ns3::LogComponent (name, __FILE__)
203 
212 #define NS_LOG_COMPONENT_DEFINE_MASK(name, mask) \
213  static ns3::LogComponent g_log = ns3::LogComponent (name, __FILE__, mask)
214 
220 #define NS_LOG_ERROR(msg) \
221  NS_LOG (ns3::LOG_ERROR, msg)
222 
228 #define NS_LOG_WARN(msg) \
229  NS_LOG (ns3::LOG_WARN, msg)
230 
236 #define NS_LOG_DEBUG(msg) \
237  NS_LOG (ns3::LOG_DEBUG, msg)
238 
244 #define NS_LOG_INFO(msg) \
245  NS_LOG (ns3::LOG_INFO, msg)
246 
252 #define NS_LOG_LOGIC(msg) \
253  NS_LOG (ns3::LOG_LOGIC, msg)
254 
255 
256 namespace ns3 {
257 
263 void LogComponentPrintList (void);
264 
271 typedef void (*LogTimePrinter)(std::ostream &os);
278 typedef void (*LogNodePrinter)(std::ostream &os);
279 
292 
305 
306 
311 {
312 public:
322  LogComponent (const std::string & name,
323  const std::string & file,
324  const enum LogLevel mask = LOG_NONE);
331  bool IsEnabled (const enum LogLevel level) const;
337  bool IsNoneEnabled (void) const;
343  void Enable (const enum LogLevel level);
349  void Disable (const enum LogLevel level);
355  char const *Name (void) const;
360  std::string File (void) const;
367  static std::string GetLevelLabel(const enum LogLevel level);
373  void SetMask (const enum LogLevel level);
374 
383  typedef std::map<std::string, LogComponent *> ComponentList;
384 
395  static ComponentList *GetComponentList (void);
396 
397 
398 private:
403  void EnvVarCheck (void);
404 
405  int32_t m_levels;
406  int32_t m_mask;
407  std::string m_name;
408  std::string m_file;
409 
410 }; // class LogComponent
411 
412 
417 {
418  bool m_first;
419  std::ostream &m_os;
420 public:
426  ParameterLogger (std::ostream &os);
427 
435  template<typename T>
436  ParameterLogger& operator<< (T param);
437 
438 };
439 
440 template<typename T>
443 {
444  if (m_first)
445  {
446  m_os << param;
447  m_first = false;
448  }
449  else
450  {
451  m_os << ", " << param;
452  }
453  return *this;
454 }
455 
461 template<>
463 ParameterLogger::operator<< <std::string>(const std::string param);
464 
470 template<>
472 ParameterLogger::operator<< <const char *>(const char * param);
473 
474 } // namespace ns3
475  // \ingroup logging
477 
478 #endif /* NS3_LOG_H */
LOG_LOGIC and above.
Definition: log.h:109
LogComponent(const std::string &name, const std::string &file, const enum LogLevel mask=LOG_NONE)
Constructor.
Definition: log.cc:113
ParameterLogger & operator<<(T param)
Write a function parameter on the output stream, separating parameters after the first by ...
Definition: log.h:442
void LogComponentDisable(char const *name, enum LogLevel level)
Disable the logging output associated with that log component.
Definition: log.cc:387
std::ostream & m_os
Underlying output stream.
Definition: log.h:419
No logging.
Definition: log.h:91
bool m_first
First argument flag, doesn't get ,.
Definition: log.h:418
A single log component configuration.
Definition: log.h:310
ParameterLogger(std::ostream &os)
Constructor.
Definition: log.cc:635
LOG_ERROR and above.
Definition: log.h:94
void Disable(const enum LogLevel level)
Disable logging at level for this LogComponent.
Definition: log.cc:297
void(* LogTimePrinter)(std::ostream &os)
Function signature for prepending the simulation time to a log message.
Definition: log.h:271
LOG_INFO and above.
Definition: log.h:103
void EnvVarCheck(void)
Parse the NS_LOG environment variable for options relating to this LogComponent.
Definition: log.cc:134
void(* LogNodePrinter)(std::ostream &os)
Function signature for prepending the node id to a log message.
Definition: log.h:278
LOG_FUNCTION and above.
Definition: log.h:106
std::string m_file
File defining this LogComponent.
Definition: log.h:408
Rare ad-hoc debug messages.
Definition: log.h:99
Control flow tracing within functions.
Definition: log.h:108
Insert , when streaming function arguments.
Definition: log.h:416
Serious error messages only.
Definition: log.h:93
void LogComponentEnable(char const *name, enum LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:351
void Enable(const enum LogLevel level)
Enable this LogComponent at level.
Definition: log.cc:291
bool IsEnabled(const enum LogLevel level) const
Check if this LogComponent is enabled for level.
Definition: log.cc:272
void LogSetNodePrinter(LogNodePrinter printer)
Set the LogNodePrinter function to be used to prepend log messages with the node id.
Definition: log.cc:625
void SetMask(const enum LogLevel level)
Prevent the enabling of a specific LogLevel.
Definition: log.cc:285
void LogComponentPrintList(void)
Print the list of logging messages available.
Definition: log.cc:415
LogNodePrinter LogGetNodePrinter(void)
Get the LogNodePrinter function currently in use.
Definition: log.cc:629
Warning messages.
Definition: log.h:96
LOG_WARN and above.
Definition: log.h:97
Print everything.
Definition: log.h:111
Definition of empty logging macros and the NS_LOG_NOOP_INTERNAL macro.
std::map< std::string, LogComponent * > ComponentList
LogComponent name map.
Definition: log.h:383
LOG_DEBUG and above.
Definition: log.h:100
void LogComponentEnableAll(enum LogLevel level)
Enable the logging output for all registered log components.
Definition: log.cc:375
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Prefix all trace prints with simulation time.
Definition: log.h:115
void LogComponentDisableAll(enum LogLevel level)
Disable all logging for all components.
Definition: log.cc:403
Prefix all trace prints with function.
Definition: log.h:114
static ComponentList * GetComponentList(void)
Get the list of LogComponnents.
Definition: log.cc:79
Function tracing.
Definition: log.h:105
Definition of logging macros.
int32_t m_mask
Blocked LogLevels.
Definition: log.h:406
int32_t m_levels
Enabled LogLevels.
Definition: log.h:405
void LogSetTimePrinter(LogTimePrinter printer)
Set the LogTimePrinter function to be used to prepend log messages with the simulation time...
Definition: log.cc:611
std::string File(void) const
Get the compilation unit defining this LogComponent.
Definition: log.cc:309
Prefix all trace prints with simulation node.
Definition: log.h:116
All prefixes.
Definition: log.h:118
bool IsNoneEnabled(void) const
Check if all levels are disabled.
Definition: log.cc:279
LogLevel
Logging severity classes and levels.
Definition: log.h:90
Print everything.
Definition: log.h:112
Informational messages (e.g., banners).
Definition: log.h:102
char const * Name(void) const
Get the name of this LogComponent.
Definition: log.cc:303
Prefix all trace prints with log level (severity).
Definition: log.h:117
static std::string GetLevelLabel(const enum LogLevel level)
Get the string label for the given LogLevel.
Definition: log.cc:316
LogTimePrinter LogGetTimePrinter(void)
Get the LogTimePrinter function currently in use.
Definition: log.cc:620
std::string m_name
LogComponent name.
Definition: log.h:407