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 
94 {
95  LOG_NONE = 0x00000000,
96 
97  LOG_ERROR = 0x00000001,
98  LOG_LEVEL_ERROR = 0x00000001,
99 
100  LOG_WARN = 0x00000002,
101  LOG_LEVEL_WARN = 0x00000003,
102 
103  LOG_DEBUG = 0x00000004,
104  LOG_LEVEL_DEBUG = 0x00000007,
105 
106  LOG_INFO = 0x00000008,
107  LOG_LEVEL_INFO = 0x0000000f,
108 
109  LOG_FUNCTION = 0x00000010,
110  LOG_LEVEL_FUNCTION = 0x0000001f,
111 
112  LOG_LOGIC = 0x00000020,
113  LOG_LEVEL_LOGIC = 0x0000003f,
114 
115  LOG_ALL = 0x0fffffff,
117 
118  LOG_PREFIX_FUNC = 0x80000000,
119  LOG_PREFIX_TIME = 0x40000000,
120  LOG_PREFIX_NODE = 0x20000000,
121  LOG_PREFIX_LEVEL = 0x10000000,
122  LOG_PREFIX_ALL = 0xf0000000
123 };
124 
137 void LogComponentEnable (char const *name, enum LogLevel level);
138 
147 void LogComponentEnableAll (enum LogLevel level);
148 
149 
159 void LogComponentDisable (char const *name, enum LogLevel level);
160 
166 void LogComponentDisableAll (enum LogLevel level);
167 
168 
169 } // namespace ns3
170 
171 
205 #define NS_LOG_COMPONENT_DEFINE(name) \
206  static ns3::LogComponent g_log = ns3::LogComponent (name, __FILE__)
207 
216 #define NS_LOG_COMPONENT_DEFINE_MASK(name, mask) \
217  static ns3::LogComponent g_log = ns3::LogComponent (name, __FILE__, mask)
218 
228 #define NS_LOG_TEMPLATE_DECLARE LogComponent & g_log
229 
239 #define NS_LOG_TEMPLATE_DEFINE(name) g_log (GetLogComponent (name))
240 
249 #define NS_LOG_STATIC_TEMPLATE_DEFINE(name) \
250  static LogComponent & NS_UNUSED_GLOBAL (g_log) = GetLogComponent (name)
251 
257 #define NS_LOG_ERROR(msg) \
258  NS_LOG (ns3::LOG_ERROR, msg)
259 
265 #define NS_LOG_WARN(msg) \
266  NS_LOG (ns3::LOG_WARN, msg)
267 
273 #define NS_LOG_DEBUG(msg) \
274  NS_LOG (ns3::LOG_DEBUG, msg)
275 
281 #define NS_LOG_INFO(msg) \
282  NS_LOG (ns3::LOG_INFO, msg)
283 
289 #define NS_LOG_LOGIC(msg) \
290  NS_LOG (ns3::LOG_LOGIC, msg)
291 
292 
293 namespace ns3 {
294 
300 void LogComponentPrintList (void);
301 
316 
331 
332 
337 {
338 public:
348  LogComponent (const std::string & name,
349  const std::string & file,
350  const enum LogLevel mask = LOG_NONE);
357  bool IsEnabled (const enum LogLevel level) const;
363  bool IsNoneEnabled (void) const;
369  void Enable (const enum LogLevel level);
375  void Disable (const enum LogLevel level);
381  char const * Name (void) const;
386  std::string File (void) const;
393  static std::string GetLevelLabel (const enum LogLevel level);
399  void SetMask (const enum LogLevel level);
400 
409  typedef std::map<std::string, LogComponent *> ComponentList;
410 
421  static ComponentList * GetComponentList (void);
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 
453 public:
459  ParameterLogger (std::ostream &os);
460 
468  template<typename T>
469  ParameterLogger& operator<< (T param);
470 
477  template<typename T>
478  ParameterLogger& operator<< (std::vector<T> vector);
479 
480 };
481 
482 template<typename T>
485 {
486  if (m_first)
487  {
488  m_os << param;
489  m_first = false;
490  }
491  else
492  {
493  m_os << ", " << param;
494  }
495  return *this;
496 }
497 
498 template<typename T>
500 ParameterLogger::operator<< (std::vector<T> vector)
501 {
502  for (auto i : vector)
503  {
504  *this << i;
505  }
506  return *this;
507 }
508 
514 template<>
516 ParameterLogger::operator<< <std::string> (const std::string param);
517 
523 template<>
525 ParameterLogger::operator<< <const char *> (const char * param);
526 
532 template<>
534 ParameterLogger::operator<< <int8_t> (int8_t param);
535 
541 template<>
543 ParameterLogger::operator<< <uint8_t> (uint8_t param);
544 
545 } // namespace ns3
546  // \ingroup logging
548 
549 #endif /* NS3_LOG_H */
std::string File(void) const
Get the compilation unit defining this LogComponent.
Definition: log.cc:328
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:97
Prefix all trace prints with simulation node.
Definition: log.h:120
ParameterLogger & operator<<(T param)
Write a function parameter on the output stream, separating parameters after the first by ...
Definition: log.h:484
void LogComponentDisable(char const *name, enum LogLevel level)
Disable the logging output associated with that log component.
Definition: log.cc:406
std::ostream & m_os
Underlying output stream.
Definition: log.h:451
bool IsNoneEnabled(void) const
Check if all levels are disabled.
Definition: log.cc:298
Function tracing.
Definition: log.h:109
Informational messages (e.g., banners).
Definition: log.h:106
void LogSetTimePrinter(TimePrinter printer)
Set the TimePrinter function to be used to prepend log messages with the simulation time...
Definition: log.cc:631
bool m_first
First argument flag, doesn&#39;t get ,.
Definition: log.h:450
A single log component configuration.
Definition: log.h:336
ParameterLogger(std::ostream &os)
Constructor.
Definition: log.cc:655
char const * Name(void) const
Get the name of this LogComponent.
Definition: log.cc:322
void Disable(const enum LogLevel level)
Disable logging at level for this LogComponent.
Definition: log.cc:316
void EnvVarCheck(void)
Parse the NS_LOG environment variable for options relating to this LogComponent.
Definition: log.cc:152
Print everything.
Definition: log.h:115
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:640
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:370
LOG_ERROR and above.
Definition: log.h:98
void Enable(const enum LogLevel level)
Enable this LogComponent at level.
Definition: log.cc:310
void LogSetNodePrinter(NodePrinter printer)
Set the LogNodePrinter function to be used to prepend log messages with the node id.
Definition: log.cc:645
All prefixes.
Definition: log.h:122
void SetMask(const enum LogLevel level)
Prevent the enabling of a specific LogLevel.
Definition: log.cc:304
LOG_INFO and above.
Definition: log.h:107
Prefix all trace prints with log level (severity).
Definition: log.h:121
void LogComponentPrintList(void)
Print the list of logging messages available.
Definition: log.cc:434
LOG_WARN and above.
Definition: log.h:101
Control flow tracing within functions.
Definition: log.h:112
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:409
void LogComponentEnableAll(enum LogLevel level)
Enable the logging output for all registered log components.
Definition: log.cc:394
Prefix all trace prints with simulation time.
Definition: log.h:119
NodePrinter LogGetNodePrinter(void)
Get the LogNodePrinter function currently in use.
Definition: log.cc:649
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:422
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:95
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:110
LOG_DEBUG and above.
Definition: log.h:104
Prefix all trace prints with function.
Definition: log.h:118
LOG_LOGIC and above.
Definition: log.h:113
Rare ad-hoc debug messages.
Definition: log.h:103
Print everything.
Definition: log.h:116
bool IsEnabled(const enum LogLevel level) const
Check if this LogComponent is enabled for level.
Definition: log.cc:291
Warning messages.
Definition: log.h:100
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:335
std::string m_name
LogComponent name.
Definition: log.h:432