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 #include <vector>
29 
30 #include "node-printer.h"
31 #include "time-printer.h"
32 #include "log-macros-enabled.h"
33 #include "log-macros-disabled.h"
34 
88 namespace ns3 {
89 
93 enum LogLevel {
94  LOG_NONE = 0x00000000,
95 
96  LOG_ERROR = 0x00000001,
97  LOG_LEVEL_ERROR = 0x00000001,
98 
99  LOG_WARN = 0x00000002,
100  LOG_LEVEL_WARN = 0x00000003,
101 
102  LOG_DEBUG = 0x00000004,
103  LOG_LEVEL_DEBUG = 0x00000007,
104 
105  LOG_INFO = 0x00000008,
106  LOG_LEVEL_INFO = 0x0000000f,
107 
108  LOG_FUNCTION = 0x00000010,
109  LOG_LEVEL_FUNCTION = 0x0000001f,
110 
111  LOG_LOGIC = 0x00000020,
112  LOG_LEVEL_LOGIC = 0x0000003f,
113 
114  LOG_ALL = 0x0fffffff,
116 
117  LOG_PREFIX_FUNC = 0x80000000,
118  LOG_PREFIX_TIME = 0x40000000,
119  LOG_PREFIX_NODE = 0x20000000,
120  LOG_PREFIX_LEVEL = 0x10000000,
121  LOG_PREFIX_ALL = 0xf0000000
122 };
123 
136 void LogComponentEnable (char const *name, enum LogLevel level);
137 
146 void LogComponentEnableAll (enum LogLevel level);
147 
148 
158 void LogComponentDisable (char const *name, enum LogLevel level);
159 
165 void LogComponentDisableAll (enum LogLevel level);
166 
167 
168 } // namespace ns3
169 
170 
204 #define NS_LOG_COMPONENT_DEFINE(name) \
205  static ns3::LogComponent g_log = ns3::LogComponent (name, __FILE__)
206 
215 #define NS_LOG_COMPONENT_DEFINE_MASK(name, mask) \
216  static ns3::LogComponent g_log = ns3::LogComponent (name, __FILE__, mask)
217 
227 #define NS_LOG_TEMPLATE_DECLARE LogComponent & g_log
228 
238 #define NS_LOG_TEMPLATE_DEFINE(name) g_log (GetLogComponent (name))
239 
248 #define NS_LOG_STATIC_TEMPLATE_DEFINE(name) \
249  static LogComponent & NS_UNUSED_GLOBAL (g_log) = GetLogComponent (name)
250 
256 #define NS_LOG_ERROR(msg) \
257  NS_LOG (ns3::LOG_ERROR, msg)
258 
264 #define NS_LOG_WARN(msg) \
265  NS_LOG (ns3::LOG_WARN, msg)
266 
272 #define NS_LOG_DEBUG(msg) \
273  NS_LOG (ns3::LOG_DEBUG, msg)
274 
280 #define NS_LOG_INFO(msg) \
281  NS_LOG (ns3::LOG_INFO, msg)
282 
288 #define NS_LOG_LOGIC(msg) \
289  NS_LOG (ns3::LOG_LOGIC, msg)
290 
291 
292 namespace ns3 {
293 
299 void LogComponentPrintList (void);
300 
315 
330 
331 
336 {
337 public:
347  LogComponent (const std::string & name,
348  const std::string & file,
349  const enum LogLevel mask = LOG_NONE);
356  bool IsEnabled (const enum LogLevel level) const;
362  bool IsNoneEnabled (void) const;
368  void Enable (const enum LogLevel level);
374  void Disable (const enum LogLevel level);
380  char const *Name (void) const;
385  std::string File (void) const;
392  static std::string GetLevelLabel(const enum LogLevel level);
398  void SetMask (const enum LogLevel level);
399 
408  typedef std::map<std::string, LogComponent *> ComponentList;
409 
420  static ComponentList *GetComponentList (void);
421 
422 
423 private:
428  void EnvVarCheck (void);
429 
430  int32_t m_levels;
431  int32_t m_mask;
432  std::string m_name;
433  std::string m_file;
434 
435 }; // class LogComponent
436 
443 LogComponent & GetLogComponent (const std::string name);
444 
449 {
450  bool m_first;
451  std::ostream &m_os;
452 public:
458  ParameterLogger (std::ostream &os);
459 
467  template<typename T>
468  ParameterLogger& operator<< (T param);
469 
476  template<typename T>
477  ParameterLogger& operator<< (std::vector<T> vector);
478 
479 };
480 
481 template<typename T>
484 {
485  if (m_first)
486  {
487  m_os << param;
488  m_first = false;
489  }
490  else
491  {
492  m_os << ", " << param;
493  }
494  return *this;
495 }
496 
497 template<typename T>
499 ParameterLogger::operator<< (std::vector<T> vector)
500 {
501  for (auto i : vector)
502  {
503  *this << i;
504  }
505  return *this;
506 }
507 
513 template<>
515 ParameterLogger::operator<< <std::string>(const std::string param);
516 
522 template<>
524 ParameterLogger::operator<< <const char *>(const char * param);
525 
531 template<>
533  ParameterLogger::operator<< <int8_t>(int8_t param);
534 
540 template<>
542  ParameterLogger::operator<< <uint8_t>(uint8_t param);
543 
544 } // namespace ns3
545  // \ingroup logging
547 
548 #endif /* NS3_LOG_H */
std::string File(void) const
Get the compilation unit defining this LogComponent.
Definition: log.cc:327
LogComponent(const std::string &name, const std::string &file, const enum LogLevel mask=LOG_NONE)
Constructor.
Definition: log.cc:114
Serious error messages only.
Definition: log.h:96
Prefix all trace prints with simulation node.
Definition: log.h:119
ParameterLogger & operator<<(T param)
Write a function parameter on the output stream, separating parameters after the first by ...
Definition: log.h:483
void LogComponentDisable(char const *name, enum LogLevel level)
Disable the logging output associated with that log component.
Definition: log.cc:405
std::ostream & m_os
Underlying output stream.
Definition: log.h:451
bool IsNoneEnabled(void) const
Check if all levels are disabled.
Definition: log.cc:297
Function tracing.
Definition: log.h:108
Informational messages (e.g., banners).
Definition: log.h:105
void LogSetTimePrinter(TimePrinter printer)
Set the TimePrinter function to be used to prepend log messages with the simulation time...
Definition: log.cc:629
bool m_first
First argument flag, doesn&#39;t get ,.
Definition: log.h:450
A single log component configuration.
Definition: log.h:335
ParameterLogger(std::ostream &os)
Constructor.
Definition: log.cc:653
char const * Name(void) const
Get the name of this LogComponent.
Definition: log.cc:321
void Disable(const enum LogLevel level)
Disable logging at level for this LogComponent.
Definition: log.cc:315
void EnvVarCheck(void)
Parse the NS_LOG environment variable for options relating to this LogComponent.
Definition: log.cc:152
Print everything.
Definition: log.h:114
std::string m_file
File defining this LogComponent.
Definition: log.h:433
TimePrinter LogGetTimePrinter(void)
Get the LogTimePrinter function currently in use.
Definition: log.cc:638
Insert , when streaming function arguments.
Definition: log.h:448
void LogComponentEnable(char const *name, enum LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:369
LOG_ERROR and above.
Definition: log.h:97
void Enable(const enum LogLevel level)
Enable this LogComponent at level.
Definition: log.cc:309
void LogSetNodePrinter(NodePrinter printer)
Set the LogNodePrinter function to be used to prepend log messages with the node id.
Definition: log.cc:643
All prefixes.
Definition: log.h:121
void SetMask(const enum LogLevel level)
Prevent the enabling of a specific LogLevel.
Definition: log.cc:303
LOG_INFO and above.
Definition: log.h:106
Prefix all trace prints with log level (severity).
Definition: log.h:120
void LogComponentPrintList(void)
Print the list of logging messages available.
Definition: log.cc:433
LOG_WARN and above.
Definition: log.h:100
Control flow tracing within functions.
Definition: log.h:111
void(* TimePrinter)(std::ostream &os)
Function signature for features requiring a time formatter, such as logging or ShowProgress.
Definition: time-printer.h:43
Definition of empty logging macros and the NS_LOG_NOOP_INTERNAL macro.
std::map< std::string, LogComponent * > ComponentList
LogComponent name map.
Definition: log.h:408
void LogComponentEnableAll(enum LogLevel level)
Enable the logging output for all registered log components.
Definition: log.cc:393
Prefix all trace prints with simulation time.
Definition: log.h:118
NodePrinter LogGetNodePrinter(void)
Get the LogNodePrinter function currently in use.
Definition: log.cc:647
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void LogComponentDisableAll(enum LogLevel level)
Disable all logging for all components.
Definition: log.cc:421
static ComponentList * GetComponentList(void)
Get the list of LogComponnents.
Definition: log.cc:80
void(* NodePrinter)(std::ostream &os)
Function signature for prepending the node id to a log message.
Definition: node-printer.h:40
NS_LOG and related logging macro definitions.
int32_t m_mask
Blocked LogLevels.
Definition: log.h:431
int32_t m_levels
Enabled LogLevels.
Definition: log.h:430
No logging.
Definition: log.h:94
LogComponent & GetLogComponent(const std::string name)
Get the LogComponent registered with the given name.
Definition: log.cc:135
LogLevel
Logging severity classes and levels.
Definition: log.h:93
LOG_FUNCTION and above.
Definition: log.h:109
LOG_DEBUG and above.
Definition: log.h:103
Prefix all trace prints with function.
Definition: log.h:117
LOG_LOGIC and above.
Definition: log.h:112
Rare ad-hoc debug messages.
Definition: log.h:102
Print everything.
Definition: log.h:115
bool IsEnabled(const enum LogLevel level) const
Check if this LogComponent is enabled for level.
Definition: log.cc:290
Warning messages.
Definition: log.h:99
Declaration of ns3::TimePrinter function pointer type and ns3::DefaultTimePrinter function...
Declaration of ns3::NodePrinter function pointer type and ns3::DefaultNodePrinter function...
static std::string GetLevelLabel(const enum LogLevel level)
Get the string label for the given LogLevel.
Definition: log.cc:334
std::string m_name
LogComponent name.
Definition: log.h:432