15#include "ns3/core-config.h"
71 std::map<ns3::LogLevel, std::string> labels;
75 if (labels.find(lev) == labels.end())
77 std::string pad{label};
81 pad.insert(pad.size(), 5 - pad.size(),
' ');
83 std::transform(pad.begin(), pad.end(), pad.begin(), ::toupper);
149 const std::string& file,
161 if (components->find(name) != components->end())
163 NS_FATAL_ERROR(
"Log component \"" << name <<
"\" has already been registered once.");
166 components->insert(std::make_pair(name,
this));
177 ret = components->at(name);
179 catch (std::out_of_range&)
181 NS_FATAL_ERROR(
"Log component \"" << name <<
"\" does not exist.");
213 NS_ASSERT_MSG(!flags.empty(),
"Unexpected empty flags from non-empty value");
216 for (
const auto& lev : flags)
222 else if (lev ==
"all" || lev ==
"*")
226 else if (LOG_LABEL_LEVELS.find(lev) != LOG_LABEL_LEVELS.end())
228 level |= LOG_LABEL_LEVELS.at(lev);
282 auto it = LOG_LEVEL_LABELS.find(level);
283 if (it != LOG_LEVEL_LABELS.end())
294 auto logComponent = components->find(name);
296 if (logComponent == components->end())
298 NS_LOG_UNCOND(
"Logging component \"" << name <<
"\" not found.");
301 << name <<
"\" not found."
302 <<
" See above for a list of available log components");
305 logComponent->second->Enable(level);
312 for (
auto i = components->begin(); i != components->end(); i++)
314 i->second->Enable(level);
322 auto logComponent = components->find(name);
324 if (logComponent != components->end())
326 logComponent->second->Disable(level);
334 for (
auto i = components->begin(); i != components->end(); i++)
336 i->second->Disable(level);
344 std::map<std::string, LogComponent*> componentsSorted;
348 componentsSorted.insert(component);
352 for (
const auto& [name, component] : componentsSorted)
354 std::cout << name <<
"=";
355 if (component->IsNoneEnabled())
357 std::cout <<
"0" << std::endl;
368 std::cout <<
"error";
372 std::cout <<
"|warn";
376 std::cout <<
"|debug";
380 std::cout <<
"|info";
384 std::cout <<
"|function";
388 std::cout <<
"|logic";
393 std::cout <<
"|prefix_all";
399 std::cout <<
"|func";
403 std::cout <<
"|time";
407 std::cout <<
"|node";
411 std::cout <<
"|level";
414 std::cout << std::endl;
431 return components->find(componentName) != components->end();
444 for (
auto& [component, value] : dict)
446 if (component !=
"*" && component !=
"***" && !
ComponentExists(component))
448 NS_LOG_UNCOND(
"Invalid or unregistered component name \"" << component <<
"\"");
451 "Invalid or unregistered component name \""
453 <<
"\" in env variable NS_LOG, see above for a list of valid components");
461 for (
const auto& flag : flags)
464 if (flag ==
"*" || flag ==
"**")
468 bool ok = LOG_LABEL_LEVELS.find(flag) != LOG_LABEL_LEVELS.end();
472 << 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.
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.
std::vector< std::string > StringVector
Return type of SplitString.
ns3::StringValue attribute value declarations.