From 84bdfa4ac58ca6b017d5ea20f8c43cba3f458eac Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Tue, 6 Feb 2007 20:40:38 +0100 Subject: [PATCH] add debugging support --- SConstruct | 16 ++++++ src/core/debug.cc | 133 ++++++++++++++++++++++++++++++++++++++++++++++ src/core/debug.h | 64 ++++++++++++++++++++++ 3 files changed, 213 insertions(+) create mode 100644 src/core/debug.cc create mode 100644 src/core/debug.h diff --git a/SConstruct b/SConstruct index af7467119..65bc9736e 100644 --- a/SConstruct +++ b/SConstruct @@ -18,6 +18,7 @@ ns3.add(core) core.add_sources([ 'reference-list-test.cc', 'callback-test.cc', + 'debug.cc', 'ptr.cc', 'test.cc' ]) @@ -39,9 +40,24 @@ core.add_inst_headers([ 'reference-list.h', 'callback.h', 'ptr.h', + 'debug.h', 'test.h' ]) +def config_core (env, config): + retval = [] + # XXX This check is primitive but it should be + # good enough for now. + if config.CheckCHeader ('stdlib.h') == 1: + retval.append ('#define HAVE_STDLIB_H 1') + retval.append ('#define HAVE_GETENV 1') + else: + retval.append ('#undef HAVE_STDLIB_H') + retval.append ('#undef HAVE_GETENV') + return retval +core.add_config (config_core) + + # # The Simu module diff --git a/src/core/debug.cc b/src/core/debug.cc new file mode 100644 index 000000000..11892bc4b --- /dev/null +++ b/src/core/debug.cc @@ -0,0 +1,133 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2006 INRIA + * All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author: Mathieu Lacage + */ +#include +#include +#include +#include +#include "debug.h" +#include "ns3/core-config.h" + +#ifdef HAVE_STDLIB_H +#include +#endif + +namespace ns3 { + +typedef std::list > ComponentList; +typedef std::list >::iterator ComponentListI; + +static ComponentList g_components; + +DebugComponent::DebugComponent (std::string name) + : m_isEnabled (false) +{ + for (ComponentListI i = g_components.begin (); + i != g_components.end (); + i++) + { + assert (i->first.compare (name) != 0); + } + g_components.push_back (std::make_pair (name, this)); +} +bool +DebugComponent::IsEnabled (void) +{ + return m_isEnabled; +} +void +DebugComponent::Enable (void) +{ + m_isEnabled = true; +} +void +DebugComponent::Disable (void) +{ + m_isEnabled = false; +} + +void +DebugComponentEnable (char const *name) +{ + for (ComponentListI i = g_components.begin (); + i != g_components.end (); + i++) + { + if (i->first.compare (name) == 0) + { + i->second->Enable (); + break; + } + } +} +void +DebugComponentDisable (char const *name) +{ + for (ComponentListI i = g_components.begin (); + i != g_components.end (); + i++) + { + if (i->first.compare (name) == 0) + { + i->second->Disable (); + break; + } + } +} + +void +DebugComponentEnableEnvVar (void) +{ +#ifdef HAVE_GETENV + char *envVar = getenv("NS3_DEBUG"); + if (envVar == 0) + { + return; + } + std::string env = envVar; + std::string::size_type cur = 0; + std::string::size_type next = 0; + while (true) + { + next = env.find_first_of (";", cur); + if (next == std::string::npos) + { + std::string tmp = env.substr (cur, next); + DebugComponentEnable (tmp.c_str ()); + } + cur = next; + } +#endif +} + +void +DebugComponentPrintList (void) +{ + for (ComponentListI i = g_components.begin (); + i != g_components.end (); + i++) + { + std::cout << i->first << "=" << (i->second->IsEnabled ()?"enabled":"disabled") << std::endl; + } +} + + + +}; // namespace ns3 diff --git a/src/core/debug.h b/src/core/debug.h new file mode 100644 index 000000000..e1b241824 --- /dev/null +++ b/src/core/debug.h @@ -0,0 +1,64 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2006 INRIA + * All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation; + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author: Mathieu Lacage + */ +#ifndef DEBUG_H +#define DEBUG_H + +#include +#include + +namespace ns3 { + +void DebugComponentEnable (char const *name); +void DebugComponentDisable (char const *name); +void DebugComponentEnableEnvVar (void); +void DebugComponentPrintList (void); + +class DebugComponent { +public: + DebugComponent (std::string name); + bool IsEnabled (void); + void Enable (void); + void Disable (void); +private: + bool m_isEnabled; +}; + +}; // namespace ns3 + + +#ifdef NS3_DEBUG_ENABLE + +#define DEBUG_COMPONENT_DEFINE(name) \ + static DebugComponent g_debug = DebugComponent (name); + +#define DEBUG(x) \ + if (g_debug.IsEnabled ()) \ + { \ + std::cout << x << std::endl; \ + } +#else /* NS3_DEBUG_ENABLE */ + +#define DEBUG_COMPONENT_DEFINE(name) +#define DEBUG(x) + +#endif /* NS3_DEBUG_ENABLE */ + +#endif /* DEBUG_H */