From 16b634a7fcbb9ff72fd9d8e6d69ccbcbdee3b8b9 Mon Sep 17 00:00:00 2001 From: Craig Dowell Date: Thu, 13 Sep 2007 12:37:30 -0700 Subject: [PATCH] sorting out logging --- src/core/log.cc | 71 ++++++++++++++++++++++++++----------- src/core/log.h | 61 +++++++++++++++++-------------- tutorial/hello-simulator.cc | 8 ++--- 3 files changed, 89 insertions(+), 51 deletions(-) diff --git a/src/core/log.cc b/src/core/log.cc index fa1c6f6f9..64a288289 100644 --- a/src/core/log.cc +++ b/src/core/log.cc @@ -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; } diff --git a/src/core/log.h b/src/core/log.h index 3745164c8..28d075565 100644 --- a/src/core/log.h +++ b/src/core/log.h @@ -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 diff --git a/tutorial/hello-simulator.cc b/tutorial/hello-simulator.cc index c9c18cac6..84ac0914e 100644 --- a/tutorial/hello-simulator.cc +++ b/tutorial/hello-simulator.cc @@ -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"); }