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,
172 if (components->find(name) != components->end())
174 NS_FATAL_ERROR(
"Log component \"" << name <<
"\" has already been registered once.");
177 components->insert(std::make_pair(name,
this));
188 ret = components->at(name);
190 catch (std::out_of_range&)
192 NS_FATAL_ERROR(
"Log component \"" << name <<
"\" does not exist.");
224 NS_ASSERT_MSG(!flags.empty(),
"Unexpected empty flags from non-empty value");
227 for (
const auto& lev : flags)
233 else if (lev ==
"all" || lev ==
"*")
305 auto logComponent = components->find(name);
307 if (logComponent == components->end())
309 NS_LOG_UNCOND(
"Logging component \"" << name <<
"\" not found.");
312 << name <<
"\" not found."
313 <<
" See above for a list of available log components");
316 logComponent->second->Enable(level);
323 for (
auto i = components->begin(); i != components->end(); i++)
325 i->second->Enable(level);
333 auto logComponent = components->find(name);
335 if (logComponent != components->end())
337 logComponent->second->Disable(level);
345 for (
auto i = components->begin(); i != components->end(); i++)
347 i->second->Disable(level);
355 std::map<std::string, LogComponent*> componentsSorted;
359 componentsSorted.insert(component);
363 for (
const auto& [name, component] : componentsSorted)
365 std::cout << name <<
"=";
366 if (component->IsNoneEnabled())
368 std::cout <<
"0" << std::endl;
379 std::cout <<
"error";
383 std::cout <<
"|warn";
387 std::cout <<
"|debug";
391 std::cout <<
"|info";
395 std::cout <<
"|function";
399 std::cout <<
"|logic";
404 std::cout <<
"|prefix_all";
410 std::cout <<
"|func";
414 std::cout <<
"|time";
418 std::cout <<
"|node";
422 std::cout <<
"|level";
425 std::cout << std::endl;
442 return components->find(componentName) != components->end();
455 for (
auto& [component, value] : dict)
457 if (component !=
"*" && component !=
"***" && !
ComponentExists(component))
459 NS_LOG_UNCOND(
"Invalid or unregistered component name \"" << component <<
"\"");
462 "Invalid or unregistered component name \""
464 <<
"\" in env variable NS_LOG, see above for a list of valid components");
472 for (
const auto& flag : flags)
475 if (flag ==
"*" || flag ==
"**")
483 << flag <<
"\" in env variable NS_LOG for component name "
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.
std::unordered_map< std::string, LogComponent * > ComponentList
LogComponent name map.
int32_t m_mask
Blocked LogLevels.
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.
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_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 for non-trivial function calls.
@ LOG_ERROR
Serious error messages only.
@ LOG_WARN
Warning messages.
@ LOG_INFO
Something happened to change state.
@ 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
Debugging logs for key branches and decisions in a function.
@ LOG_PREFIX_NODE
Prefix all trace prints with simulation node.
@ LOG_LEVEL_INFO
LOG_INFO and above.
@ LOG_DEBUG
Full voluminous logging to support debugging.
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.