Skip to contentSkip to conversation

log: Add commands for manipulating filters

PATCH New Sean Anderson 2 participants 45 messages Oct 27, 2020 – Oct 30, 2020 Library code

lore
Share & tools
lore
#0 lore raw

v4 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.

Passing CI:
https://dev.azure.com/seanga2/u-boot/_build/results?buildId=58&view=results

Changes in v4:
- Add a space in between the <= operator and the log level
- Add some more examples to docs
- Rebase onto master
- Remove "log: Add new category names to log_cat_name" since it has been superseded.
- Wrap UCLASS_ literals with log_uc_cat when used as log categories

Changes in v3:
- Convert log_test from python to C
- Document assumption that erroneous results from log_get_cat_name begin with '<'
- Fix heading level of Filters section
- Remove a few more already-implemented features from the TODO list
- Update copyright for log_filter.c

Changes in v2:
- Add % before constants in kerneldocs
- Add a few informational commands
- Add const qualifier to log_*_name
- Add option to remove all filters to filter-remove
- Clarify filter-* help text
- Clarify wording of filter documentation
- Converted log filter-* tests to C from python
- Document log_level_t and log_category_t members
- Expand documentation of getopt() to include examples
- Expose log_has_cat and log_has_file for filter tests
- Include enum definitions instead of re-documenting them
- Print an error message if the log level is invalid.
- Remove opt prefix from getopt_state members
- Reorganize log documentation; related sections should now be more proximate

Sean Anderson (22):
log: Fix missing negation of ENOMEM
log: Fix incorrect documentation of log_filter.cat_list
log: Add additional const qualifier to arrays
log: Use CONFIG_IS_ENABLED() for LOG_TEST
log: Expose some helper functions
log: Add function to create a filter with flags
log: Add filter flag to deny on match
test: log: Convert log_test from python to C
test: log: Give tests names instead of numbers
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
cmd: log: Add commands to list categories and drivers
cmd: log: Make "log level" print all log levels
lib: Add getopt
test: Add a test for getopt
cmd: log: Add commands to manipulate filters
test: Add a test for log filter-*
doc: Add log kerneldocs to documentation
doc: Update logging documentation

 MAINTAINERS               |   1 +
 cmd/Kconfig               |   1 +
 cmd/log.c                 | 354 ++++++++++++++++++++++---
 common/log.c              |  58 ++---
 doc/api/getopt.rst        |   8 +
 doc/api/index.rst         |   1 +
 doc/develop/logging.rst   | 224 ++++++++--------
 include/getopt.h          | 130 +++++++++
 include/log.h             | 212 +++++++++++----
 include/test/log.h        |   3 +
 lib/Kconfig               |   5 +
 lib/Makefile              |   1 +
 lib/getopt.c              | 125 +++++++++
 test/lib/Makefile         |   1 +
 test/lib/getopt.c         | 123 +++++++++
 test/log/Makefile         |   1 +
 test/log/log_filter.c     | 108 ++++++++
 test/log/log_test.c       | 536 +++++++++++++++++++++++++-------------
 test/log/syslog_test.h    |   2 -
 test/py/tests/test_log.py | 104 --------
 20 files changed, 1470 insertions(+), 528 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
 create mode 100644 test/log/log_filter.c
Signature · 1 line
2.28.0
#1 lore raw

PATCH v4 1/22 log: Fix missing negation of ENOMEM

Errors returned should be negative.

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

---

(no changes since v1)

 common/log.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
common/log.c +1 -1
index b7a6ebe298..6aab189a46 100644
@@ -278,7 +278,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 v4 1/22 log: Fix missing negation of ENOMEM

Quoted 6 lines · Sean Anderson

Errors returned should be negative.

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

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20201030/746f92f9/attachment.sig>
#3 lore raw

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

In reply to Sean Anderson

Logging category lists are terminated by LOGC_END, not LOGC_NONE.

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

---

(no changes since v2)

Changes in v2:
- Also fix misdocumentation of for log_add_filter()

 include/log.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
include/log.h +2 -2
index 6de5e611c7..5fde401bd9 100644
@@ -370,7 +370,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
@@ -448,7 +448,7 @@ int do_log_test(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]);
  *
  * @drv_name: Driver name to add the filter to (since each driver only has a
  *	single device)
- * @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 v4 2/22 log: Fix incorrect documentation of log_filter.cat_list

Quoted 6 lines · Sean Anderson

Logging category lists are terminated by LOGC_END, not LOGC_NONE.

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

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20201030/cf524906/attachment.sig>
#5 lore raw

PATCH v4 3/22 log: Add additional const qualifier to arrays

In reply to Sean Anderson

Both these arrays and their members are const. Fixes checkpatch complaint.

---

(no changes since v2)

Changes in v2:
- New

 common/log.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
common/log.c +2 -2
index 6aab189a46..8d5d71ccb4 100644
@@ -13,7 +13,7 @@
 
 DECLARE_GLOBAL_DATA_PTR;
 
-static const char *log_cat_name[LOGC_COUNT - LOGC_NONE] = {
+static const char *const log_cat_name[LOGC_COUNT - LOGC_NONE] = {
 	"none",
 	"arch",
 	"board",
@@ -28,7 +28,7 @@ static const char *log_cat_name[LOGC_COUNT - LOGC_NONE] = {
 	"acpi",
 };
 
-static const char *log_level_name[LOGL_COUNT] = {
+static const char *const log_level_name[LOGL_COUNT] = {
 	"EMERG",
 	"ALERT",
 	"CRIT",
Signature · 1 line
2.28.0
#6 lore raw

PATCH v4 3/22 log: Add additional const qualifier to arrays

Quoted 4 lines · Sean Anderson

Both these arrays and their members are const. Fixes checkpatch complaint.

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20201030/91c53e98/attachment.sig>
#7 lore raw

PATCH v4 4/22 log: Use CONFIG_IS_ENABLED() for LOG_TEST

In reply to Sean Anderson

Checkpatch complains about using #ifdef for CONFIG variables.

---

(no changes since v1)

 cmd/log.c | 4 ++--
 1 file changed, 2 insertions(+), 2 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"
Signature · 1 line
2.28.0
#8 lore raw

PATCH v4 4/22 log: Use CONFIG_IS_ENABLED() for LOG_TEST

Quoted 4 lines · Sean Anderson

Checkpatch complains about using #ifdef for CONFIG variables.

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20201030/c8f90521/attachment.sig>
#9 lore raw

PATCH v4 5/22 log: Expose some helper functions

In reply to Sean Anderson

These functions are required by "cmd: log: Add commands to manipulate filters" and "test: Add a test for log filter-*".

---

(no changes since v2)

Changes in v2:
- Expose log_has_cat and log_has_file for filter tests

 common/log.c  | 23 +++--------------------
 include/log.h | 31 +++++++++++++++++++++++++++++++
 2 files changed, 34 insertions(+), 20 deletions(-)
common/log.c +3 -20
index 8d5d71ccb4..74ae7f0ea8 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;
 
@@ -105,15 +105,7 @@ static struct log_device *log_device_find_by_name(const char *drv_name)
 	return NULL;
 }
 
-/**
- * log_has_cat() - check if a log category exists within a list
- *
- * @cat_list: List of categories to check, at most LOGF_MAX_CATEGORIES entries
- *	long, terminated by LC_END if fewer
- * @cat: Category to search for
- * @return true if @cat is in @cat_list, else false
- */
-static bool log_has_cat(enum log_category_t cat_list[], enum log_category_t cat)
+bool log_has_cat(enum log_category_t cat_list[], enum log_category_t cat)
 {
 	int i;
 
@@ -125,16 +117,7 @@ static bool log_has_cat(enum log_category_t cat_list[], enum log_category_t cat)
 	return false;
 }
 
-/**
- * log_has_file() - check if a file is with a list
- *
- * @file_list: List of files to check, separated by comma
- * @file: File to check for. This string is matched against the end of each
- *	file in the list, i.e. ignoring any preceding path. The list is
- *	intended to consist of relative pathnames, e.g. common/main.c,cmd/log.c
- * @return true if @file is in @file_list, else false
- */
-static bool log_has_file(const char *file_list, const char *file)
+bool log_has_file(const char *file_list, const char *file)
 {
 	int file_len = strlen(file);
 	const char *s, *p;
include/log.h +31 -0
index 5fde401bd9..06eb0003fe 100644
@@ -427,6 +427,37 @@ 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_has_cat() - check if a log category exists within a list
+ *
+ * @cat_list: List of categories to check, at most %LOGF_MAX_CATEGORIES entries
+ *	long, terminated by %LC_END if fewer
+ * @cat: Category to search for
+ *
+ * Return: ``true`` if @cat is in @cat_list, else ``false``
+ */
+bool log_has_cat(enum log_category_t cat_list[], enum log_category_t cat);
+
+/**
+ * log_has_file() - check if a file is with a list
+ *
+ * @file_list: List of files to check, separated by comma
+ * @file: File to check for. This string is matched against the end of each
+ *	file in the list, i.e. ignoring any preceding path. The list is
+ *	intended to consist of relative pathnames, e.g. common/main.c,cmd/log.c
+ *
+ * Return: ``true`` if @file is in @file_list, else ``false``
+ */
+bool log_has_file(const char *file_list, const char *file);
+
 /* 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
#10 lore raw

PATCH v4 5/22 log: Expose some helper functions

Quoted 5 lines · Sean Anderson

These functions are required by "cmd: log: Add commands to manipulate filters" and "test: Add a test for log filter-*".

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20201030/ba607a53/attachment.sig>
#11 lore raw

PATCH v4 6/22 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.

---

(no changes since v1)

 common/log.c  |  6 ++++--
 include/log.h | 29 +++++++++++++++++++++++++++--
 2 files changed, 31 insertions(+), 4 deletions(-)
common/log.c +4 -2
index 74ae7f0ea8..a14b227231 100644
@@ -230,8 +230,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;
@@ -245,6 +246,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 06eb0003fe..4e59a2cf74 100644
@@ -474,6 +474,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_END). 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
  *
@@ -488,8 +507,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
#12 lore raw

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

Quoted 4 lines · Sean Anderson

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

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20201030/1260ff66/attachment.sig>
#13 lore raw

PATCH v4 7/22 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.

---

(no changes since v1)

 common/log.c  | 12 ++++++++++--
 include/log.h | 11 ++++++++++-
 2 files changed, 20 insertions(+), 3 deletions(-)
common/log.c +10 -2
index a14b227231..6299c8b2d4 100644
@@ -164,7 +164,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;
@@ -268,7 +272,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 4e59a2cf74..018180e788 100644
@@ -359,13 +359,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
#14 lore raw

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

Quoted 10 lines · 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.

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20201030/5a6b23c0/attachment.sig>
#15 lore raw

PATCH v4 8/22 test: log: Convert log_test from python to C

In reply to Sean Anderson

When rebasing this series I had to renumber all my log tests because someone made another log test in the meantime. This involved updaing a number in several places (C and python), and it wasn't checked by the compiler. So I though "how hard could it be to just rewrite in C?" And though it wasn't hard, it *was* tedious. Tests are numbered the same as before to allow for easier review.

A note that if a test fails, everything after it will probably also fail. This is because that test won't clean up its filters. There's no easy way to do the cleanup, except perhaps removing all filters in a wrapper function.

---

(no changes since v3)

Changes in v3:
- New

 cmd/log.c                 |   6 -
 include/test/log.h        |   2 +
 test/log/log_test.c       | 446 ++++++++++++++++++++++----------------
 test/log/syslog_test.h    |   2 -
 test/py/tests/test_log.py | 104 ---------
 5 files changed, 260 insertions(+), 300 deletions(-)
cmd/log.c +0 -6
index 16a6ef7539..d20bfdf744 100644
@@ -105,9 +105,6 @@ 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, "", ""),
-#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, "", ""),
 };
@@ -133,9 +130,6 @@ 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"
-#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"
 	"\teach letter indicates something that should be displayed:\n"
 	"\tc=category, l=level, F=file, L=line number, f=function, m=msg\n"
include/test/log.h +2 -0
index c661cde75a..772e197806 100644
@@ -10,6 +10,8 @@
 
 #include <test/test.h>
 
+#define LOGF_TEST (BIT(LOGF_FUNC) | BIT(LOGF_MSG))
+
 /* Declare a new logging test */
 #define LOG_TEST(_name) UNIT_TEST(_name, 0, log_test)
 
test/log/log_test.c +258 -188

473 lines not on this page · raw message

test/log/syslog_test.h +0 -2
index 1310257bfe..bfaa6daef8 100644
@@ -8,8 +8,6 @@
 #ifndef __SYSLOG_TEST_H
 #define __SYSLOG_TEST_H
 
-#define LOGF_TEST (BIT(LOGF_FUNC) | BIT(LOGF_MSG))
-
 /**
  * struct sb_log_env - private data for sandbox ethernet driver
  *
test/py/tests/test_log.py +0 -104

110 lines not on this page · raw message

Signature · 1 line
2.28.0
#16 lore raw

PATCH v4 8/22 test: log: Convert log_test from python to C

Quoted 7 lines · Sean Anderson

When rebasing this series I had to renumber all my log tests because someone made another log test in the meantime. This involved updaing a number in several places (C and python), and it wasn't checked by the compiler. So I though "how hard could it be to just rewrite in C?" And though it wasn't hard, it *was* tedious. Tests are numbered the same as before to allow for easier review.

Quoted 6 lines

A note that if a test fails, everything after it will probably also fail. This is because that test won't clean up its filters. There's no easy way to do the cleanup, except perhaps removing all filters in a wrapper function.

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20201030/78729935/attachment.sig>
#17 lore raw

PATCH v4 9/22 test: log: Give tests names instead of numbers

In reply to Sean Anderson

Now that the log test command is no more, we can give the log tests proper names.

---

(no changes since v3)

Changes in v3:
- New

 test/log/log_test.c | 48 ++++++++++++++++++++++-----------------------
 1 file changed, 24 insertions(+), 24 deletions(-)
test/log/log_test.c +24 -24
index c5ca6dcb7a..4ac378fdad 100644
@@ -72,7 +72,7 @@ static int do_check_log_entries(struct unit_test_state *uts, int flags, int min,
 #define check_log_entries_none() check_log_entries_flags(0)
 
 /* Check a category filter using the first category */
-int log_test_00(struct unit_test_state *uts)
+int log_test_cat_allow(struct unit_test_state *uts)
 {
 	enum log_category_t cat_list[] = {
 		log_uc_cat(UCLASS_MMC), log_uc_cat(UCLASS_SPI),
@@ -94,10 +94,10 @@ int log_test_00(struct unit_test_state *uts)
 	ut_assertok(log_remove_filter("console", filt));
 	return 0;
 }
-LOG_TEST_FLAGS(log_test_00, UT_TESTF_CONSOLE_REC);
+LOG_TEST_FLAGS(log_test_cat_allow, UT_TESTF_CONSOLE_REC);
 
 /* Check a category filter that should block log entries */
-int log_test_02(struct unit_test_state *uts)
+int log_test_cat_deny_implicit(struct unit_test_state *uts)
 {
 	enum log_category_t cat_list[] = {
 		log_uc_cat(UCLASS_MMC),  LOGC_NONE, LOGC_END
@@ -114,10 +114,10 @@ int log_test_02(struct unit_test_state *uts)
 	ut_assertok(log_remove_filter("console", filt));
 	return 0;
 }
-LOG_TEST_FLAGS(log_test_02, UT_TESTF_CONSOLE_REC);
+LOG_TEST_FLAGS(log_test_cat_deny_implicit, UT_TESTF_CONSOLE_REC);
 
 /* Check passing and failing file filters */
-int log_test_03(struct unit_test_state *uts)
+int log_test_file(struct unit_test_state *uts)
 {
 	int filt;
 
@@ -135,10 +135,10 @@ int log_test_03(struct unit_test_state *uts)
 	ut_assertok(log_remove_filter("console", filt));
 	return 0;
 }
-LOG_TEST_FLAGS(log_test_03, UT_TESTF_CONSOLE_REC);
+LOG_TEST_FLAGS(log_test_file, UT_TESTF_CONSOLE_REC);
 
 /* Check a passing file filter (second in list) */
-int log_test_05(struct unit_test_state *uts)
+int log_test_file_second(struct unit_test_state *uts)
 {
 	int filt;
 
@@ -152,10 +152,10 @@ int log_test_05(struct unit_test_state *uts)
 	ut_assertok(log_remove_filter("console", filt));
 	return 0;
 }
-LOG_TEST_FLAGS(log_test_05, UT_TESTF_CONSOLE_REC);
+LOG_TEST_FLAGS(log_test_file_second, UT_TESTF_CONSOLE_REC);
 
 /* Check a passing file filter (middle of list) */
-int log_test_06(struct unit_test_state *uts)
+int log_test_file_mid(struct unit_test_state *uts)
 {
 	int filt;
 
@@ -170,10 +170,10 @@ int log_test_06(struct unit_test_state *uts)
 	ut_assertok(log_remove_filter("console", filt));
 	return 0;
 }
-LOG_TEST_FLAGS(log_test_06, UT_TESTF_CONSOLE_REC);
+LOG_TEST_FLAGS(log_test_file_mid, UT_TESTF_CONSOLE_REC);
 
 /* Check a log level filter */
-int log_test_07(struct unit_test_state *uts)
+int log_test_level(struct unit_test_state *uts)
 {
 	int filt;
 
@@ -188,10 +188,10 @@ int log_test_07(struct unit_test_state *uts)
 	ut_assertok(log_remove_filter("console", filt));
 	return 0;
 }
-LOG_TEST_FLAGS(log_test_07, UT_TESTF_CONSOLE_REC);
+LOG_TEST_FLAGS(log_test_level, UT_TESTF_CONSOLE_REC);
 
 /* Check two filters, one of which passes everything */
-int log_test_08(struct unit_test_state *uts)
+int log_test_double(struct unit_test_state *uts)
 {
 	int filt1, filt2;
 
@@ -208,10 +208,10 @@ int log_test_08(struct unit_test_state *uts)
 	ut_assertok(log_remove_filter("console", filt2));
 	return 0;
 }
-LOG_TEST_FLAGS(log_test_08, UT_TESTF_CONSOLE_REC);
+LOG_TEST_FLAGS(log_test_double, UT_TESTF_CONSOLE_REC);
 
 /* Check three filters, which together pass everything */
-int log_test_09(struct unit_test_state *uts)
+int log_test_triple(struct unit_test_state *uts)
 {
 	int filt1, filt2, filt3;
 
@@ -231,9 +231,9 @@ int log_test_09(struct unit_test_state *uts)
 	ut_assertok(log_remove_filter("console", filt3));
 	return 0;
 }
-LOG_TEST_FLAGS(log_test_09, UT_TESTF_CONSOLE_REC);
+LOG_TEST_FLAGS(log_test_triple, UT_TESTF_CONSOLE_REC);
 
-int do_log_test_10(struct unit_test_state *uts)
+int do_log_test_helpers(struct unit_test_state *uts)
 {
 	int i;
 
@@ -255,18 +255,18 @@ int do_log_test_10(struct unit_test_state *uts)
 	return 0;
 }
 
-int log_test_10(struct unit_test_state *uts)
+int log_test_helpers(struct unit_test_state *uts)
 {
 	int ret;
 
 	gd->log_fmt = LOGF_TEST;
-	ret = do_log_test_10(uts);
+	ret = do_log_test_helpers(uts);
 	gd->log_fmt = log_get_default_format();
 	return ret;
 }
-LOG_TEST_FLAGS(log_test_10, UT_TESTF_CONSOLE_REC);
+LOG_TEST_FLAGS(log_test_helpers, UT_TESTF_CONSOLE_REC);
 
-int do_log_test_11(struct unit_test_state *uts)
+int do_log_test_disable(struct unit_test_state *uts)
 {
 	ut_assertok(console_record_reset_enable());
 	log_err("default\n");
@@ -282,13 +282,13 @@ int do_log_test_11(struct unit_test_state *uts)
 	return 0;
 }
 
-int log_test_11(struct unit_test_state *uts)
+int log_test_disable(struct unit_test_state *uts)
 {
 	int ret;
 
 	gd->log_fmt = LOGF_TEST;
-	ret = do_log_test_10(uts);
+	ret = do_log_test_disable(uts);
 	gd->log_fmt = log_get_default_format();
 	return ret;
 }
-LOG_TEST_FLAGS(log_test_11, UT_TESTF_CONSOLE_REC);
+LOG_TEST_FLAGS(log_test_disable, UT_TESTF_CONSOLE_REC);
Signature · 1 line
2.28.0
#18 lore raw

PATCH v4 9/22 test: log: Give tests names instead of numbers

Quoted 4 lines · Sean Anderson

Now that the log test command is no more, we can give the log tests proper names.

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20201030/bd225739/attachment.sig>
#19 lore raw

PATCH v4 10/22 test: Add tests for LOGFF_DENY

In reply to Sean Anderson

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

---

(no changes since v3)

Changes in v3:
- Modified to be completely written in C

 test/log/log_test.c | 67 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 67 insertions(+)
test/log/log_test.c +67 -0
index 4ac378fdad..e4ab999a7d 100644
@@ -292,3 +292,70 @@ int log_test_disable(struct unit_test_state *uts)
 	return ret;
 }
 LOG_TEST_FLAGS(log_test_disable, UT_TESTF_CONSOLE_REC);
+
+/* Check denying based on category */
+int log_test_cat_deny(struct unit_test_state *uts)
+{
+	int filt1, filt2;
+	enum log_category_t cat_list[] = {
+		log_uc_cat(UCLASS_SPI), LOGC_END
+	};
+
+	filt1 = log_add_filter("console", cat_list, LOGL_MAX, NULL);
+	ut_assert(filt1 >= 0);
+	filt2 = log_add_filter_flags("console", cat_list, LOGL_MAX, NULL,
+				     LOGFF_DENY);
+	ut_assert(filt2 >= 0);
+
+	ut_assertok(console_record_reset_enable());
+	log_run_cat(UCLASS_SPI);
+	check_log_entries_none();
+
+	ut_assertok(log_remove_filter("console", filt1));
+	ut_assertok(log_remove_filter("console", filt2));
+	return 0;
+}
+LOG_TEST_FLAGS(log_test_cat_deny, UT_TESTF_CONSOLE_REC);
+
+/* Check denying based on file */
+int log_test_file_deny(struct unit_test_state *uts)
+{
+	int filt1, filt2;
+
+	filt1 = log_add_filter("console", NULL, LOGL_MAX, "file");
+	ut_assert(filt1 >= 0);
+	filt2 = log_add_filter_flags("console", NULL, LOGL_MAX, "file",
+				     LOGFF_DENY);
+	ut_assert(filt2 >= 0);
+
+	ut_assertok(console_record_reset_enable());
+	log_run_file("file");
+	check_log_entries_none();
+
+	ut_assertok(log_remove_filter("console", filt1));
+	ut_assertok(log_remove_filter("console", filt2));
+	return 0;
+}
+LOG_TEST_FLAGS(log_test_file_deny, UT_TESTF_CONSOLE_REC);
+
+/* Check denying based on level */
+int log_test_level_deny(struct unit_test_state *uts)
+{
+	int filt1, filt2;
+
+	filt1 = log_add_filter("console", NULL, LOGL_INFO, NULL);
+	ut_assert(filt1 >= 0);
+	filt2 = log_add_filter_flags("console", NULL, LOGL_WARNING, NULL,
+				     LOGFF_DENY);
+	ut_assert(filt2 >= 0);
+
+	ut_assertok(console_record_reset_enable());
+	log_run();
+	check_log_entries_flags_levels(EXPECT_LOG | EXPECT_DIRECT,
+				       LOGL_WARNING + 1, _LOG_MAX_LEVEL);
+
+	ut_assertok(log_remove_filter("console", filt1));
+	ut_assertok(log_remove_filter("console", filt2));
+	return 0;
+}
+LOG_TEST_FLAGS(log_test_level_deny, UT_TESTF_CONSOLE_REC);
Signature · 1 line
2.28.0
#20 lore raw

PATCH v4 10/22 test: Add tests for LOGFF_DENY

Quoted 4 lines · Sean Anderson

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

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20201030/2b89046b/attachment.sig>
#21 lore raw

PATCH v4 11/22 log: Add filter flag to match greater than a log level

In reply to Sean Anderson

This is the complement 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.

---

(no changes since v1)

 common/log.c  | 12 +++++++++---
 include/log.h | 10 ++++++----
 2 files changed, 15 insertions(+), 7 deletions(-)
common/log.c +9 -3
index 6299c8b2d4..9a79b95a95 100644
@@ -156,11 +156,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;
@@ -235,7 +241,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;
@@ -263,7 +269,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 018180e788..ec2d6fdb01 100644
@@ -367,6 +367,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,
 };
 
 /**
@@ -382,7 +384,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
@@ -391,7 +393,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;
 };
@@ -492,14 +494,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_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_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
#22 lore raw

PATCH v4 11/22 log: Add filter flag to match greater than a log level

Quoted 6 lines · Sean Anderson

This is the complement 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.

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20201030/144f7aa4/attachment.sig>
#23 lore raw

PATCH v4 12/22 test: Add test for LOGFF_MIN

In reply to Sean Anderson

This tests log filters matching on a minimum level.

---

(no changes since v3)

Changes in v3:
- Modified to be completely written in C

 test/log/log_test.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
test/log/log_test.c +23 -0
index e4ab999a7d..ea4fc6bc30 100644
@@ -359,3 +359,26 @@ int log_test_level_deny(struct unit_test_state *uts)
 	return 0;
 }
 LOG_TEST_FLAGS(log_test_level_deny, UT_TESTF_CONSOLE_REC);
+
+/* Check matching based on minimum level */
+int log_test_min(struct unit_test_state *uts)
+{
+	int filt1, filt2;
+
+	filt1 = log_add_filter_flags("console", NULL, LOGL_WARNING, NULL,
+				     LOGFF_LEVEL_MIN);
+	ut_assert(filt1 >= 0);
+	filt2 = log_add_filter_flags("console", NULL, LOGL_INFO, NULL,
+				     LOGFF_DENY | LOGFF_LEVEL_MIN);
+	ut_assert(filt2 >= 0);
+
+	ut_assertok(console_record_reset_enable());
+	log_run();
+	check_log_entries_flags_levels(EXPECT_LOG | EXPECT_DIRECT,
+				       LOGL_WARNING, LOGL_INFO - 1);
+
+	ut_assertok(log_remove_filter("console", filt1));
+	ut_assertok(log_remove_filter("console", filt2));
+	return 0;
+}
+LOG_TEST_FLAGS(log_test_min, UT_TESTF_CONSOLE_REC);
Signature · 1 line
2.28.0
#24 lore raw

PATCH v4 12/22 test: Add test for LOGFF_MIN

Quoted 4 lines · Sean Anderson

This tests log filters matching on a minimum level.

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20201030/48eb510c/attachment.sig>
#25 lore raw

PATCH v4 13/22 cmd: log: Use sub-commands for log

In reply to Sean Anderson

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

---

(no changes since v1)

 cmd/log.c | 31 ++++---------------------------
 1 file changed, 4 insertions(+), 27 deletions(-)
cmd/log.c +4 -27
index d20bfdf744..82e3a7b62f 100644
@@ -103,30 +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, "", ""),
-	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"
@@ -139,7 +115,8 @@ 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),
+	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
#26 lore raw

PATCH v4 13/22 cmd: log: Use sub-commands for log

Quoted 4 lines · Sean Anderson

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

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20201030/4889f32d/attachment.sig>
#27 lore raw

PATCH v4 14/22 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.

---

(no changes since v2)

Changes in v2:
- Print an error message if the log level is invalid.

 cmd/log.c | 29 +++++++++++++++++++++++------
 1 file changed, 23 insertions(+), 6 deletions(-)
cmd/log.c +23 -6
index 82e3a7b62f..651e50358c 100644
@@ -11,23 +11,40 @@
 
 static char log_fmt_chars[LOGF_COUNT] = "clFLfm";
 
+static enum log_level_t parse_log_level(char *const arg)
+{
+	enum log_level_t ret;
+	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;
+	}
+
+	ret = log_get_level_by_name(arg);
+	if (ret == LOGL_NONE)
+		printf("Unknown log level \"%s\"\n", arg);
+	return ret;
+}
+
 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
#28 lore raw

PATCH v4 14/22 cmd: log: Split off log level parsing

Quoted 6 lines · 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.

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20201030/ed7e24a6/attachment.sig>
#29 lore raw

PATCH v4 15/22 cmd: log: Add commands to list categories and drivers

In reply to Sean Anderson

This allows users to query which categories and drivers are available on their system. This allows them to construct filter-add commands without (e.g.) adjusting the log format to show categories and drivers.

---

(no changes since v3)

Changes in v3:
- Document assumption that erroneous results from log_get_cat_name begin with '<'

Changes in v2:
- New

 cmd/log.c     | 35 +++++++++++++++++++++++++++++++++++
 common/log.c  |  1 +
 include/log.h |  5 +++--
 3 files changed, 39 insertions(+), 2 deletions(-)
cmd/log.c +35 -0
index 651e50358c..8d8d8a8172 100644
@@ -47,6 +47,37 @@ static int do_log_level(struct cmd_tbl *cmdtp, int flag, int argc,
 	return CMD_RET_SUCCESS;
 }
 
+static int do_log_categories(struct cmd_tbl *cmdtp, int flag, int argc,
+			     char *const argv[])
+{
+	enum log_category_t cat;
+	const char *name;
+
+	for (cat = LOGC_FIRST; cat < LOGC_COUNT; cat++) {
+		name = log_get_cat_name(cat);
+		/*
+		 * Invalid category names (e.g. <invalid> or <missing>) begin
+		 * with '<'.
+		 */
+		if (name[0] == '<')
+			continue;
+		printf("%s\n", name);
+	}
+
+	return CMD_RET_SUCCESS;
+}
+
+static int do_log_drivers(struct cmd_tbl *cmdtp, int flag, int argc,
+			  char *const argv[])
+{
+	struct log_device *ldev;
+
+	list_for_each_entry(ldev, &gd->log_head, sibling_node)
+		printf("%s\n", ldev->drv->name);
+
+	return CMD_RET_SUCCESS;
+}
+
 static int do_log_format(struct cmd_tbl *cmdtp, int flag, int argc,
 			 char *const argv[])
 {
@@ -123,6 +154,8 @@ static int do_log_rec(struct cmd_tbl *cmdtp, int flag, int argc,
 #ifdef CONFIG_SYS_LONGHELP
 static char log_help_text[] =
 	"level - get/set log level\n"
+	"categories - list log categories\n"
+	"drivers - list log drivers\n"
 	"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"
@@ -134,6 +167,8 @@ static char log_help_text[] =
 
 U_BOOT_CMD_WITH_SUBCMDS(log, "log system", log_help_text,
 	U_BOOT_SUBCMD_MKENT(level, 2, 1, do_log_level),
+	U_BOOT_SUBCMD_MKENT(categories, 1, 1, do_log_categories),
+	U_BOOT_SUBCMD_MKENT(drivers, 1, 1, do_log_drivers),
 	U_BOOT_SUBCMD_MKENT(format, 2, 1, do_log_format),
 	U_BOOT_SUBCMD_MKENT(rec, 7, 1, do_log_rec),
 );
common/log.c +1 -0
index 9a79b95a95..95ba8350c6 100644
@@ -41,6 +41,7 @@ static const char *const log_level_name[LOGL_COUNT] = {
 	"IO",
 };
 
+/* All error responses MUST begin with '<' */
 const char *log_get_cat_name(enum log_category_t cat)
 {
 	const char *name;
include/log.h +3 -2
index ec2d6fdb01..7a839d55aa 100644
@@ -409,8 +409,9 @@ struct log_filter {
  * 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) if found, or
- *	 "<invalid>" if invalid, or "<missing>" if not found
+ * @return: category name (which may be a uclass driver name) if found, or
+ *	   "<invalid>" if invalid, or "<missing>" if not found. All error
+ *	   responses begin with '<'.
  */
 const char *log_get_cat_name(enum log_category_t cat);
 
Signature · 1 line
2.28.0
#30 lore raw

PATCH v4 15/22 cmd: log: Add commands to list categories and drivers

Quoted 6 lines · Sean Anderson

This allows users to query which categories and drivers are available on their system. This allows them to construct filter-add commands without (e.g.) adjusting the log format to show categories and drivers.

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20201030/19138cb1/attachment.sig>
#31 lore raw

PATCH v4 16/22 cmd: log: Make "log level" print all log levels

In reply to Sean Anderson

This makes the log level command print all valid log levels. The default log level is annotated. This provides an easy way to see which log levels are compiled-in.

---

(no changes since v2)

Changes in v2:
- New

 cmd/log.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
cmd/log.c +9 -3
index 8d8d8a8172..596bc73f47 100644
@@ -34,14 +34,20 @@ static enum log_level_t parse_log_level(char *const arg)
 static int do_log_level(struct cmd_tbl *cmdtp, int flag, int argc,
 			char *const argv[])
 {
+	enum log_level_t log_level;
+
 	if (argc > 1) {
-		enum log_level_t log_level = parse_log_level(argv[1]);
+		log_level = parse_log_level(argv[1]);
 
 		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);
+		for (log_level = LOGL_FIRST; log_level <= _LOG_MAX_LEVEL;
+		     log_level++)
+			printf("%s%s\n", log_get_level_name(log_level),
+			       log_level == gd->default_log_level ?
+			       " (default)" : "");
 	}
 
 	return CMD_RET_SUCCESS;
@@ -153,7 +159,7 @@ static int do_log_rec(struct cmd_tbl *cmdtp, int flag, int argc,
 
 #ifdef CONFIG_SYS_LONGHELP
 static char log_help_text[] =
-	"level - get/set log level\n"
+	"level [<level>] - get/set log level\n"
 	"categories - list log categories\n"
 	"drivers - list log drivers\n"
 	"log format <fmt> - set log output format. <fmt> is a string where\n"
Signature · 1 line
2.28.0
#32 lore raw

PATCH v4 16/22 cmd: log: Make "log level" print all log levels

Quoted 6 lines · Sean Anderson

This makes the log level command print all valid log levels. The default log level is annotated. This provides an easy way to see which log levels are compiled-in.

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20201030/df9cf4b6/attachment.sig>
#33 lore raw

PATCH v4 17/22 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.

---

(no changes since v2)

Changes in v2:
- Expand documentation of getopt() to include examples
- Remove opt prefix from getopt_state members

 MAINTAINERS        |   1 +
 doc/api/getopt.rst |   8 +++
 doc/api/index.rst  |   1 +
 include/getopt.h   | 130 +++++++++++++++++++++++++++++++++++++++++++++
 lib/Kconfig        |   5 ++
 lib/Makefile       |   1 +
 lib/getopt.c       | 125 +++++++++++++++++++++++++++++++++++++++++++
 7 files changed, 271 insertions(+)
 create mode 100644 doc/api/getopt.rst
 create mode 100644 include/getopt.h
 create mode 100644 lib/getopt.c
MAINTAINERS +1 -0
index fc4fad46ee..cb3151c765 100644
@@ -756,6 +756,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 787b6778e5..ae4a1b6c63 100644
@@ -8,6 +8,7 @@ U-Boot API documentation
 
    dfu
    efi
+   getopt
    linker_lists
    pinctrl
    rng
include/getopt.h new file +130 -0

130 lines not on this page · raw message

lib/Kconfig +5 -0
index 37aae73a26..79651eaad1 100644
@@ -550,6 +550,11 @@ config SPL_HEXDUMP
 	  This enables functions for printing dumps of binary data in
 	  SPL.
 
+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
#34 lore raw

PATCH v4 17/22 lib: Add getopt

Quoted 20 lines · 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?

Quoted 4 lines

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.

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20201030/013db9ef/attachment.sig>
#35 lore raw

PATCH v4 18/22 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.

---

(no changes since v1)

 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 15cd512506..98a9abf40e 100644
@@ -14,3 +14,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
#36 lore raw

PATCH v4 18/22 test: Add a test for getopt

Quoted 5 lines · 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.

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20201030/fa9e14d2/attachment.sig>
#37 lore raw

PATCH v4 19/22 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.

---

Changes in v4:
- Add a space in between the <= operator and the log level

Changes in v2:
- Add option to remove all filters to filter-remove
- Clarify filter-* help text

 cmd/Kconfig |   1 +
 cmd/log.c   | 241 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 242 insertions(+)
cmd/Kconfig +1 -0
index 11f299da2b..debe2f5401 100644
@@ -2225,6 +2225,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 +241 -0

266 lines not on this page · raw message

Signature · 1 line
2.28.0
#38 lore raw

PATCH v4 19/22 cmd: log: Add commands to manipulate filters

Quoted 10 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.

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20201030/fa1a08fd/attachment.sig>
#39 lore raw

PATCH v4 20/22 test: Add a test for log filter-*

In reply to Sean Anderson

This exercises a few success and failure modes of the log filter-* commands. log filter-list is not tested because it's purely informational. I don't think there's a good way to test it except by testing if the output of the command exactly matches a sample run.

---

Changes in v4:
- Wrap UCLASS_ literals with log_uc_cat when used as log categories

Changes in v3:
- Update copyright for log_filter.c

Changes in v2:
- Converted log filter-* tests to C from python

 include/test/log.h    |   1 +
 test/log/Makefile     |   1 +
 test/log/log_filter.c | 108 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 110 insertions(+)
 create mode 100644 test/log/log_filter.c
include/test/log.h +1 -0
index 772e197806..e902891450 100644
@@ -14,5 +14,6 @@
 
 /* Declare a new logging test */
 #define LOG_TEST(_name) UNIT_TEST(_name, 0, log_test)
+#define LOG_TEST_FLAGS(_name, _flags) UNIT_TEST(_name, _flags, log_test)
 
 #endif /* __TEST_LOG_H__ */
test/log/Makefile +1 -0
index 52e2f7b41c..9a6cdbe643 100644
@@ -3,6 +3,7 @@
 # Copyright (c) 2017 Google, Inc
 
 obj-$(CONFIG_LOG_TEST) += log_test.o
+obj-$(CONFIG_CMD_LOG) += log_filter.o
 
 ifdef CONFIG_UT_LOG
 
test/log/log_filter.c new file +108 -0

108 lines not on this page · raw message

Signature · 1 line
2.28.0
#40 lore raw

PATCH v4 20/22 test: Add a test for log filter-*

Quoted 7 lines · Sean Anderson

This exercises a few success and failure modes of the log filter-* commands. log filter-list is not tested because it's purely informational. I don't think there's a good way to test it except by testing if the output of the command exactly matches a sample run.

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20201030/9eb79e63/attachment.sig>
#41 lore raw

PATCH v4 21/22 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.

---

(no changes since v2)

Changes in v2:
- Add % before constants in kerneldocs
- Document log_level_t and log_category_t members

 doc/develop/logging.rst |   5 ++
 include/log.h           | 142 +++++++++++++++++++++++++---------------
 2 files changed, 95 insertions(+), 52 deletions(-)
doc/develop/logging.rst +5 -0
index 28340a4aac..52ccd2009e 100644
@@ -291,3 +291,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 +90 -52

262 lines not on this page · raw message

Signature · 1 line
2.28.0
#42 lore raw

PATCH v4 21/22 doc: Add log kerneldocs to documentation

Quoted 5 lines · Sean Anderson

The functions in log.h are already mostly documented, so add them to the generated documentation.

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20201030/6cbc9299/attachment.sig>
#43 lore raw

PATCH v4 22/22 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.

---

Changes in v4:
- Add some more examples to docs

Changes in v3:
- Fix heading level of Filters section
- Remove a few more already-implemented features from the TODO list

Changes in v2:
- Add a few informational commands
- Clarify wording of filter documentation
- Include enum definitions instead of re-documenting them
- Reorganize log documentation; related sections should now be more proximate

 doc/develop/logging.rst | 221 ++++++++++++++++++++--------------------
 1 file changed, 110 insertions(+), 111 deletions(-)
doc/develop/logging.rst +110 -111
index 52ccd2009e..7fdd1132ef 100644
@@ -21,23 +21,13 @@ is visible from the basic console output.
 U-Boot's logging feature aims to satisfy this goal for both users and
 developers.
 
-
 Logging levels
 --------------
 
-There are a number logging levels available, in increasing order of verbosity:
-
-* LOGL_EMERG - Printed before U-Boot halts
-* LOGL_ALERT - Indicates action must be taken immediate or U-Boot will crash
-* LOGL_CRIT - Indicates a critical error that will cause boot failure
-* LOGL_ERR - Indicates an error that may cause boot failure
-* LOGL_WARNING - Warning about an unexpected condition
-* LOGL_NOTE - Important information about progress
-* LOGL_INFO - Information about normal boot progress
-* LOGL_DEBUG - Debug information (useful for debugging a driver or subsystem)
-* LOGL_DEBUG_CONTENT - Debug message showing full message content
-* LOGL_DEBUG_IO - Debug message showing hardware I/O access
+There are a number logging levels available.
 
+.. kernel-doc:: include/log.h
+   :identifiers: log_level_t
 
 Logging category
 ----------------
@@ -46,16 +36,8 @@ Logging can come from a wide variety of places within U-Boot. Each log message
 has a category which is intended to allow messages to be filtered according to
 their source.
 
-The following main categories are defined:
-
-* LOGC_NONE - Unknown category (e.g. a debug() statement)
-* UCLASS\_... - Related to a particular uclass (e.g. UCLASS_USB)
-* LOGC_ARCH - Related to architecture-specific code
-* LOGC_BOARD - Related to board-specific code
-* LOGC_CORE - Related to core driver-model support
-* LOGC_DT - Related to device tree control
-* LOGC_EFI - Related to EFI implementation
-
+.. kernel-doc:: include/log.h
+   :identifiers: log_category_t
 
 Enabling logging
 ----------------
@@ -72,7 +54,6 @@ If CONFIG_LOG is not set, then no logging will be available.
 The above have SPL and TPL versions also, e.g. CONFIG_SPL_LOG_MAX_LEVEL and
 CONFIG_TPL_LOG_MAX_LEVEL.
 
-
 Temporary logging within a single file
 --------------------------------------
 
@@ -83,12 +64,52 @@ Sometimes it is useful to turn on logging just in one file. You can use this
    #define LOG_DEBUG
 
 to enable building in of all logging statements in a single file. Put it at
-the top of the file, before any #includes. This overrides any log-level setting
-in U-Boot, including CONFIG_LOG_DEFAULT_LEVEL, but just for that file.
+the top of the file, before any #includes.
 
+To actually get U-Boot to output this you need to also set the default logging
+level - e.g. set CONFIG_LOG_DEFAULT_LEVEL to 7 (:c:type:`LOGL_DEBUG`) or more.
+Otherwise debug output is suppressed and will not be generated.
+
+Using DEBUG
+-----------
+
+U-Boot has traditionally used a #define called DEBUG to enable debugging on a
+file-by-file basis. The debug() macro compiles to a printf() statement if
+DEBUG is enabled, and an empty statement if not.
+
+With logging enabled, debug() statements are interpreted as logging output
+with a level of LOGL_DEBUG and a category of LOGC_NONE.
+
+The logging facilities are intended to replace DEBUG, but if DEBUG is defined
+at the top of a file, then it takes precedence. This means that debug()
+statements will result in output to the console and this output will not be
+logged.
+
+Logging statements
+------------------
+
+The main logging function is:
+
+.. code-block:: c
+
+   log(category, level, format_string, ...)
+
+Also debug() and error() will generate log records  - these use LOG_CATEGORY
+as the category, so you should #define this right at the top of the source
+file to ensure the category is correct.
+
+You can also define CONFIG_LOG_ERROR_RETURN to enable the log_ret() macro. This
+can be used whenever your function returns an error value:
+
+.. code-block:: c
+
+   return log_ret(uclass_first_device(UCLASS_MMC, &dev));
+
+This will write a log record when an error code is detected (a value < 0). This
+can make it easier to trace errors that are generated deep in the call stack.
 
 Convenience functions
----------------------
+~~~~~~~~~~~~~~~~~~~~~
 
 A number of convenience functions are available to shorten the code needed
 for logging:
@@ -116,36 +137,6 @@ or
 
 Remember that all uclasses IDs are log categories too.
 
-
-Log command
------------
-
-The 'log' command provides access to several features:
-
-* level - access the default log level
-* format - access the console log format
-* rec - output a log record
-* test - run tests
-
-Type 'help log' for details.
-
-
-Using DEBUG
------------
-
-U-Boot has traditionally used a #define called DEBUG to enable debugging on a
-file-by-file basis. The debug() macro compiles to a printf() statement if
-DEBUG is enabled, and an empty statement if not.
-
-With logging enabled, debug() statements are interpreted as logging output
-with a level of LOGL_DEBUG and a category of LOGC_NONE.
-
-The logging facilities are intended to replace DEBUG, but if DEBUG is defined
-at the top of a file, then it takes precedence. This means that debug()
-statements will result in output to the console and this output will not be
-logged.
-
-
 Logging destinations
 --------------------
 
@@ -159,9 +150,40 @@ enabled or disabled independently:
 The syslog driver sends the value of environmental variable 'log_hostname' as
 HOSTNAME if available.
 
+Filters
+-------
+
+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:
+
+* 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.
+
+Log command
+-----------
+
+The 'log' command provides access to several features:
+
+* level - list log levels or set the default log level
+* categories - list log categories
+* drivers - list log drivers
+* filter-list - list filters
+* filter-add - add a new filter
+* filter-remove - remove filters
+* format - access the console log format
+* rec - output a log record
+
+Type 'help log' for details.
 
 Log format
-----------
+~~~~~~~~~~
 
 You can control the log format using the 'log format' command. The basic
 format is::
@@ -169,50 +191,43 @@ format is::
    LEVEL.category,file.c:123-func() message
 
 In the above, file.c:123 is the filename where the log record was generated and
-func() is the function name. By default ('log format default') only the
-function name and message are displayed on the console. You can control which
-fields are present, but not the field order.
+func() is the function name. By default ('log format default') only the message
+is displayed on the console. You can control which fields are present, but not
+the field order.
 
+Adding Filters
+~~~~~~~~~~~~~~
 
-Filters
--------
+To add new filters at runtime, use the 'log filter-add' command. For example, to
+suppress messages from the SPI and MMC subsystems, run::
 
-Filters are attached to log drivers to control what those drivers emit. Only
-records that pass through the filter make it to the driver.
+    log filter-add -D -c spi -c mmc
 
-Filters can be based on several criteria:
+You will also need to add another filter to allow other messages (because the
+default filter no longer applies)::
 
-* maximum log level
-* in a set of categories
-* in a set of files
+    log filter-add -A -l info
 
-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.
+Log levels may be either symbolic names (like above) or numbers. For example, to
+disable all debug and above (log level 7) messages from ``drivers/core/lists.c``
+and ``drivers/core/ofnode.c``, run::
 
+    log filter-add -D -f drivers/core/lists.c,drivers/core/ofnode.c -L 7
 
-Logging statements
-------------------
+To view active filters, use the 'log filter-list' command. Some example output
+is::
 
-The main logging function is:
-
-.. code-block:: c
-
-   log(category, level, format_string, ...)
-
-Also debug() and error() will generate log records  - these use LOG_CATEGORY
-as the category, so you should #define this right@the top of the source
-file to ensure the category is correct.
-
-You can also define CONFIG_LOG_ERROR_RETURN to enable the log_ret() macro. This
-can be used whenever your function returns an error value:
-
-.. code-block:: c
-
-   return log_ret(uclass_first_device(UCLASS_MMC, &dev));
-
-This will write a log record when an error code is detected (a value < 0). This
-can make it easier to trace errors that are generated deep in the call stack.
+    => log filter-list
+    num policy level            categories files
+      2   deny >= DEBUG                    drivers/core/lists.c,drivers/core/ofnode.c
+      0   deny <= IO                   spi
+                                       mmc
+      1  allow <= INFO
 
+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'.
 
 Code size
 ---------
@@ -229,13 +244,12 @@ The last option turns every debug() statement into a logging call, which
 bloats the code hugely. The advantage is that it is then possible to enable
 all logging within U-Boot.
 
-
 To Do
 -----
 
 There are lots of useful additions that could be made. None of the below is
-implemented! If you do one, please add a test in test/py/tests/test_log.py
-
+implemented! If you do one, please add a test in test/log/log_test.c
+log filter-add -D -f drivers/core/lists.c,drivers/core/ofnode.c -l 6
 Convenience functions to support setting the category:
 
 * log_arch(level, format_string, ...) - category LOGC_ARCH
@@ -256,25 +270,15 @@ Convert error() statements in the code to log() statements
 
 Figure out what to do with BUG(), BUG_ON() and warn_non_spl()
 
-Figure out what to do with assert()
-
 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
 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
 
@@ -285,12 +289,7 @@ 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
+Add a command to delete existing log records.
 
 Logging API
 -----------
Signature · 1 line
2.28.0
#44 lore raw

PATCH v4 22/22 doc: Update logging documentation

Quoted 6 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.

Please note I accepted the newer wording to include describing the log continuation support. If this should be called out specifically again, please do a follow-up.

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20201030/2ad10e48/attachment.sig>