sorting out logging

This commit is contained in:
Craig Dowell
2007-09-13 12:37:30 -07:00
parent 873176a3e6
commit 16b634a7fc
3 changed files with 89 additions and 51 deletions

View File

@@ -101,11 +101,23 @@ LogComponentEnableEnvVar (void)
cur_lev = next_lev + 1;
next_lev = tmp.find ("|", cur_lev);
std::string lev = tmp.substr (cur_lev, next_lev - cur_lev);
if (lev == "debug")
if (lev == "error")
{
level |= LOG_LEVEL_ERROR;
}
else if (lev == "warn")
{
level |= LOG_LEVEL_WARN;
}
else if (lev == "debug")
{
level |= LOG_LEVEL_DEBUG;
}
else if (lev == "func")
else if (lev == "info")
{
level |= LOG_LEVEL_INFO;
}
else if (lev == "function")
{
level |= LOG_LEVEL_FUNCTION;
}
@@ -113,13 +125,13 @@ LogComponentEnableEnvVar (void)
{
level |= LOG_LEVEL_PARAM;
}
else if (lev == "warn")
else if (lev == "logic")
{
level |= LOG_LEVEL_WARN;
level |= LOG_LEVEL_LOGIC;
}
else if (lev == "error")
else if (lev == "all")
{
level |= LOG_LEVEL_ERROR;
level |= LOG_LEVEL_ALL;
}
} while (next_lev != std::string::npos);
}
@@ -159,7 +171,6 @@ LogComponentEnableEnvVar (void)
#endif
}
LogComponent::LogComponent (char const * name)
: m_levels (0)
{
@@ -172,22 +183,28 @@ LogComponent::LogComponent (char const * name)
}
components->push_back (std::make_pair (name, this));
}
bool
LogComponent::IsEnabled (enum LogLevel level) const
{
LogComponentEnableEnvVar ();
return (level & m_levels) == 1;
// return (level & m_levels) ? 1 : 0;
return m_levels >= level;
}
bool
LogComponent::IsNoneEnabled (void) const
{
return m_levels == 0;
}
void
LogComponent::Enable (enum LogLevel level)
{
m_levels |= level;
}
void
LogComponent::Disable (enum LogLevel level)
{
@@ -209,6 +226,7 @@ LogComponentEnable (char const *name, enum LogLevel level)
}
}
}
void
LogComponentDisable (char const *name, enum LogLevel level)
{
@@ -225,7 +243,6 @@ LogComponentDisable (char const *name, enum LogLevel level)
}
}
void
LogComponentPrintList (void)
{
@@ -240,25 +257,37 @@ LogComponentPrintList (void)
std::cout << "0" << std::endl;
continue;
}
if (i->second->IsEnabled (LOG_LEVEL_DEBUG))
if (i->second->IsEnabled (LOG_LEVEL_ERROR))
{
std::cout << "debug";
}
if (i->second->IsEnabled (LOG_LEVEL_FUNCTION))
{
std::cout << "|func";
}
if (i->second->IsEnabled (LOG_LEVEL_PARAM))
{
std::cout << "|param";
std::cout << "error";
}
if (i->second->IsEnabled (LOG_LEVEL_WARN))
{
std::cout << "|warn";
}
if (i->second->IsEnabled (LOG_LEVEL_ERROR))
if (i->second->IsEnabled (LOG_LEVEL_DEBUG))
{
std::cout << "|error";
std::cout << "|debug";
}
if (i->second->IsEnabled (LOG_LEVEL_INFO))
{
std::cout << "|info";
}
if (i->second->IsEnabled (LOG_LEVEL_FUNCTION))
{
std::cout << "|function";
}
if (i->second->IsEnabled (LOG_LEVEL_PARAM))
{
std::cout << "|param";
}
if (i->second->IsEnabled (LOG_LEVEL_LOGIC))
{
std::cout << "|logic";
}
if (i->second->IsEnabled (LOG_LEVEL_ALL))
{
std::cout << "|all";
}
std::cout << std::endl;
}

View File

@@ -74,7 +74,7 @@
* defined with the NS_LOG_COMPONENT_DEFINE macro in the
* same file.
*/
#define NS_LOG(level,msg) \
#define NS_LOG(level, msg) \
do \
{ \
if (g_log.IsEnabled (level)) \
@@ -84,39 +84,48 @@
} \
while (false)
#define NS_LOG_DEBUG(msg) \
NS_LOG (ns3::LOG_LEVEL_DEBUG,msg)
#define NS_LOG_FUNCTION \
NS_LOG (ns3::LOG_LEVEL_FUNCTION, __PRETTY_PRINT__)
#define NS_LOG_PARAM(msg) \
NS_LOG (ns3::LOG_LEVEL_PARAM,msg)
#define NS_LOG_ERROR(msg) \
NS_LOG(ns3::LOG_LEVEL_ERROR, msg)
#define NS_LOG_WARN(msg) \
NS_LOG (ns3::LOG_LEVEL_WARN,msg)
NS_LOG(ns3::LOG_LEVEL_WARN, msg)
#define NS_LOG_ERROR(msg) \
NS_LOG (ns3::LOG_LEVEL_ERROR,msg)
#define NS_LOG_DEBUG(msg) \
NS_LOG(ns3::LOG_LEVEL_DEBUG, msg)
#define NS_LOG_UNCOND(msg) \
do \
{ \
std::clog << msg << std::endl; \
} \
#define NS_LOG_INFO(msg) \
NS_LOG(ns3::LOG_LEVEL_INFO, msg)
#define NS_LOG_FUNCTION(msg) \
NS_LOG(ns3::LOG_LEVEL_FUNCTION, msg)
#define NS_LOG_PARAM(msg) \
NS_LOG(ns3::LOG_LEVEL_PARAM, msg)
#define NS_LOG_LOGIC(msg) \
NS_LOG(ns3::LOG_LEVEL_LOGIC, msg)
#define NS_LOG_ALL(msg) \
NS_LOG(ns3::LOG_LEVEL_ALL, msg)
#define NS_LOG_UNCOND(msg) \
do \
{ \
std::clog << msg << std::endl; \
} \
while (false)
namespace ns3 {
enum LogLevel {
LOG_LEVEL_DEBUG = 1<<0,
LOG_LEVEL_FUNCTION = 1<<1,
LOG_LEVEL_PARAM = 1<<2,
LOG_LEVEL_WARN = 1<<3,
LOG_LEVEL_ERROR = 1<<4,
LOG_LEVEL_LAST = 1<<31
LOG_LEVEL_ERROR = 1<<0, // serious error messages only
LOG_LEVEL_WARN = 1<<1, // add warning messages
LOG_LEVEL_DEBUG = 1<<2, // add rare ad-hoc debug messages
LOG_LEVEL_INFO = 1<<3, // add informational messages (e.g., banners)
LOG_LEVEL_FUNCTION = 1<<4, // add function tracing
LOG_LEVEL_PARAM = 1<<5, // add parameters to functions
LOG_LEVEL_LOGIC = 1<<6, // add control flow tracing within functions
LOG_LEVEL_ALL = 1<<30 // print everything
};
/**
@@ -159,7 +168,7 @@ public:
void Enable (enum LogLevel level);
void Disable (enum LogLevel level);
private:
uint32_t m_levels;
int32_t m_levels;
};
} // namespace ns3

View File

@@ -14,16 +14,16 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "ns3/debug.h"
#include "ns3/log.h"
using namespace ns3;
NS_DEBUG_COMPONENT_DEFINE ("HelloSimulator");
NS_LOG_COMPONENT_DEFINE ("HelloSimulator");
int
main (int argc, char *argv[])
{
DebugComponentEnable("HelloSimulator");
LogComponentEnable ("HelloSimulator", LOG_LEVEL_INFO);
NS_DEBUG("Hello Simulator");
NS_LOG_INFO ("Hello Simulator");
}