eina_log optimization: allow to compile out some debug messages.
authorbarbieri <barbieri@7cbeb6ba-43b4-40fd-8cce-4c39aea84d33>
Fri, 4 Sep 2009 21:28:50 +0000 (21:28 +0000)
committerbarbieri <barbieri@7cbeb6ba-43b4-40fd-8cce-4c39aea84d33>
Fri, 4 Sep 2009 21:28:50 +0000 (21:28 +0000)
Users may opt to set EINA_LOG_LEVEL_MAXIMUM to some integer and macro
will then evaluate to check for that value before actually call
eina_log_print() macro. By using optimizations compilers will
effectivelly compile out the code if it is never reached, thus saving
the check and function call in possible critical paths.

git-svn-id: svn+ssh://svn.enlightenment.org/var/svn/e/trunk/eina@42269 7cbeb6ba-43b4-40fd-8cce-4c39aea84d33

configure.ac
src/include/eina_log.h

index de888c8..b6bf2f1 100644 (file)
@@ -94,6 +94,23 @@ if test "x$have_safety_checks" = "xyes"; then
 fi
 AC_SUBST(EINA_CONFIGURE_SAFETY_CHECKS)
 
+with_max_log_level="<unset>"
+AC_ARG_WITH(internal-maximum-log-level,
+   [AC_HELP_STRING([--with-internal-maximum-log-level=NUMBER],
+                   [limit eina internal log level to the given number, any call to EINA_LOG() with values greater than this will be compiled out, ignoring runtime settings, but saving function calls.])],
+   [
+    if test "x${withval}" != "xno"; then
+       if echo "${withval}" | grep '^[[0-9]]\+$' >/dev/null 2>/dev/null; then
+          AC_MSG_NOTICE([ignoring any EINA_LOG() with level greater than ${withval}])
+          AC_DEFINE_UNQUOTED(EINA_LOG_LEVEL_MAXIMUM, ${withval}, [if set, logging is limited to this amount.])
+          with_max_log_level="${withval}"
+       else
+          AC_MSG_ERROR([--with-internal-maximum-log-level takes a decimal number, got "${withval}" instead.])
+       fi
+    fi
+    ], [:])
+
+
 # Choose best memory pool
 AC_ARG_ENABLE([default-mempool],
    [AC_HELP_STRING([--enable-default-mempool], [Default memory allocator could be faster for some computer. @<:@default=disabled@:>@])],
@@ -409,6 +426,7 @@ echo "Configuration Options Summary:"
 echo
 echo "  Magic debug..........: ${have_magic_debug}"
 echo "  Safety checks........: ${have_safety_checks}"
+echo "  Maximum log level....: ${with_max_log_level}"
 echo "  Report string usage..: ${have_stringshare_usage}"
 echo "  Default mempool......: ${have_default_mempool}"
 echo "  Thread Support.......: ${have_pthread}"
index 2fb9904..b18e91a 100644 (file)
@@ -52,9 +52,28 @@ EAPI extern int EINA_LOG_DOMAIN_GLOBAL;
 /**
  * @def EINA_LOG(DOM, LEVEL, fmt, ...)
  * Logs a message on the specified domain, level and format.
- */
+ *
+ * @note if @c EINA_LOG_LEVEL_MAXIMUM is defined, then messages larger
+ *       than this value will be ignored regardless of current domain
+ *       level, the eina_log_print() is not even called! Most
+ *       compilers will just detect the two integers make the branch
+ *       impossible and remove the branch and function call all
+ *       together. Take this as optimization tip and possible remove
+ *       debug messages from binaries to be deployed, saving on hot
+ *       paths. Never define @c EINA_LOG_LEVEL_MAXIMUM on public
+ *       header files.
+ */
+#ifdef EINA_LOG_LEVEL_MAXIMUM
+#define EINA_LOG(DOM, LEVEL, fmt, ...)                                 \
+  do {                                                                 \
+     if (LEVEL <= EINA_LOG_LEVEL_MAXIMUM)                              \
+       eina_log_print(DOM, LEVEL, __FILE__, __FUNCTION__, __LINE__,    \
+                     fmt, ##__VA_ARGS__);                              \
+  } while (0)
+#else
 #define EINA_LOG(DOM, LEVEL, fmt, ...) \
        eina_log_print(DOM, LEVEL, __FILE__, __FUNCTION__, __LINE__, fmt, ##__VA_ARGS__)
+#endif
 
 /**
  * @def EINA_LOG_DOM_CRIT(DOM, fmt, ...)