Eina safety: Add internal function to log errors
authorJean-Philippe Andre <jp.andre@samsung.com>
Tue, 15 Dec 2015 07:56:13 +0000 (16:56 +0900)
committerJean-Philippe Andre <jp.andre@samsung.com>
Tue, 15 Dec 2015 08:32:50 +0000 (17:32 +0900)
The main reason is convenience for debugging when using GDB,
this will give a simple breakpoint for all safety check failures.

Also, this creates a more visible log domain (red).

src/lib/eina/eina_safety_checks.c
src/lib/eina/eina_safety_checks.h

index 1ee6991..57b863d 100644 (file)
 #include "eina_log.h"
 #include "eina_safety_checks.h"
 
-/*============================================================================*
-*                                  Local                                     *
-*============================================================================*/
+EAPI Eina_Error EINA_ERROR_SAFETY_FAILED = 0;
 
-/*============================================================================*
-*                                 Global                                     *
-*============================================================================*/
+static int EINA_SAFETY_LOG_DOMAIN = 0;
+static int initcnt = 0;
 
 /**
- * @internal
- * @brief Shut down the safety checks module.
+ * Log entry-point called every time an eina safety check fails.
  *
- * @return #EINA_TRUE on success, #EINA_FALSE on failure.
+ * One purpose of this dedicated function is to provide a convenient breakpoint
+ * for GDB debugging. Also, this gives it a dedicated log domain, rather than
+ * using the default one.
  *
- * This function shuts down the error module set up by
- * eina_safety_checks_init(). It is called by eina_shutdown().
- *
- * @see eina_shutdown()
+ * @since 1.17
+ * @internal
  */
 Eina_Bool
 eina_safety_checks_shutdown(void)
 {
+   if (!initcnt) return EINA_FALSE;
+   if (!(--initcnt))
+     {
+        eina_log_domain_unregister(EINA_SAFETY_LOG_DOMAIN);
+        EINA_SAFETY_LOG_DOMAIN = 0;
+     }
    return EINA_TRUE;
 }
 
-/*============================================================================*
-*                                   API                                      *
-*============================================================================*/
-
-/**
- * @cond LOCAL
- */
-
-EAPI Eina_Error EINA_ERROR_SAFETY_FAILED = 0;
-
-/**
- * @endcond
- */
-
 /**
  * @internal
  * @brief Initialize the safety checks module.
@@ -78,9 +66,24 @@ EAPI Eina_Error EINA_ERROR_SAFETY_FAILED = 0;
 Eina_Bool
 eina_safety_checks_init(void)
 {
+   if (!(initcnt++))
+     {
+        EINA_SAFETY_LOG_DOMAIN = eina_log_domain_register("eina_safety", EINA_COLOR_RED);
+     }
    return EINA_TRUE;
 }
 
-/**
- * @}
- */
+EAPI void
+_eina_safety_error(const char *file, const char *func, int line, const char *str)
+{
+   if (EINA_SAFETY_LOG_DOMAIN)
+     {
+        eina_log_print(EINA_SAFETY_LOG_DOMAIN, EINA_LOG_LEVEL_ERR,
+                       file, func, line, "%s", str);
+     }
+   else
+     {
+        eina_log_print(EINA_LOG_DOMAIN_DEFAULT, EINA_LOG_LEVEL_ERR,
+                       file, func, line, "%s", str);
+     }
+}
index d40eeb9..656054b 100644 (file)
@@ -93,12 +93,29 @@ EAPI extern Eina_Error EINA_ERROR_SAFETY_FAILED;
 
 #include "eina_log.h"
 
+# ifdef EFL_BETA_API_SUPPORT
+/**
+ * Log entry-point called every time an eina safety check fails.
+ *
+ * One purpose of this dedicated function is to provide a convenient breakpoint
+ * for GDB debugging. Also, this gives it a dedicated log domain, rather than
+ * using the default one.
+ *
+ * @since 1.17
+ * @internal
+ */
+EAPI void _eina_safety_error(const char *file, const char *func, int line, const char *str);
+#  define EINA_SAFETY_ERROR(msg) _eina_safety_error(__FILE__, __FUNCTION__, __LINE__, msg)
+# else
+#  define EINA_SAFETY_ERROR(msg) EINA_LOG_ERR("%s", msg)
+# endif
+
 #define EINA_SAFETY_ON_NULL_RETURN(exp)                                   \
   do                                                                      \
     {                                                                     \
        if (EINA_UNLIKELY((exp) == NULL))                                  \
          {                                                                \
-            EINA_LOG_ERR("%s", "safety check failed: " # exp " == NULL"); \
+            EINA_SAFETY_ERROR("safety check failed: " # exp " == NULL");  \
             return;                                                       \
          }                                                                \
     }                                                                     \
@@ -109,7 +126,7 @@ EAPI extern Eina_Error EINA_ERROR_SAFETY_FAILED;
     {                                                                     \
        if (EINA_UNLIKELY((exp) == NULL))                                  \
          {                                                                \
-            EINA_LOG_ERR("%s", "safety check failed: " # exp " == NULL"); \
+            EINA_SAFETY_ERROR("safety check failed: " # exp " == NULL");  \
             return (val);                                                 \
          }                                                                \
     }                                                                     \
@@ -120,7 +137,7 @@ EAPI extern Eina_Error EINA_ERROR_SAFETY_FAILED;
     {                                                                     \
        if (EINA_UNLIKELY((exp) == NULL))                                  \
          {                                                                \
-            EINA_LOG_ERR("%s", "safety check failed: " # exp " == NULL"); \
+            EINA_SAFETY_ERROR("safety check failed: " # exp " == NULL");  \
             goto label;                                                   \
          }                                                                \
     }                                                                     \
@@ -131,7 +148,7 @@ EAPI extern Eina_Error EINA_ERROR_SAFETY_FAILED;
     {                                                                     \
        if (EINA_UNLIKELY(exp))                                            \
          {                                                                \
-            EINA_LOG_ERR("%s", "safety check failed: " # exp " is true"); \
+            EINA_SAFETY_ERROR("safety check failed: " # exp " is true");  \
             return;                                                       \
          }                                                                \
     }                                                                     \
@@ -142,7 +159,7 @@ EAPI extern Eina_Error EINA_ERROR_SAFETY_FAILED;
     {                                                                     \
        if (EINA_UNLIKELY(exp))                                            \
          {                                                                \
-            EINA_LOG_ERR("%s", "safety check failed: " # exp " is true"); \
+            EINA_SAFETY_ERROR("safety check failed: " # exp " is true");  \
             return val;                                                   \
          }                                                                \
     }                                                                     \
@@ -153,7 +170,7 @@ EAPI extern Eina_Error EINA_ERROR_SAFETY_FAILED;
     {                                                                     \
        if (EINA_UNLIKELY(exp))                                            \
          {                                                                \
-            EINA_LOG_ERR("%s", "safety check failed: " # exp " is true"); \
+            EINA_SAFETY_ERROR("safety check failed: " # exp " is true");  \
             goto label;                                                   \
          }                                                                \
     }                                                                     \
@@ -164,7 +181,7 @@ EAPI extern Eina_Error EINA_ERROR_SAFETY_FAILED;
     {                                                                      \
        if (EINA_UNLIKELY(!(exp)))                                          \
          {                                                                 \
-            EINA_LOG_ERR("%s", "safety check failed: " # exp " is false"); \
+            EINA_SAFETY_ERROR("safety check failed: " # exp " is false");  \
             return;                                                        \
          }                                                                 \
     }                                                                      \
@@ -175,7 +192,7 @@ EAPI extern Eina_Error EINA_ERROR_SAFETY_FAILED;
     {                                                                      \
        if (EINA_UNLIKELY(!(exp)))                                          \
          {                                                                 \
-            EINA_LOG_ERR("%s", "safety check failed: " # exp " is false"); \
+            EINA_SAFETY_ERROR("safety check failed: " # exp " is false");  \
             return val;                                                    \
          }                                                                 \
     }                                                                      \
@@ -186,7 +203,7 @@ EAPI extern Eina_Error EINA_ERROR_SAFETY_FAILED;
     {                                                                      \
        if (EINA_UNLIKELY(!(exp)))                                          \
          {                                                                 \
-            EINA_LOG_ERR("%s", "safety check failed: " # exp " is false"); \
+            EINA_SAFETY_ERROR("safety check failed: " # exp " is false");  \
             goto label;                                                    \
          }                                                                 \
     }                                                                      \