26 #include "ns3/core-config.h"
82 std::map<ns3::LogLevel, std::string> labels;
86 if (labels.find(lev) == labels.end())
88 std::string pad{
label};
92 pad.insert(pad.size(), 5 - pad.size(),
' ');
94 std::transform(pad.begin(), pad.end(), pad.begin(), ::toupper);
160 const std::string&
file,
171 for (LogComponent::ComponentList::const_iterator i = components->begin();
172 i != components->end();
175 if (i->first == name)
177 NS_FATAL_ERROR(
"Log component \"" << name <<
"\" has already been registered once.");
180 components->insert(std::make_pair(name,
this));
191 ret = components->at(name);
193 catch (std::out_of_range&)
195 NS_FATAL_ERROR(
"Log component \"" << name <<
"\" does not exist.");
227 NS_ASSERT_MSG(!flags.empty(),
"Unexpected empty flags from non-empty value");
230 for (
const auto& lev : flags)
236 else if (lev ==
"all" || lev ==
"*")
308 LogComponent::ComponentList::const_iterator i;
309 for (i = components->begin(); i != components->end(); i++)
311 if (i->first == name)
313 i->second->Enable(level);
317 if (i == components->end())
320 NS_LOG_UNCOND(
"Logging component \"" << name <<
"\" not found.");
323 << name <<
"\" not found."
324 <<
" See above for a list of available log components");
332 for (LogComponent::ComponentList::const_iterator i = components->begin();
333 i != components->end();
336 i->second->Enable(level);
344 for (LogComponent::ComponentList::const_iterator i = components->begin();
345 i != components->end();
348 if (i->first == name)
350 i->second->Disable(level);
360 for (LogComponent::ComponentList::const_iterator i = components->begin();
361 i != components->end();
364 i->second->Disable(level);
372 for (LogComponent::ComponentList::const_iterator i = components->begin();
373 i != components->end();
376 std::cout << i->first <<
"=";
377 if (i->second->IsNoneEnabled())
379 std::cout <<
"0" << std::endl;
390 std::cout <<
"error";
394 std::cout <<
"|warn";
398 std::cout <<
"|debug";
402 std::cout <<
"|info";
406 std::cout <<
"|function";
410 std::cout <<
"|logic";
415 std::cout <<
"|prefix_all";
421 std::cout <<
"|func";
425 std::cout <<
"|time";
429 std::cout <<
"|node";
433 std::cout <<
"|level";
436 std::cout << std::endl;
452 LogComponent::ComponentList::const_iterator i;
453 for (i = components->begin(); i != components->end(); i++)
455 if (i->first == componentName)
475 for (
auto& [component,
value] : dict)
477 if (component !=
"*" && component !=
"***" && !
ComponentExists(component))
479 NS_LOG_UNCOND(
"Invalid or unregistered component name \"" << component <<
"\"");
482 "Invalid or unregistered component name \""
484 <<
"\" in env variable NS_LOG, see above for a list of valid components");
492 for (
const auto& flag : flags)
495 if (flag ==
"*" || flag ==
"**")
503 << flag <<
"\" in env variable NS_LOG for component name "
560 ParameterLogger::operator<< <std::string>(
const std::string& param)
563 m_os <<
"\"" << param <<
"\"";
570 (*this) << std::string(param);
576 ParameterLogger::operator<< <int8_t>(
const int8_t param)
578 (*this) <<
static_cast<int16_t
>(param);
584 ParameterLogger::operator<< <uint8_t>(
const uint8_t param)
586 (*this) <<
static_cast<uint16_t
>(param);
NS_ASSERT() and NS_ASSERT_MSG() macro definitions.
static KeyFoundType Get(const std::string &envvar, const std::string &key="", const std::string &delim=";")
Get the value corresponding to a key from an environment variable.
static std::shared_ptr< Dictionary > GetDictionary(const std::string &envvar, const std::string &delim=";")
Get the dictionary for a particular environment variable.
A single log component configuration.
static ComponentList * GetComponentList()
Get the list of LogComponents.
void Enable(const LogLevel level)
Enable this LogComponent at level.
bool IsEnabled(const LogLevel level) const
Check if this LogComponent is enabled for level.
std::string File() const
Get the compilation unit defining this LogComponent.
int32_t m_levels
Enabled LogLevels.
void Disable(const LogLevel level)
Disable logging at level for this LogComponent.
static std::string GetLevelLabel(const LogLevel level)
Get the string label for the given LogLevel.
void EnvVarCheck()
Parse the NS_LOG environment variable for options relating to this LogComponent.
std::string m_file
File defining this LogComponent.
bool IsNoneEnabled() const
Check if all levels are disabled.
std::string Name() const
Get the name of this LogComponent.
int32_t m_mask
Blocked LogLevels.
std::map< std::string, LogComponent * > ComponentList
LogComponent name map.
LogComponent(const std::string &name, const std::string &file, const LogLevel mask=LOG_NONE)
Constructor.
void SetMask(const LogLevel level)
Prevent the enabling of a specific LogLevel.
std::string m_name
LogComponent name.
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 the undocumented print-list token in NS_LOG which triggers printing of the list of log co...
Class Environment declaration.
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_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
#define NS_LOG_UNCOND(msg)
Output the requested message unconditionally.
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.
const std::map< std::string, ns3::LogLevel > LOG_LABEL_LEVELS
Mapping of log level text names to values.
const std::map< ns3::LogLevel, std::string > LOG_LEVEL_LABELS
Inverse mapping of level values to log level text names.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void LogComponentEnable(const std::string &name, LogLevel level)
Enable the logging output associated with that log component.
static PrintList g_printList
Invoke handler for print-list in NS_LOG environment variable.
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(* NodePrinter)(std::ostream &os)
Function signature for prepending the node id to a log message.
std::vector< std::string > StringVector
Return type of SplitString.
NodePrinter LogGetNodePrinter()
Get the LogNodePrinter function currently in use.
StringVector SplitString(const std::string &str, const std::string &delim)
Split a string on a delimiter.
void LogComponentDisable(const std::string &name, LogLevel level)
Disable the logging output associated with that log component.
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_ALL
Print everything.
@ 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.
void LogComponentDisableAll(LogLevel level)
Disable all logging for all components.
LogComponent & GetLogComponent(const std::string name)
Get the LogComponent registered with the given name.
void LogComponentEnableAll(LogLevel level)
Enable the logging output for all registered log components.
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.
ns3::StringValue attribute value declarations.