Skip to contentSkip to conversation

log: Add commands for manipulating filters

PATCH Superseded Sean Anderson 3 participants 32 messages Oct 6, 2020 Library code

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover log: Add commands for manipulating filters

This series adds several commands for adding, listing, and removing log filters. It also adds getopt, since the filter-add command needs to have several optional arguments to be complete, and positional specification of those arguments would have been difficult.

Sean Anderson (18):
log: Fix missing negation of ENOMEM
log: Fix incorrect documentation of log_filter.cat_list
log: Add new category names to log_cat_name
log: Use CONFIG_IS_ENABLED() for LOG_TEST
log: Expose log_device_find_by_name
log: Add function to create a filter with flags
log: Add filter flag to deny on match
test: Add tests for LOGFF_DENY
log: Add filter flag to match greater than a log level
test: Add test for LOGFF_MIN
cmd: log: Use sub-commands for log
cmd: log: Split off log level parsing
lib: Add getopt
test: Add a test for getopt
cmd: log: Add commands to manipulate filters
test: py: Add a test for log filter-*
doc: Add log kerneldocs to documentation
doc: Update logging documentation

 MAINTAINERS               |   1 +
 cmd/Kconfig               |   1 +
 cmd/log.c                 | 277 +++++++++++++++++++++++++++++++++-----
 common/log.c              |  37 +++--
 doc/api/getopt.rst        |   8 ++
 doc/api/index.rst         |   1 +
 doc/develop/logging.rst   |  47 +++++--
 include/getopt.h          | 105 +++++++++++++++
 include/log.h             | 166 ++++++++++++++++-------
 lib/Kconfig               |   5 +
 lib/Makefile              |   1 +
 lib/getopt.c              | 125 +++++++++++++++++
 test/lib/Makefile         |   1 +
 test/lib/getopt.c         | 123 +++++++++++++++++
 test/log/log_test.c       |  94 ++++++++++++-
 test/py/tests/test_log.py |  65 ++++++++-
 16 files changed, 951 insertions(+), 106 deletions(-)
 create mode 100644 doc/api/getopt.rst
 create mode 100644 include/getopt.h
 create mode 100644 lib/getopt.c
 create mode 100644 test/lib/getopt.c
Signature · 1 line
2.28.0
#1 lore raw

PATCH 1/18 log: Fix missing negation of ENOMEM

Errors returned should be negative.

Fixes 45fac9fc18 ("log: Correct missing free() on error in log_add_filter()")

---

 common/log.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
common/log.c +1 -1
index 9a5f100da3..3f6f4bdc2a 100644
@@ -268,7 +268,7 @@ int log_add_filter(const char *drv_name, enum log_category_t cat_list[],
 	if (file_list) {
 		filt->file_list = strdup(file_list);
 		if (!filt->file_list) {
-			ret = ENOMEM;
+			ret = -ENOMEM;
 			goto err;
 		}
 	}
Signature · 1 line
2.28.0
#2 lore raw

PATCH 1/18 log: Fix missing negation of ENOMEM

Quoted 16 lines · Sean Anderson

Errors returned should be negative.

Fixes 45fac9fc18 ("log: Correct missing free() on error in log_add_filter()")

---

 common/log.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
common/log.c +0 -0
index 9a5f100da3..3f6f4bdc2a 100644

common/log.c int log_add_filter(const char *drv_name, enum log_category_t cat_list[], · line 274

Quoted 8 lines
common/log.c +1 -1
@@ -268,7 +268,7 @@ int log_add_filter(const char *drv_name, enum log_category_t cat_list[],
 	if (file_list) {
 		filt->file_list = strdup(file_list);
 		if (!filt->file_list) {
-			ret = ENOMEM;
+			ret = -ENOMEM;
 			goto err;
 		}
 	}

According to the function description for log_add_filter() in include/log.h errors should be returned as negative numbers.

#3 lore raw

PATCH 2/18 log: Fix incorrect documentation of log_filter.cat_list

In reply to Sean Anderson

This list is expected to be terminated by LOGC_END, not LOGC_NONE.

Fixes e9c8d49d54 ("log: Add an implementation of logging")

---

 include/log.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/log.h +1 -1
index 2859ce1f2e..670b9a665d 100644
@@ -349,7 +349,7 @@ enum log_filter_flags {
  *	new filter, and must be provided when removing a previously added
  *	filter.
  * @flags: Flags for this filter (LOGFF_...)
- * @cat_list: List of categories to allow (terminated by LOGC_none). If empty
+ * @cat_list: List of categories to allow (terminated by LOGC_END). If empty
  *	then all categories are permitted. Up to LOGF_MAX_CATEGORIES entries
  *	can be provided
  * @max_level: Maximum log level to allow
Signature · 1 line
2.28.0
#4 lore raw

PATCH 2/18 log: Fix incorrect documentation of log_filter.cat_list

Quoted 1 line · Sean Anderson

This list is expected to be terminated by LOGC_END, not LOGC_NONE.

%s/This list is expected to be/Logging category lists are/

Quoted 15 lines
Fixes e9c8d49d54 ("log: Add an implementation of logging")

---

 include/log.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/log.h +0 -0
index 2859ce1f2e..670b9a665d 100644

include/log.h enum log_filter_flags { · line 355

Quoted 8 lines
include/log.h +1 -1
@@ -349,7 +349,7 @@ enum log_filter_flags {
  *	new filter, and must be provided when removing a previously added
  *	filter.
  * @flags: Flags for this filter (LOGFF_...)
- * @cat_list: List of categories to allow (terminated by LOGC_none). If empty
+ * @cat_list: List of categories to allow (terminated by LOGC_END). If empty
  *	then all categories are permitted. Up to LOGF_MAX_CATEGORIES entries
  *	can be provided
  * @max_level: Maximum log level to allow

This matches function log_has_cat().

Except for the commit message:

#5 lore raw

PATCH 3/18 log: Add new category names to log_cat_name

In reply to Sean Anderson

Without every category between LOGC_NONE and LOGC_COUNT present in log_cat_name, log_get_cat_by_name will dereference NULL pointers if it doesn't find a name early enough.

Fixes c3aed5db59 ("sandbox: spi: Add more logging")
Fixes a5c13b68e7 ("sandbox: log: Add a category for sandbox")
Fixes 9f407d4ef0 ("Add core support for a bloblist to convey data from SPL")
Fixes cce61fc428 ("dm: devres: Convert to use logging")
Fixes 7ca2850cbc ("dm: core: Add basic ACPI support")

---

 common/log.c | 5 +++++
 1 file changed, 5 insertions(+)
common/log.c +5 -0
index 3f6f4bdc2a..09e39b0eca 100644
@@ -21,6 +21,11 @@ static const char *log_cat_name[LOGC_COUNT - LOGC_NONE] = {
 	"driver-model",
 	"device-tree",
 	"efi",
+	"alloc",
+	"sandbox",
+	"bloblist",
+	"devres",
+	"acpi",
 };
 
 static const char *log_level_name[LOGL_COUNT] = {
Signature · 1 line
2.28.0
#6 lore raw

PATCH 3/18 log: Add new category names to log_cat_name

common/log.c static const char *log_cat_name[LOGC_COUNT - LOGC_NONE] = {

Quoted 21 lines · Sean Anderson

Without every category between LOGC_NONE and LOGC_COUNT present in log_cat_name, log_get_cat_by_name will dereference NULL pointers if it doesn't find a name early enough.

Fixes c3aed5db59 ("sandbox: spi: Add more logging")
Fixes a5c13b68e7 ("sandbox: log: Add a category for sandbox")
Fixes 9f407d4ef0 ("Add core support for a bloblist to convey data from SPL")
Fixes cce61fc428 ("dm: devres: Convert to use logging")
Fixes 7ca2850cbc ("dm: core: Add basic ACPI support")

---

 common/log.c | 5 +++++
 1 file changed, 5 insertions(+)
common/log.c +0 -0
index 3f6f4bdc2a..09e39b0eca 100644
@@ -21,6 +21,11 @@ static const char *log_cat_name[LOGC_COUNT - LOGC_NONE] = {

Please, make the array log_cat_name[] unsized and add a compile time check like:

#if ARRAY_SIZE(log_cat_name) != LOGC_COUNT - LOGC_NONE #error missing logging category name
#endif

Best regards

Heinrich

Quoted 12 lines
common/log.c +5 -0
 	"driver-model",
 	"device-tree",
 	"efi",
+	"alloc",
+	"sandbox",
+	"bloblist",
+	"devres",
+	"acpi",
 };
 
 static const char *log_level_name[LOGL_COUNT] = {
#7 lore raw

PATCH 4/18 log: Use CONFIG_IS_ENABLED() for LOG_TEST

In reply to Sean Anderson

Checkpatch complains about using #ifdef for CONFIG variables.

---

 cmd/log.c           | 4 ++--
 test/log/log_test.c | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)
cmd/log.c +2 -2
index 6afe6ead25..16a6ef7539 100644
@@ -105,7 +105,7 @@ static int do_log_rec(struct cmd_tbl *cmdtp, int flag, int argc,
 
 static struct cmd_tbl log_sub[] = {
 	U_BOOT_CMD_MKENT(level, CONFIG_SYS_MAXARGS, 1, do_log_level, "", ""),
-#ifdef CONFIG_LOG_TEST
+#if CONFIG_IS_ENABLED(LOG_TEST)
 	U_BOOT_CMD_MKENT(test, 2, 1, do_log_test, "", ""),
 #endif
 	U_BOOT_CMD_MKENT(format, CONFIG_SYS_MAXARGS, 1, do_log_format, "", ""),
@@ -133,7 +133,7 @@ static int do_log(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
 #ifdef CONFIG_SYS_LONGHELP
 static char log_help_text[] =
 	"level - get/set log level\n"
-#ifdef CONFIG_LOG_TEST
+#if CONFIG_IS_ENABLED(LOG_TEST)
 	"log test - run log tests\n"
 #endif
 	"log format <fmt> - set log output format. <fmt> is a string where\n"
test/log/log_test.c +1 -1
index 4245372d65..787f680971 100644
@@ -201,7 +201,7 @@ static int log_test(int testnum)
 	return 0;
 }
 
-#ifdef CONFIG_LOG_TEST
+#if CONFIG_IS_ENABLED(LOG_TEST)
 int do_log_test(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
 {
 	int testnum = 0;
Signature · 1 line
2.28.0
#8 lore raw

PATCH 5/18 log: Expose log_device_find_by_name

In reply to Sean Anderson

This function is required by "cmd: log: Add commands to manipulate filters".

---

 common/log.c  | 2 +-
 include/log.h | 8 ++++++++
 2 files changed, 9 insertions(+), 1 deletion(-)
common/log.c +1 -1
index 09e39b0eca..bcb9e2634a 100644
@@ -93,7 +93,7 @@ enum log_level_t log_get_level_by_name(const char *name)
 	return LOGL_NONE;
 }
 
-static struct log_device *log_device_find_by_name(const char *drv_name)
+struct log_device *log_device_find_by_name(const char *drv_name)
 {
 	struct log_device *ldev;
 
include/log.h +8 -0
index 670b9a665d..3496382bda 100644
@@ -402,6 +402,14 @@ const char *log_get_level_name(enum log_level_t level);
  */
 enum log_level_t log_get_level_by_name(const char *name);
 
+/**
+ * log_device_find_by_name() - Look up a log device by its driver's name
+ *
+ * @drv_name: Name of the driver
+ * @return the log device, or NULL if not found
+ */
+struct log_device *log_device_find_by_name(const char *drv_name);
+
 /* Log format flags (bit numbers) for gd->log_fmt. See log_fmt_chars */
 enum log_fmt {
 	LOGF_CAT	= 0,
Signature · 1 line
2.28.0
#9 lore raw

PATCH 6/18 log: Add function to create a filter with flags

In reply to Sean Anderson

This function exposes a way to specify flags when creating a filter.

---

 common/log.c  |  6 ++++--
 include/log.h | 29 +++++++++++++++++++++++++++--
 2 files changed, 31 insertions(+), 4 deletions(-)
common/log.c +4 -2
index bcb9e2634a..b42c4fad60 100644
@@ -242,8 +242,9 @@ int _log(enum log_category_t cat, enum log_level_t level, const char *file,
 	return 0;
 }
 
-int log_add_filter(const char *drv_name, enum log_category_t cat_list[],
-		   enum log_level_t max_level, const char *file_list)
+int log_add_filter_flags(const char *drv_name, enum log_category_t cat_list[],
+			 enum log_level_t max_level, const char *file_list,
+			 int flags)
 {
 	struct log_filter *filt;
 	struct log_device *ldev;
@@ -257,6 +258,7 @@ int log_add_filter(const char *drv_name, enum log_category_t cat_list[],
 	if (!filt)
 		return -ENOMEM;
 
+	filt->flags = flags;
 	if (cat_list) {
 		filt->flags |= LOGFF_HAS_CAT;
 		for (i = 0; ; i++) {
include/log.h +27 -2
index 3496382bda..9116466fcf 100644
@@ -426,6 +426,25 @@ enum log_fmt {
 /* Handle the 'log test' command */
 int do_log_test(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
 
+/**
+ * log_add_filter_flags() - Add a new filter to a log device, specifying flags
+ *
+ * @drv_name: Driver name to add the filter to (since each driver only has a
+ *	single device)
+ * @flags: Flags for this filter (LOGFF_...)
+ * @cat_list: List of categories to allow (terminated by LOGC_none). If empty
+ *	then all categories are permitted. Up to LOGF_MAX_CATEGORIES entries
+ *	can be provided
+ * @max_level: Maximum log level to allow
+ * @file_list: List of files to allow, separated by comma. If NULL then all
+ *	files are permitted
+ * @return the sequence number of the new filter (>=0) if the filter was added,
+ *	or a -ve value on error
+ */
+int log_add_filter_flags(const char *drv_name, enum log_category_t cat_list[],
+			 enum log_level_t max_level, const char *file_list,
+			 int flags);
+
 /**
  * log_add_filter() - Add a new filter to a log device
  *
@@ -440,8 +459,14 @@ int do_log_test(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
  * @return the sequence number of the new filter (>=0) if the filter was added,
  *	or a -ve value on error
  */
-int log_add_filter(const char *drv_name, enum log_category_t cat_list[],
-		   enum log_level_t max_level, const char *file_list);
+static inline int log_add_filter(const char *drv_name,
+				 enum log_category_t cat_list[],
+				 enum log_level_t max_level,
+				 const char *file_list)
+{
+	return log_add_filter_flags(drv_name, cat_list, max_level, file_list,
+				    0);
+}
 
 /**
  * log_remove_filter() - Remove a filter from a log device
Signature · 1 line
2.28.0
#10 lore raw

PATCH 7/18 log: Add filter flag to deny on match

In reply to Sean Anderson

Without this flag, log filters can only explicitly accept messages. Allowing denial makes it easier to filter certain subsystems. Unlike allow-ing filters, deny-ing filters are added to the beginning of the filter list. This should do the Right Thing most of the time, but it's less-universal than allowing filters to be inserted anywhere. If this becomes a problem, then perhaps log_filter_add* should take a filter number to insert before/after.

---

 common/log.c  | 12 ++++++++++--
 include/log.h | 11 ++++++++++-
 2 files changed, 20 insertions(+), 3 deletions(-)
common/log.c +10 -2
index b42c4fad60..0b9071ba1c 100644
@@ -178,7 +178,11 @@ static bool log_passes_filters(struct log_device *ldev, struct log_rec *rec)
 		if (filt->file_list &&
 		    !log_has_file(filt->file_list, rec->file))
 			continue;
-		return true;
+
+		if (filt->flags & LOGFF_DENY)
+			return false;
+		else
+			return true;
 	}
 
 	return false;
@@ -280,7 +284,11 @@ int log_add_filter_flags(const char *drv_name, enum log_category_t cat_list[],
 		}
 	}
 	filt->filter_num = ldev->next_filter_num++;
-	list_add_tail(&filt->sibling_node, &ldev->filter_head);
+	/* Add deny filters to the beginning of the list */
+	if (flags & LOGFF_DENY)
+		list_add(&filt->sibling_node, &ldev->filter_head);
+	else
+		list_add_tail(&filt->sibling_node, &ldev->filter_head);
 
 	return filt->filter_num;
 
include/log.h +10 -1
index 9116466fcf..00647cafbd 100644
@@ -338,13 +338,22 @@ enum {
 	LOGF_MAX_CATEGORIES = 5,	/* maximum categories per filter */
 };
 
+/**
+ * enum log_filter_flags - Flags which modify a filter
+ */
 enum log_filter_flags {
-	LOGFF_HAS_CAT		= 1 << 0,	/* Filter has a category list */
+	/** @LOGFF_HAS_CAT: Filter has a category list */
+	LOGFF_HAS_CAT	= 1 << 0,
+	/** @LOGFF_DENY: Filter denies matching messages */
+	LOGFF_DENY	= 1 << 1,
 };
 
 /**
  * struct log_filter - criterial to filter out log messages
  *
+ * If a message matches all criteria, then it is allowed. If LOGFF_DENY is set,
+ * then it is denied instead.
+ *
  * @filter_num: Sequence number of this filter.  This is returned when adding a
  *	new filter, and must be provided when removing a previously added
  *	filter.
Signature · 1 line
2.28.0
#11 lore raw

PATCH 8/18 test: Add tests for LOGFF_DENY

In reply to Sean Anderson

This adds some tests for log filters which deny if they match.

---

 test/log/log_test.c       | 69 +++++++++++++++++++++++++++++++++++++++
 test/py/tests/test_log.py | 21 ++++++++++--
 2 files changed, 88 insertions(+), 2 deletions(-)
test/log/log_test.c +69 -0
index 787f680971..6a71d3db7d 100644
@@ -196,6 +196,75 @@ static int log_test(int testnum)
 		log_io("level %d\n", LOGL_DEBUG_IO);
 		break;
 	}
+	case 11: {
+		/* Check denying based on category */
+		int filt1, filt2;
+		enum log_category_t cat_list[] = {
+			log_uc_cat(UCLASS_SPI), LOGC_END
+		};
+
+		ret = log_add_filter("console", cat_list, LOGL_MAX, NULL);
+		if (ret < 0)
+			return ret;
+		filt1 = ret;
+		ret = log_add_filter_flags("console", cat_list, LOGL_MAX, NULL,
+					   LOGFF_DENY);
+		if (ret < 0)
+			return ret;
+		filt2 = ret;
+		log_run(UCLASS_SPI, "file");
+		ret = log_remove_filter("console", filt1);
+		if (ret < 0)
+			return ret;
+		ret = log_remove_filter("console", filt2);
+		if (ret < 0)
+			return ret;
+		break;
+	}
+	case 12: {
+		/* Check denying based on file */
+		int filt1, filt2;
+
+		ret = log_add_filter("console", NULL, LOGL_MAX, "file");
+		if (ret < 0)
+			return ret;
+		filt1 = ret;
+		ret = log_add_filter_flags("console", NULL, LOGL_MAX, "file",
+					   LOGFF_DENY);
+		if (ret < 0)
+			return ret;
+		filt2 = ret;
+		log_run(UCLASS_SPI, "file");
+		ret = log_remove_filter("console", filt1);
+		if (ret < 0)
+			return ret;
+		ret = log_remove_filter("console", filt2);
+		if (ret < 0)
+			return ret;
+		break;
+	}
+	case 13: {
+		/* Check denying based on level */
+		int filt1, filt2;
+
+		ret = log_add_filter("console", NULL, LOGL_INFO, NULL);
+		if (ret < 0)
+			return ret;
+		filt1 = ret;
+		ret = log_add_filter_flags("console", NULL, LOGL_WARNING, NULL,
+					   LOGFF_DENY);
+		if (ret < 0)
+			return ret;
+		filt2 = ret;
+		log_run(UCLASS_SPI, "file");
+		ret = log_remove_filter("console", filt1);
+		if (ret < 0)
+			return ret;
+		ret = log_remove_filter("console", filt2);
+		if (ret < 0)
+			return ret;
+		break;
+	}
 	}
 
 	return 0;
test/py/tests/test_log.py +19 -2
index ddc28f19ee..fabf3001cb 100644
@@ -15,7 +15,8 @@ LOGL_FIRST, LOGL_WARNING, LOGL_INFO = (0, 4, 6)
 @pytest.mark.buildconfigspec('cmd_log')
 def test_log(u_boot_console):
     """Test that U-Boot logging works correctly."""
-    def check_log_entries(lines, mask, max_level=LOGL_INFO):
+    def check_log_entries(lines, mask, max_level=LOGL_INFO,
+		          min_level=LOGL_FIRST):
         """Check that the expected log records appear in the output
 
         Args:
@@ -24,8 +25,9 @@ def test_log(u_boot_console):
                 bit 0: standard log line
                 bit 1: _log line
             max_level: maximum log level to expect in the output
+            min_level: minimum log level to expect in the output
         """
-        for i in range(max_level):
+        for i in range(min_level, max_level + 1):
             if mask & 1:
                 assert 'log_run() log %d' % i == next(lines)
             if mask & 3:
@@ -92,6 +94,18 @@ def test_log(u_boot_console):
         for i in range(7):
             assert 'log_test() level %d' % i == next(lines)
 
+    def test11():
+        lines = run_test(11)
+        assert next(lines, None) == None
+
+    def test12():
+        lines = run_test(12)
+        assert next(lines, None) == None
+
+    def test13():
+        lines = run_test(13)
+        check_log_entries(lines, 1, LOGL_INFO, LOGL_WARNING + 1)
+
     # TODO([email protected]): Consider structuring this as separate tests
     cons = u_boot_console
     test0()
@@ -105,6 +119,9 @@ def test_log(u_boot_console):
     test8()
     test9()
     test10()
+    test11()
+    test12()
+    test13()
 
 @pytest.mark.buildconfigspec('cmd_log')
 def test_log_format(u_boot_console):
Signature · 1 line
2.28.0
#12 lore raw

PATCH 9/18 log: Add filter flag to match greater than a log level

In reply to Sean Anderson

This is the compliment of the existing behavior to match only messages with a log level less than a threshold. This is primarily useful in conjunction with LOGFF_DENY.

---

 common/log.c  | 12 +++++++++---
 include/log.h | 10 ++++++----
 2 files changed, 15 insertions(+), 7 deletions(-)
common/log.c +9 -3
index 0b9071ba1c..a9b039456c 100644
@@ -170,11 +170,17 @@ static bool log_passes_filters(struct log_device *ldev, struct log_rec *rec)
 	}
 
 	list_for_each_entry(filt, &ldev->filter_head, sibling_node) {
-		if (rec->level > filt->max_level)
+		if (filt->flags & LOGFF_LEVEL_MIN) {
+			if (rec->level < filt->level)
+				continue;
+		} else if (rec->level > filt->level) {
 			continue;
+		}
+
 		if ((filt->flags & LOGFF_HAS_CAT) &&
 		    !log_has_cat(filt->cat_list, rec->cat))
 			continue;
+
 		if (filt->file_list &&
 		    !log_has_file(filt->file_list, rec->file))
 			continue;
@@ -247,7 +253,7 @@ int _log(enum log_category_t cat, enum log_level_t level, const char *file,
 }
 
 int log_add_filter_flags(const char *drv_name, enum log_category_t cat_list[],
-			 enum log_level_t max_level, const char *file_list,
+			 enum log_level_t level, const char *file_list,
 			 int flags)
 {
 	struct log_filter *filt;
@@ -275,7 +281,7 @@ int log_add_filter_flags(const char *drv_name, enum log_category_t cat_list[],
 				break;
 		}
 	}
-	filt->max_level = max_level;
+	filt->level = level;
 	if (file_list) {
 		filt->file_list = strdup(file_list);
 		if (!filt->file_list) {
include/log.h +6 -4
index 00647cafbd..4e5e3159f6 100644
@@ -346,6 +346,8 @@ enum log_filter_flags {
 	LOGFF_HAS_CAT	= 1 << 0,
 	/** @LOGFF_DENY: Filter denies matching messages */
 	LOGFF_DENY	= 1 << 1,
+	/** @LOGFF_LEVEL_MIN: Filter's level is a minimum, not a maximum */
+	LOGFF_LEVEL_MIN = 1 << 2,
 };
 
 /**
@@ -361,7 +363,7 @@ enum log_filter_flags {
  * @cat_list: List of categories to allow (terminated by LOGC_END). If empty
  *	then all categories are permitted. Up to LOGF_MAX_CATEGORIES entries
  *	can be provided
- * @max_level: Maximum log level to allow
+ * @level: Maximum (or minimum, if LOGFF_MIN_LEVEL) log level to allow
  * @file_list: List of files to allow, separated by comma. If NULL then all
  *	files are permitted
  * @sibling_node: Next filter in the list of filters for this log device
@@ -370,7 +372,7 @@ struct log_filter {
 	int filter_num;
 	int flags;
 	enum log_category_t cat_list[LOGF_MAX_CATEGORIES];
-	enum log_level_t max_level;
+	enum log_level_t level;
 	const char *file_list;
 	struct list_head sibling_node;
 };
@@ -444,14 +446,14 @@ int do_log_test(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
  * @cat_list: List of categories to allow (terminated by LOGC_none). If empty
  *	then all categories are permitted. Up to LOGF_MAX_CATEGORIES entries
  *	can be provided
- * @max_level: Maximum log level to allow
+ * @level: Maximum (or minimum, if LOGFF_LEVEL_MIN) log level to allow
  * @file_list: List of files to allow, separated by comma. If NULL then all
  *	files are permitted
  * @return the sequence number of the new filter (>=0) if the filter was added,
  *	or a -ve value on error
  */
 int log_add_filter_flags(const char *drv_name, enum log_category_t cat_list[],
-			 enum log_level_t max_level, const char *file_list,
+			 enum log_level_t level, const char *file_list,
 			 int flags);
 
 /**
Signature · 1 line
2.28.0
#13 lore raw

PATCH 10/18 test: Add test for LOGFF_MIN

In reply to Sean Anderson

This tests log filters matching on a minimum level.

---

 test/log/log_test.c       | 23 +++++++++++++++++++++++
 test/py/tests/test_log.py |  5 +++++
 2 files changed, 28 insertions(+)
test/log/log_test.c +23 -0
index 6a71d3db7d..da433e332b 100644
@@ -265,6 +265,29 @@ static int log_test(int testnum)
 			return ret;
 		break;
 	}
+	case 14: {
+		/* Check matching based on minimum level */
+		int filt1, filt2;
+
+		ret = log_add_filter_flags("console", NULL, LOGL_WARNING, NULL,
+					   LOGFF_LEVEL_MIN);
+		if (ret < 0)
+			return ret;
+		filt1 = ret;
+		ret = log_add_filter_flags("console", NULL, LOGL_INFO, NULL,
+					   LOGFF_DENY | LOGFF_LEVEL_MIN);
+		if (ret < 0)
+			return ret;
+		filt2 = ret;
+		log_run(UCLASS_SPI, "file");
+		ret = log_remove_filter("console", filt1);
+		if (ret < 0)
+			return ret;
+		ret = log_remove_filter("console", filt2);
+		if (ret < 0)
+			return ret;
+		break;
+	}
 	}
 
 	return 0;
test/py/tests/test_log.py +5 -0
index fabf3001cb..f191c84c02 100644
@@ -106,6 +106,10 @@ def test_log(u_boot_console):
         lines = run_test(13)
         check_log_entries(lines, 1, LOGL_INFO, LOGL_WARNING + 1)
 
+    def test14():
+        lines = run_test(14)
+        check_log_entries(lines, 1, LOGL_INFO - 1, LOGL_WARNING)
+
     # TODO([email protected]): Consider structuring this as separate tests
     cons = u_boot_console
     test0()
@@ -122,6 +126,7 @@ def test_log(u_boot_console):
     test11()
     test12()
     test13()
+    test14()
 
 @pytest.mark.buildconfigspec('cmd_log')
 def test_log_format(u_boot_console):
Signature · 1 line
2.28.0
#14 lore raw

PATCH 11/18 cmd: log: Use sub-commands for log

In reply to Sean Anderson

This reduces duplicate code, and makes adding new sub-commands easier.

---

 cmd/log.c | 37 +++++++------------------------------
 1 file changed, 7 insertions(+), 30 deletions(-)
cmd/log.c +7 -30
index 16a6ef7539..e55ace9e14 100644
@@ -103,33 +103,6 @@ static int do_log_rec(struct cmd_tbl *cmdtp, int flag, int argc,
 	return 0;
 }
 
-static struct cmd_tbl log_sub[] = {
-	U_BOOT_CMD_MKENT(level, CONFIG_SYS_MAXARGS, 1, do_log_level, "", ""),
-#if CONFIG_IS_ENABLED(LOG_TEST)
-	U_BOOT_CMD_MKENT(test, 2, 1, do_log_test, "", ""),
-#endif
-	U_BOOT_CMD_MKENT(format, CONFIG_SYS_MAXARGS, 1, do_log_format, "", ""),
-	U_BOOT_CMD_MKENT(rec, CONFIG_SYS_MAXARGS, 1, do_log_rec, "", ""),
-};
-
-static int do_log(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
-{
-	struct cmd_tbl *cp;
-
-	if (argc < 2)
-		return CMD_RET_USAGE;
-
-	/* drop initial "log" arg */
-	argc--;
-	argv++;
-
-	cp = find_cmd_tbl(argv[0], log_sub, ARRAY_SIZE(log_sub));
-	if (cp)
-		return cp->cmd(cmdtp, flag, argc, argv);
-
-	return CMD_RET_USAGE;
-}
-
 #ifdef CONFIG_SYS_LONGHELP
 static char log_help_text[] =
 	"level - get/set log level\n"
@@ -145,7 +118,11 @@ static char log_help_text[] =
 	;
 #endif
 
-U_BOOT_CMD(
-	log, CONFIG_SYS_MAXARGS, 1, do_log,
-	"log system", log_help_text
+U_BOOT_CMD_WITH_SUBCMDS(log, "log system", log_help_text,
+	U_BOOT_SUBCMD_MKENT(level, 2, 1, do_log_level),
+#if CONFIG_IS_ENABLED(LOG_TEST)
+	U_BOOT_SUBCMD_MKENT(test, 2, 1, do_log_test),
+#endif
+	U_BOOT_SUBCMD_MKENT(format, 2, 1, do_log_format),
+	U_BOOT_SUBCMD_MKENT(rec, 7, 1, do_log_rec),
 );
Signature · 1 line
2.28.0
#15 lore raw

PATCH 12/18 cmd: log: Split off log level parsing

In reply to Sean Anderson

Move parsing of log level into its own function so it can be re-used. This also adds support for using log level names instead of just the integer equivalent.

---

 cmd/log.c | 25 +++++++++++++++++++------
 1 file changed, 19 insertions(+), 6 deletions(-)
cmd/log.c +19 -6
index e55ace9e14..71bfdbbc0f 100644
@@ -11,23 +11,36 @@
 
 static char log_fmt_chars[LOGF_COUNT] = "clFLfm";
 
+static enum log_level_t parse_log_level(char *const arg)
+{
+	ulong level;
+
+	if (!strict_strtoul(arg, 10, &level)) {
+		if (level > _LOG_MAX_LEVEL) {
+			printf("Only log levels <= %d are supported\n",
+			       _LOG_MAX_LEVEL);
+			return LOGL_NONE;
+		}
+		return level;
+	}
+
+	return log_get_level_by_name(arg);
+}
+
 static int do_log_level(struct cmd_tbl *cmdtp, int flag, int argc,
 			char *const argv[])
 {
 	if (argc > 1) {
-		long log_level = simple_strtol(argv[1], NULL, 10);
+		enum log_level_t log_level = parse_log_level(argv[1]);
 
-		if (log_level < 0 || log_level > _LOG_MAX_LEVEL) {
-			printf("Only log levels <= %d are supported\n",
-			       _LOG_MAX_LEVEL);
+		if (log_level == LOGL_NONE)
 			return CMD_RET_FAILURE;
-		}
 		gd->default_log_level = log_level;
 	} else {
 		printf("Default log level: %d\n", gd->default_log_level);
 	}
 
-	return 0;
+	return CMD_RET_SUCCESS;
 }
 
 static int do_log_format(struct cmd_tbl *cmdtp, int flag, int argc,
Signature · 1 line
2.28.0
#16 lore raw

PATCH 13/18 lib: Add getopt

In reply to Sean Anderson

Some commands can get very unweildy if they have too many positional arguments. Adding options makes them easier to read, remember, and understand.

This implementation of getopt has been taken from barebox, which has had option support for quite a while. I have made a few modifications to their version, such as the removal of opterr in favor of a separate getopt_silent function. In addition, I have moved all global variables into struct getopt_context.

The getopt from barebox also re-orders the arguments passed to it so that non-options are placed last. This allows users to specify options anywhere. For example, ls -l foo/ -R would be re-ordered to ls -l -R foo/ as getopt parsed the options. However, this feature conflicts with the const argv in cmd_tbl->cmd. This was originally added in 54841ab50c ("Make sure that argv[] argument pointers are not modified."). The reason stated in that commit is that hush requires argv to stay unmodified. Has this situation changed? Barebox also uses hush, and does not have this problem. Perhaps we could use their fix?

I have assigned maintenance of getopt to Simon Glass, as it is currently only used by the log command. I would also be fine maintaining it.

---

 MAINTAINERS        |   1 +
 doc/api/getopt.rst |   8 +++
 doc/api/index.rst  |   1 +
 include/getopt.h   | 105 +++++++++++++++++++++++++++++++++++++
 lib/Kconfig        |   5 ++
 lib/Makefile       |   1 +
 lib/getopt.c       | 125 +++++++++++++++++++++++++++++++++++++++++++++
 7 files changed, 246 insertions(+)
 create mode 100644 doc/api/getopt.rst
 create mode 100644 include/getopt.h
 create mode 100644 lib/getopt.c
MAINTAINERS +1 -0
index 85babd1908..45ef4321d7 100644
@@ -747,6 +747,7 @@ T:	git https://gitlab.denx.de/u-boot/u-boot.git
 F:	common/log*
 F:	cmd/log.c
 F:	doc/develop/logging.rst
+F:	lib/getopt.c
 F:	test/log/
 F:	test/py/tests/test_log.py
 
doc/api/getopt.rst new file +8 -0
new file mode 100644
index 0000000000..773f79aeb6
@@ -0,0 +1,8 @@
+.. SPDX-License-Identifier: GPL-2.0+
+.. Copyright (C) 2020 Sean Anderson <[email protected]>
+
+Option Parsing
+==============
+
+.. kernel-doc:: include/getopt.h
+   :internal:
doc/api/index.rst +1 -0
index b7eb5725f2..6c849c50b6 100644
@@ -8,6 +8,7 @@ U-Boot API documentation
 
    dfu
    efi
+   getopt
    linker_lists
    rng
    serial
include/getopt.h new file +105 -0

105 lines not on this page · raw message

lib/Kconfig +5 -0
index 8efb154f73..a3346eee04 100644
@@ -542,6 +542,11 @@ config HEXDUMP
 	help
 	  This enables functions for printing dumps of binary data.
 
+config GETOPT
+	bool "Enable getopt"
+	help
+	  This enables functions for parsing command-line options.
+
 config OF_LIBFDT
 	bool "Enable the FDT library"
 	default y if OF_CONTROL
lib/Makefile +1 -0
index 0cd7bea282..7c7fb9aae7 100644
@@ -106,6 +106,7 @@ obj-y += string.o
 obj-y += tables_csum.o
 obj-y += time.o
 obj-y += hexdump.o
+obj-$(CONFIG_GETOPT) += getopt.o
 obj-$(CONFIG_TRACE) += trace.o
 obj-$(CONFIG_LIB_UUID) += uuid.o
 obj-$(CONFIG_LIB_RAND) += rand.o
lib/getopt.c new file +125 -0

125 lines not on this page · raw message

Signature · 1 line
2.28.0
#17 lore raw

PATCH 14/18 test: Add a test for getopt

In reply to Sean Anderson

A few of these tests were inspired by those in glibc. The syntax for invoking test_getopt is a bit funky, but it's necessary so that the CPP can parse the arguments correctly.

---

 test/lib/Makefile |   1 +
 test/lib/getopt.c | 123 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 124 insertions(+)
 create mode 100644 test/lib/getopt.c
test/lib/Makefile +1 -0
index 22236f8587..3140c2dad7 100644
@@ -13,3 +13,4 @@ obj-$(CONFIG_ERRNO_STR) += test_errno_str.o
 obj-$(CONFIG_UT_LIB_ASN1) += asn1.o
 obj-$(CONFIG_UT_LIB_RSA) += rsa.o
 obj-$(CONFIG_AES) += test_aes.o
+obj-$(CONFIG_GETOPT) += getopt.o
test/lib/getopt.c new file +123 -0

123 lines not on this page · raw message

Signature · 1 line
2.28.0
#18 lore raw

PATCH 15/18 cmd: log: Add commands to manipulate filters

In reply to Sean Anderson

This adds several commands to add, list, and remove log filters. Due to the complexity of adding a filter, log filter-list uses options instead of positional arguments.

These commands have been added as subcommands to log by using a dash to join the subcommand and subsubcommand. This is stylistic, and they could be converted to proper subsubcommands if it is wished.

---

 cmd/Kconfig |   1 +
 cmd/log.c   | 213 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 214 insertions(+)
cmd/Kconfig +1 -0
index 999b6cf239..c5cb112171 100644
@@ -2255,6 +2255,7 @@ config CMD_KGDB
 config CMD_LOG
 	bool "log - Generation, control and access to logging"
 	select LOG
+	select GETOPT
 	help
 	  This provides access to logging features. It allows the output of
 	  log data to be controlled to a limited extent (setting up the default
cmd/log.c +213 -0

237 lines not on this page · raw message

Signature · 1 line
2.28.0
#19 lore raw

PATCH 15/18 cmd: log: Add commands to manipulate filters

Quoted 236 lines · Sean Anderson

This adds several commands to add, list, and remove log filters. Due to the complexity of adding a filter, log filter-list uses options instead of positional arguments.

These commands have been added as subcommands to log by using a dash to join the subcommand and subsubcommand. This is stylistic, and they could be converted to proper subsubcommands if it is wished.

---

 cmd/Kconfig |   1 +
 cmd/log.c   | 213 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 214 insertions(+)
cmd/Kconfig +1 -0
index 999b6cf239..c5cb112171 100644
@@ -2255,6 +2255,7 @@ config CMD_KGDB
 config CMD_LOG
 	bool "log - Generation, control and access to logging"
 	select LOG
+	select GETOPT
 	help
 	  This provides access to logging features. It allows the output of
 	  log data to be controlled to a limited extent (setting up the default
cmd/log.c +190 -0
index 71bfdbbc0f..c9eaff7b94 100644
@@ -7,6 +7,7 @@
 #include <common.h>
 #include <command.h>
 #include <dm.h>
+#include <getopt.h>
 #include <log.h>
 
 static char log_fmt_chars[LOGF_COUNT] = "clFLfm";
@@ -43,6 +44,195 @@ static int do_log_level(struct cmd_tbl *cmdtp, int flag, int argc,
 	return CMD_RET_SUCCESS;
 }
 
+static int do_log_filter_list(struct cmd_tbl *cmdtp, int flag, int argc,
+			      char *const argv[])
+{
+	int opt;
+	const char *drv_name = "console";
+	struct getopt_state gs;
+	struct log_filter *filt;
+	struct log_device *ldev;
+
+	getopt_init_state(&gs);
+	while ((opt = getopt(&gs, argc, argv, "d:")) > 0) {
+		switch (opt) {
+		case 'd':
+			drv_name = gs.optarg;
+			break;
+		default:
+			return CMD_RET_USAGE;
+		}
+	}
+
+	if (gs.optind != argc)
+		return CMD_RET_USAGE;
+
+	ldev = log_device_find_by_name(drv_name);
+	if (!ldev) {
+		printf("Could not find log device for \"%s\"\n", drv_name);
+		return CMD_RET_FAILURE;
+	}
+
+	printf("num policy level           categories files\n");
+	list_for_each_entry(filt, &ldev->filter_head, sibling_node) {
+		printf("%3d %6.6s %s%-7.7s ", filt->filter_num,
+		       filt->flags & LOGFF_DENY ? "deny" : "allow",
+		       filt->flags & LOGFF_LEVEL_MIN ? ">=" : "<=",
+		       log_get_level_name(filt->level));
+
+		if (filt->flags & LOGFF_HAS_CAT) {
+			int i;
+
+			if (filt->cat_list[0] != LOGC_END)
+				printf("%16.16s %s\n",
+				       log_get_cat_name(filt->cat_list[0]),
+				       filt->file_list ? filt->file_list : "");
+
+			for (i = 1; i < LOGF_MAX_CATEGORIES &&
+				    filt->cat_list[i] != LOGC_END; i++)
+				printf("%20c %16.16s\n", ' ',
+				       log_get_cat_name(filt->cat_list[i]));
+		} else {
+			printf("%16c %s\n", ' ',
+			       filt->file_list ? filt->file_list : "");
+		}
+	}
+
+	return CMD_RET_SUCCESS;
+}
+
+static int do_log_filter_add(struct cmd_tbl *cmdtp, int flag, int argc,
+			     char *const argv[])
+{
+	bool level_set = false;
+	bool print_num = false;
+	bool type_set = false;
+	char *file_list = NULL;
+	const char *drv_name = "console";
+	int opt, err;
+	int cat_count = 0;
+	int flags = 0;
+	enum log_category_t cat_list[LOGF_MAX_CATEGORIES + 1];
+	enum log_level_t level = LOGL_MAX;
+	struct getopt_state gs;
+
+	getopt_init_state(&gs);
+	while ((opt = getopt(&gs, argc, argv, "Ac:d:Df:l:L:p")) > 0) {
+		switch (opt) {
+		case 'A':
+#define do_type() do { \
+			if (type_set) { \
+				printf("Allow or deny set twice\n"); \
+				return CMD_RET_USAGE; \
+			} \
+			type_set = true; \
+} while (0)
+			do_type();
+			break;
+		case 'c': {
+			enum log_category_t cat;
+
+			if (cat_count >= LOGF_MAX_CATEGORIES) {
+				printf("Too many categories\n");
+				return CMD_RET_FAILURE;
+			}
+
+			cat = log_get_cat_by_name(gs.optarg);
+			if (cat == LOGC_NONE) {
+				printf("Unknown category \"%s\"\n", gs.optarg);
+				return CMD_RET_FAILURE;
+			}
+
+			cat_list[cat_count++] = cat;
+			break;
+		}
+		case 'd':
+			drv_name = gs.optarg;
+			break;
+		case 'D':
+			do_type();
+			flags |= LOGFF_DENY;
+			break;
+		case 'f':
+			file_list = gs.optarg;
+			break;
+		case 'l':
+#define do_level() do { \
+			if (level_set) { \
+				printf("Log level set twice\n"); \
+				return CMD_RET_USAGE; \
+			} \
+			level = parse_log_level(gs.optarg); \
+			if (level == LOGL_NONE) \
+				return CMD_RET_FAILURE; \
+			level_set = true; \
+} while (0)
+			do_level();
+			break;
+		case 'L':
+			do_level();
+			flags |= LOGFF_LEVEL_MIN;
+			break;
+		case 'p':
+			print_num = true;
+			break;
+		default:
+			return CMD_RET_USAGE;
+		}
+	}
+
+	if (gs.optind != argc)
+		return CMD_RET_USAGE;
+
+	cat_list[cat_count] = LOGC_END;
+	err = log_add_filter_flags(drv_name, cat_count ? cat_list : NULL, level,
+				   file_list, flags);
+	if (err < 0) {
+		printf("Could not add filter (err = %d)\n", err);
+		return CMD_RET_FAILURE;
+	} else if (print_num) {
+		printf("%d\n", err);
+	}
+
+	return CMD_RET_SUCCESS;
+}
+
+static int do_log_filter_remove(struct cmd_tbl *cmdtp, int flag, int argc,
+				char *const argv[])
+{
+	int opt, err;
+	ulong filter_num;
+	const char *drv_name = "console";
+	struct getopt_state gs;
+
+	getopt_init_state(&gs);
+	while ((opt = getopt(&gs, argc, argv, "d:")) > 0) {
+		switch (opt) {
+		case 'd':
+			drv_name = gs.optarg;
+			break;
+		default:
+			return CMD_RET_USAGE;
+		}
+	}
+
+	if (gs.optind + 1 != argc)
+		return CMD_RET_USAGE;
+
+	if (strict_strtoul(argv[gs.optind], 10, &filter_num)) {
+		printf("Invalid filter number \"%s\"\n", argv[gs.optind]);
+		return CMD_RET_FAILURE;
+	}
+
+	err = log_remove_filter(drv_name, filter_num);
+	if (err) {
+		printf("Could not remove filter (err = %d)\n", err);
+		return CMD_RET_FAILURE;
+	}
+
+	return CMD_RET_SUCCESS;
+}
+
 static int do_log_format(struct cmd_tbl *cmdtp, int flag, int argc,
 			 char *const argv[])
 {

cmd/log.c static char log_help_text[] = · line 313

Quoted 2 lines
cmd/log.c +0 -0
@@ -122,6 +312,25 @@ static char log_help_text[] =
 #if CONFIG_IS_ENABLED(LOG_TEST)
 	"log test - run log tests\n"

This is probably what a typical user is least interested in. Please, move it to the end of the description.

cmd/log.c static char log_help_text[] =

Quoted 3 lines
cmd/log.c +2 -0
 #endif
+	"log filter-list [OPTIONS] - list all filters for a log driver\n"
+	"\t-d <driver> - Specify the log driver to add the filter to; defaults\n"> +	"\t              to console\n"

The text seems not to relate to filter-list.

Why don't we filter all log drivers by default? When would a user be interested to filter by driver?

How should I know which drivers and categories exist on my system?

cmd/log.c static char log_help_text[] = · line 327

Quoted 10 lines
cmd/log.c +10 -0
+	"log filter-add [OPTIONS] - add a new filter to a driver\n"
+	"\t-A - Allow messages matching this filter; mutually exclusive with -D\n"
+	"\t     This is the default.\n"
+	"\t-c <category> - Category to match; may be specified multiple times\n"
+	"\t-d <driver> - Specify the log driver to add the filter to; defaults\n"
+	"\t              to console\n"
+	"\t-D - Deny messages matching this filter; mutually exclusive with -A\n"
+	"\t-f <files_list> - A comma-separated list of files to match\n"
+	"\t-l <level> - Match log levels below this level; mutually exclusive\n"
+	"\t             with -L\n"

Is this below or below and equal?

cmd/log.c static char log_help_text[] = · line 329

Quoted 2 lines
cmd/log.c +2 -0
+	"\t-L <level> - Match log levels above this level; mutually exclusive\n"
+	"\t             with -l\n"

Is this above or above and equal?

cmd/log.c static char log_help_text[] = · line 330

Quoted 1 line
cmd/log.c +1 -0
+	"\t-p - Print the filter number on success\n"

Why do we need this parameter? Can't we simply always print the filter numbers or never?

cmd/log.c static char log_help_text[] = · line 333

Quoted 3 lines
cmd/log.c +3 -0
+	"log filter-remove [OPTIONS] <num> - Remove filter number <num>\n"
+	"\t-d <driver> - Specify the log driver to add the filter to; defaults\n"
+	"\t              to console\n"

This entry seems not to be related to removal.

How do I simply delete all filters?
How do I remove all filters for a category?

Best regards

Heinrich

Quoted 15 lines
cmd/log.c +4 -0
 	"log format <fmt> - set log output format. <fmt> is a string where\n"
 	"\teach letter indicates something that should be displayed:\n"
 	"\tc=category, l=level, F=file, L=line number, f=function, m=msg\n"
@@ -136,6 +345,10 @@ U_BOOT_CMD_WITH_SUBCMDS(log, "log system", log_help_text,
 #if CONFIG_IS_ENABLED(LOG_TEST)
 	U_BOOT_SUBCMD_MKENT(test, 2, 1, do_log_test),
 #endif
+	U_BOOT_SUBCMD_MKENT(filter-list, 3, 1, do_log_filter_list),
+	U_BOOT_SUBCMD_MKENT(filter-add, CONFIG_SYS_MAXARGS, 1,
+			    do_log_filter_add),
+	U_BOOT_SUBCMD_MKENT(filter-remove, 4, 1, do_log_filter_remove),
 	U_BOOT_SUBCMD_MKENT(format, 2, 1, do_log_format),
 	U_BOOT_SUBCMD_MKENT(rec, 7, 1, do_log_rec),
 );
#20 lore raw

PATCH 15/18 cmd: log: Add commands to manipulate filters

Quoted 234 lines · Heinrich Schuchardt
Quoted 233 lines · Sean Anderson

This adds several commands to add, list, and remove log filters. Due to the complexity of adding a filter, log filter-list uses options instead of positional arguments.

These commands have been added as subcommands to log by using a dash to join the subcommand and subsubcommand. This is stylistic, and they could be converted to proper subsubcommands if it is wished.

---

 cmd/Kconfig |   1 +
 cmd/log.c   | 213 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 214 insertions(+)
cmd/Kconfig +1 -0
index 999b6cf239..c5cb112171 100644
@@ -2255,6 +2255,7 @@ config CMD_KGDB
 config CMD_LOG
 	bool "log - Generation, control and access to logging"
 	select LOG
+	select GETOPT
 	help
 	  This provides access to logging features. It allows the output of
 	  log data to be controlled to a limited extent (setting up the default
cmd/log.c +190 -0
index 71bfdbbc0f..c9eaff7b94 100644
@@ -7,6 +7,7 @@
 #include <common.h>
 #include <command.h>
 #include <dm.h>
+#include <getopt.h>
 #include <log.h>
 
 static char log_fmt_chars[LOGF_COUNT] = "clFLfm";
@@ -43,6 +44,195 @@ static int do_log_level(struct cmd_tbl *cmdtp, int flag, int argc,
 	return CMD_RET_SUCCESS;
 }
 
+static int do_log_filter_list(struct cmd_tbl *cmdtp, int flag, int argc,
+			      char *const argv[])
+{
+	int opt;
+	const char *drv_name = "console";
+	struct getopt_state gs;
+	struct log_filter *filt;
+	struct log_device *ldev;
+
+	getopt_init_state(&gs);
+	while ((opt = getopt(&gs, argc, argv, "d:")) > 0) {
+		switch (opt) {
+		case 'd':
+			drv_name = gs.optarg;
+			break;
+		default:
+			return CMD_RET_USAGE;
+		}
+	}
+
+	if (gs.optind != argc)
+		return CMD_RET_USAGE;
+
+	ldev = log_device_find_by_name(drv_name);
+	if (!ldev) {
+		printf("Could not find log device for \"%s\"\n", drv_name);
+		return CMD_RET_FAILURE;
+	}
+
+	printf("num policy level           categories files\n");
+	list_for_each_entry(filt, &ldev->filter_head, sibling_node) {
+		printf("%3d %6.6s %s%-7.7s ", filt->filter_num,
+		       filt->flags & LOGFF_DENY ? "deny" : "allow",
+		       filt->flags & LOGFF_LEVEL_MIN ? ">=" : "<=",
+		       log_get_level_name(filt->level));
+
+		if (filt->flags & LOGFF_HAS_CAT) {
+			int i;
+
+			if (filt->cat_list[0] != LOGC_END)
+				printf("%16.16s %s\n",
+				       log_get_cat_name(filt->cat_list[0]),
+				       filt->file_list ? filt->file_list : "");
+
+			for (i = 1; i < LOGF_MAX_CATEGORIES &&
+				    filt->cat_list[i] != LOGC_END; i++)
+				printf("%20c %16.16s\n", ' ',
+				       log_get_cat_name(filt->cat_list[i]));
+		} else {
+			printf("%16c %s\n", ' ',
+			       filt->file_list ? filt->file_list : "");
+		}
+	}
+
+	return CMD_RET_SUCCESS;
+}
+
+static int do_log_filter_add(struct cmd_tbl *cmdtp, int flag, int argc,
+			     char *const argv[])
+{
+	bool level_set = false;
+	bool print_num = false;
+	bool type_set = false;
+	char *file_list = NULL;
+	const char *drv_name = "console";
+	int opt, err;
+	int cat_count = 0;
+	int flags = 0;
+	enum log_category_t cat_list[LOGF_MAX_CATEGORIES + 1];
+	enum log_level_t level = LOGL_MAX;
+	struct getopt_state gs;
+
+	getopt_init_state(&gs);
+	while ((opt = getopt(&gs, argc, argv, "Ac:d:Df:l:L:p")) > 0) {
+		switch (opt) {
+		case 'A':
+#define do_type() do { \
+			if (type_set) { \
+				printf("Allow or deny set twice\n"); \
+				return CMD_RET_USAGE; \
+			} \
+			type_set = true; \
+} while (0)
+			do_type();
+			break;
+		case 'c': {
+			enum log_category_t cat;
+
+			if (cat_count >= LOGF_MAX_CATEGORIES) {
+				printf("Too many categories\n");
+				return CMD_RET_FAILURE;
+			}
+
+			cat = log_get_cat_by_name(gs.optarg);
+			if (cat == LOGC_NONE) {
+				printf("Unknown category \"%s\"\n", gs.optarg);
+				return CMD_RET_FAILURE;
+			}
+
+			cat_list[cat_count++] = cat;
+			break;
+		}
+		case 'd':
+			drv_name = gs.optarg;
+			break;
+		case 'D':
+			do_type();
+			flags |= LOGFF_DENY;
+			break;
+		case 'f':
+			file_list = gs.optarg;
+			break;
+		case 'l':
+#define do_level() do { \
+			if (level_set) { \
+				printf("Log level set twice\n"); \
+				return CMD_RET_USAGE; \
+			} \
+			level = parse_log_level(gs.optarg); \
+			if (level == LOGL_NONE) \
+				return CMD_RET_FAILURE; \
+			level_set = true; \
+} while (0)
+			do_level();
+			break;
+		case 'L':
+			do_level();
+			flags |= LOGFF_LEVEL_MIN;
+			break;
+		case 'p':
+			print_num = true;
+			break;
+		default:
+			return CMD_RET_USAGE;
+		}
+	}
+
+	if (gs.optind != argc)
+		return CMD_RET_USAGE;
+
+	cat_list[cat_count] = LOGC_END;
+	err = log_add_filter_flags(drv_name, cat_count ? cat_list : NULL, level,
+				   file_list, flags);
+	if (err < 0) {
+		printf("Could not add filter (err = %d)\n", err);
+		return CMD_RET_FAILURE;
+	} else if (print_num) {
+		printf("%d\n", err);
+	}
+
+	return CMD_RET_SUCCESS;
+}
+
+static int do_log_filter_remove(struct cmd_tbl *cmdtp, int flag, int argc,
+				char *const argv[])
+{
+	int opt, err;
+	ulong filter_num;
+	const char *drv_name = "console";
+	struct getopt_state gs;
+
+	getopt_init_state(&gs);
+	while ((opt = getopt(&gs, argc, argv, "d:")) > 0) {
+		switch (opt) {
+		case 'd':
+			drv_name = gs.optarg;
+			break;
+		default:
+			return CMD_RET_USAGE;
+		}
+	}
+
+	if (gs.optind + 1 != argc)
+		return CMD_RET_USAGE;
+
+	if (strict_strtoul(argv[gs.optind], 10, &filter_num)) {
+		printf("Invalid filter number \"%s\"\n", argv[gs.optind]);
+		return CMD_RET_FAILURE;
+	}
+
+	err = log_remove_filter(drv_name, filter_num);
+	if (err) {
+		printf("Could not remove filter (err = %d)\n", err);
+		return CMD_RET_FAILURE;
+	}
+
+	return CMD_RET_SUCCESS;
+}
+
 static int do_log_format(struct cmd_tbl *cmdtp, int flag, int argc,
Quoted 8 lines

cmd/log.c static char log_help_text[] = · line 313

Quoted 5 lines
cmd/log.c +0 -0
 			 char *const argv[])
 {
@@ -122,6 +312,25 @@ static char log_help_text[] =
 #if CONFIG_IS_ENABLED(LOG_TEST)
 	"log test - run log tests\n"

This is probably what a typical user is least interested in. Please, move it to the end of the description.

Ok.

Quoted 6 lines

cmd/log.c static char log_help_text[] =

Quoted 4 lines
cmd/log.c +2 -0
 #endif
+	"log filter-list [OPTIONS] - list all filters for a log driver\n"
+	"\t-d <driver> - Specify the log driver to add the filter to; defaults\n"> +	"\t              to console\n"

The text seems not to relate to filter-list.

Oh, whoops, the text should be something like

Quoted 1 line

cmd/log.c static char log_help_text[] = · line 317

Quoted 1 line
cmd/log.c +1 -0
+	"\t-d <driver> - Specify the log driver to list filters from; defaults to console\n"

I originally wrote that line for the log filter-add command.

Quoted 2 lines

Why don't we filter all log drivers by default? When would a user be interested to filter by driver?

Good question. This part of the API seems to have no real users at the moment. One use could be in restricting the log level over ethernet. For example, you could have the default log level on the system be INFO, but deny messages above WARNING from the syslog driver. It might be easier to do this via a driver-specific log level instead. However, I think we should leave per-driver filters in until it becomes clear that they are not necessary.

Quoted 15 lines

How should I know which drivers and categories exist on my system?

cmd/log.c static char log_help_text[] = · line 327

Quoted 10 lines
cmd/log.c +10 -0
+	"log filter-add [OPTIONS] - add a new filter to a driver\n"
+	"\t-A - Allow messages matching this filter; mutually exclusive with -D\n"
+	"\t     This is the default.\n"
+	"\t-c <category> - Category to match; may be specified multiple times\n"
+	"\t-d <driver> - Specify the log driver to add the filter to; defaults\n"
+	"\t              to console\n"
+	"\t-D - Deny messages matching this filter; mutually exclusive with -A\n"
+	"\t-f <files_list> - A comma-separated list of files to match\n"
+	"\t-l <level> - Match log levels below this level; mutually exclusive\n"
+	"\t             with -L\n"

Is this below or below and equal?

Below and equal. Perhaps something like

Quoted 1 line

cmd/log.c static char log_help_text[] = · line 328

Quoted 1 line
cmd/log.c +1 -0
+	"\t-l <level> - Match log levels less than or equal to <level>; mutually exclusive\n"

would be better.

Quoted 5 lines

cmd/log.c static char log_help_text[] = · line 329

Quoted 3 lines
cmd/log.c +2 -0
+	"\t-L <level> - Match log levels above this level; mutually exclusive\n"
+	"\t             with -l\n"

Is this above or above and equal?

This is above and equal.

There is a bit of asymmetry here, as both of the comparison operators include the listed level. I'm not sure what the most ergonomic choice here is.

Quoted 5 lines

cmd/log.c static char log_help_text[] = · line 330

Quoted 2 lines
cmd/log.c +1 -0
+	"\t-p - Print the filter number on success\n"

Why do we need this parameter? Can't we simply always print the filter numbers or never?

Commands should be silent if they succeed, especially simple ones like this. For a human, it is always possible to find the filter number of a given filter by examining the output of log filter-list. However, it is useful for machines (e.g. for testing) if there is an option to get the filter number when it is produced.

Quoted 5 lines

cmd/log.c static char log_help_text[] = · line 333

Quoted 3 lines
cmd/log.c +3 -0
+	"log filter-remove [OPTIONS] <num> - Remove filter number <num>\n"
+	"\t-d <driver> - Specify the log driver to add the filter to; defaults\n"
+	"\t              to console\n"

This entry seems not to be related to removal.

Yes, same mistake as above.

Quoted 2 lines

How do I simply delete all filters?

You must delete them one-by-one. This could be added.

Quoted 1 line

How do I remove all filters for a category?

You must delete them one-by-one. This could also be added.

However, I would like to keep the number of features to a minimum until it becomes clear how these filters will be used. I think a remove-all version would probably be useful, but I'm less convinced about removing filters for a category. Should there also be a command to remove all filters for a given file?

--Sean

#21 lore raw

PATCH 15/18 cmd: log: Add commands to manipulate filters

In reply to Sean Anderson

Hi Sean,

Quoted 9 lines · Sean Anderson

This adds several commands to add, list, and remove log filters. Due to the complexity of adding a filter, log filter-list uses options instead of positional arguments.

These commands have been added as subcommands to log by using a dash to join the subcommand and subsubcommand. This is stylistic, and they could be converted to proper subsubcommands if it is wished.

Quoted 6 lines

---

 cmd/Kconfig |   1 +
 cmd/log.c   | 213 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 214 insertions(+)

I see you have a lot of comments already, It's great to see this feature. But please do add some sort of test for the command itself.

You don't need to test that the filters work; that sort of thing belongs in the log tests. But the command test should check that various commands are interpreted correctly, result in some action (you can check the log state directly if you provide a helper function to export data structures) and output things correctly (for commands that produce output).

Regards,
Simon

#22 lore raw

PATCH 15/18 cmd: log: Add commands to manipulate filters

Quoted 22 lines · Simon Glass

Hi Sean,

Quoted 9 lines · Sean Anderson

This adds several commands to add, list, and remove log filters. Due to the complexity of adding a filter, log filter-list uses options instead of positional arguments.

These commands have been added as subcommands to log by using a dash to join the subcommand and subsubcommand. This is stylistic, and they could be converted to proper subsubcommands if it is wished.

Quoted 6 lines

---

 cmd/Kconfig |   1 +
 cmd/log.c   | 213 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 214 insertions(+)

I see you have a lot of comments already, It's great to see this feature. But please do add some sort of test for the command itself.

Quoted 6 lines

You don't need to test that the filters work; that sort of thing belongs in the log tests. But the command test should check that various commands are interpreted correctly, result in some action (you can check the log state directly if you provide a helper function to export data structures) and output things correctly (for commands that produce output).

Is the test in the next patch inadequate?

--Sean

#23 lore raw

PATCH 16/18 test: py: Add a test for log filter-*

In reply to Sean Anderson

This exercises a few success and failure modes of the log filter-* commands.

---

 test/py/tests/test_log.py | 39 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)
test/py/tests/test_log.py +39 -0
index f191c84c02..87c0c21575 100644
@@ -154,3 +154,42 @@ def test_log_format(u_boot_console):
         run_with_format('FLfm', 'file.c:123-func() msg')
         run_with_format('lm', 'NOTICE. msg')
         run_with_format('m', 'msg')
+
+ at pytest.mark.buildconfigspec('cmd_log')
+def test_log_filter(u_boot_console):
+    c = u_boot_console
+
+    # Test invalid options
+    assert 'rc:1' in c.run_command('log filter-add -AD; echo rc:$?')
+    assert 'rc:1' in c.run_command('log filter-add -l1 -L1; echo rc:$?')
+    assert 'rc:1' in c.run_command('log filter-add -l1 -L1; echo rc:$?')
+    assert 'rc:1' in c.run_command('log filter-add -lfoo; echo rc:$?')
+    assert 'rc:1' in c.run_command('log filter-add -cfoo; echo rc:$?')
+    assert 'rc:1' in c.run_command('log filter-add -ccore -ccore -ccore -ccore -ccore -ccore; echo rc:$?')
+
+    # Test filters
+    assert c.run_command('log format m') == ''
+    filt1 = c.run_command('log filter-add -p')
+    assert c.run_command('log rec mmc warning file 123 func msg') == 'msg'
+    filt2 = c.run_command('log filter-add -p -DL warning -cmmc -cspi -ffile')
+    assert c.run_command('log rec mmc warning file 123 func msg') == ''
+    assert c.run_command('log rec spi warning file 123 func msg') == ''
+    assert c.run_command('log rec arch warning file 123 func msg') == 'msg'
+    assert c.run_command('log rec mmc warning file2 123 func msg') == 'msg'
+    assert c.run_command('log rec mmc err file 123 func msg') == 'msg'
+
+    # "Test" filter-list; ignore the header
+    resp = c.run_command('log filter-list')[:-1]
+    assert filt1 in resp
+    assert filt2 in resp
+    assert 'allow' in resp
+    assert 'deny' in resp
+    assert '>=WARNING' in resp
+    assert '<=IO' in resp
+    assert 'mmc' in resp
+    assert 'spi' in resp
+    assert 'file' in resp
+
+    # Ensure removal works
+    assert c.run_command('log filter-remove ' + filt2) == ''
+    assert c.run_command('log rec mmc warning file 123 func msg') == 'msg'
Signature · 1 line
2.28.0
#24 lore raw

PATCH 16/18 test: py: Add a test for log filter-*

Hi Sean,

Quoted 10 lines · Sean Anderson

This exercises a few success and failure modes of the log filter-* commands.

---

 test/py/tests/test_log.py | 39 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)

Can this test be written in C?

Regards,
Simon

#25 lore raw

PATCH 16/18 test: py: Add a test for log filter-*

Quoted 7 lines · Simon Glass

Hi Sean,

Quoted 4 lines · Sean Anderson

This exercises a few success and failure modes of the log filter-* commands.

Quoted 8 lines
Quoted 6 lines

---

 test/py/tests/test_log.py | 39 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 39 insertions(+)

Can this test be written in C?

Probably.

--Sean

#26 lore raw

PATCH 17/18 doc: Add log kerneldocs to documentation

In reply to Sean Anderson

The functions in log.h are already mostly documented, so add them to the generated documentation. This commit adds no new documentation, so several enum members are left undocumented.

---

 doc/develop/logging.rst |   5 ++
 include/log.h           | 118 +++++++++++++++++++++++++---------------
 2 files changed, 79 insertions(+), 44 deletions(-)
doc/develop/logging.rst +5 -0
index 7ce8482ab6..6a22062073 100644
@@ -288,3 +288,8 @@ information
 Add a command to add new log records and delete existing records.
 
 Provide additional log() functions - e.g. logc() to specify the category
+
+Logging API
+-----------
+.. kernel-doc:: include/log.h
+   :internal:
include/log.h +74 -44

223 lines not on this page · raw message

Signature · 1 line
2.28.0
#27 lore raw

PATCH 18/18 doc: Update logging documentation

In reply to Sean Anderson

This updates logging documentation with some examples of the new commands added in the previous commits. It also removes some items from the to-do list which have been implemented.

---

 doc/develop/logging.rst | 42 +++++++++++++++++++++++++++++------------
 1 file changed, 30 insertions(+), 12 deletions(-)
doc/develop/logging.rst +30 -12
index 6a22062073..8b6237f829 100644
@@ -174,18 +174,45 @@ fields are present, but not the field order.
 Filters
 -------
 
-Filters are attached to log drivers to control what those drivers emit. Only
-records that pass through the filter make it to the driver.
+Filters are attached to log drivers to control what those drivers emit. FIlters
+can either allow or deny a log message when they match it. Only records which
+are allowed by a filter make it to the driver.
 
 Filters can be based on several criteria:
 
-* maximum log level
+* minimum or maximum log level
 * in a set of categories
 * in a set of files
 
 If no filters are attached to a driver then a default filter is used, which
 limits output to records with a level less than CONFIG_MAX_LOG_LEVEL.
 
+Adding Filters
+~~~~~~~~~~~~~~
+
+To add new filters at runtime, use the 'log filter-add' command. For example, to
+filter messages from the SPI subsystem, run::
+
+    log filter-add -D -c spi
+
+You will also need to allow everything else to emit messages (because the
+default filter no longer applies)::
+
+    log filter-add -A -l debug
+
+To view active filters, use the 'log filter-list' command. Some example output
+is::
+
+    => log filter-list
+    num policy level           categories files
+      0   deny <=IO                   spi
+      1  allow <=DEBUG
+
+Note that filters are processed in-order from top to bottom, not in the order of
+their filter number. Filters are added to the top of the list if they deny when
+they match, and to the bottom if they allow when they match. For more
+information, consult the usage of the 'log' command, by running 'help log'.
+
 
 Logging statements
 ------------------
@@ -259,8 +286,6 @@ Add a way to browse log records
 
 Add a way to record log records for browsing using an external tool
 
-Add commands to add and remove filters
-
 Add commands to add and remove log devices
 
 Allow sharing of printf format strings in log records to reduce storage size
@@ -268,10 +293,6 @@ for large numbers of log records
 
 Add a command-line option to sandbox to set the default logging level
 
-Convert core driver model code to use logging
-
-Convert uclasses to use logging with the correct category
-
 Consider making log() calls emit an automatic newline, perhaps with a logn()
 function to avoid that
 
@@ -282,9 +303,6 @@ number dropped due to them being generated before the log system was ready.
 
 Add a printf() format string pragma so that log statements are checked properly
 
-Enhance the log console driver to show level / category / file / line
-information
-
 Add a command to add new log records and delete existing records.
 
 Provide additional log() functions - e.g. logc() to specify the category
Signature · 1 line
2.28.0
#28 lore raw

PATCH 18/18 doc: Update logging documentation

Quoted 29 lines · Sean Anderson

This updates logging documentation with some examples of the new commands added in the previous commits. It also removes some items from the to-do list which have been implemented.

---

 doc/develop/logging.rst | 42 +++++++++++++++++++++++++++++------------
 1 file changed, 30 insertions(+), 12 deletions(-)
doc/develop/logging.rst +4 -3
index 6a22062073..8b6237f829 100644
@@ -174,18 +174,45 @@ fields are present, but not the field order.
 Filters
 -------
 
-Filters are attached to log drivers to control what those drivers emit. Only
-records that pass through the filter make it to the driver.
+Filters are attached to log drivers to control what those drivers emit. FIlters
+can either allow or deny a log message when they match it. Only records which
+are allowed by a filter make it to the driver.
 
 Filters can be based on several criteria:
 
-* maximum log level
+* minimum or maximum log level
 * in a set of categories

doc/develop/logging.rst fields are present, but not the field order. · line 194

Quoted 10 lines
doc/develop/logging.rst +5 -0
 * in a set of files
 
 If no filters are attached to a driver then a default filter is used, which
 limits output to records with a level less than CONFIG_MAX_LOG_LEVEL.
 
+Adding Filters
+~~~~~~~~~~~~~~
+
+To add new filters at runtime, use the 'log filter-add' command. For example, to
+filter messages from the SPI subsystem, run::

Does "filter" mean I see only messages from SPI or does this mean I see no messages from SPI? If you mean suppress, please, use that word or something similar.

doc/develop/logging.rst fields are present, but not the field order. · line 201

Quoted 7 lines
doc/develop/logging.rst +7 -0
+
+    log filter-add -D -c spi
+
+You will also need to allow everything else to emit messages (because the
+default filter no longer applies)::
+
+    log filter-add -A -l debug

Thank you for starting the documenation.

Unfortunately after reading the lines above I have no clue what the meaning of -D -A -c -l are.

Users will need a complete description of the log command with all parameters.

Best regards

Heinrich

Quoted 48 lines
doc/develop/logging.rst +14 -9
+
+To view active filters, use the 'log filter-list' command. Some example output
+is::
+
+    => log filter-list
+    num policy level           categories files
+      0   deny <=IO                   spi
+      1  allow <=DEBUG
+
+Note that filters are processed in-order from top to bottom, not in the order of
+their filter number. Filters are added to the top of the list if they deny when
+they match, and to the bottom if they allow when they match. For more
+information, consult the usage of the 'log' command, by running 'help log'.
+
 
 Logging statements
 ------------------
@@ -259,8 +286,6 @@ Add a way to browse log records
 
 Add a way to record log records for browsing using an external tool
 
-Add commands to add and remove filters
-
 Add commands to add and remove log devices
 
 Allow sharing of printf format strings in log records to reduce storage size
@@ -268,10 +293,6 @@ for large numbers of log records
 
 Add a command-line option to sandbox to set the default logging level
 
-Convert core driver model code to use logging
-
-Convert uclasses to use logging with the correct category
-
 Consider making log() calls emit an automatic newline, perhaps with a logn()
 function to avoid that
 
@@ -282,9 +303,6 @@ number dropped due to them being generated before the log system was ready.
 
 Add a printf() format string pragma so that log statements are checked properly
 
-Enhance the log console driver to show level / category / file / line
-information
-
 Add a command to add new log records and delete existing records.
 
 Provide additional log() functions - e.g. logc() to specify the category
#29 lore raw

PATCH 18/18 doc: Update logging documentation

Quoted 35 lines · Heinrich Schuchardt
Quoted 34 lines · Sean Anderson

This updates logging documentation with some examples of the new commands added in the previous commits. It also removes some items from the to-do list which have been implemented.

---

 doc/develop/logging.rst | 42 +++++++++++++++++++++++++++++------------
 1 file changed, 30 insertions(+), 12 deletions(-)
doc/develop/logging.rst +4 -3
index 6a22062073..8b6237f829 100644
@@ -174,18 +174,45 @@ fields are present, but not the field order.
 Filters
 -------
 
-Filters are attached to log drivers to control what those drivers emit. Only
-records that pass through the filter make it to the driver.
+Filters are attached to log drivers to control what those drivers emit. FIlters
+can either allow or deny a log message when they match it. Only records which
+are allowed by a filter make it to the driver.
 
 Filters can be based on several criteria:
 
-* maximum log level
+* minimum or maximum log level
 * in a set of categories
 * in a set of files
 
 If no filters are attached to a driver then a default filter is used, which
 limits output to records with a level less than CONFIG_MAX_LOG_LEVEL.
 
Quoted 9 lines

doc/develop/logging.rst fields are present, but not the field order. · line 194

Quoted 5 lines
doc/develop/logging.rst +5 -0
+Adding Filters
+~~~~~~~~~~~~~~
+
+To add new filters at runtime, use the 'log filter-add' command. For example, to
+filter messages from the SPI subsystem, run::

Does "filter" mean I see only messages from SPI or does this mean I see no messages from SPI? If you mean suppress, please, use that word or something similar.

Ok.

Quoted 11 lines

doc/develop/logging.rst fields are present, but not the field order. · line 201

Quoted 8 lines
doc/develop/logging.rst +7 -0
+
+    log filter-add -D -c spi
+
+You will also need to allow everything else to emit messages (because the
+default filter no longer applies)::
+
+    log filter-add -A -l debug

Thank you for starting the documenation.

Quoted 2 lines

Unfortunately after reading the lines above I have no clue what the meaning of -D -A -c -l are.

These options are currently documented in the help text for log. Should they be added here? I am concerned that duplicating documentation will result in drift over time.

Quoted 8 lines

Users will need a complete description of the log command with all parameters.

Best regards

Heinrich

Quoted 13 lines

doc/develop/logging.rst fields are present, but not the field order. · line 214

Quoted 13 lines
doc/develop/logging.rst +13 -0
+
+To view active filters, use the 'log filter-list' command. Some example output
+is::
+
+    => log filter-list
+    num policy level           categories files
+      0   deny <=IO                   spi
+      1  allow <=DEBUG
+
+Note that filters are processed in-order from top to bottom, not in the order of
+their filter number. Filters are added to the top of the list if they deny when
+they match, and to the bottom if they allow when they match. For more
+information, consult the usage of the 'log' command, by running 'help log'.

Here is my suggestion of where to find a complete list of options.

--Sean

Quoted 35 lines
Quoted 34 lines
doc/develop/logging.rst +0 -9
 Logging statements
 ------------------
@@ -259,8 +286,6 @@ Add a way to browse log records
 
 Add a way to record log records for browsing using an external tool
 
-Add commands to add and remove filters
-
 Add commands to add and remove log devices
 
 Allow sharing of printf format strings in log records to reduce storage size
@@ -268,10 +293,6 @@ for large numbers of log records
 
 Add a command-line option to sandbox to set the default logging level
 
-Convert core driver model code to use logging
-
-Convert uclasses to use logging with the correct category
-
 Consider making log() calls emit an automatic newline, perhaps with a logn()
 function to avoid that
 
@@ -282,9 +303,6 @@ number dropped due to them being generated before the log system was ready.
 
 Add a printf() format string pragma so that log statements are checked properly
 
-Enhance the log console driver to show level / category / file / line
-information
-
 Add a command to add new log records and delete existing records.
 
 Provide additional log() functions - e.g. logc() to specify the category
#30 lore raw

PATCH 18/18 doc: Update logging documentation

Quoted 59 lines · Sean Anderson
Quoted 35 lines · Heinrich Schuchardt
Quoted 34 lines · Sean Anderson

This updates logging documentation with some examples of the new commands added in the previous commits. It also removes some items from the to-do list which have been implemented.

---

 doc/develop/logging.rst | 42 +++++++++++++++++++++++++++++------------
 1 file changed, 30 insertions(+), 12 deletions(-)
doc/develop/logging.rst +4 -3
index 6a22062073..8b6237f829 100644
@@ -174,18 +174,45 @@ fields are present, but not the field order.
 Filters
 -------
 
-Filters are attached to log drivers to control what those drivers emit. Only
-records that pass through the filter make it to the driver.
+Filters are attached to log drivers to control what those drivers emit. FIlters
+can either allow or deny a log message when they match it. Only records which
+are allowed by a filter make it to the driver.
 
 Filters can be based on several criteria:
 
-* maximum log level
+* minimum or maximum log level
 * in a set of categories
 * in a set of files
 
 If no filters are attached to a driver then a default filter is used, which
 limits output to records with a level less than CONFIG_MAX_LOG_LEVEL.
 
Quoted 9 lines

doc/develop/logging.rst fields are present, but not the field order. · line 194

Quoted 5 lines
doc/develop/logging.rst +5 -0
+Adding Filters
+~~~~~~~~~~~~~~
+
+To add new filters at runtime, use the 'log filter-add' command. For example, to
+filter messages from the SPI subsystem, run::

Does "filter" mean I see only messages from SPI or does this mean I see no messages from SPI? If you mean suppress, please, use that word or something similar.

Ok.

Quoted 11 lines

doc/develop/logging.rst fields are present, but not the field order. · line 201

Quoted 8 lines
doc/develop/logging.rst +7 -0
+
+    log filter-add -D -c spi
+
+You will also need to allow everything else to emit messages (because the
+default filter no longer applies)::
+
+    log filter-add -A -l debug

Thank you for starting the documenation.

Quoted 6 lines
Quoted 2 lines

Unfortunately after reading the lines above I have no clue what the meaning of -D -A -c -l are.

These options are currently documented in the help text for log. Should they be added here? I am concerned that duplicating documentation will result in drift over time.

If I look at https://u-boot.readthedocs.io/en/latest/, it is currently far from what users needs.

The chapter "Develop U-Boot" seems to be the wrong place for describing commands.

We need a chapter in our online documentation documenting all commands in enough detail for a rookie to understand how to use them.

Something like like https://man7.org/linux/man-pages/man1/git-commit.1.html.

Best regards

Heinrich

Quoted 63 lines
Quoted 9 lines

Users will need a complete description of the log command with all parameters.

Best regards

Heinrich

Quoted 13 lines

doc/develop/logging.rst fields are present, but not the field order. · line 214

Quoted 13 lines
doc/develop/logging.rst +13 -0
+
+To view active filters, use the 'log filter-list' command. Some example output
+is::
+
+    => log filter-list
+    num policy level           categories files
+      0   deny <=IO                   spi
+      1  allow <=DEBUG
+
+Note that filters are processed in-order from top to bottom, not in the order of
+their filter number. Filters are added to the top of the list if they deny when
+they match, and to the bottom if they allow when they match. For more
+information, consult the usage of the 'log' command, by running 'help log'.

Here is my suggestion of where to find a complete list of options.

--Sean

Quoted 35 lines
Quoted 34 lines
doc/develop/logging.rst +0 -9
 Logging statements
 ------------------
@@ -259,8 +286,6 @@ Add a way to browse log records
 
 Add a way to record log records for browsing using an external tool
 
-Add commands to add and remove filters
-
 Add commands to add and remove log devices
 
 Allow sharing of printf format strings in log records to reduce storage size
@@ -268,10 +293,6 @@ for large numbers of log records
 
 Add a command-line option to sandbox to set the default logging level
 
-Convert core driver model code to use logging
-
-Convert uclasses to use logging with the correct category
-
 Consider making log() calls emit an automatic newline, perhaps with a logn()
 function to avoid that
 
@@ -282,9 +303,6 @@ number dropped due to them being generated before the log system was ready.
 
 Add a printf() format string pragma so that log statements are checked properly
 
-Enhance the log console driver to show level / category / file / line
-information
-
 Add a command to add new log records and delete existing records.
 
 Provide additional log() functions - e.g. logc() to specify the category
#31 lore raw

PATCH 18/18 doc: Update logging documentation

Quoted 67 lines · Heinrich Schuchardt
Quoted 59 lines · Sean Anderson
Quoted 35 lines · Heinrich Schuchardt
Quoted 34 lines · Sean Anderson

This updates logging documentation with some examples of the new commands added in the previous commits. It also removes some items from the to-do list which have been implemented.

---

 doc/develop/logging.rst | 42 +++++++++++++++++++++++++++++------------
 1 file changed, 30 insertions(+), 12 deletions(-)
doc/develop/logging.rst +4 -3
index 6a22062073..8b6237f829 100644
@@ -174,18 +174,45 @@ fields are present, but not the field order.
 Filters
 -------
 
-Filters are attached to log drivers to control what those drivers emit. Only
-records that pass through the filter make it to the driver.
+Filters are attached to log drivers to control what those drivers emit. FIlters
+can either allow or deny a log message when they match it. Only records which
+are allowed by a filter make it to the driver.
 
 Filters can be based on several criteria:
 
-* maximum log level
+* minimum or maximum log level
 * in a set of categories
 * in a set of files
 
 If no filters are attached to a driver then a default filter is used, which
 limits output to records with a level less than CONFIG_MAX_LOG_LEVEL.
 
Quoted 9 lines

doc/develop/logging.rst fields are present, but not the field order. · line 194

Quoted 5 lines
doc/develop/logging.rst +5 -0
+Adding Filters
+~~~~~~~~~~~~~~
+
+To add new filters at runtime, use the 'log filter-add' command. For example, to
+filter messages from the SPI subsystem, run::

Does "filter" mean I see only messages from SPI or does this mean I see no messages from SPI? If you mean suppress, please, use that word or something similar.

Ok.

Quoted 11 lines

doc/develop/logging.rst fields are present, but not the field order. · line 201

Quoted 8 lines
doc/develop/logging.rst +7 -0
+
+    log filter-add -D -c spi
+
+You will also need to allow everything else to emit messages (because the
+default filter no longer applies)::
+
+    log filter-add -A -l debug

Thank you for starting the documenation.

Quoted 6 lines
Quoted 2 lines

Unfortunately after reading the lines above I have no clue what the meaning of -D -A -c -l are.

These options are currently documented in the help text for log. Should they be added here? I am concerned that duplicating documentation will result in drift over time.

Quoted 5 lines

If I look at https://u-boot.readthedocs.io/en/latest/, it is currently far from what users needs.

The chapter "Develop U-Boot" seems to be the wrong place for describing commands.

Well, I think that this really depends on who the log subsystem is intended for. From what I can tell, it was originally intended as an aid to developers. Some users are starting to make use of it (e.g. via syslog), but most users probably have CONFIG_LOG disabled to reduce boot times. The commands I have added are also developer-oriented. While users certainly could use the new filter flags, they would probably want to create their filters very early (e.g. as soon as the log subsystem comes up). Going forward, this may not be the best place for log documentation.

Quoted 3 lines

We need a chapter in our online documentation documenting all commands in enough detail for a rookie to understand how to use them.

Yes, I agree. If possible, they should be generated from U-Boot source files, so that the documentation can live close to the commands (and so existing help strings could be used). I'm not really sure what the best way to go about that is. Perhaps kerneldoc could be (ab)used? In any case, such a change is outside the scope of this patch.

--Sean