24#include "ns3/core-config.h"
82 const char* envVar = std::getenv(
"NS_LOG");
83 if (envVar ==
nullptr || std::strlen(envVar) == 0)
87 std::string env = envVar;
88 std::string::size_type cur = 0;
89 std::string::size_type next = 0;
90 while (next != std::string::npos)
92 next = env.find_first_of(
':', cur);
93 std::string tmp = std::string(env, cur, next - cur);
94 if (tmp ==
"print-list")
105 const std::string&
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 ==
nullptr || 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 ==
"*"))))
235 else if (lev ==
"level_error")
239 else if (lev ==
"level_warn")
243 else if (lev ==
"level_debug")
247 else if (lev ==
"level_info")
251 else if (lev ==
"level_function")
255 else if (lev ==
"level_logic")
259 else if (lev ==
"level_all")
263 else if (lev ==
"**")
269 }
while (next_lev != std::string::npos);
361 LogComponent::ComponentList::const_iterator i;
362 for (i = components->begin(); i != components->end(); i++)
364 if (i->first == name)
366 i->second->Enable(level);
370 if (i == components->end())
375 << name <<
"\" not found. See above for a list of available log components");
383 for (LogComponent::ComponentList::const_iterator i = components->begin();
384 i != components->end();
387 i->second->Enable(level);
395 for (LogComponent::ComponentList::const_iterator i = components->begin();
396 i != components->end();
399 if (i->first == name)
401 i->second->Disable(level);
411 for (LogComponent::ComponentList::const_iterator i = components->begin();
412 i != components->end();
415 i->second->Disable(level);
423 for (LogComponent::ComponentList::const_iterator i = components->begin();
424 i != components->end();
427 std::cout << i->first <<
"=";
428 if (i->second->IsNoneEnabled())
430 std::cout <<
"0" << std::endl;
441 std::cout <<
"error";
445 std::cout <<
"|warn";
449 std::cout <<
"|debug";
453 std::cout <<
"|info";
457 std::cout <<
"|function";
461 std::cout <<
"|logic";
466 std::cout <<
"|prefix_all";
472 std::cout <<
"|func";
476 std::cout <<
"|time";
480 std::cout <<
"|node";
484 std::cout <<
"|level";
487 std::cout << std::endl;
502 const char* name = componentName.c_str();
504 LogComponent::ComponentList::const_iterator i;
505 for (i = components->begin(); i != components->end(); i++)
507 if (i->first == name)
525 const char* envVar = std::getenv(
"NS_LOG");
526 if (envVar ==
nullptr || std::strlen(envVar) == 0)
531 std::string env = envVar;
532 std::string::size_type cur = 0;
533 std::string::size_type next = 0;
535 while (next != std::string::npos)
537 next = env.find_first_of(
':', cur);
538 std::string tmp = std::string(env, cur, next - cur);
539 std::string::size_type equal = tmp.find(
'=');
540 std::string component;
541 if (equal == std::string::npos)
545 if (
ComponentExists(component) || component ==
"*" || component ==
"***")
553 "Invalid or unregistered component name \""
555 <<
"\" in env variable NS_LOG, see above for a list of valid components");
560 component = tmp.substr(0, equal);
563 std::string::size_type cur_lev;
564 std::string::size_type next_lev = equal;
567 cur_lev = next_lev + 1;
568 next_lev = tmp.find(
'|', cur_lev);
569 std::string lev = tmp.substr(cur_lev, next_lev - cur_lev);
570 if (lev ==
"error" || lev ==
"warn" || lev ==
"debug" || lev ==
"info" ||
571 lev ==
"function" || lev ==
"logic" || lev ==
"all" ||
572 lev ==
"prefix_func" || lev ==
"func" || lev ==
"prefix_time" ||
573 lev ==
"time" || lev ==
"prefix_node" || lev ==
"node" ||
574 lev ==
"prefix_level" || lev ==
"level" || lev ==
"prefix_all" ||
575 lev ==
"level_error" || lev ==
"level_warn" || lev ==
"level_debug" ||
576 lev ==
"level_info" || lev ==
"level_function" || lev ==
"level_logic" ||
577 lev ==
"level_all" || lev ==
"*" || lev ==
"**")
584 << lev <<
"\" in env variable NS_LOG for component name "
587 }
while (next_lev != std::string::npos);
593 "Invalid or unregistered component name \""
595 <<
"\" in env variable NS_LOG, see above for a list of valid components");
651ParameterLogger::operator<< <std::string>(
const std::string& param)
654 m_os <<
"\"" << param <<
"\"";
661 (*this) << std::string(param);
667ParameterLogger::operator<< <int8_t>(
const int8_t param)
669 (*this) <<
static_cast<int16_t
>(param);
675ParameterLogger::operator<< <uint8_t>(
const uint8_t param)
677 (*this) <<
static_cast<uint16_t
>(param);
NS_ASSERT() and NS_ASSERT_MSG() macro definitions.
A single log component configuration.
const char * Name() const
Get the name of this LogComponent.
static ComponentList * GetComponentList()
Get the list of LogComponnents.
void Enable(const enum LogLevel level)
Enable this LogComponent at level.
std::string File() const
Get the compilation unit defining this LogComponent.
int32_t m_levels
Enabled LogLevels.
static std::string GetLevelLabel(const enum LogLevel level)
Get the string label for the given LogLevel.
void EnvVarCheck()
Parse the NS_LOG environment variable for options relating to this LogComponent.
void Disable(const enum LogLevel level)
Disable logging at level for this LogComponent.
std::string m_file
File defining this LogComponent.
bool IsNoneEnabled() const
Check if all levels are disabled.
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.
int32_t m_mask
Blocked LogLevels.
std::map< std::string, LogComponent * > ComponentList
LogComponent name map.
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.
ParameterLogger & operator<<(T param)
Write a function parameter on the output stream, separating parameters after the first by ,...
void CommaRest()
Add , before every parameter after the first.
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 void CheckEnvironmentVariables()
Parse the NS_LOG environment variable.
static bool ComponentExists(std::string componentName)
Check if a log component exists.
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(* TimePrinter)(std::ostream &os)
Function signature for features requiring a time formatter, such as logging or ShowProgress.
void LogComponentDisable(const char *name, enum LogLevel level)
Disable the logging output associated with that log component.
void(* NodePrinter)(std::ostream &os)
Function signature for prepending the node id to a log message.
NodePrinter LogGetNodePrinter()
Get the LogNodePrinter function currently in use.
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()
Get the LogTimePrinter function currently in use.
LogComponent & GetLogComponent(const std::string name)
Get the LogComponent registered with the given name.
void LogComponentEnable(const char *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 LogComponentPrintList()
Print the list of logging messages available.
void LogComponentEnableAll(enum LogLevel level)
Enable the logging output for all registered log components.