Bug 1170: Formulate best practices for dealing with unused debug variables.
authorAndrey Mazo <ahippo@yandex.com>
Sun, 23 Mar 2014 19:08:54 +0400
changeset 10687 1330d4ee94e8
parent 10686 62079cdaafc4
child 10688 ad36a71c737c
Bug 1170: Formulate best practices for dealing with unused debug variables. Fixed clang-3.4 unused function warnings in optimized builds like the following: """ ../examples/energy/energy-model-example.cc:39:1: error: unused function 'PrintReceivedPacket' [-Werror,-Wunused-function] PrintReceivedPacket (Address& from) ^ 1 error generated. """ Implemented "if (false)" trick inside NS_LOG* macros for optimized builds. "sizeof()" trick might be a little better but it produces the following warning with clang-3.4: """ ../examples/energy/energy-model-example.cc:39:1: error: function 'PrintReceivedPacket' is not needed and will not be emitted [-Werror,-Wunneeded-internal-declaration] PrintReceivedPacket (Address& from) ^ 1 error generated. """ Macros from log.h, that depend on NS3_LOG_ENABLE, were moved to log-macros-enabled.h and log-macros-disabled.h to make log.h smaller.
src/core/model/log-macros-disabled.h
src/core/model/log-macros-enabled.h
src/core/model/log.h
src/core/wscript
src/internet/test/tcp-test.cc
src/olsr/model/olsr-routing-protocol.cc
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/core/model/log-macros-disabled.h	Sun Mar 23 19:08:54 2014 +0400
@@ -0,0 +1,50 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2014 Andrey Mazo
+ *
+ * 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: Andrey Mazo <ahippo@yandex.com>
+ */
+
+#ifndef NS3_LOG_MACROS_DISABLED_H
+#define NS3_LOG_MACROS_DISABLED_H
+
+#ifndef NS3_LOG_ENABLE
+
+#define NS_LOG_NOOP_INTERNAL(msg)           \
+  do                                        \
+    {                                       \
+      if (false)                            \
+        {                                   \
+          std::clog << msg;                 \
+        }                                   \
+    }                                       \
+  while (false)
+
+#define NS_LOG(level, msg) \
+        NS_LOG_NOOP_INTERNAL (msg)
+
+#define NS_LOG_FUNCTION_NOARGS()
+
+#define NS_LOG_FUNCTION(parameters) \
+        NS_LOG_NOOP_INTERNAL (parameters)
+
+#define NS_LOG_UNCOND(msg) \
+        NS_LOG_NOOP_INTERNAL (msg)
+
+
+#endif /* !NS3_LOG_ENABLE */
+
+#endif /* NS3_LOG_MACROS_DISABLED_H */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/core/model/log-macros-enabled.h	Sun Mar 23 19:08:54 2014 +0400
@@ -0,0 +1,224 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/*
+ * Copyright (c) 2006,2007 INRIA
+ *
+ * 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 NS3_LOG_MACROS_ENABLED_H
+#define NS3_LOG_MACROS_ENABLED_H
+
+#ifdef NS3_LOG_ENABLE
+
+
+/**
+ * \ingroup logging
+ * Append the simulation time to a log message.
+ * \internal
+ * Logging implementation macro; should not be called directly.
+ */
+#define NS_LOG_APPEND_TIME_PREFIX                               \
+  if (g_log.IsEnabled (ns3::LOG_PREFIX_TIME))                   \
+    {                                                           \
+      ns3::LogTimePrinter printer = ns3::LogGetTimePrinter ();  \
+      if (printer != 0)                                         \
+        {                                                       \
+          (*printer)(std::clog);                                \
+          std::clog << " ";                                     \
+        }                                                       \
+    }
+
+/**
+ * \ingroup logging
+ * Append the simulation node id to a log message.
+ * \internal
+ * Logging implementation macro; should not be called directly.
+ */
+#define NS_LOG_APPEND_NODE_PREFIX                               \
+  if (g_log.IsEnabled (ns3::LOG_PREFIX_NODE))                   \
+    {                                                           \
+      ns3::LogNodePrinter printer = ns3::LogGetNodePrinter ();  \
+      if (printer != 0)                                         \
+        {                                                       \
+          (*printer)(std::clog);                                \
+          std::clog << " ";                                     \
+        }                                                       \
+    }
+
+/**
+ * \ingroup logging
+ * Append the function name to a log message.
+ * \internal
+ * Logging implementation macro; should not be called directly.
+ */
+#define NS_LOG_APPEND_FUNC_PREFIX                               \
+  if (g_log.IsEnabled (ns3::LOG_PREFIX_FUNC))                   \
+    {                                                           \
+      std::clog << g_log.Name () << ":" <<                      \
+      __FUNCTION__ << "(): ";                                   \
+    }                                                           \
+
+/**
+ * \ingroup logging
+ * Append the log severity level to a log message.
+ * \internal
+ * Logging implementation macro; should not be called directly.
+ */
+#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
+/**
+ * \ingroup logging
+ * Append the node id to a log message.
+ *
+ * This is implemented locally in `.cc` files because
+ * the the relevant variable is only known there.
+ *
+ * Preferred format is something like (assuming the node id is
+ * accessible from `var`:
+ * \code
+ *   if (var)
+ *     {
+ *       std::clog << "[node " << var->GetObject<Node> ()->GetId () << "] ";
+ *     }
+ * \endcode
+ *
+ * \internal
+ * Logging implementation macro; should not be called directly.
+ *
+ */
+#define NS_LOG_APPEND_CONTEXT
+#endif /* NS_LOG_APPEND_CONTEXT */
+
+
+/**
+ * \ingroup logging
+ *
+ * This macro allows you to log an arbitrary message at a specific
+ * log level.
+ *
+ * The log message is expected to be a C++ ostream
+ * message such as "my string" << aNumber << "my oth stream".
+ *
+ * Typical usage looks like:
+ * \code
+ * NS_LOG (LOG_DEBUG, "a number="<<aNumber<<", anotherNumber="<<anotherNumber);
+ * \endcode
+ *
+ * \param level the log level
+ * \param msg the message to log
+ * \internal
+ * Logging implementation macro; should not be called directly.
+ */
+#define NS_LOG(level, msg)                                      \
+  do                                                            \
+    {                                                           \
+      if (g_log.IsEnabled (level))                              \
+        {                                                       \
+          NS_LOG_APPEND_TIME_PREFIX;                            \
+          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;                        \
+        }                                                       \
+    }                                                           \
+  while (false)
+
+/**
+ * \ingroup logging
+ *
+ * Output the name of the function.
+ *
+ * This should be used only in static functions; most member functions
+ * should instead use NS_LOG_FUNCTION().
+ */
+#define NS_LOG_FUNCTION_NOARGS()                                \
+  do                                                            \
+    {                                                           \
+      if (g_log.IsEnabled (ns3::LOG_FUNCTION))                  \
+        {                                                       \
+          NS_LOG_APPEND_TIME_PREFIX;                            \
+          NS_LOG_APPEND_NODE_PREFIX;                            \
+          NS_LOG_APPEND_CONTEXT;                                \
+          std::clog << g_log.Name () << ":"                     \
+                    << __FUNCTION__ << "()" << std::endl;       \
+        }                                                       \
+    }                                                           \
+  while (false)
+
+
+/**
+ * \ingroup logging
+ *
+ * If log level LOG_FUNCTION is enabled, this macro will output
+ * all input parameters separated by ", ".
+ *
+ * Typical usage looks like:
+ * \code
+ * NS_LOG_FUNCTION (aNumber<<anotherNumber);
+ * \endcode
+ * And the output will look like:
+ * \code
+ * Component:Function (aNumber, anotherNumber)
+ * \endcode
+ *
+ * To facilitate function tracing, most functions should begin with
+ * (at least) NS_LOG_FUNCTION(this).  Static functions should use
+ * NS_LOG_FUNCTION_NOARGS() instead.
+ *
+ * \param parameters the parameters to output.
+ */
+#define NS_LOG_FUNCTION(parameters)                             \
+  do                                                            \
+    {                                                           \
+      if (g_log.IsEnabled (ns3::LOG_FUNCTION))                  \
+        {                                                       \
+          NS_LOG_APPEND_TIME_PREFIX;                            \
+          NS_LOG_APPEND_NODE_PREFIX;                            \
+          NS_LOG_APPEND_CONTEXT;                                \
+          std::clog << g_log.Name () << ":"                     \
+                    << __FUNCTION__ << "(";                     \
+          ns3::ParameterLogger (std::clog) << parameters;       \
+          std::clog << ")" << std::endl;                        \
+        }                                                       \
+    }                                                           \
+  while (false)
+
+
+/**
+ * \ingroup logging
+ *
+ * Output the requested message unconditionaly.
+ *
+ * \param msg the message to log
+ */
+#define NS_LOG_UNCOND(msg)              \
+  do                                    \
+    {                                   \
+      std::clog << msg << std::endl;    \
+    }                                   \
+  while (false)
+
+
+#endif /* NS3_LOG_ENABLE */
+
+#endif /* NS3_LOG_MACROS_ENABLED_H */
--- a/src/core/model/log.h	Sat Mar 29 18:16:59 2014 +0100
+++ b/src/core/model/log.h	Sun Mar 23 19:08:54 2014 +0400
@@ -26,6 +26,9 @@
 #include <stdint.h>
 #include <map>
 
+#include "log-macros-enabled.h"
+#include "log-macros-disabled.h"
+
 namespace ns3 {
 
 /**
@@ -182,127 +185,6 @@
 
 /**
  * \ingroup logging
- * Append the simulation time to a log message.
- * \internal
- * Logging implementation macro; should not be called directly.
- */
-#define NS_LOG_APPEND_TIME_PREFIX                               \
-  if (g_log.IsEnabled (ns3::LOG_PREFIX_TIME))                   \
-    {                                                           \
-      ns3::LogTimePrinter printer = ns3::LogGetTimePrinter ();  \
-      if (printer != 0)                                         \
-        {                                                       \
-          (*printer)(std::clog);                                \
-          std::clog << " ";                                     \
-        }                                                       \
-    }
-
-/**
- * \ingroup logging
- * Append the simulation node id to a log message.
- * \internal
- * Logging implementation macro; should not be called directly.
- */
-#define NS_LOG_APPEND_NODE_PREFIX                               \
-  if (g_log.IsEnabled (ns3::LOG_PREFIX_NODE))                   \
-    {                                                           \
-      ns3::LogNodePrinter printer = ns3::LogGetNodePrinter ();  \
-      if (printer != 0)                                         \
-        {                                                       \
-          (*printer)(std::clog);                                \
-          std::clog << " ";                                     \
-        }                                                       \
-    }
-
-/**
- * \ingroup logging
- * Append the function name to a log message.
- * \internal
- * Logging implementation macro; should not be called directly.
- */
-#define NS_LOG_APPEND_FUNC_PREFIX                               \
-  if (g_log.IsEnabled (ns3::LOG_PREFIX_FUNC))                   \
-    {                                                           \
-      std::clog << g_log.Name () << ":" <<                      \
-      __FUNCTION__ << "(): ";                                   \
-    }                                                           \
-
-/**
- * \ingroup logging
- * Append the log severity level to a log message.
- * \internal
- * Logging implementation macro; should not be called directly.
- */
-#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
-/**
- * \ingroup logging
- * Append the node id to a log message.
- *
- * This is implemented locally in `.cc` files because
- * the the relevant variable is only known there.
- *
- * Preferred format is something like (assuming the node id is
- * accessible from `var`:
- * \code
- *   if (var)
- *     {
- *       std::clog << "[node " << var->GetObject<Node> ()->GetId () << "] ";
- *     }
- * \endcode
- *
- * \internal
- * Logging implementation macro; should not be called directly.
- *
- */
-#define NS_LOG_APPEND_CONTEXT
-#endif /* NS_LOG_APPEND_CONTEXT */
-
-
-#ifdef NS3_LOG_ENABLE
-
-/**
- * \ingroup logging
- *
- * This macro allows you to log an arbitrary message at a specific
- * log level.
- *
- * The log message is expected to be a C++ ostream
- * message such as "my string" << aNumber << "my oth stream".
- *
- * Typical usage looks like:
- * \code
- * NS_LOG (LOG_DEBUG, "a number="<<aNumber<<", anotherNumber="<<anotherNumber);
- * \endcode
- *
- * \param level the log level
- * \param msg the message to log
- * \internal
- * Logging implementation macro; should not be called directly.
- */
-#define NS_LOG(level, msg)                                      \
-  do                                                            \
-    {                                                           \
-      if (g_log.IsEnabled (level))                              \
-        {                                                       \
-          NS_LOG_APPEND_TIME_PREFIX;                            \
-          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;                        \
-        }                                                       \
-    }                                                           \
-  while (false)
-
-/**
- * \ingroup logging
  *
  * Use \ref NS_LOG to output a message of level LOG_ERROR.
  *
@@ -344,67 +226,6 @@
 /**
  * \ingroup logging
  *
- * Output the name of the function.
- *
- * This should be used only in static functions; most member functions
- * should instead use NS_LOG_FUNCTION().
- */
-#define NS_LOG_FUNCTION_NOARGS()                                \
-  do                                                            \
-    {                                                           \
-      if (g_log.IsEnabled (ns3::LOG_FUNCTION))                  \
-        {                                                       \
-          NS_LOG_APPEND_TIME_PREFIX;                            \
-          NS_LOG_APPEND_NODE_PREFIX;                            \
-          NS_LOG_APPEND_CONTEXT;                                \
-          std::clog << g_log.Name () << ":"                     \
-                    << __FUNCTION__ << "()" << std::endl;       \
-        }                                                       \
-    }                                                           \
-  while (false)
-
-
-/**
- * \ingroup logging
- *
- * If log level LOG_FUNCTION is enabled, this macro will output
- * all input parameters separated by ", ".
- *
- * Typical usage looks like:
- * \code
- * NS_LOG_FUNCTION (aNumber<<anotherNumber);
- * \endcode
- * And the output will look like:
- * \code
- * Component:Function (aNumber, anotherNumber)
- * \endcode
- *
- * To facilitate function tracing, most functions should begin with
- * (at least) NS_LOG_FUNCTION(this).  Static functions should use
- * NS_LOG_FUNCTION_NOARGS() instead.
- *
- * \param parameters the parameters to output.
- */
-#define NS_LOG_FUNCTION(parameters)                             \
-  do                                                            \
-    {                                                           \
-      if (g_log.IsEnabled (ns3::LOG_FUNCTION))                  \
-        {                                                       \
-          NS_LOG_APPEND_TIME_PREFIX;                            \
-          NS_LOG_APPEND_NODE_PREFIX;                            \
-          NS_LOG_APPEND_CONTEXT;                                \
-          std::clog << g_log.Name () << ":"                     \
-                    << __FUNCTION__ << "(";                     \
-          ns3::ParameterLogger (std::clog) << parameters;       \
-          std::clog << ")" << std::endl;                        \
-        }                                                       \
-    }                                                           \
-  while (false)
-
-
-/**
- * \ingroup logging
- *
  * Use \ref NS_LOG to output a message of level LOG_LOGIC
  *
  * \param msg the message to log
@@ -412,33 +233,6 @@
 #define NS_LOG_LOGIC(msg) \
   NS_LOG (ns3::LOG_LOGIC, msg)
 
-/**
- * \ingroup logging
- *
- * Output the requested message unconditionaly.
- *
- * \param msg the message to log
- */
-#define NS_LOG_UNCOND(msg)              \
-  do                                    \
-    {                                   \
-      std::clog << msg << std::endl;    \
-    }                                   \
-  while (false)
-
-#else /* LOG_ENABLE */
-
-#define NS_LOG(level, msg)
-#define NS_LOG_ERROR(msg)
-#define NS_LOG_WARN(msg)
-#define NS_LOG_DEBUG(msg)
-#define NS_LOG_INFO(msg)
-#define NS_LOG_FUNCTION_NOARGS()
-#define NS_LOG_FUNCTION(msg)
-#define NS_LOG_LOGIC(msg)
-#define NS_LOG_UNCOND(msg)
-
-#endif /* LOG_ENABLE */
 
 namespace ns3 {
 
--- a/src/core/wscript	Sat Mar 29 18:16:59 2014 +0100
+++ b/src/core/wscript	Sun Mar 23 19:08:54 2014 +0400
@@ -248,6 +248,8 @@
         'model/ptr.h',
         'model/object.h',
         'model/log.h',
+        'model/log-macros-enabled.h',
+        'model/log-macros-disabled.h',
         'model/assert.h',
         'model/breakpoint.h',
         'model/fatal-error.h',
--- a/src/internet/test/tcp-test.cc	Sat Mar 29 18:16:59 2014 +0100
+++ b/src/internet/test/tcp-test.cc	Sun Mar 23 19:08:54 2014 +0400
@@ -108,14 +108,12 @@
   return oss.str ();
 }
 
-#ifdef NS3_LOG_ENABLE
-static std::string GetString (Ptr<Packet> p)
+static inline std::string GetString (Ptr<Packet> p)
 {
   std::ostringstream oss;
   p->CopyData (&oss, p->GetSize ());
   return oss.str ();
 }
-#endif /* NS3_LOG_ENABLE */
 
 TcpTestCase::TcpTestCase (uint32_t totalStreamSize,
                           uint32_t sourceWriteSize,
--- a/src/olsr/model/olsr-routing-protocol.cc	Sat Mar 29 18:16:59 2014 +0100
+++ b/src/olsr/model/olsr-routing-protocol.cc	Sun Mar 23 19:08:54 2014 +0400
@@ -2456,9 +2456,7 @@
 
   if (nb_tuple != NULL)
     {
-#ifdef NS3_LOG_ENABLE
       int statusBefore = nb_tuple->status;
-#endif // NS3_LOG_ENABLE
 
       bool hasSymmetricLink = false;