]> git.ipfire.org Git - people/ms/u-boot.git/commitdiff
log: Add functions to convert IDs to/from names
authorSimon Glass <sjg@chromium.org>
Thu, 28 Dec 2017 20:14:16 +0000 (13:14 -0700)
committerSimon Glass <sjg@chromium.org>
Sat, 3 Feb 2018 17:09:13 +0000 (10:09 -0700)
Category and level both use an enum for their ID values. Add functions to
convert these IDs to strings and vice versa. This will allow the log to
output the strings instead of the (inscrutable) values.

At the same time, add a new 'driver-model' category, to cover core
driver-model functions and fix an incorrect value for LOGL_MAX.

Tests will be added with the new 'log' subcommands.

Signed-off-by: Simon Glass <sjg@chromium.org>
(Updated to correct clang warnings)

common/log.c
include/log.h

index 45e46dd5209118fcffd0333c3a91bdbeb78bee6a..d5f1cc50777d7618699166605c890fa1f50a460f 100644 (file)
 #include <common.h>
 #include <log.h>
 #include <malloc.h>
+#include <dm/uclass.h>
 
 DECLARE_GLOBAL_DATA_PTR;
 
+static const char *log_cat_name[LOGC_COUNT - LOGC_NONE] = {
+       "none",
+       "arch",
+       "board",
+       "core",
+       "driver-model",
+       "device-tree",
+};
+
+static const char *log_level_name[LOGL_COUNT] = {
+       "EMERG",
+       "ALERT",
+       "CRIT",
+       "ERR",
+       "WARNING",
+       "NOTICE",
+       "INFO",
+       "DEBUG",
+       "CONTENT",
+       "IO",
+};
+
+const char *log_get_cat_name(enum log_category_t cat)
+{
+       if (cat > LOGC_COUNT)
+               return "invalid";
+       if (cat >= LOGC_NONE)
+               return log_cat_name[cat - LOGC_NONE];
+
+       return uclass_get_name((enum uclass_id)cat);
+}
+
+enum log_category_t log_get_cat_by_name(const char *name)
+{
+       enum uclass_id id;
+       int i;
+
+       for (i = LOGC_NONE; i < LOGC_COUNT; i++)
+               if (!strcmp(name, log_cat_name[i - LOGC_NONE]))
+                       return i;
+       id = uclass_get_by_name(name);
+       if (id != UCLASS_INVALID)
+               return (enum log_category_t)id;
+
+       return LOGC_NONE;
+}
+
+const char *log_get_level_name(enum log_level_t level)
+{
+       if (level >= LOGL_COUNT)
+               return "INVALID";
+       return log_level_name[level];
+}
+
+enum log_level_t log_get_level_by_name(const char *name)
+{
+       int i;
+
+       for (i = 0; i < LOGL_COUNT; i++) {
+               if (!strcasecmp(log_level_name[i], name))
+                       return i;
+       }
+
+       return LOGL_NONE;
+}
+
 static struct log_device *log_device_find_by_name(const char *drv_name)
 {
        struct log_device *ldev;
index 8083b64831d9208726777753d6ed6955173a3812..5133213acf56be94797cc23aae45c501c42859a0 100644 (file)
@@ -27,8 +27,10 @@ enum log_level_t {
        LOGL_DEBUG_IO,          /* Debug message showing hardware I/O access */
 
        LOGL_COUNT,
+       LOGL_NONE,
+
        LOGL_FIRST = LOGL_EMERG,
-       LOGL_MAX = LOGL_DEBUG,
+       LOGL_MAX = LOGL_DEBUG_IO,
 };
 
 /**
@@ -42,7 +44,8 @@ enum log_category_t {
        LOGC_ARCH,
        LOGC_BOARD,
        LOGC_CORE,
-       LOGC_DT,
+       LOGC_DM,        /* Core driver-model */
+       LOGC_DT,        /* Device-tree */
 
        LOGC_COUNT,
        LOGC_END,
@@ -256,6 +259,38 @@ struct log_filter {
 #define LOG_DRIVER(_name) \
        ll_entry_declare(struct log_driver, _name, log_driver)
 
+/**
+ * log_get_cat_name() - Get the name of a category
+ *
+ * @cat: Category to look up
+ * @return category name (which may be a uclass driver name)
+ */
+const char *log_get_cat_name(enum log_category_t cat);
+
+/**
+ * log_get_cat_by_name() - Look up a category by name
+ *
+ * @name: Name to look up
+ * @return category ID, or LOGC_NONE if not found
+ */
+enum log_category_t log_get_cat_by_name(const char *name);
+
+/**
+ * log_get_level_name() - Get the name of a log level
+ *
+ * @level: Log level to look up
+ * @return log level name (in ALL CAPS)
+ */
+const char *log_get_level_name(enum log_level_t level);
+
+/**
+ * log_get_level_by_name() - Look up a log level by name
+ *
+ * @name: Name to look up
+ * @return log level ID, or LOGL_NONE if not found
+ */
+enum log_level_t log_get_level_by_name(const char *name);
+
 /* Handle the 'log test' command */
 int do_log_test(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]);