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 "log-macros-enabled.h"
31 #include "log-macros-disabled.h"
32 
86 namespace ns3 {
87 
91 enum LogLevel {
92  LOG_NONE = 0x00000000,
93 
94  LOG_ERROR = 0x00000001,
95  LOG_LEVEL_ERROR = 0x00000001,
96 
97  LOG_WARN = 0x00000002,
98  LOG_LEVEL_WARN = 0x00000003,
99 
100  LOG_DEBUG = 0x00000004,
101  LOG_LEVEL_DEBUG = 0x00000007,
102 
103  LOG_INFO = 0x00000008,
104  LOG_LEVEL_INFO = 0x0000000f,
105 
106  LOG_FUNCTION = 0x00000010,
107  LOG_LEVEL_FUNCTION = 0x0000001f,
108 
109  LOG_LOGIC = 0x00000020,
110  LOG_LEVEL_LOGIC = 0x0000003f,
111 
112  LOG_ALL = 0x0fffffff,
114 
115  LOG_PREFIX_FUNC = 0x80000000,
116  LOG_PREFIX_TIME = 0x40000000,
117  LOG_PREFIX_NODE = 0x20000000,
118  LOG_PREFIX_LEVEL = 0x10000000,
119  LOG_PREFIX_ALL = 0xf0000000
120 };
121 
134 void LogComponentEnable (char const *name, enum LogLevel level);
135 
144 void LogComponentEnableAll (enum LogLevel level);
145 
146 
156 void LogComponentDisable (char const *name, enum LogLevel level);
157 
163 void LogComponentDisableAll (enum LogLevel level);
164 
165 
166 } // namespace ns3
167 
168 
202 #define NS_LOG_COMPONENT_DEFINE(name) \
203  static ns3::LogComponent g_log = ns3::LogComponent (name, __FILE__)
204 
213 #define NS_LOG_COMPONENT_DEFINE_MASK(name, mask) \
214  static ns3::LogComponent g_log = ns3::LogComponent (name, __FILE__, mask)
215 
225 #define NS_LOG_TEMPLATE_DECLARE LogComponent & g_log
226 
236 #define NS_LOG_TEMPLATE_DEFINE(name) g_log (GetLogComponent (name))
237 
246 #define NS_LOG_STATIC_TEMPLATE_DEFINE(name) \
247  static LogComponent & NS_UNUSED_GLOBAL (g_log) = GetLogComponent (name)
248 
254 #define NS_LOG_ERROR(msg) \
255  NS_LOG (ns3::LOG_ERROR, msg)
256 
262 #define NS_LOG_WARN(msg) \
263  NS_LOG (ns3::LOG_WARN, msg)
264 
270 #define NS_LOG_DEBUG(msg) \
271  NS_LOG (ns3::LOG_DEBUG, msg)
272 
278 #define NS_LOG_INFO(msg) \
279  NS_LOG (ns3::LOG_INFO, msg)
280 
286 #define NS_LOG_LOGIC(msg) \
287  NS_LOG (ns3::LOG_LOGIC, msg)
288 
289 
290 namespace ns3 {
291 
297 void LogComponentPrintList (void);
298 
305 typedef void (*LogTimePrinter)(std::ostream &os);
312 typedef void (*LogNodePrinter)(std::ostream &os);
313 
326 
339 
340 
345 {
346 public:
356  LogComponent (const std::string & name,
357  const std::string & file,
358  const enum LogLevel mask = LOG_NONE);
365  bool IsEnabled (const enum LogLevel level) const;
371  bool IsNoneEnabled (void) const;
377  void Enable (const enum LogLevel level);
383  void Disable (const enum LogLevel level);
389  char const *Name (void) const;
394  std::string File (void) const;
401  static std::string GetLevelLabel(const enum LogLevel level);
407  void SetMask (const enum LogLevel level);
408 
417  typedef std::map<std::string, LogComponent *> ComponentList;
418 
429  static ComponentList *GetComponentList (void);
430 
431 
432 private:
437  void EnvVarCheck (void);
438 
439  int32_t m_levels;
440  int32_t m_mask;
441  std::string m_name;
442  std::string m_file;
443 
444 }; // class LogComponent
445 
452 LogComponent & GetLogComponent (const std::string name);
453 
458 {
459  bool m_first;
460  std::ostream &m_os;
461 public:
467  ParameterLogger (std::ostream &os);
468 
476  template<typename T>
477  ParameterLogger& operator<< (T param);
478 
485  template<typename T>
486  ParameterLogger& operator<< (std::vector<T> vector);
487 
488 };
489 
490 template<typename T>
493 {
494  if (m_first)
495  {
496  m_os << param;
497  m_first = false;
498  }
499  else
500  {
501  m_os << ", " << param;
502  }
503  return *this;
504 }
505 
506 template<typename T>
508 ParameterLogger::operator<< (std::vector<T> vector)
509 {
510  for (auto i : vector)
511  {
512  *this << i;
513  }
514  return *this;
515 }
516 
522 template<>
524 ParameterLogger::operator<< <std::string>(const std::string param);
525 
531 template<>
533 ParameterLogger::operator<< <const char *>(const char * param);
534 
540 template<>
542  ParameterLogger::operator<< <int8_t>(int8_t param);
543 
549 template<>
551  ParameterLogger::operator<< <uint8_t>(uint8_t param);
552 
553 } // namespace ns3
554  // \ingroup logging
556 
557 #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:94
Prefix all trace prints with simulation node.
Definition: log.h:117
ParameterLogger & operator<<(T param)
Write a function parameter on the output stream, separating parameters after the first by ...
Definition: log.h:492
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:460
bool IsNoneEnabled(void) const
Check if all levels are disabled.
Definition: log.cc:297
Function tracing.
Definition: log.h:106
Informational messages (e.g., banners).
Definition: log.h:103
bool m_first
First argument flag, doesn&#39;t get ,.
Definition: log.h:459
A single log component configuration.
Definition: log.h:344
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(* LogTimePrinter)(std::ostream &os)
Function signature for prepending the simulation time to a log message.
Definition: log.h:305
void EnvVarCheck(void)
Parse the NS_LOG environment variable for options relating to this LogComponent.
Definition: log.cc:152
void(* LogNodePrinter)(std::ostream &os)
Function signature for prepending the node id to a log message.
Definition: log.h:312
Print everything.
Definition: log.h:112
std::string m_file
File defining this LogComponent.
Definition: log.h:442
Insert , when streaming function arguments.
Definition: log.h:457
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:95
void Enable(const enum LogLevel level)
Enable this LogComponent at level.
Definition: log.cc:309
All prefixes.
Definition: log.h:119
void LogSetNodePrinter(LogNodePrinter printer)
Set the LogNodePrinter function to be used to prepend log messages with the node id.
Definition: log.cc:643
void SetMask(const enum LogLevel level)
Prevent the enabling of a specific LogLevel.
Definition: log.cc:303
LOG_INFO and above.
Definition: log.h:104
Prefix all trace prints with log level (severity).
Definition: log.h:118
void LogComponentPrintList(void)
Print the list of logging messages available.
Definition: log.cc:433
LOG_WARN and above.
Definition: log.h:98
Control flow tracing within functions.
Definition: log.h:109
LogNodePrinter LogGetNodePrinter(void)
Get the LogNodePrinter function currently in use.
Definition: log.cc:647
Definition of empty logging macros and the NS_LOG_NOOP_INTERNAL macro.
std::map< std::string, LogComponent * > ComponentList
LogComponent name map.
Definition: log.h:417
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:116
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
NS_LOG and related logging macro definitions.
int32_t m_mask
Blocked LogLevels.
Definition: log.h:440
int32_t m_levels
Enabled LogLevels.
Definition: log.h:439
No logging.
Definition: log.h:92
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:91
void LogSetTimePrinter(LogTimePrinter printer)
Set the LogTimePrinter function to be used to prepend log messages with the simulation time...
Definition: log.cc:629
LOG_FUNCTION and above.
Definition: log.h:107
LOG_DEBUG and above.
Definition: log.h:101
Prefix all trace prints with function.
Definition: log.h:115
LOG_LOGIC and above.
Definition: log.h:110
Rare ad-hoc debug messages.
Definition: log.h:100
Print everything.
Definition: log.h:113
bool IsEnabled(const enum LogLevel level) const
Check if this LogComponent is enabled for level.
Definition: log.cc:290
Warning messages.
Definition: log.h:97
static std::string GetLevelLabel(const enum LogLevel level)
Get the string label for the given LogLevel.
Definition: log.cc:334
LogTimePrinter LogGetTimePrinter(void)
Get the LogTimePrinter function currently in use.
Definition: log.cc:638
std::string m_name
LogComponent name.
Definition: log.h:441