[Bug 1496] Print the LOG_LEVEL (severity) in NS_LOG messages.
--- a/src/core/model/log.cc Mon Nov 05 14:07:02 2012 -0500
+++ b/src/core/model/log.cc Mon Nov 05 12:10:55 2012 -0800
@@ -22,16 +22,17 @@
#include <list>
#include <utility>
#include <iostream>
+#include <string.h>
#include "assert.h"
#include "ns3/core-config.h"
#include "fatal-error.h"
#ifdef HAVE_GETENV
-#include <cstring>
+#include <string.h>
#endif
#ifdef HAVE_STDLIB_H
-#include <cstdlib>
+#include <stdlib.h>
#endif
namespace ns3 {
@@ -127,7 +128,7 @@
component = tmp;
if (component == myName || component == "*")
{
- int level = LOG_ALL | LOG_PREFIX_TIME | LOG_PREFIX_FUNC | LOG_PREFIX_NODE;
+ int level = LOG_ALL | LOG_PREFIX_TIME | LOG_PREFIX_FUNC | LOG_PREFIX_NODE | LOG_PREFIX_LEVEL;
Enable ((enum LogLevel)level);
return;
}
@@ -185,6 +186,14 @@
{
level |= LOG_PREFIX_NODE;
}
+ else if (lev == "prefix_level")
+ {
+ level |= LOG_PREFIX_LEVEL;
+ }
+ else if (lev == "prefix_all")
+ {
+ level |= LOG_PREFIX_FUNC | LOG_PREFIX_TIME | LOG_PREFIX_NODE | LOG_PREFIX_LEVEL;
+ }
else if (lev == "level_error")
{
level |= LOG_LEVEL_ERROR;
@@ -255,6 +264,26 @@
return m_name;
}
+std::map<enum LogLevel, std::string>
+LogComponent::LevelLabels() const
+{
+ std::map<enum LogLevel, std::string> labels;
+ labels[LOG_ERROR] = "ERROR";
+ labels[LOG_WARN] = "WARN";
+ labels[LOG_DEBUG] = "DEBUG";
+ labels[LOG_INFO] = "INFO";
+ labels[LOG_LOGIC] = "LOGIC";
+
+ return labels;
+}
+
+std::string
+LogComponent::GetLevelLabel(const enum LogLevel level) const
+{
+ static std::map<enum LogLevel, std::string> levelLabel = LevelLabels ();
+ return levelLabel[level];
+}
+
void
LogComponentEnable (char const *name, enum LogLevel level)
@@ -389,7 +418,7 @@
{
#ifdef HAVE_GETENV
char *envVar = getenv ("NS_LOG");
- if (envVar == 0 || std::strlen(envVar) == 0)
+ if (envVar == 0 || strlen(envVar) == 0)
{
return;
}
--- a/src/core/model/log.h Mon Nov 05 14:07:02 2012 -0500
+++ b/src/core/model/log.h Mon Nov 05 12:10:55 2012 -0800
@@ -24,6 +24,7 @@
#include <string>
#include <iostream>
#include <stdint.h>
+#include <map>
namespace ns3 {
@@ -48,12 +49,13 @@
LOG_LOGIC = 0x00000020, // control flow tracing within functions
LOG_LEVEL_LOGIC = 0x0000003f,
- LOG_ALL = 0x1fffffff, // print everything
+ LOG_ALL = 0x0fffffff, // 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_NODE = 0x20000000 // prefix all trace prints with simulation node
+ LOG_PREFIX_NODE = 0x20000000, // prefix all trace prints with simulation node
+ LOG_PREFIX_LEVEL = 0x10000000 // prefix all trace prints with log level (severity)
};
/**
@@ -148,6 +150,13 @@
__FUNCTION__ << "(): "; \
} \
+#define NS_LOG_APPEND_LEVEL_PREFIX(level) \
+ if (g_log.IsEnabled (ns3::LOG_PREFIX_LEVEL)) \
+ { \
+ std::clog << "[" << g_log.GetLevelLabel (level) << "] "; \
+ } \
+
+
#ifndef NS_LOG_APPEND_CONTEXT
#define NS_LOG_APPEND_CONTEXT
#endif /* NS_LOG_APPEND_CONTEXT */
@@ -212,6 +221,7 @@
NS_LOG_APPEND_NODE_PREFIX; \
NS_LOG_APPEND_CONTEXT; \
NS_LOG_APPEND_FUNC_PREFIX; \
+ NS_LOG_APPEND_LEVEL_PREFIX (level); \
std::clog << msg << std::endl; \
} \
} \
@@ -380,7 +390,9 @@
void Enable (enum LogLevel level);
void Disable (enum LogLevel level);
char const *Name (void) const;
+ std::string GetLevelLabel(const enum LogLevel level) const;
private:
+ std::map<enum LogLevel, std::string> LevelLabels() const;
int32_t m_levels;
char const *m_name;
};