Print node context in log messages

This commit is contained in:
Guillaume Seguin
2009-11-14 17:47:05 +01:00
parent 5ce9ad2d78
commit 94cf348701
3 changed files with 54 additions and 3 deletions

View File

@@ -35,6 +35,7 @@
namespace ns3 {
LogTimePrinter g_logTimePrinter = 0;
LogNodePrinter g_logNodePrinter = 0;
typedef std::list<std::pair <std::string, LogComponent *> > ComponentList;
typedef std::list<std::pair <std::string, LogComponent *> >::iterator ComponentListI;
@@ -124,7 +125,7 @@ LogComponent::EnvVarCheck (char const * name)
component = tmp;
if (component == myName || component == "*")
{
int level = LOG_ALL | LOG_PREFIX_TIME | LOG_PREFIX_FUNC;
int level = LOG_ALL | LOG_PREFIX_TIME | LOG_PREFIX_FUNC | LOG_PREFIX_NODE;
Enable ((enum LogLevel)level);
return;
}
@@ -178,6 +179,10 @@ LogComponent::EnvVarCheck (char const * name)
{
level |= LOG_PREFIX_TIME;
}
else if (lev == "prefix_node")
{
level |= LOG_PREFIX_NODE;
}
else if (lev == "level_error")
{
level |= LOG_LEVEL_ERROR;
@@ -360,6 +365,15 @@ LogTimePrinter LogGetTimePrinter(void)
return g_logTimePrinter;
}
void LogSetNodePrinter (LogNodePrinter printer)
{
g_logNodePrinter = printer;
}
LogNodePrinter LogGetNodePrinter(void)
{
return g_logNodePrinter;
}
ParameterLogger::ParameterLogger (std::ostream &os)
: m_itemNumber (0),

View File

@@ -48,11 +48,12 @@ enum LogLevel {
LOG_LOGIC = 0x00000020, // control flow tracing within functions
LOG_LEVEL_LOGIC = 0x0000003f,
LOG_ALL = 0x3fffffff, // print everything
LOG_ALL = 0x1fffffff, // print everything
LOG_LEVEL_ALL = LOG_ALL,
LOG_PREFIX_FUNC = 0x80000000, // prefix all trace prints with function
LOG_PREFIX_TIME = 0x40000000 // prefix all trace prints with simulation time
LOG_PREFIX_TIME = 0x40000000, // prefix all trace prints with simulation time
LOG_PREFIX_NODE = 0x20000000 // prefix all trace prints with simulation node
};
/**
@@ -160,6 +161,17 @@ void LogComponentDisableAll (enum LogLevel level);
} \
}
#define NS_LOG_APPEND_NODE_PREFIX \
if (g_log.IsEnabled (ns3::LOG_PREFIX_NODE)) \
{ \
ns3::LogNodePrinter printer = ns3::LogGetNodePrinter (); \
if (printer != 0) \
{ \
(*printer) (std::clog); \
std::clog << " "; \
} \
}
#define NS_LOG_APPEND_FUNC_PREFIX \
if (g_log.IsEnabled (ns3::LOG_PREFIX_FUNC)) \
{ \
@@ -192,6 +204,7 @@ void LogComponentDisableAll (enum LogLevel level);
if (g_log.IsEnabled (level)) \
{ \
NS_LOG_APPEND_TIME_PREFIX; \
NS_LOG_APPEND_NODE_PREFIX; \
NS_LOG_APPEND_CONTEXT; \
NS_LOG_APPEND_FUNC_PREFIX; \
std::clog << msg << std::endl; \
@@ -246,6 +259,7 @@ void LogComponentDisableAll (enum LogLevel level);
if (g_log.IsEnabled (ns3::LOG_FUNCTION)) \
{ \
NS_LOG_APPEND_TIME_PREFIX; \
NS_LOG_APPEND_NODE_PREFIX; \
NS_LOG_APPEND_CONTEXT; \
std::clog << g_log.Name () << ":" \
<< __FUNCTION__ << "()" << std::endl; \
@@ -276,6 +290,7 @@ void LogComponentDisableAll (enum LogLevel level);
if (g_log.IsEnabled (ns3::LOG_FUNCTION)) \
{ \
NS_LOG_APPEND_TIME_PREFIX; \
NS_LOG_APPEND_NODE_PREFIX; \
NS_LOG_APPEND_CONTEXT; \
std::clog << g_log.Name () << ":" \
<< __FUNCTION__ << "("; \
@@ -321,10 +336,14 @@ namespace ns3 {
void LogComponentPrintList (void);
typedef void (*LogTimePrinter) (std::ostream &os);
typedef void (*LogNodePrinter) (std::ostream &os);
void LogSetTimePrinter (LogTimePrinter);
LogTimePrinter LogGetTimePrinter(void);
void LogSetNodePrinter (LogNodePrinter);
LogNodePrinter LogGetNodePrinter(void);
class LogComponent {
public:
@@ -384,6 +403,8 @@ public:
#define LogSetTimePrinter(printer)
#define LogGetTimePrinter
#define LogSetNodePrinter(printer)
#define LogGetNodePrinter
#endif /* LOG_ENABLE */

View File

@@ -65,6 +65,19 @@ TimePrinter (std::ostream &os)
os << Simulator::Now ().GetSeconds () << "s";
}
static void
NodePrinter (std::ostream &os)
{
if (Simulator::GetContext () == 0xffffffff)
{
os << "-1";
}
else
{
os << Simulator::GetContext ();
}
}
#endif /* NS3_LOG_ENABLE */
static SimulatorImpl **PeekImpl (void)
@@ -105,6 +118,7 @@ static SimulatorImpl * GetImpl (void)
// in an infinite recursion until the stack explodes.
//
LogSetTimePrinter (&TimePrinter);
LogSetNodePrinter (&NodePrinter);
}
return *pimpl;
}
@@ -125,6 +139,7 @@ Simulator::Destroy (void)
* the stack explodes.
*/
LogSetTimePrinter (0);
LogSetNodePrinter (0);
(*pimpl)->Destroy ();
(*pimpl)->Unref ();
*pimpl = 0;
@@ -335,6 +350,7 @@ Simulator::SetImplementation (Ptr<SimulatorImpl> impl)
// in an infinite recursion until the stack explodes.
//
LogSetTimePrinter (&TimePrinter);
LogSetNodePrinter (&NodePrinter);
}
Ptr<SimulatorImpl>
Simulator::GetImplementation (void)