Use static array for log level strings

This commit is contained in:
Nils O. Selåsdal
2013-06-05 20:51:53 +02:00
parent f2bbca8718
commit 972381bf13
+29 -12
View File
@@ -7,6 +7,7 @@
#include <syslog.h> #include <syslog.h>
#include <sys/queue.h> #include <sys/queue.h>
#include "ucore/logging.h" #include "ucore/logging.h"
#include "ucore/utils.h"
struct uc_log_destination { struct uc_log_destination {
@@ -59,23 +60,39 @@ static const struct uc_log_module g_unknown_module = {
//this list an anything contained within it. //this list an anything contained within it.
static SLIST_HEAD(, uc_log_destination) g_destinations; static SLIST_HEAD(, uc_log_destination) g_destinations;
static int uc_log_reopen_file(struct uc_log_destination *dest); static int uc_log_reopen_file(struct uc_log_destination *dest);
//Uses enum UC_LOG_LEVEL as index
static const char *const g_debug_levels[] = {
"NONE",
"DEBUG",
"UNKNOWN_2",
"INFO",
"UNKNOWN_4"
"WARNING",
"UNKNOWN_6",
"ERROR"
};
inline const char *uc_ll_2_str(enum UC_LOG_LEVEL l) inline const char *uc_ll_2_str(enum UC_LOG_LEVEL l)
{ {
switch(l) { if (l >= 0 && l < (int)ARRAY_SIZE(g_debug_levels)) {
case UC_LL_NONE: return g_debug_levels[l];
return "NONE"; } else {
case UC_LL_DEBUG: return "???";
return "DEBUG"; }
case UC_LL_INFO: }
return "INFO";
case UC_LL_WARNING: //returns -1 if the string is not a known log level
return "WARNING"; static enum UC_LOG_LEVEL uc_str_2_ll(const char *str)
case UC_LL_ERROR: {
return "ERROR"; size_t i;
for (i = 0; i < ARRAY_SIZE(g_debug_levels); i++) {
if (strcmp(str, g_debug_levels[i]) == 0) {
return (int)i;
}
} }
return "???"; return -1;
} }
static inline int uc_ll_2_syslog(enum UC_LOG_LEVEL l) static inline int uc_ll_2_syslog(enum UC_LOG_LEVEL l)