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
88namespace 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
124
137void LogComponentEnable (char const *name, enum LogLevel level);
138
147void LogComponentEnableAll (enum LogLevel level);
148
149
159void LogComponentDisable (char const *name, enum LogLevel level);
160
166void 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 [[maybe_unused]] static LogComponent & 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
293namespace ns3 {
294
300void LogComponentPrintList (void);
301
316
331
332
337{
338public:
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
423private:
428 void EnvVarCheck (void);
429
432 std::string m_name;
433 std::string m_file;
434
435}; // class LogComponent
436
443LogComponent & GetLogComponent (const std::string name);
444
449{
450 bool m_first;
451 std::ostream &m_os;
452
453public:
459 ParameterLogger (std::ostream &os);
460
468 template<typename T>
470
477 template<typename T>
478 ParameterLogger& operator<< (std::vector<T> vector);
479
480};
481
482template<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
498template<typename T>
500ParameterLogger::operator<< (std::vector<T> vector)
501{
502 for (auto i : vector)
503 {
504 *this << i;
505 }
506 return *this;
507}
508
514template<>
516ParameterLogger::operator<< <std::string> (const std::string param);
517
523template<>
525ParameterLogger::operator<< <const char *> (const char * param);
526
532template<>
534ParameterLogger::operator<< <int8_t> (int8_t param);
535
541template<>
543ParameterLogger::operator<< <uint8_t> (uint8_t param);
544
545} // namespace ns3
546 // \ingroup logging
548
549#endif /* NS3_LOG_H */
A single log component configuration.
Definition: log.h:337
void Enable(const enum LogLevel level)
Enable this LogComponent at level.
Definition: log.cc:301
int32_t m_levels
Enabled LogLevels.
Definition: log.h:430
static std::string GetLevelLabel(const enum LogLevel level)
Get the string label for the given LogLevel.
Definition: log.cc:326
static ComponentList * GetComponentList(void)
Get the list of LogComponnents.
Definition: log.cc:75
void Disable(const enum LogLevel level)
Disable logging at level for this LogComponent.
Definition: log.cc:307
bool IsNoneEnabled(void) const
Check if all levels are disabled.
Definition: log.cc:289
std::string m_file
File defining this LogComponent.
Definition: log.h:433
std::string File(void) const
Get the compilation unit defining this LogComponent.
Definition: log.cc:319
void SetMask(const enum LogLevel level)
Prevent the enabling of a specific LogLevel.
Definition: log.cc:295
LogComponent(const std::string &name, const std::string &file, const enum LogLevel mask=LOG_NONE)
Constructor.
Definition: log.cc:107
std::map< std::string, LogComponent * > ComponentList
LogComponent name map.
Definition: log.h:409
char const * Name(void) const
Get the name of this LogComponent.
Definition: log.cc:313
int32_t m_mask
Blocked LogLevels.
Definition: log.h:431
void EnvVarCheck(void)
Parse the NS_LOG environment variable for options relating to this LogComponent.
Definition: log.cc:145
std::string m_name
LogComponent name.
Definition: log.h:432
bool IsEnabled(const enum LogLevel level) const
Check if this LogComponent is enabled for level.
Definition: log.cc:282
Insert , when streaming function arguments.
Definition: log.h:449
bool m_first
First argument flag, doesn't get ,.
Definition: log.h:450
ParameterLogger & operator<<(T param)
Write a function parameter on the output stream, separating parameters after the first by ,...
Definition: log.h:484
ParameterLogger(std::ostream &os)
Constructor.
Definition: log.cc:644
std::ostream & m_os
Underlying output stream.
Definition: log.h:451
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:413
void LogSetTimePrinter(TimePrinter printer)
Set the TimePrinter function to be used to prepend log messages with the simulation time.
Definition: log.cc:620
void LogComponentPrintList(void)
Print the list of logging messages available.
Definition: log.cc:425
void(* TimePrinter)(std::ostream &os)
Function signature for features requiring a time formatter, such as logging or ShowProgress.
Definition: time-printer.h:43
NodePrinter LogGetNodePrinter(void)
Get the LogNodePrinter function currently in use.
Definition: log.cc:638
void(* NodePrinter)(std::ostream &os)
Function signature for prepending the node id to a log message.
Definition: node-printer.h:40
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(void)
Get the LogTimePrinter function currently in use.
Definition: log.cc:629
LogComponent & GetLogComponent(const std::string name)
Get the LogComponent registered with the given name.
Definition: log.cc:128
void LogComponentDisable(char const *name, enum LogLevel level)
Disable the logging output associated with that log component.
Definition: log.cc:397
void LogComponentEnable(char const *name, enum LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:361
void LogSetNodePrinter(NodePrinter printer)
Set the LogNodePrinter function to be used to prepend log messages with the node id.
Definition: log.cc:634
void LogComponentEnableAll(enum LogLevel level)
Enable the logging output for all registered log components.
Definition: log.cc:385
Declaration of ns3::NodePrinter function pointer type and ns3::DefaultNodePrinter function.
Declaration of ns3::TimePrinter function pointer type and ns3::DefaultTimePrinter function.