From: Simon Glass <sjg@chromium.org>
Date: Sat, 12 Sep 2020 18:28:49 +0000 (-0600)
Subject: log: Add a way to enable/disable a log device
X-Git-Tag: v2025.01-rc5-pxa1908~2183^2~4
X-Git-Url: http://git.dujemihanovic.xyz/projects?a=commitdiff_plain;h=3d03ab6361a4a2b60e84da46c547b8ace01a60eb;p=u-boot.git

log: Add a way to enable/disable a log device

At present all log devices are enabled by default. Add a function to allow
devices to be disabled or enabled at runtime.

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

diff --git a/common/log.c b/common/log.c
index d6dfabb09a..1b10f6f180 100644
--- a/common/log.c
+++ b/common/log.c
@@ -308,6 +308,44 @@ int log_remove_filter(const char *drv_name, int filter_num)
 	return -ENOENT;
 }
 
+/**
+ * log_find_device_by_drv() - Find a device by its driver
+ *
+ * @drv: Log driver
+ * @return Device associated with that driver, or NULL if not found
+ */
+static struct log_device *log_find_device_by_drv(struct log_driver *drv)
+{
+	struct log_device *ldev;
+
+	list_for_each_entry(ldev, &gd->log_head, sibling_node) {
+		if (ldev->drv == drv)
+			return ldev;
+	}
+	/*
+	 * It is quite hard to pass an invalid driver since passing an unknown
+	 * LOG_GET_DRIVER(xxx) would normally produce a compilation error. But
+	 * it is possible to pass NULL, for example, so this
+	 */
+
+	return NULL;
+}
+
+int log_device_set_enable(struct log_driver *drv, bool enable)
+{
+	struct log_device *ldev;
+
+	ldev = log_find_device_by_drv(drv);
+	if (!ldev)
+		return -ENOENT;
+	if (enable)
+		ldev->flags |= LOGDF_ENABLE;
+	else
+		ldev->flags &= ~LOGDF_ENABLE;
+
+	return 0;
+}
+
 int log_init(void)
 {
 	struct log_driver *drv = ll_entry_start(struct log_driver, log_driver);
diff --git a/include/log.h b/include/log.h
index d28bc1ef0e..4acc087b2e 100644
--- a/include/log.h
+++ b/include/log.h
@@ -388,6 +388,10 @@ struct log_filter {
 #define LOG_DRIVER(_name) \
 	ll_entry_declare(struct log_driver, _name, log_driver)
 
+/* Get a pointer to a given driver */
+#define LOG_GET_DRIVER(__name)						\
+	ll_entry_get(struct log_driver, __name, log_driver)
+
 /**
  * log_get_cat_name() - Get the name of a category
  *
@@ -465,6 +469,19 @@ int log_add_filter(const char *drv_name, enum log_category_t cat_list[],
  */
 int log_remove_filter(const char *drv_name, int filter_num);
 
+/**
+ * log_device_set_enable() - Enable or disable a log device
+ *
+ * Devices are referenced by their driver, so use LOG_GET_DRIVER(name) to pass
+ * the driver to this function. For example if the driver is declared with
+ * LOG_DRIVER(wibble) then pass LOG_GET_DRIVER(wibble) here.
+ *
+ * @drv: Driver of device to enable
+ * @enable: true to enable, false to disable
+ * @return 0 if OK, -ENOENT if the driver was not found
+ */
+int log_device_set_enable(struct log_driver *drv, bool enable);
+
 #if CONFIG_IS_ENABLED(LOG)
 /**
  * log_init() - Set up the log system ready for use
diff --git a/test/log/log_test.c b/test/log/log_test.c
index fdee5e6757..6a60ff6be3 100644
--- a/test/log/log_test.c
+++ b/test/log/log_test.c
@@ -196,6 +196,13 @@ static int log_test(int testnum)
 		log_io("level %d\n", LOGL_DEBUG_IO);
 		break;
 	}
+	case 11:
+		log_err("default\n");
+		ret = log_device_set_enable(LOG_GET_DRIVER(console), false);
+		log_err("disabled\n");
+		ret = log_device_set_enable(LOG_GET_DRIVER(console), true);
+		log_err("enabled\n");
+		break;
 	}
 
 	return 0;
diff --git a/test/py/tests/test_log.py b/test/py/tests/test_log.py
index ddc28f19ee..275f9382d2 100644
--- a/test/py/tests/test_log.py
+++ b/test/py/tests/test_log.py
@@ -92,6 +92,13 @@ def test_log(u_boot_console):
         for i in range(7):
             assert 'log_test() level %d' % i == next(lines)
 
+    def test11():
+        """Test use of log_device_set_enable()"""
+        lines = run_test(11)
+        assert 'log_test() default'
+        # disabled should not be displayed
+        assert 'log_test() enabled'
+
     # TODO(sjg@chromium.org): Consider structuring this as separate tests
     cons = u_boot_console
     test0()
@@ -105,6 +112,7 @@ def test_log(u_boot_console):
     test8()
     test9()
     test10()
+    test11()
 
 @pytest.mark.buildconfigspec('cmd_log')
 def test_log_format(u_boot_console):