eina log level utilities.

couple of functions that are useful outside and should be fast.



SVN revision: 45996
This commit is contained in:
Gustavo Sverzut Barbieri 2010-02-09 01:43:58 +00:00
parent f7cd9786f1
commit 191d87d78a
5 changed files with 248 additions and 16 deletions

View File

@ -4,6 +4,7 @@ EINAHEADERS = \
eina_safety_checks.h \
eina_error.h \
eina_log.h \
eina_inline_log.x \
eina_fp.h \
eina_inline_f32p32.x \
eina_inline_f16p16.x \

View File

@ -0,0 +1,197 @@
/* EINA - EFL data type library
* Copyright (C) 2002-2008 Gustavo Sverzut Barbieri
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library;
* if not, see <http://www.gnu.org/licenses/>.
*/
#ifndef EINA_LOG_INLINE_H_
#define EINA_LOG_INLINE_H_
/**
* @addtogroup Eina_Log_Group Log
*
* @{
*/
/**
* Checks whenever the given level should be printed out.
*
* This is useful to enable certain blocks of code just when given
* level is to be used.
*
* @code
* #include <Eina.h>
*
* void my_func(void *data)
* {
* if (eina_log_level_check(EINA_LOG_LEVEL_WARN))
* expensive_debugging_code(data);
*
* my_func_code(data);
* }
* @endcode
*
* @return #EINA_TRUE if level is equal or smaller than the current global
* logging level.
*/
static inline Eina_Bool
eina_log_level_check(int level)
{
return eina_log_level_get() <= level;
}
/**
* Checks whenever the given level should be printed out.
*
* This is useful to enable certain blocks of code just when given
* level is to be used.
*
* @code
* #include <Eina.h>
*
* extern int _my_log_dom;
*
* void my_func(void *data)
* {
* if (eina_log_domain_level_check(_my_log_dom, EINA_LOG_LEVEL_WARN))
* expensive_debugging_code(data);
*
* my_func_code(data);
* }
* @endcode
*
* @return #EINA_TRUE if level is equal or smaller than the current
* domain logging level.
*/
static inline Eina_Bool
eina_log_domain_level_check(int domain, int level)
{
int dom_level = eina_log_domain_registered_level_get(domain);
if (EINA_UNLIKELY(dom_level == EINA_LOG_LEVEL_UNKNOWN))
return EINA_FALSE;
return dom_level <= level;
}
/**
* Function to format the level as a 3 character (+1 null byte) string.
*
* This function converts the given level to a known string name (CRI,
* ERR, WRN, INF or DBG) or a zero-padded 3-character string. In any
* case the last byte will contain a trailing null byte.
*
* If extreme level values are used (greater than 999 and smaller than
* -99), then the value will just consider the less significant
* part. This is so uncommon that users should handle this in their
* code.
*
* @param level what level value to use.
* @param name where to write the actual value.
*
* @return pointer to @p name.
*/
static inline const char *
eina_log_level_name_get(int level, char name[4])
{
#define BCPY(A, B, C) \
do { name[0] = A; name[1] = B; name[2] = C; } while (0)
if (EINA_UNLIKELY(level < 0))
{
name[0] = '-';
name[1] = '0' + (-level / 10) % 10;
name[2] = '0' + (-level % 10);
}
else if (EINA_UNLIKELY(level >= EINA_LOG_LEVELS))
{
name[0] = '0' + (level / 100) % 10;
name[1] = '0' + (level / 10) % 10;
name[2] = '0' + level % 10;
}
else if (level == 0)
BCPY('C', 'R', 'I');
else if (level == 1)
BCPY('E', 'R', 'R');
else if (level == 2)
BCPY('W', 'R', 'N');
else if (level == 3)
BCPY('I', 'N', 'F');
else if (level == 4)
BCPY('D', 'B', 'G');
else
BCPY('?', '?', '?');
name[3] = '\0';
return name;
}
/**
* Function to get recommended color value for level.
*
* This function will not check if colors are enabled or not before
* returning the level color. If you desire such check, use
* eina_log_level_color_if_enabled_get().
*
* @param level what level value to use.
*
* @return pointer to null byte terminated ANSI color string to be
* used in virtual terminals supporting VT100 color codes.
*
* @see eina_log_level_color_if_enabled_get()
*/
static inline const char *
eina_log_level_color_get(int level)
{
if (level <= 0)
return EINA_COLOR_LIGHTRED;
else if (level == 1)
return EINA_COLOR_RED;
else if (level == 2)
return EINA_COLOR_YELLOW;
else if (level == 3)
return EINA_COLOR_GREEN;
else if (level == 4)
return EINA_COLOR_LIGHTBLUE;
else
return EINA_COLOR_BLUE;
}
/**
* Function to get recommended color value for level, if colors are
* enabled.
*
* This function will check if colors are enabled or not before
* returning the level color. If colors are disabled, then empty
* string is returned.
*
* @param level what level value to use.
*
* @return pointer to null byte terminated ANSI color string to be
* used in virtual terminals supporting VT100 color codes. If
* colors are disabled, the empty string is returned.
*/
static inline const char *
eina_log_level_color_if_enabled_get(int level)
{
if (eina_log_color_disable_get())
return "";
return eina_log_level_color_get(level);
}
/**
* @}
*/
#endif /* EINA_LOG_INLINE_H_ */

View File

@ -205,6 +205,8 @@ EAPI void eina_log_print_cb_set(Eina_Log_Print_Cb cb, void *data) EINA_ARG_
EAPI void eina_log_level_set(int level);
EAPI int eina_log_level_get(void) EINA_WARN_UNUSED_RESULT;
static inline Eina_Bool eina_log_level_check(int level);
EAPI Eina_Bool eina_log_main_thread_check(void) EINA_CONST EINA_WARN_UNUSED_RESULT;
EAPI void eina_log_color_disable_set(Eina_Bool disabled);
@ -221,6 +223,7 @@ EAPI int eina_log_abort_on_critical_level_get(void) EINA_WARN_UNUSED_RESUL
EAPI void eina_log_domain_level_set(const char *domain_name, int level) EINA_ARG_NONNULL(1);
EAPI int eina_log_domain_level_get(const char *domain_name) EINA_WARN_UNUSED_RESULT EINA_ARG_NONNULL(1);
EAPI int eina_log_domain_registered_level_get(int domain) EINA_WARN_UNUSED_RESULT;
static inline Eina_Bool eina_log_domain_level_check(int domain, int level);
/*
@ -243,6 +246,8 @@ EAPI void eina_log_print_cb_stdout(const Eina_Log_Domain *d, Eina_Log_Level leve
EAPI void eina_log_print_cb_stderr(const Eina_Log_Domain *d, Eina_Log_Level level, const char *file, const char *fnc, int line, const char *fmt, void *data, va_list args);
EAPI void eina_log_print_cb_file(const Eina_Log_Domain *d, Eina_Log_Level level, const char *file, const char *fnc, int line, const char *fmt, void *data, va_list args);
#include "eina_inline_log.x"
/**
* @}
*/

View File

@ -436,16 +436,10 @@ static Eina_Log_Level _log_level = EINA_LOG_LEVEL_CRITICAL;
static Eina_Log_Level _log_level = EINA_LOG_LEVEL_ERR;
#endif
// Default colors and levels
static const char *_colors[] = { // + 1 for higher than debug
EINA_COLOR_LIGHTRED, // EINA_LOG_LEVEL_CRITICAL
EINA_COLOR_RED, // EINA_LOG_LEVEL_ERR
EINA_COLOR_YELLOW, // EINA_LOG_LEVEL_WARN
EINA_COLOR_GREEN, // EINA_LOG_LEVEL_INFO
EINA_COLOR_LIGHTBLUE, // EINA_LOG_LEVEL_DBG
EINA_COLOR_BLUE, // Higher than DEBUG
};
/* NOTE: if you change this, also change:
* eina_log_print_level_name_get()
* eina_log_print_level_name_color_get()
*/
static const char *_names[] = {
"CRI",
"ERR",
@ -458,12 +452,16 @@ static inline void
eina_log_print_level_name_get(int level, const char **p_name)
{
static char buf[4];
/* NOTE: if you change this, also change
* eina_log_print_level_name_color_get()
* eina_log_level_name_get() (at eina_inline_log.x)
*/
if (EINA_UNLIKELY(level < 0))
{
snprintf(buf, sizeof(buf), "%03d", level);
*p_name = buf;
}
else if (EINA_UNLIKELY(level > EINA_LOG_LEVELS))
else if (EINA_UNLIKELY(level >= EINA_LOG_LEVELS))
{
snprintf(buf, sizeof(buf), "%03d", level);
*p_name = buf;
@ -476,23 +474,24 @@ static inline void
eina_log_print_level_name_color_get(int level, const char **p_name, const char **p_color)
{
static char buf[4];
/* NOTE: if you change this, also change:
* eina_log_print_level_name_get()
*/
if (EINA_UNLIKELY(level < 0))
{
snprintf(buf, sizeof(buf), "%03d", level);
*p_name = buf;
*p_color = _colors[0];
}
else if (EINA_UNLIKELY(level > EINA_LOG_LEVELS))
else if (EINA_UNLIKELY(level >= EINA_LOG_LEVELS))
{
snprintf(buf, sizeof(buf), "%03d", level);
*p_name = buf;
*p_color = _colors[EINA_LOG_LEVELS];
}
else
{
*p_name = _names[level];
*p_color = _colors[level];
}
*p_color = eina_log_level_color_get(level);
}
#define DECLARE_LEVEL_NAME(level) const char *name; eina_log_print_level_name_get(level, &name)
@ -1001,7 +1000,6 @@ eina_log_init(void)
int color_disable;
assert((sizeof(_names)/sizeof(_names[0])) == EINA_LOG_LEVELS);
assert((sizeof(_colors)/sizeof(_colors[0])) == EINA_LOG_LEVELS + 1);
if ((tmp = getenv(EINA_LOG_ENV_COLOR_DISABLE)))
color_disable = atoi(tmp);

View File

@ -188,6 +188,36 @@ START_TEST(eina_log_customize)
eina_log_abort_on_critical_set(EINA_FALSE);
fail_if(eina_log_domain_registered_level_get(d) != EINA_LOG_LEVEL_UNKNOWN);
#undef test_set_get_bool
#undef test_set_get
eina_shutdown();
}
END_TEST
START_TEST(eina_log_level_name)
{
char name[4];
fail_if(!eina_init());
#define tst(level, str) \
eina_log_level_name_get(level, name); \
fail_if(strcmp(name, str) != 0)
tst(0, "CRI");
tst(1, "ERR");
tst(2, "WRN");
tst(3, "INF");
tst(4, "DBG");
tst(5, "005");
tst(12, "012");
tst(369, "369");
tst(-1, "-01");
tst(-48, "-48");
#undef tst
eina_shutdown();
}
END_TEST
@ -201,4 +231,5 @@ eina_test_log(TCase *tc)
tcase_add_test(tc, eina_log_domains_slot_reuse);
tcase_add_test(tc, eina_log_level_indexes);
tcase_add_test(tc, eina_log_customize);
tcase_add_test(tc, eina_log_level_name);
}