Eina safety: Add internal function to log errors

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).
This commit is contained in:
Jean-Philippe Andre 2015-12-15 16:56:13 +09:00
parent 1f75cfc3cf
commit 5ad9654fbc
2 changed files with 60 additions and 40 deletions

View File

@ -25,45 +25,33 @@
#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;
/**
* 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
* @brief Shut down the safety checks module.
*
* @return #EINA_TRUE on success, #EINA_FALSE on failure.
*
* This function shuts down the error module set up by
* eina_safety_checks_init(). It is called by eina_shutdown().
*
* @see eina_shutdown()
*/
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);
}
}

View 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; \
} \
} \