A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
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
35/**
36 * \file
37 * \ingroup logging
38 * Debug message logging
39 */
40
41/**
42 * \ingroup debugging
43 * \defgroup logging Logging
44 *
45 * \brief Logging functions and macros
46 *
47 * LOG functionality: macros which allow developers to
48 * send information to the \c std::clog output stream.
49 *
50 * All logging messages are disabled by default. To enable selected logging
51 * messages, use the ns3::LogComponentEnable
52 * function or use the NS_LOG environment variable
53 *
54 * Use the environment variable NS_LOG to define a ':'-separated list of
55 * logging components to enable. For example (using bash syntax),
56 * \code
57 * $ NS_LOG="OlsrAgent" ./ns3 run ...
58 * \endcode
59 * would enable one component at all log levels.
60 * \code
61 * $NS_LOG="OlsrAgent:Ipv4L3Protocol" ./ns3 run ...
62 * \endcode
63 * would enable two components, at all log levels, etc.
64 * \c NS_LOG="*" will enable all available log components at all levels.
65 *
66 * To control more selectively the log levels for each component, use
67 * this syntax:
68 * \code
69 * $ NS_LOG='Component1=func|warn:Component2=error|debug'
70 * \endcode
71 * This example would enable the \c func, and \c warn log
72 * levels for 'Component1' and the \c error and \c debug log levels
73 * for 'Component2'. The wildcard '*' can be used here as well. For example
74 * \c NS_LOG='*=level_all|prefix' would enable all log levels and prefix all
75 * prints with the component and function names.
76 *
77 * A note on NS_LOG_FUNCTION() and NS_LOG_FUNCTION_NOARGS():
78 * generally, use of (at least) NS_LOG_FUNCTION(this) is preferred,
79 * with the any function parameters added:
80 * \code
81 * NS_LOG_FUNCTION (this << arg1 << args);
82 * \endcode
83 * Use NS_LOG_FUNCTION_NOARGS() only in static functions with no arguments.
84 */
85/** @{ */
86
87namespace ns3
88{
89
90/**
91 * Logging severity classes and levels.
92 */
94{
95 LOG_NONE = 0x00000000, //!< No logging.
96
97 LOG_ERROR = 0x00000001, //!< Serious error messages only.
98 LOG_LEVEL_ERROR = 0x00000001, //!< LOG_ERROR and above.
99
100 LOG_WARN = 0x00000002, //!< Warning messages.
101 LOG_LEVEL_WARN = 0x00000003, //!< LOG_WARN and above.
102
103 LOG_INFO = 0x00000004, //!< Something happened to change state.
104 LOG_LEVEL_INFO = 0x00000007, //!< LOG_INFO and above.
105
106 LOG_FUNCTION = 0x00000008, //!< Function tracing for non-trivial function calls.
107 LOG_LEVEL_FUNCTION = 0x0000000f, //!< LOG_FUNCTION and above.
108
109 LOG_LOGIC = 0x00000010, //!< Debugging logs for key branches and decisions in a function.
110 LOG_LEVEL_LOGIC = 0x0000001f, //!< LOG_LOGIC and above.
111
112 LOG_DEBUG = 0x00000020, //!< Full voluminous logging to support debugging.
113 LOG_LEVEL_DEBUG = 0x0000003f, //!< LOG_DEBUG and above.
114
115 LOG_ALL = 0x0fffffff, //!< Print everything.
116 LOG_LEVEL_ALL = LOG_ALL, //!< Print everything.
117
118 LOG_PREFIX_FUNC = 0x80000000, //!< Prefix all trace prints with function.
119 LOG_PREFIX_TIME = 0x40000000, //!< Prefix all trace prints with simulation time.
120 LOG_PREFIX_NODE = 0x20000000, //!< Prefix all trace prints with simulation node.
121 LOG_PREFIX_LEVEL = 0x10000000, //!< Prefix all trace prints with log level (severity).
122 LOG_PREFIX_ALL = 0xf0000000 //!< All prefixes.
124
125/**
126 * Enable the logging output associated with that log component.
127 *
128 * The logging output can be later disabled with a call
129 * to ns3::LogComponentDisable.
130 *
131 * Same as running your program with the NS_LOG environment
132 * variable set as NS_LOG='name=level'.
133 *
134 * \param [in] name The log component name.
135 * \param [in] level The logging level.
136 */
137void LogComponentEnable(const std::string& name, LogLevel level);
138
139/**
140 * Enable the logging output for all registered log components.
141 *
142 * Same as running your program with the NS_LOG environment
143 * variable set as NS_LOG='*=level'
144 *
145 * \param [in] level The logging level.
146 */
148
149/**
150 * Disable the logging output associated with that log component.
151 *
152 * The logging output can be later re-enabled with a call
153 * to LogComponentEnable.
154 *
155 * \param [in] name The log component name.
156 * \param [in] level The logging level.
157 */
158void LogComponentDisable(const std::string& name, LogLevel level);
159
160/**
161 * Disable all logging for all components.
162 *
163 * \param [in] level The logging level.
164 */
166
167} // namespace ns3
168
169/**
170 * Define a Log component with a specific name.
171 *
172 * This macro should be used at the top of every file in which you want
173 * to use the NS_LOG macro. This macro defines a new
174 * "log component" which can be later selectively enabled
175 * or disabled with the ns3::LogComponentEnable and
176 * ns3::LogComponentDisable functions or with the NS_LOG
177 * environment variable.
178 *
179 * LogComponent names should be simple string tokens, _i.e._,
180 * "ArfWifiManager", not "ns3::ArfWifiManager".
181 *
182 * This macro should be placed within namespace ns3. If functions
183 * outside of namespace ns3 require access to logging, the preferred
184 * solution is to add the following 'using' directive at file scope,
185 * outside of namespace ns3, and after the inclusion of
186 * NS_LOG_COMPONENT_DEFINE, such as follows:
187 * \code
188 * namespace ns3 {
189 * NS_LOG_COMPONENT_DEFINE ("...");
190 *
191 * // Definitions within the ns3 namespace
192 *
193 * } // namespace ns3
194 *
195 * using ns3::g_log;
196 *
197 * // Further definitions outside of the ns3 namespace
198 *\endcode
199 *
200 * \param [in] name The log component name.
201 */
202#define NS_LOG_COMPONENT_DEFINE(name) \
203 static ns3::LogComponent g_log = ns3::LogComponent(name, __FILE__)
204
205/**
206 * Define a logging component with a mask.
207 *
208 * See LogComponent().
209 *
210 * \param [in] name The log component name.
211 * \param [in] mask The default mask.
212 */
213#define NS_LOG_COMPONENT_DEFINE_MASK(name, mask) \
214 static ns3::LogComponent g_log = ns3::LogComponent(name, __FILE__, mask)
215
216/**
217 * Declare a reference to a Log component.
218 *
219 * This macro should be used in the declaration of template classes
220 * to allow their methods (defined in an header file) to make use of
221 * the NS_LOG_* macros. This macro should be used in the private
222 * section to prevent subclasses from using the same log component
223 * as the base class.
224 */
225#define NS_LOG_TEMPLATE_DECLARE LogComponent& g_log
226
227/**
228 * Initialize a reference to a Log component.
229 *
230 * This macro should be used in the constructor of template classes
231 * to allow their methods (defined in an header file) to make use of
232 * the NS_LOG_* macros.
233 *
234 * \param [in] name The log component name.
235 */
236#define NS_LOG_TEMPLATE_DEFINE(name) g_log(GetLogComponent(name))
237
238/**
239 * Declare and initialize a reference to a Log component.
240 *
241 * This macro should be used in static template methods to allow their
242 * methods (defined in an header file) to make use of the NS_LOG_* macros.
243 *
244 * \param [in] name The log component name.
245 */
246#define NS_LOG_STATIC_TEMPLATE_DEFINE(name) \
247 static LogComponent& g_log [[maybe_unused]] = GetLogComponent(name)
248
249/**
250 * Use \ref NS_LOG to output a message of level LOG_ERROR.
251 *
252 * \param [in] msg The message to log.
253 */
254#define NS_LOG_ERROR(msg) NS_LOG(ns3::LOG_ERROR, msg)
255
256/**
257 * Use \ref NS_LOG to output a message of level LOG_WARN.
258 *
259 * \param [in] msg The message to log.
260 */
261#define NS_LOG_WARN(msg) NS_LOG(ns3::LOG_WARN, msg)
262
263/**
264 * Use \ref NS_LOG to output a message of level LOG_DEBUG.
265 *
266 * \param [in] msg The message to log.
267 */
268#define NS_LOG_DEBUG(msg) NS_LOG(ns3::LOG_DEBUG, msg)
269
270/**
271 * Use \ref NS_LOG to output a message of level LOG_INFO.
272 *
273 * \param [in] msg The message to log.
274 */
275#define NS_LOG_INFO(msg) NS_LOG(ns3::LOG_INFO, msg)
276
277/**
278 * Use \ref NS_LOG to output a message of level LOG_LOGIC
279 *
280 * \param [in] msg The message to log.
281 */
282#define NS_LOG_LOGIC(msg) NS_LOG(ns3::LOG_LOGIC, msg)
283
284namespace ns3
285{
286
287/**
288 * Print the list of logging messages available.
289 * Same as running your program with the NS_LOG environment
290 * variable set as NS_LOG=print-list
291 */
293
294/**
295 * Set the TimePrinter function to be used
296 * to prepend log messages with the simulation time.
297 *
298 * The default is DefaultTimePrinter().
299 *
300 * \param [in] lp The TimePrinter function.
301 */
303/**
304 * Get the LogTimePrinter function currently in use.
305 * \returns The current LogTimePrinter function.
306 */
308
309/**
310 * Set the LogNodePrinter function to be used
311 * to prepend log messages with the node id.
312 *
313 * The default is DefaultNodePrinter().
314 *
315 * \param [in] np The LogNodePrinter function.
316 */
318/**
319 * Get the LogNodePrinter function currently in use.
320 * \returns The current LogNodePrinter function.
321 */
323
324/**
325 * A single log component configuration.
326 */
328{
329 public:
330 /**
331 * Constructor.
332 *
333 * \param [in] name The user-visible name for this component.
334 * \param [in] file The source code file which defined this LogComponent.
335 * \param [in] mask LogLevels blocked for this LogComponent. Blocking
336 * a log level helps prevent recursion by logging in
337 * functions which help implement the logging facility.
338 */
339 LogComponent(const std::string& name, const std::string& file, const LogLevel mask = LOG_NONE);
340 /**
341 * Check if this LogComponent is enabled for \c level
342 *
343 * \param [in] level The level to check for.
344 * \return \c true if we are enabled at \c level.
345 */
346 bool IsEnabled(const LogLevel level) const;
347 /**
348 * Check if all levels are disabled.
349 *
350 * \return \c true if all levels are disabled.
351 */
352 bool IsNoneEnabled() const;
353 /**
354 * Enable this LogComponent at \c level
355 *
356 * \param [in] level The LogLevel to enable.
357 */
358 void Enable(const LogLevel level);
359 /**
360 * Disable logging at \c level for this LogComponent.
361 *
362 * \param [in] level The LogLevel to disable.
363 */
364 void Disable(const LogLevel level);
365 /**
366 * Get the name of this LogComponent.
367 *
368 * \return The name of this LogComponent.
369 */
370 std::string Name() const;
371 /**
372 * Get the compilation unit defining this LogComponent.
373 * \returns The file name.
374 */
375 std::string File() const;
376 /**
377 * Get the string label for the given LogLevel.
378 *
379 * \param [in] level The LogLevel to get the label for.
380 * \return The string label for \c level.
381 */
382 static std::string GetLevelLabel(const LogLevel level);
383 /**
384 * Prevent the enabling of a specific LogLevel.
385 *
386 * \param [in] level The LogLevel to block.
387 */
388 void SetMask(const LogLevel level);
389
390 /**
391 * LogComponent name map.
392 *
393 * \internal
394 * This should really be considered an internal API.
395 * It is exposed here to allow print-introspected-doxygen.cc
396 * to generate a list of all LogComponents.
397 */
398 using ComponentList = std::unordered_map<std::string, LogComponent*>;
399
400 /**
401 * Get the list of LogComponents.
402 *
403 * \internal
404 * This should really be considered an internal API.
405 * It is exposed here to allow print-introspected-doxygen.cc
406 * to generate a list of all LogComponents.
407 *
408 * \returns The list of LogComponents.
409 */
411
412 private:
413 /**
414 * Parse the `NS_LOG` environment variable for options relating to this
415 * LogComponent.
416 */
417 void EnvVarCheck();
418
419 int32_t m_levels; //!< Enabled LogLevels.
420 int32_t m_mask; //!< Blocked LogLevels.
421 std::string m_name; //!< LogComponent name.
422 std::string m_file; //!< File defining this LogComponent.
423
424}; // class LogComponent
425
426/**
427 * Get the LogComponent registered with the given name.
428 *
429 * \param [in] name The name of the LogComponent.
430 * \return a reference to the requested LogComponent
431 */
432LogComponent& GetLogComponent(const std::string name);
433
434/**
435 * Insert `, ` when streaming function arguments.
436 */
438{
439 public:
440 /**
441 * Constructor.
442 *
443 * \param [in] os Underlying output stream.
444 */
445 ParameterLogger(std::ostream& os);
446
447 /**
448 * Write a function parameter on the output stream,
449 * separating parameters after the first by `,` strings.
450 *
451 * \param [in] param The function parameter.
452 * \return This ParameterLogger, so it's chainable.
453 */
454 template <typename T>
455 ParameterLogger& operator<<(const T& param);
456
457 /**
458 * Overload for vectors, to print each element.
459 *
460 * \param [in] vector The vector of parameters
461 * \return This ParameterLogger, so it's chainable.
462 */
463 template <typename T>
464 ParameterLogger& operator<<(const std::vector<T>& vector);
465
466 private:
467 /** Add `, ` before every parameter after the first. */
468 void CommaRest();
469
470 bool m_first{true}; //!< First argument flag, doesn't get `, `.
471 std::ostream& m_os; //!< Underlying output stream.
472};
473
474template <typename T>
476ParameterLogger::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
497template <typename T>
499ParameterLogger::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
510/**@}*/ // \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
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
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.