26 #include "ns3/core-config.h"
89 char *envVar = getenv (
"NS_LOG");
94 std::string env = envVar;
95 std::string::size_type cur = 0;
96 std::string::size_type next = 0;
97 while (next != std::string::npos)
99 next = env.find_first_of (
":", cur);
100 std::string tmp = std::string (env, cur, next-cur);
101 if (tmp ==
"print-list")
114 const std::string & file,
116 : m_levels (0), m_mask (mask), m_name (name), m_file (file)
121 for (LogComponent::ComponentList::const_iterator i = components->begin ();
122 i != components->end ();
125 if (i->first == name)
127 NS_FATAL_ERROR (
"Log component \""<<name<<
"\" has already been registered once.");
130 components->insert (std::make_pair (name,
this));
137 char *envVar = getenv (
"NS_LOG");
142 std::string env = envVar;
144 std::string::size_type cur = 0;
145 std::string::size_type next = 0;
146 while (next != std::string::npos)
148 next = env.find_first_of (
":", cur);
149 std::string tmp = std::string (env, cur, next-cur);
150 std::string::size_type equal = tmp.find (
"=");
151 std::string component;
152 if (equal == std::string::npos)
155 if (component ==
m_name || component ==
"*" || component ==
"***")
164 component = tmp.substr (0, equal);
165 if (component ==
m_name || component ==
"*")
168 std::string::size_type cur_lev;
169 std::string::size_type next_lev = equal;
170 bool pre_pipe =
true;
173 cur_lev = next_lev + 1;
174 next_lev = tmp.find (
"|", cur_lev);
175 std::string lev = tmp.substr (cur_lev, next_lev - cur_lev);
180 else if (lev ==
"warn")
184 else if (lev ==
"debug")
188 else if (lev ==
"info")
192 else if (lev ==
"function")
196 else if (lev ==
"logic")
200 else if ( pre_pipe && ( (lev ==
"all") || (lev ==
"*") ) )
204 else if ( (lev ==
"prefix_func") || (lev ==
"func") )
208 else if ( (lev ==
"prefix_time") || (lev ==
"time") )
212 else if ( (lev ==
"prefix_node") || (lev ==
"node") )
216 else if ( (lev ==
"prefix_level") || (lev ==
"level") )
220 else if ( (lev ==
"prefix_all") ||
221 (!pre_pipe && ( (lev ==
"all") || (lev ==
"*") ) )
226 else if (lev ==
"level_error")
230 else if (lev ==
"level_warn")
234 else if (lev ==
"level_debug")
238 else if (lev ==
"level_info")
242 else if (lev ==
"level_function")
246 else if (lev ==
"level_logic")
250 else if (lev ==
"level_all")
254 else if (lev ==
"**")
260 }
while (next_lev != std::string::npos);
354 LogComponent::ComponentList::const_iterator i;
355 for (i = components->begin ();
356 i != components->end ();
359 if (i->first.compare (name) == 0)
361 i->second->Enable (level);
365 if (i == components->end())
370 "\" not found. See above for a list of available log components");
378 for (LogComponent::ComponentList::const_iterator i = components->begin ();
379 i != components->end ();
382 i->second->Enable (level);
390 for (LogComponent::ComponentList::const_iterator i = components->begin ();
391 i != components->end ();
394 if (i->first.compare (name) == 0)
396 i->second->Disable (level);
406 for (LogComponent::ComponentList::const_iterator i = components->begin ();
407 i != components->end ();
410 i->second->Disable (level);
418 for (LogComponent::ComponentList::const_iterator i = components->begin ();
419 i != components->end ();
422 std::cout << i->first <<
"=";
423 if (i->second->IsNoneEnabled ())
425 std::cout <<
"0" << std::endl;
436 std::cout <<
"error";
438 if (i->second->IsEnabled (
LOG_WARN))
440 std::cout <<
"|warn";
444 std::cout <<
"|debug";
446 if (i->second->IsEnabled (
LOG_INFO))
448 std::cout <<
"|info";
452 std::cout <<
"|function";
456 std::cout <<
"|logic";
461 std::cout <<
"|prefix_all";
467 std::cout <<
"|func";
471 std::cout <<
"|time";
475 std::cout <<
"|node";
479 std::cout <<
"|level";
482 std::cout << std::endl;
496 char const*name=componentName.c_str();
498 LogComponent::ComponentList::const_iterator i;
499 for (i = components->begin ();
500 i != components->end ();
503 if (i->first.compare (name) == 0)
521 char *envVar = getenv (
"NS_LOG");
522 if (envVar == 0 || std::strlen(envVar) == 0)
526 std::string env = envVar;
528 std::string::size_type cur = 0;
529 std::string::size_type next = 0;
531 while (next != std::string::npos)
533 next = env.find_first_of (
":", cur);
534 std::string tmp = std::string (env, cur, next-cur);
535 std::string::size_type equal = tmp.find (
"=");
536 std::string component;
537 if (equal == std::string::npos)
541 if (
ComponentExists(component) || component ==
"*" || component ==
"***")
548 NS_FATAL_ERROR(
"Invalid or unregistered component name \"" << component <<
549 "\" in env variable NS_LOG, see above for a list of valid components");
554 component = tmp.substr (0, equal);
557 std::string::size_type cur_lev;
558 std::string::size_type next_lev = equal;
561 cur_lev = next_lev + 1;
562 next_lev = tmp.find (
"|", cur_lev);
563 std::string lev = tmp.substr (cur_lev, next_lev - cur_lev);
571 || lev ==
"prefix_func"
573 || lev ==
"prefix_time"
575 || lev ==
"prefix_node"
577 || lev ==
"prefix_level"
579 || lev ==
"prefix_all"
580 || lev ==
"level_error"
581 || lev ==
"level_warn"
582 || lev ==
"level_debug"
583 || lev ==
"level_info"
584 || lev ==
"level_function"
585 || lev ==
"level_logic"
586 || lev ==
"level_all"
596 "\" in env variable NS_LOG for component name " << component);
598 }
while (next_lev != std::string::npos);
603 NS_FATAL_ERROR(
"Invalid or unregistered component name \"" << component <<
604 "\" in env variable NS_LOG, see above for a list of valid components");
NS_FATAL_x macro definitions.
LogComponent(const std::string &name, const std::string &file, const enum LogLevel mask=LOG_NONE)
Constructor.
Serious error messages only.
Prefix all trace prints with simulation node.
void LogComponentDisable(char const *name, enum LogLevel level)
Disable the logging output associated with that log component.
static bool ComponentExists(std::string componentName)
Check if a log component exists.
Informational messages (e.g., banners).
static void CheckEnvironmentVariables(void)
Parse the NS_LOG environment variable.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
static LogTimePrinter g_logTimePrinter
The LogTimePrinter.
ParameterLogger(std::ostream &os)
Constructor.
void Disable(const enum LogLevel level)
Disable logging at level for this LogComponent.
void EnvVarCheck(void)
Parse the NS_LOG environment variable for options relating to this LogComponent.
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
std::string m_file
File defining this LogComponent.
void LogComponentEnable(char const *name, enum LogLevel level)
Enable the logging output associated with that log component.
void Enable(const enum LogLevel level)
Enable this LogComponent at level.
bool IsEnabled(const enum LogLevel level) const
Check if this LogComponent is enabled for level.
void LogSetNodePrinter(LogNodePrinter printer)
Set the LogNodePrinter function to be used to prepend log messages with the node id.
void SetMask(const enum LogLevel level)
Prevent the enabling of a specific LogLevel.
Definition of assertion macros NS_ASSERT() and NS_ASSERT_MSG().
Prefix all trace prints with log level (severity).
void LogComponentPrintList(void)
Print the list of logging messages available.
Control flow tracing within functions.
LogNodePrinter LogGetNodePrinter(void)
Get the LogNodePrinter function currently in use.
std::map< std::string, LogComponent * > ComponentList
LogComponent name map.
void LogComponentEnableAll(enum LogLevel level)
Enable the logging output for all registered log components.
Prefix all trace prints with simulation time.
void LogComponentDisableAll(enum LogLevel level)
Disable all logging for all components.
static ComponentList * GetComponentList(void)
Get the list of LogComponnents.
int32_t m_mask
Blocked LogLevels.
int32_t m_levels
Enabled LogLevels.
static PrintList g_printList
Invoke handler for print-list in NS_LOG environment variable.
LogLevel
Logging severity classes and levels.
void LogSetTimePrinter(LogTimePrinter printer)
Set the LogTimePrinter function to be used to prepend log messages with the simulation time...
Handler for print-list token in NS_LOG to print the list of log components.
std::string File(void) const
Get the compilation unit defining this LogComponent.
static LogNodePrinter g_logNodePrinter
The LogNodePrinter.
void(* LogTimePrinter)(std::ostream &os)
Function signature for prepending the simulation time to a log message.
bool IsNoneEnabled(void) const
Check if all levels are disabled.
void(* LogNodePrinter)(std::ostream &os)
Function signature for prepending the node id to a log message.
Prefix all trace prints with function.
Rare ad-hoc debug messages.
char const * Name(void) const
Get the name of this LogComponent.
static std::string GetLevelLabel(const enum LogLevel level)
Get the string label for the given LogLevel.
LogTimePrinter LogGetTimePrinter(void)
Get the LogTimePrinter function currently in use.
std::string m_name
LogComponent name.