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 <map>
30#include <stdint.h>
31#include <string>
32#include <type_traits>
33#include <vector>
34
87namespace 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_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
124
137void LogComponentEnable(const char* name, enum LogLevel level);
138
147void LogComponentEnableAll(enum LogLevel level);
148
158void LogComponentDisable(const char* name, enum LogLevel level);
159
165void LogComponentDisableAll(enum 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
284namespace ns3
285{
286
293
308
323
328{
329 public:
339 LogComponent(const std::string& name,
340 const std::string& file,
341 const enum LogLevel mask = LOG_NONE);
348 bool IsEnabled(const enum LogLevel level) const;
354 bool IsNoneEnabled() const;
360 void Enable(const enum LogLevel level);
366 void Disable(const enum LogLevel level);
372 const char* Name() const;
377 std::string File() const;
384 static std::string GetLevelLabel(const enum LogLevel level);
390 void SetMask(const enum LogLevel level);
391
400 typedef std::map<std::string, LogComponent*> ComponentList;
401
413
414 private:
419 void EnvVarCheck();
420
423 std::string m_name;
424 std::string m_file;
425
426}; // class LogComponent
427
434LogComponent& GetLogComponent(const std::string name);
435
440{
441 public:
447 ParameterLogger(std::ostream& os);
448
458 template <typename T, typename U = std::enable_if_t<std::is_arithmetic_v<T>>>
459 ParameterLogger& operator<<(T param);
460
470 template <typename T, typename U = std::enable_if_t<!std::is_arithmetic_v<T>>>
471 ParameterLogger& operator<<(const T& param);
472
479 template <typename T>
480 ParameterLogger& operator<<(const std::vector<T>& vector);
481
488 ParameterLogger& operator<<(const char* param);
489
490 private:
492 void CommaRest();
493
494 bool m_first{true};
495 std::ostream& m_os;
496};
497
498template <typename T, typename U>
501{
502 CommaRest();
503 m_os << param;
504 return *this;
505}
506
507template <typename T, typename U>
509ParameterLogger::operator<<(const T& param)
510{
511 CommaRest();
512 m_os << param;
513 return *this;
514}
515
516template <typename T>
518ParameterLogger::operator<<(const std::vector<T>& vector)
519{
520 for (const auto& i : vector)
521 {
522 *this << i;
523 }
524 return *this;
525}
526
532template <>
533ParameterLogger& ParameterLogger::operator<< <std::string>(const std::string& param);
534
540template <>
541ParameterLogger& ParameterLogger::operator<< <int8_t>(const int8_t param);
542
548template <>
549ParameterLogger& ParameterLogger::operator<< <uint8_t>(const uint8_t param);
550
551} // namespace ns3
552 // \ingroup logging
554
555#endif /* NS3_LOG_H */
A single log component configuration.
Definition: log.h:328
const char * Name() const
Get the name of this LogComponent.
Definition: log.cc:310
static ComponentList * GetComponentList()
Get the list of LogComponnents.
Definition: log.cc:74
void Enable(const enum LogLevel level)
Enable this LogComponent at level.
Definition: log.cc:298
std::string File() const
Get the compilation unit defining this LogComponent.
Definition: log.cc:316
int32_t m_levels
Enabled LogLevels.
Definition: log.h:421
static std::string GetLevelLabel(const enum LogLevel level)
Get the string label for the given LogLevel.
Definition: log.cc:323
void EnvVarCheck()
Parse the NS_LOG environment variable for options relating to this LogComponent.
Definition: log.cc:145
void Disable(const enum LogLevel level)
Disable logging at level for this LogComponent.
Definition: log.cc:304
std::string m_file
File defining this LogComponent.
Definition: log.h:424
bool IsNoneEnabled() const
Check if all levels are disabled.
Definition: log.cc:286
void SetMask(const enum LogLevel level)
Prevent the enabling of a specific LogLevel.
Definition: log.cc:292
LogComponent(const std::string &name, const std::string &file, const enum LogLevel mask=LOG_NONE)
Constructor.
Definition: log.cc:104
int32_t m_mask
Blocked LogLevels.
Definition: log.h:422
std::map< std::string, LogComponent * > ComponentList
LogComponent name map.
Definition: log.h:400
std::string m_name
LogComponent name.
Definition: log.h:423
bool IsEnabled(const enum LogLevel level) const
Check if this LogComponent is enabled for level.
Definition: log.cc:279
Insert , when streaming function arguments.
Definition: log.h:440
ParameterLogger & operator<<(T param)
Write a function parameter on the output stream, separating parameters after the first by ,...
Definition: log.h:500
void CommaRest()
Add , before every parameter after the first.
Definition: log.cc:637
bool m_first
First argument flag, doesn't get ,.
Definition: log.h:494
ParameterLogger(std::ostream &os)
Constructor.
Definition: log.cc:631
std::ostream & m_os
Underlying output stream.
Definition: log.h:495
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 LogComponentDisableAll(enum LogLevel level)
Disable all logging for all components.
Definition: log.cc:408
void LogSetTimePrinter(TimePrinter printer)
Set the TimePrinter function to be used to prepend log messages with the simulation time.
Definition: log.cc:603
void(* TimePrinter)(std::ostream &os)
Function signature for features requiring a time formatter, such as logging or ShowProgress.
Definition: time-printer.h:43
void LogComponentDisable(const char *name, enum LogLevel level)
Disable the logging output associated with that log component.
Definition: log.cc:392
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:626
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:113
@ 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.
Definition: log.h:109
@ LOG_ERROR
Serious error messages only.
Definition: log.h:97
@ LOG_WARN
Warning messages.
Definition: log.h:100
@ LOG_INFO
Informational messages (e.g., banners).
Definition: log.h:106
@ LOG_PREFIX_ALL
All prefixes.
Definition: log.h:122
@ LOG_LEVEL_FUNCTION
LOG_FUNCTION and above.
Definition: log.h:110
@ 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:104
@ LOG_PREFIX_LEVEL
Prefix all trace prints with log level (severity).
Definition: log.h:121
@ LOG_LOGIC
Control flow tracing within functions.
Definition: log.h:112
@ LOG_PREFIX_NODE
Prefix all trace prints with simulation node.
Definition: log.h:120
@ LOG_LEVEL_INFO
LOG_INFO and above.
Definition: log.h:107
@ LOG_DEBUG
Rare ad-hoc debug messages.
Definition: log.h:103
TimePrinter LogGetTimePrinter()
Get the LogTimePrinter function currently in use.
Definition: log.cc:614
LogComponent & GetLogComponent(const std::string name)
Get the LogComponent registered with the given name.
Definition: log.cc:128
void LogComponentEnable(const char *name, enum LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:358
void LogSetNodePrinter(NodePrinter printer)
Set the LogNodePrinter function to be used to prepend log messages with the node id.
Definition: log.cc:620
void LogComponentPrintList()
Print the list of logging messages available.
Definition: log.cc:420
void LogComponentEnableAll(enum LogLevel level)
Enable the logging output for all registered log components.
Definition: log.cc:380
Declaration of ns3::NodePrinter function pointer type and ns3::DefaultNodePrinter function.
Declaration of ns3::TimePrinter function pointer type and ns3::DefaultTimePrinter function.