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);
NS_ASSERT() and NS_ASSERT_MSG() macro definitions.
A single log component configuration.
void Enable(const enum LogLevel level)
Enable this LogComponent at level.
int32_t m_levels
Enabled LogLevels.
static std::string GetLevelLabel(const enum LogLevel level)
Get the string label for the given LogLevel.
static ComponentList * GetComponentList(void)
Get the list of LogComponnents.
void Disable(const enum LogLevel level)
Disable logging at level for this LogComponent.
bool IsNoneEnabled(void) const
Check if all levels are disabled.
std::string m_file
File defining this LogComponent.
std::string File(void) const
Get the compilation unit defining this LogComponent.
void SetMask(const enum LogLevel level)
Prevent the enabling of a specific LogLevel.
LogComponent(const std::string &name, const std::string &file, const enum LogLevel mask=LOG_NONE)
Constructor.
std::map< std::string, LogComponent * > ComponentList
LogComponent name map.
char const * Name(void) const
Get the name of this LogComponent.
int32_t m_mask
Blocked LogLevels.
void EnvVarCheck(void)
Parse the NS_LOG environment variable for options relating to this LogComponent.
std::string m_name
LogComponent name.
bool IsEnabled(const enum LogLevel level) const
Check if this LogComponent is enabled for level.
Insert , when streaming function arguments.
bool m_first
First argument flag, doesn't get ,.
ParameterLogger(std::ostream &os)
Constructor.
std::ostream & m_os
Underlying output stream.
Handler for print-list token in NS_LOG to print the list of log components.
NS_FATAL_x macro definitions.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
static NodePrinter g_logNodePrinter
The Log NodePrinter.
static bool ComponentExists(std::string componentName)
Check if a log component exists.
static void CheckEnvironmentVariables(void)
Parse the NS_LOG environment variable.
static TimePrinter g_logTimePrinter
The Log TimePrinter.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
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.
void LogSetTimePrinter(TimePrinter printer)
Set the TimePrinter function to be used to prepend log messages with the simulation time.
void LogComponentPrintList(void)
Print the list of logging messages available.
void(* TimePrinter)(std::ostream &os)
Function signature for features requiring a time formatter, such as logging or ShowProgress.
NodePrinter LogGetNodePrinter(void)
Get the LogNodePrinter function currently in use.
void(* NodePrinter)(std::ostream &os)
Function signature for prepending the node id to a log message.
LogLevel
Logging severity classes and levels.
@ LOG_LEVEL_ALL
Print everything.
@ LOG_PREFIX_FUNC
Prefix all trace prints with function.
@ LOG_LEVEL_LOGIC
LOG_LOGIC and above.
@ LOG_PREFIX_TIME
Prefix all trace prints with simulation time.
@ LOG_FUNCTION
Function tracing.
@ LOG_ERROR
Serious error messages only.
@ LOG_WARN
Warning messages.
@ LOG_INFO
Informational messages (e.g., banners).
@ LOG_PREFIX_ALL
All prefixes.
@ LOG_LEVEL_FUNCTION
LOG_FUNCTION and above.
@ LOG_LEVEL_ERROR
LOG_ERROR and above.
@ LOG_LEVEL_WARN
LOG_WARN and above.
@ LOG_LEVEL_DEBUG
LOG_DEBUG and above.
@ LOG_PREFIX_LEVEL
Prefix all trace prints with log level (severity).
@ LOG_LOGIC
Control flow tracing within functions.
@ LOG_PREFIX_NODE
Prefix all trace prints with simulation node.
@ LOG_LEVEL_INFO
LOG_INFO and above.
@ LOG_DEBUG
Rare ad-hoc debug messages.
TimePrinter LogGetTimePrinter(void)
Get the LogTimePrinter function currently in use.
LogComponent & GetLogComponent(const std::string name)
Get the LogComponent registered with the given name.
void LogComponentDisable(char const *name, enum LogLevel level)
Disable the logging output associated with that log component.
void LogComponentEnable(char const *name, enum LogLevel level)
Enable the logging output associated with that log component.
void LogSetNodePrinter(NodePrinter printer)
Set the LogNodePrinter function to be used to prepend log messages with the node id.
void LogComponentEnableAll(enum LogLevel level)
Enable the logging output for all registered log components.