[PATCH v2 02/15] common: console: add log_writefile to write log into new file

Ahmad Fatoum a.fatoum at pengutronix.de
Fri Apr 30 14:29:36 BST 2021


It can be useful to dump the log into the file, e.g. when doing an
update from a USB flash drive with no serial peer attached.
Add a function to facilitate this.

Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
---
 common/console_common.c | 20 ++++++++++++++++++++
 include/printk.h        |  1 +
 2 files changed, 21 insertions(+)

diff --git a/common/console_common.c b/common/console_common.c
index 4c1230464c0b..aa9e943ef0e1 100644
--- a/common/console_common.c
+++ b/common/console_common.c
@@ -182,6 +182,26 @@ static int console_common_init(void)
 }
 device_initcall(console_common_init);
 
+int log_writefile(const char *filepath)
+{
+	int ret = 0, nbytes = 0, fd = -1;
+	struct log_entry *log;
+
+	fd = open(filepath, O_WRONLY | O_CREAT | O_TRUNC);
+	if (fd < 0)
+		return -errno;
+
+	list_for_each_entry(log, &barebox_logbuf, list) {
+		ret = dputs(fd, log->msg);
+		if (ret < 0)
+			break;
+		nbytes += ret;
+	}
+
+	close(fd);
+	return ret < 0 ? ret : nbytes;
+}
+
 void log_print(unsigned flags, unsigned levels)
 {
 	struct log_entry *log;
diff --git a/include/printk.h b/include/printk.h
index 94a25ec9ebac..f83ad3bf07f0 100644
--- a/include/printk.h
+++ b/include/printk.h
@@ -141,6 +141,7 @@ extern void log_clean(unsigned int limit);
 #define BAREBOX_LOG_PRINT_ALERT		BIT(1)
 #define BAREBOX_LOG_PRINT_EMERG		BIT(0)
 
+int log_writefile(const char *filepath);
 void log_print(unsigned flags, unsigned levels);
 
 struct va_format {
-- 
2.29.2




More information about the barebox mailing list