add debugging support
authorMathieu Lacage <mathieu.lacage@sophia.inria.fr>
Tue, 06 Feb 2007 20:40:38 +0100
changeset 265 fb0c531530f2
parent 231 8516b1203fbd
child 266 a451b8daa1e6
add debugging support
SConstruct
src/core/debug.cc
src/core/debug.h
--- a/SConstruct	Tue Feb 06 19:59:28 2007 +0100
+++ b/SConstruct	Tue Feb 06 20:40:38 2007 +0100
@@ -18,6 +18,7 @@
 core.add_sources([
     'reference-list-test.cc',
     'callback-test.cc',
+    'debug.cc',
     'ptr.cc',
     'test.cc'
     ])
@@ -39,9 +40,24 @@
     '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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/core/debug.cc	Tue Feb 06 20:40:38 2007 +0100
@@ -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 <mathieu.lacage@sophia.inria.fr>
+ */
+#include <cassert>
+#include <list>
+#include <utility>
+#include <iostream>
+#include "debug.h"
+#include "ns3/core-config.h"
+
+#ifdef HAVE_STDLIB_H
+#include <stdlib.h>
+#endif
+
+namespace ns3 {
+
+typedef std::list<std::pair <std::string, DebugComponent *> > ComponentList;
+typedef std::list<std::pair <std::string, DebugComponent *> >::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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/core/debug.h	Tue Feb 06 20:40:38 2007 +0100
@@ -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 <mathieu.lacage@sophia.inria.fr>
+ */
+#ifndef DEBUG_H
+#define DEBUG_H
+
+#include <string>
+#include <iostream>
+
+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 */