generalize log level converters between strings and urj_log_level_t types

git-svn-id: https://urjtag.svn.sourceforge.net/svnroot/urjtag/trunk@1934 b68d4a1b-bc3d-0410-92ed-d4ac073336b7
master
Mike Frysinger 14 years ago
parent 1ddde8e7ec
commit f10d155e19

@ -37,6 +37,12 @@
* src/cmd/cmd_bfin.c, src/cmd/cmd_set.c: Use new tokens arg to selectively
add matches for specific sub-commands.
* include/urjtag/log.h, src/global/log-error.c: Add new helpers for
converting between string versions of log levels and the actual log
level value.
* src/cmd/cmd_debug.c: Drop local log level converters and use new
common helpers.
2011-06-27 Jie Zhang <jie.zhang@analog.com>
* include/urjtag/Makefile.am (nodist_pkginclude_HEADERS): New and

@ -66,4 +66,22 @@ int urj_do_log (urj_log_level_t level, const char *fmt, ...)
urj_log (URJ_LOG_LEVEL_WARNING, __VA_ARGS__); \
} while (0)
/**
* Convert the named level into the corresponding urj_log_level_t.
*
* @param slevel the string to translate
*
* @return log level on success; -1 on error
*/
urj_log_level_t urj_string_log_level (const char *slevel);
/**
* Convert the urj_log_level_t into a string.
*
* @param level the level to translate
*
* @return the name of the log level on success; "unknown" on error
*/
const char *urj_log_level_string (urj_log_level_t level);
#endif /* URJ_LOG_H */

@ -34,51 +34,6 @@
#include "cmd.h"
static urj_log_level_t
string_to_log_level (const char *strlevel)
{
if (0)
;
else if (strcasecmp(strlevel, "all") == 0)
return URJ_LOG_LEVEL_ALL;
else if (strcasecmp(strlevel, "comm") == 0)
return URJ_LOG_LEVEL_COMM;
else if (strcasecmp(strlevel, "debug") == 0)
return URJ_LOG_LEVEL_DEBUG;
else if (strcasecmp(strlevel, "detail") == 0)
return URJ_LOG_LEVEL_DETAIL;
else if (strcasecmp(strlevel, "normal") == 0)
return URJ_LOG_LEVEL_NORMAL;
else if (strcasecmp(strlevel, "warning") == 0)
return URJ_LOG_LEVEL_WARNING;
else if (strcasecmp(strlevel, "error") == 0)
return URJ_LOG_LEVEL_ERROR;
else if (strcasecmp(strlevel, "silent") == 0)
return URJ_LOG_LEVEL_SILENT;
else
return -1;
}
static const char *
log_level_to_string (urj_log_level_t level)
{
/* sanity for string_to_log_level() return */
if (level == -1)
goto case_default;
switch (level) {
case URJ_LOG_LEVEL_ALL: return "all";
case URJ_LOG_LEVEL_COMM: return "comm";
case URJ_LOG_LEVEL_DEBUG: return "debug";
case URJ_LOG_LEVEL_DETAIL: return "detail";
case URJ_LOG_LEVEL_NORMAL: return "normal";
case URJ_LOG_LEVEL_WARNING: return "warning";
case URJ_LOG_LEVEL_ERROR: return "error";
case URJ_LOG_LEVEL_SILENT: return "silent";
case_default:
default: return "unknown";
}
}
static int
cmd_debug_run (urj_chain_t *chain, char *params[])
{
@ -87,13 +42,13 @@ cmd_debug_run (urj_chain_t *chain, char *params[])
/* display current log level */
case 1:
urj_log (URJ_LOG_LEVEL_NORMAL, _("Current log level is '%s'\n"),
log_level_to_string (urj_log_state.level));
urj_log_level_string (urj_log_state.level));
return URJ_STATUS_OK;
/* set log level */
case 2:
{
urj_log_level_t new_level = string_to_log_level (params[1]);
urj_log_level_t new_level = urj_string_log_level (params[1]);
if (new_level == -1)
{
urj_error_set (URJ_ERROR_SYNTAX, "unknown log level '%s'", params[1]);

@ -90,6 +90,46 @@ urj_error_reset (void)
urj_error_state.errnum = URJ_ERROR_OK;
}
static const struct {
const urj_log_level_t level;
const char *name;
} levels[] = {
#define L(LVL, lvl) { URJ_LOG_LEVEL_##LVL, #lvl, }
L(ALL, all),
L(COMM, comm),
L(DEBUG, debug),
L(DETAIL, detail),
L(NORMAL, normal),
L(WARNING, warning),
L(ERROR, error),
L(SILENT, silent),
#undef L
};
const char *
urj_log_level_string (urj_log_level_t level)
{
size_t i;
for (i = 0; i < ARRAY_SIZE (levels); ++i)
if (levels[i].level == level)
return levels[i].name;
return "unknown";
}
urj_log_level_t
urj_string_log_level (const char *slevel)
{
size_t i;
for (i = 0; i < ARRAY_SIZE (levels); ++i)
if (!strcmp (levels[i].name, slevel))
return levels[i].level;
return -1;
}
const char *
urj_error_string (urj_error_t err)
{

Loading…
Cancel
Save