diff mbox series

[22/22] log: Allow padding of the function name

Message ID 20210705223300.2139971-21-sjg@chromium.org
State Accepted
Commit 72fa1ad8d9fe58153ca72e3886b8d846299e8fff
Delegated to: Simon Glass
Headers show
Series Various fixes and enhancements | expand

Commit Message

Simon Glass July 5, 2021, 10:33 p.m. UTC
At present when function names are logged, the output is a little hard to
read since every function is a different length. Add a way to pad the
names so that the log messages line up vertically. This doesn't work if
the function name is very long, but it makes a big difference in most
cases.

Use 20 characters as a default since this covers the vast majority of
functions.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 common/Kconfig       |  8 ++++++++
 common/log_console.c |  2 +-
 test/log/log_test.c  | 16 +++++++++-------
 3 files changed, 18 insertions(+), 8 deletions(-)

Comments

Simon Glass July 17, 2021, 8:39 p.m. UTC | #1
At present when function names are logged, the output is a little hard to
read since every function is a different length. Add a way to pad the
names so that the log messages line up vertically. This doesn't work if
the function name is very long, but it makes a big difference in most
cases.

Use 20 characters as a default since this covers the vast majority of
functions.

Signed-off-by: Simon Glass <sjg@chromium.org>
---

 common/Kconfig       |  8 ++++++++
 common/log_console.c |  2 +-
 test/log/log_test.c  | 16 +++++++++-------
 3 files changed, 18 insertions(+), 8 deletions(-)

Applied to u-boot-dm, thanks!
diff mbox series

Patch

diff --git a/common/Kconfig b/common/Kconfig
index 21407b547da..b77e17b5cbd 100644
--- a/common/Kconfig
+++ b/common/Kconfig
@@ -340,6 +340,14 @@  config LOGF_FUNC
 	  Show the function name in log messages by default. This value can
 	  be overridden using the 'log format' command.
 
+config LOGF_FUNC_PAD
+	int "Number of characters to use for function"
+	default 20
+	help
+	  Sets the field width to use when showing the function. Set this to
+	  a larger value if you have lots of long function names, and want
+	  things to line up.
+
 config LOG_SYSLOG
 	bool "Log output to syslog server"
 	depends on NET
diff --git a/common/log_console.c b/common/log_console.c
index 3f6177499ef..f1dcc04b97c 100644
--- a/common/log_console.c
+++ b/common/log_console.c
@@ -38,7 +38,7 @@  static int log_console_emit(struct log_device *ldev, struct log_rec *rec)
 		if (fmt & BIT(LOGF_LINE))
 			printf("%d-", rec->line);
 		if (fmt & BIT(LOGF_FUNC))
-			printf("%s()", rec->func);
+			printf("%*s()", CONFIG_LOGF_FUNC_PAD, rec->func);
 	}
 	if (fmt & BIT(LOGF_MSG))
 		printf("%s%s", add_space ? " " : "", rec->msg);
diff --git a/test/log/log_test.c b/test/log/log_test.c
index f1e67509c17..db7170f3042 100644
--- a/test/log/log_test.c
+++ b/test/log/log_test.c
@@ -62,9 +62,9 @@  static int do_check_log_entries(struct unit_test_state *uts, int flags, int min,
 
 	for (i = min; i <= max; i++) {
 		if (flags & EXPECT_LOG)
-			ut_assert_nextline("do_log_run() log %d", i);
+			ut_assert_nextline("          do_log_run() log %d", i);
 		if (flags & EXPECT_DIRECT)
-			ut_assert_nextline("func() _log %d", i);
+			ut_assert_nextline("                func() _log %d", i);
 		if (flags & EXPECT_DEBUG) {
 			ut_assert_nextline("log %d", i);
 			ut_assert_nextline("_log %d", i);
@@ -72,11 +72,12 @@  static int do_check_log_entries(struct unit_test_state *uts, int flags, int min,
 	}
 	if (flags & EXPECT_EXTRA)
 		for (; i <= LOGL_MAX ; i++)
-			ut_assert_nextline("func() _log %d", i);
+			ut_assert_nextline("                func() _log %d", i);
 
 	for (i = LOGL_FIRST; i < LOGL_COUNT; i++) {
 		if (flags & EXPECT_FORCE)
-			ut_assert_nextline("func() _log force %d", i);
+			ut_assert_nextline("                func() _log force %d",
+					   i);
 		if (flags & EXPECT_DEBUG)
 			ut_assert_nextline("_log force %d", i);
 	}
@@ -277,7 +278,8 @@  int do_log_test_helpers(struct unit_test_state *uts)
 	log_io("level %d\n", LOGL_DEBUG_IO);
 
 	for (i = LOGL_EMERG; i <= _LOG_MAX_LEVEL; i++)
-		ut_assert_nextline("%s() level %d", __func__, i);
+		ut_assert_nextline("%*s() level %d", CONFIG_LOGF_FUNC_PAD,
+				   __func__, i);
 	ut_assert_console_end();
 	return 0;
 }
@@ -297,14 +299,14 @@  int do_log_test_disable(struct unit_test_state *uts)
 {
 	ut_assertok(console_record_reset_enable());
 	log_err("default\n");
-	ut_assert_nextline("%s() default", __func__);
+	ut_assert_nextline("%*s() default", CONFIG_LOGF_FUNC_PAD, __func__);
 
 	ut_assertok(log_device_set_enable(LOG_GET_DRIVER(console), false));
 	log_err("disabled\n");
 
 	ut_assertok(log_device_set_enable(LOG_GET_DRIVER(console), true));
 	log_err("enabled\n");
-	ut_assert_nextline("%s() enabled", __func__);
+	ut_assert_nextline("%*s() enabled", CONFIG_LOGF_FUNC_PAD, __func__);
 	ut_assert_console_end();
 	return 0;
 }