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 ==
"*")
237 else if (LOG_LABEL_LEVELS.find(lev) != LOG_LABEL_LEVELS.end())
239 level |= LOG_LABEL_LEVELS.at(lev);
293 auto it = LOG_LEVEL_LABELS.find(level);
294 if (it != LOG_LEVEL_LABELS.end())
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 (LogComponent::ComponentList::const_iterator i = components->begin();
324 i != components->end();
327 i->second->Enable(level);
335 auto logComponent = components->find(name);
337 if (logComponent != components->end())
339 logComponent->second->Disable(level);
347 for (LogComponent::ComponentList::const_iterator i = components->begin();
348 i != components->end();
351 i->second->Disable(level);
359 std::map<std::string, LogComponent*> componentsSorted;
363 componentsSorted.insert(component);
367 for (
const auto& [name, component] : componentsSorted)
369 std::cout << name <<
"=";
370 if (component->IsNoneEnabled())
372 std::cout <<
"0" << std::endl;
383 std::cout <<
"error";
387 std::cout <<
"|warn";
391 std::cout <<
"|debug";
395 std::cout <<
"|info";
399 std::cout <<
"|function";
403 std::cout <<
"|logic";
408 std::cout <<
"|prefix_all";
414 std::cout <<
"|func";
418 std::cout <<
"|time";
422 std::cout <<
"|node";
426 std::cout <<
"|level";
429 std::cout << std::endl;
446 return components->find(componentName) != components->end();
459 for (
auto& [component, value] : dict)
461 if (component !=
"*" && component !=
"***" && !
ComponentExists(component))
463 NS_LOG_UNCOND(
"Invalid or unregistered component name \"" << component <<
"\"");
466 "Invalid or unregistered component name \""
468 <<
"\" in env variable NS_LOG, see above for a list of valid components");
476 for (
const auto& flag : flags)
479 if (flag ==
"*" || flag ==
"**")
483 bool ok = LOG_LABEL_LEVELS.find(flag) != LOG_LABEL_LEVELS.end();
487 << flag <<
"\" in env variable NS_LOG for component name "
544ParameterLogger::operator<< <std::string>(
const std::string& param)
547 m_os <<
"\"" << param <<
"\"";
554 (*this) << std::string(param);
560ParameterLogger::operator<< <int8_t>(
const int8_t param)
562 (*this) <<
static_cast<int16_t
>(param);
568ParameterLogger::operator<< <uint8_t>(
const uint8_t param)
570 (*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.
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.
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_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.