|
A Discrete-Event Network Simulator
|
API
|
Go to the documentation of this file.
27 #include "ns3/core-config.h"
84 const char *envVar = std::getenv (
"NS_LOG");
85 if (envVar == 0 || std::strlen (envVar) == 0)
89 std::string env = envVar;
90 std::string::size_type cur = 0;
91 std::string::size_type next = 0;
92 while (next != std::string::npos)
94 next = env.find_first_of (
":", cur);
95 std::string tmp = std::string (env, cur, next - cur);
96 if (tmp ==
"print-list")
108 const std::string &
file,
110 : m_levels (0), m_mask (mask), m_name (name), m_file (
file)
115 for (LogComponent::ComponentList::const_iterator i = components->begin ();
116 i != components->end ();
119 if (i->first == name)
121 NS_FATAL_ERROR (
"Log component \"" << name <<
"\" has already been registered once.");
124 components->insert (std::make_pair (name,
this));
135 ret = components->at (name);
137 catch (std::out_of_range&)
139 NS_FATAL_ERROR (
"Log component \"" << name <<
"\" does not exist.");
147 const char *envVar = std::getenv (
"NS_LOG");
148 if (envVar == 0 || std::strlen (envVar) == 0)
152 std::string env = envVar;
154 std::string::size_type cur = 0;
155 std::string::size_type next = 0;
156 while (next != std::string::npos)
158 next = env.find_first_of (
":", cur);
159 std::string tmp = std::string (env, cur, next - cur);
160 std::string::size_type equal = tmp.find (
"=");
161 std::string component;
162 if (equal == std::string::npos)
165 if (component ==
m_name || component ==
"*" || component ==
"***")
174 component = tmp.substr (0, equal);
175 if (component ==
m_name || component ==
"*")
178 std::string::size_type cur_lev;
179 std::string::size_type next_lev = equal;
180 bool pre_pipe =
true;
183 cur_lev = next_lev + 1;
184 next_lev = tmp.find (
"|", cur_lev);
185 std::string lev = tmp.substr (cur_lev, next_lev - cur_lev);
190 else if (lev ==
"warn")
194 else if (lev ==
"debug")
198 else if (lev ==
"info")
202 else if (lev ==
"function")
206 else if (lev ==
"logic")
210 else if ( pre_pipe && ( (lev ==
"all") || (lev ==
"*") ) )
214 else if ( (lev ==
"prefix_func") || (lev ==
"func") )
218 else if ( (lev ==
"prefix_time") || (lev ==
"time") )
222 else if ( (lev ==
"prefix_node") || (lev ==
"node") )
226 else if ( (lev ==
"prefix_level") || (lev ==
"level") )
230 else if ( (lev ==
"prefix_all")
231 || (!pre_pipe && ( (lev ==
"all") || (lev ==
"*") ) )
236 else if (lev ==
"level_error")
240 else if (lev ==
"level_warn")
244 else if (lev ==
"level_debug")
248 else if (lev ==
"level_info")
252 else if (lev ==
"level_function")
256 else if (lev ==
"level_logic")
260 else if (lev ==
"level_all")
264 else if (lev ==
"**")
271 while (next_lev != std::string::npos);
364 LogComponent::ComponentList::const_iterator i;
365 for (i = components->begin ();
366 i != components->end ();
369 if (i->first.compare (name) == 0)
371 i->second->Enable (level);
375 if (i == components->end ())
380 "\" not found. See above for a list of available log components");
388 for (LogComponent::ComponentList::const_iterator i = components->begin ();
389 i != components->end ();
392 i->second->Enable (level);
400 for (LogComponent::ComponentList::const_iterator i = components->begin ();
401 i != components->end ();
404 if (i->first.compare (name) == 0)
406 i->second->Disable (level);
416 for (LogComponent::ComponentList::const_iterator i = components->begin ();
417 i != components->end ();
420 i->second->Disable (level);
428 for (LogComponent::ComponentList::const_iterator i = components->begin ();
429 i != components->end ();
432 std::cout << i->first <<
"=";
433 if (i->second->IsNoneEnabled ())
435 std::cout <<
"0" << std::endl;
446 std::cout <<
"error";
448 if (i->second->IsEnabled (
LOG_WARN))
450 std::cout <<
"|warn";
454 std::cout <<
"|debug";
456 if (i->second->IsEnabled (
LOG_INFO))
458 std::cout <<
"|info";
462 std::cout <<
"|function";
466 std::cout <<
"|logic";
471 std::cout <<
"|prefix_all";
477 std::cout <<
"|func";
481 std::cout <<
"|time";
485 std::cout <<
"|node";
489 std::cout <<
"|level";
492 std::cout << std::endl;
506 char const*name = componentName.c_str ();
508 LogComponent::ComponentList::const_iterator i;
509 for (i = components->begin ();
510 i != components->end ();
513 if (i->first.compare (name) == 0)
530 const char *envVar = std::getenv (
"NS_LOG");
531 if (envVar == 0 || std::strlen (envVar) == 0)
536 std::string env = envVar;
537 std::string::size_type cur = 0;
538 std::string::size_type next = 0;
540 while (next != std::string::npos)
542 next = env.find_first_of (
":", cur);
543 std::string tmp = std::string (env, cur, next - cur);
544 std::string::size_type equal = tmp.find (
"=");
545 std::string component;
546 if (equal == std::string::npos)
550 if (
ComponentExists (component) || component ==
"*" || component ==
"***")
557 NS_FATAL_ERROR (
"Invalid or unregistered component name \"" << component <<
558 "\" in env variable NS_LOG, see above for a list of valid components");
563 component = tmp.substr (0, equal);
566 std::string::size_type cur_lev;
567 std::string::size_type next_lev = equal;
570 cur_lev = next_lev + 1;
571 next_lev = tmp.find (
"|", cur_lev);
572 std::string lev = tmp.substr (cur_lev, next_lev - cur_lev);
580 || lev ==
"prefix_func"
582 || lev ==
"prefix_time"
584 || lev ==
"prefix_node"
586 || lev ==
"prefix_level"
588 || lev ==
"prefix_all"
589 || lev ==
"level_error"
590 || lev ==
"level_warn"
591 || lev ==
"level_debug"
592 || lev ==
"level_info"
593 || lev ==
"level_function"
594 || lev ==
"level_logic"
595 || lev ==
"level_all"
605 "\" in env variable NS_LOG for component name " << component);
608 while (next_lev != std::string::npos);
613 NS_FATAL_ERROR (
"Invalid or unregistered component name \"" << component <<
614 "\" in env variable NS_LOG, see above for a list of valid components");
651 ParameterLogger::operator<< <std::string> (
const std::string param)
655 m_os <<
"\"" << param <<
"\"";
660 m_os <<
", \"" << param <<
"\"";
667 ParameterLogger::operator<< <const char *> (
const char * param)
669 (*this) << std::string (param);
675 ParameterLogger::operator<< <int8_t> (
const int8_t param)
679 m_os << static_cast<int16_t> (param);
684 m_os <<
", " <<
static_cast<int16_t
> (param);
691 ParameterLogger::operator<< <uint8_t> (
const uint8_t param)
695 m_os << static_cast<uint16_t> (param);
700 m_os <<
", " <<
static_cast<uint16_t
> (param);
void LogSetTimePrinter(TimePrinter printer)
Set the TimePrinter function to be used to prepend log messages with the simulation time.
@ LOG_LEVEL_ERROR
LOG_ERROR and above.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
NS_FATAL_x macro definitions.
@ LOG_ERROR
Serious error messages only.
@ LOG_LEVEL_INFO
LOG_INFO and above.
@ LOG_FUNCTION
Function tracing.
LogComponent & GetLogComponent(const std::string name)
Get the LogComponent registered with the given name.
@ LOG_LEVEL_FUNCTION
LOG_FUNCTION and above.
bool IsNoneEnabled(void) const
Check if all levels are disabled.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void LogComponentDisable(char const *name, enum LogLevel level)
Disable the logging output associated with that log component.
@ LOG_PREFIX_NODE
Prefix all trace prints with simulation node.
static std::string GetLevelLabel(const enum LogLevel level)
Get the string label for the given LogLevel.
@ LOG_LEVEL_ALL
Print everything.
@ LOG_LEVEL_WARN
LOG_WARN and above.
void LogComponentEnable(char const *name, enum LogLevel level)
Enable the logging output associated with that log component.
void LogComponentEnableAll(enum LogLevel level)
Enable the logging output for all registered log components.
NS_ASSERT() and NS_ASSERT_MSG() macro definitions.
ParameterLogger(std::ostream &os)
Constructor.
void EnvVarCheck(void)
Parse the NS_LOG environment variable for options relating to this LogComponent.
void LogSetNodePrinter(NodePrinter printer)
Set the LogNodePrinter function to be used to prepend log messages with the node id.
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
bool m_first
First argument flag, doesn't get ,.
static void CheckEnvironmentVariables(void)
Parse the NS_LOG environment variable.
void Disable(const enum LogLevel level)
Disable logging at level for this LogComponent.
Handler for print-list token in NS_LOG to print the list of log components.
NodePrinter LogGetNodePrinter(void)
Get the LogNodePrinter function currently in use.
static TimePrinter g_logTimePrinter
The Log TimePrinter.
void Enable(const enum LogLevel level)
Enable this LogComponent at level.
@ LOG_PREFIX_LEVEL
Prefix all trace prints with log level (severity).
std::map< std::string, LogComponent * > ComponentList
LogComponent name map.
std::ostream & m_os
Underlying output stream.
TimePrinter LogGetTimePrinter(void)
Get the LogTimePrinter function currently in use.
static ComponentList * GetComponentList(void)
Get the list of LogComponnents.
@ LOG_LEVEL_DEBUG
LOG_DEBUG and above.
static NodePrinter g_logNodePrinter
The Log NodePrinter.
@ LOG_PREFIX_TIME
Prefix all trace prints with simulation time.
bool IsEnabled(const enum LogLevel level) const
Check if this LogComponent is enabled for level.
@ LOG_LOGIC
Control flow tracing within functions.
std::string m_file
File defining this LogComponent.
@ LOG_PREFIX_FUNC
Prefix all trace prints with function.
@ LOG_PREFIX_ALL
All prefixes.
void(* NodePrinter)(std::ostream &os)
Function signature for prepending the node id to a log message.
LogLevel
Logging severity classes and levels.
Insert , when streaming function arguments.
void(* TimePrinter)(std::ostream &os)
Function signature for features requiring a time formatter, such as logging or ShowProgress.
static bool ComponentExists(std::string componentName)
Check if a log component exists.
LogComponent(const std::string &name, const std::string &file, const enum LogLevel mask=LOG_NONE)
Constructor.
A single log component configuration.
int32_t m_mask
Blocked LogLevels.
int32_t m_levels
Enabled LogLevels.
@ LOG_WARN
Warning messages.
void LogComponentPrintList(void)
Print the list of logging messages available.
std::string m_name
LogComponent name.
@ LOG_DEBUG
Rare ad-hoc debug messages.
void SetMask(const enum LogLevel level)
Prevent the enabling of a specific LogLevel.
std::string File(void) const
Get the compilation unit defining this LogComponent.
@ LOG_LEVEL_LOGIC
LOG_LOGIC and above.
char const * Name(void) const
Get the name of this LogComponent.
static PrintList g_printList
Invoke handler for print-list in NS_LOG environment variable.
void LogComponentDisableAll(enum LogLevel level)
Disable all logging for all components.
@ LOG_INFO
Informational messages (e.g., banners).