A Discrete-Event Network Simulator
API
log.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2006,2007 INRIA
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18  */
19 
20 #ifndef NS3_LOG_H
21 #define NS3_LOG_H
22 
23 #include "log-macros-disabled.h"
24 #include "log-macros-enabled.h"
25 #include "node-printer.h"
26 #include "time-printer.h"
27 
28 #include <iostream>
29 #include <stdint.h>
30 #include <string>
31 #include <type_traits>
32 #include <unordered_map>
33 #include <vector>
34 
87 namespace ns3
88 {
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_INFO = 0x00000004,
104  LOG_LEVEL_INFO = 0x00000007,
105 
106  LOG_FUNCTION = 0x00000008,
107  LOG_LEVEL_FUNCTION = 0x0000000f,
108 
109  LOG_LOGIC = 0x00000010,
110  LOG_LEVEL_LOGIC = 0x0000001f,
111 
112  LOG_DEBUG = 0x00000020,
113  LOG_LEVEL_DEBUG = 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(const std::string& name, LogLevel level);
138 
147 void LogComponentEnableAll(LogLevel level);
148 
158 void LogComponentDisable(const std::string& name, LogLevel level);
159 
165 void LogComponentDisableAll(LogLevel level);
166 
167 } // namespace ns3
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& g_log [[maybe_unused]] = GetLogComponent(name)
248 
254 #define NS_LOG_ERROR(msg) NS_LOG(ns3::LOG_ERROR, msg)
255 
261 #define NS_LOG_WARN(msg) NS_LOG(ns3::LOG_WARN, msg)
262 
268 #define NS_LOG_DEBUG(msg) NS_LOG(ns3::LOG_DEBUG, msg)
269 
275 #define NS_LOG_INFO(msg) NS_LOG(ns3::LOG_INFO, msg)
276 
282 #define NS_LOG_LOGIC(msg) NS_LOG(ns3::LOG_LOGIC, msg)
283 
284 namespace ns3
285 {
286 
292 void LogComponentPrintList();
293 
308 
323 
328 {
329  public:
339  LogComponent(const std::string& name, const std::string& file, const LogLevel mask = LOG_NONE);
346  bool IsEnabled(const LogLevel level) const;
352  bool IsNoneEnabled() const;
358  void Enable(const LogLevel level);
364  void Disable(const LogLevel level);
370  std::string Name() const;
375  std::string File() const;
382  static std::string GetLevelLabel(const LogLevel level);
388  void SetMask(const LogLevel level);
389 
398  using ComponentList = std::unordered_map<std::string, LogComponent*>;
399 
411 
412  private:
417  void EnvVarCheck();
418 
419  int32_t m_levels;
420  int32_t m_mask;
421  std::string m_name;
422  std::string m_file;
423 
424 }; // class LogComponent
425 
432 LogComponent& GetLogComponent(const std::string name);
433 
438 {
439  public:
445  ParameterLogger(std::ostream& os);
446 
454  template <typename T>
455  ParameterLogger& operator<<(const T& param);
456 
463  template <typename T>
464  ParameterLogger& operator<<(const std::vector<T>& vector);
465 
466  private:
468  void CommaRest();
469 
470  bool m_first{true};
471  std::ostream& m_os;
472 };
473 
474 template <typename T>
476 ParameterLogger::operator<<(const T& param)
477 {
478  CommaRest();
479 
480  if constexpr (std::is_convertible_v<T, std::string>)
481  {
482  m_os << "\"" << param << "\"";
483  }
484  else if constexpr (std::is_arithmetic_v<T>)
485  {
486  // Use + unary operator to cast uint8_t / int8_t to uint32_t / int32_t, respectively
487  m_os << +param;
488  }
489  else
490  {
491  m_os << param;
492  }
493 
494  return *this;
495 }
496 
497 template <typename T>
499 ParameterLogger::operator<<(const std::vector<T>& vector)
500 {
501  for (const auto& i : vector)
502  {
503  *this << i;
504  }
505  return *this;
506 }
507 
508 } // namespace ns3
509  // \ingroup logging
511 
512 #endif /* NS3_LOG_H */
A single log component configuration.
Definition: log.h:328
static ComponentList * GetComponentList()
Get the list of LogComponents.
Definition: log.cc:143
void Enable(const LogLevel level)
Enable this LogComponent at level.
Definition: log.cc:266
bool IsEnabled(const LogLevel level) const
Check if this LogComponent is enabled for level.
Definition: log.cc:247
std::string File() const
Get the compilation unit defining this LogComponent.
Definition: log.cc:284
int32_t m_levels
Enabled LogLevels.
Definition: log.h:419
void Disable(const LogLevel level)
Disable logging at level for this LogComponent.
Definition: log.cc:272
static std::string GetLevelLabel(const LogLevel level)
Get the string label for the given LogLevel.
Definition: log.cc:291
void EnvVarCheck()
Parse the NS_LOG environment variable for options relating to this LogComponent.
Definition: log.cc:198
std::string m_file
File defining this LogComponent.
Definition: log.h:422
bool IsNoneEnabled() const
Check if all levels are disabled.
Definition: log.cc:254
std::string Name() const
Get the name of this LogComponent.
Definition: log.cc:278
std::unordered_map< std::string, LogComponent * > ComponentList
LogComponent name map.
Definition: log.h:398
int32_t m_mask
Blocked LogLevels.
Definition: log.h:420
LogComponent(const std::string &name, const std::string &file, const LogLevel mask=LOG_NONE)
Constructor.
Definition: log.cc:159
void SetMask(const LogLevel level)
Prevent the enabling of a specific LogLevel.
Definition: log.cc:260
std::string m_name
LogComponent name.
Definition: log.h:421
Insert , when streaming function arguments.
Definition: log.h:438
void CommaRest()
Add , before every parameter after the first.
Definition: log.cc:526
bool m_first
First argument flag, doesn't get ,.
Definition: log.h:470
ParameterLogger & operator<<(const T &param)
Write a function parameter on the output stream, separating parameters after the first by ,...
Definition: log.h:476
ParameterLogger(std::ostream &os)
Constructor.
Definition: log.cc:520
std::ostream & m_os
Underlying output stream.
Definition: log.h:471
Definition of empty logging macros and the NS_LOG_NOOP_INTERNAL macro.
NS_LOG and related logging macro definitions.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void LogComponentEnable(const std::string &name, LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:302
void LogSetTimePrinter(TimePrinter printer)
Set the TimePrinter function to be used to prepend log messages with the simulation time.
Definition: log.cc:492
void(* TimePrinter)(std::ostream &os)
Function signature for features requiring a time formatter, such as logging or ShowProgress.
Definition: time-printer.h:43
void(* NodePrinter)(std::ostream &os)
Function signature for prepending the node id to a log message.
Definition: node-printer.h:40
NodePrinter LogGetNodePrinter()
Get the LogNodePrinter function currently in use.
Definition: log.cc:515
void LogComponentDisable(const std::string &name, LogLevel level)
Disable the logging output associated with that log component.
Definition: log.cc:330
LogLevel
Logging severity classes and levels.
Definition: log.h:94
@ LOG_LEVEL_ALL
Print everything.
Definition: log.h:116
@ LOG_PREFIX_FUNC
Prefix all trace prints with function.
Definition: log.h:118
@ LOG_LEVEL_LOGIC
LOG_LOGIC and above.
Definition: log.h:110
@ LOG_NONE
No logging.
Definition: log.h:95
@ LOG_PREFIX_TIME
Prefix all trace prints with simulation time.
Definition: log.h:119
@ LOG_FUNCTION
Function tracing for non-trivial function calls.
Definition: log.h:106
@ LOG_ERROR
Serious error messages only.
Definition: log.h:97
@ LOG_WARN
Warning messages.
Definition: log.h:100
@ LOG_INFO
Something happened to change state.
Definition: log.h:103
@ LOG_PREFIX_ALL
All prefixes.
Definition: log.h:122
@ LOG_LEVEL_FUNCTION
LOG_FUNCTION and above.
Definition: log.h:107
@ LOG_LEVEL_ERROR
LOG_ERROR and above.
Definition: log.h:98
@ LOG_ALL
Print everything.
Definition: log.h:115
@ LOG_LEVEL_WARN
LOG_WARN and above.
Definition: log.h:101
@ LOG_LEVEL_DEBUG
LOG_DEBUG and above.
Definition: log.h:113
@ LOG_PREFIX_LEVEL
Prefix all trace prints with log level (severity).
Definition: log.h:121
@ LOG_LOGIC
Debugging logs for key branches and decisions in a function.
Definition: log.h:109
@ LOG_PREFIX_NODE
Prefix all trace prints with simulation node.
Definition: log.h:120
@ LOG_LEVEL_INFO
LOG_INFO and above.
Definition: log.h:104
@ LOG_DEBUG
Full voluminous logging to support debugging.
Definition: log.h:112
TimePrinter LogGetTimePrinter()
Get the LogTimePrinter function currently in use.
Definition: log.cc:503
void LogComponentDisableAll(LogLevel level)
Disable all logging for all components.
Definition: log.cc:342
LogComponent & GetLogComponent(const std::string name)
Get the LogComponent registered with the given name.
Definition: log.cc:181
void LogComponentEnableAll(LogLevel level)
Enable the logging output for all registered log components.
Definition: log.cc:320
void LogSetNodePrinter(NodePrinter printer)
Set the LogNodePrinter function to be used to prepend log messages with the node id.
Definition: log.cc:509
void LogComponentPrintList()
Print the list of logging messages available.
Definition: log.cc:352
Declaration of ns3::NodePrinter function pointer type and ns3::DefaultNodePrinter function.
Declaration of ns3::TimePrinter function pointer type and ns3::DefaultTimePrinter function.