git: 25bfa448602c - main - Add device and ifnet logging methods, similar to device_printf / if_printf

Adrian Chadd adrian at FreeBSD.org
Mon Mar 22 00:02:42 UTC 2021


The branch main has been updated by adrian:

URL: https://cgit.FreeBSD.org/src/commit/?id=25bfa448602cac74723115d0b0bd145ac795b685

commit 25bfa448602cac74723115d0b0bd145ac795b685
Author:     Adrian Chadd <adrian at FreeBSD.org>
AuthorDate: 2021-03-21 18:49:05 +0000
Commit:     Adrian Chadd <adrian at FreeBSD.org>
CommitDate: 2021-03-22 00:02:34 +0000

    Add device and ifnet logging methods, similar to device_printf / if_printf
    
    * device_printf() is effectively a printf
    * if_printf() is effectively a LOG_INFO
    
    This allows subsystems to log device/netif stuff using different log levels,
    rather than having to invent their own way to prefix unit/netif  names.
    
    Differential Revision: https://reviews.freebsd.org/D29320
    Reviewed by: imp
---
 sys/kern/subr_bus.c | 41 +++++++++++++++++++++++++++++++++++++++++
 sys/net/if.c        | 26 +++++++++++++++++++++++---
 sys/net/if_var.h    |  1 +
 sys/sys/bus.h       |  1 +
 4 files changed, 66 insertions(+), 3 deletions(-)

diff --git a/sys/kern/subr_bus.c b/sys/kern/subr_bus.c
index ecd6c9685e36..31975fb8977c 100644
--- a/sys/kern/subr_bus.c
+++ b/sys/kern/subr_bus.c
@@ -2435,6 +2435,47 @@ device_printf(device_t dev, const char * fmt, ...)
 	return (retval);
 }
 
+/**
+ * @brief Print the name of the device followed by a colon, a space
+ * and the result of calling log() with the value of @p fmt and
+ * the following arguments.
+ *
+ * @returns the number of characters printed
+ */
+int
+device_log(device_t dev, int pri, const char * fmt, ...)
+{
+	char buf[128];
+	struct sbuf sb;
+	const char *name;
+	va_list ap;
+	size_t retval;
+
+	retval = 0;
+
+	sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN);
+
+	name = device_get_name(dev);
+
+	if (name == NULL)
+		sbuf_cat(&sb, "unknown: ");
+	else
+		sbuf_printf(&sb, "%s%d: ", name, device_get_unit(dev));
+
+	va_start(ap, fmt);
+	sbuf_vprintf(&sb, fmt, ap);
+	va_end(ap);
+
+	sbuf_finish(&sb);
+
+	log(pri, "%.*s", (int) sbuf_len(&sb), sbuf_data(&sb));
+	retval = sbuf_len(&sb);
+
+	sbuf_delete(&sb);
+
+	return (retval);
+}
+
 /**
  * @internal
  */
diff --git a/sys/net/if.c b/sys/net/if.c
index 86c60cfcfa7f..bbd677e0153c 100644
--- a/sys/net/if.c
+++ b/sys/net/if.c
@@ -3968,15 +3968,35 @@ if_initname(struct ifnet *ifp, const char *name, int unit)
 		strlcpy(ifp->if_xname, name, IFNAMSIZ);
 }
 
+static int
+if_vlog(struct ifnet *ifp, int pri, const char *fmt, va_list ap)
+{
+	char if_fmt[256];
+
+	snprintf(if_fmt, sizeof(if_fmt), "%s: %s", ifp->if_xname, fmt);
+	vlog(pri, if_fmt, ap);
+	return (0);
+}
+
+
 int
 if_printf(struct ifnet *ifp, const char *fmt, ...)
 {
-	char if_fmt[256];
 	va_list ap;
 
-	snprintf(if_fmt, sizeof(if_fmt), "%s: %s", ifp->if_xname, fmt);
 	va_start(ap, fmt);
-	vlog(LOG_INFO, if_fmt, ap);
+	if_vlog(ifp, LOG_INFO, fmt, ap);
+	va_end(ap);
+	return (0);
+}
+
+int
+if_log(struct ifnet *ifp, int pri, const char *fmt, ...)
+{
+	va_list ap;
+
+	va_start(ap, fmt);
+	if_vlog(ifp, pri, fmt, ap);
 	va_end(ap);
 	return (0);
 }
diff --git a/sys/net/if_var.h b/sys/net/if_var.h
index 33a737880a8d..1f82dd028379 100644
--- a/sys/net/if_var.h
+++ b/sys/net/if_var.h
@@ -659,6 +659,7 @@ void	if_free(struct ifnet *);
 void	if_initname(struct ifnet *, const char *, int);
 void	if_link_state_change(struct ifnet *, int);
 int	if_printf(struct ifnet *, const char *, ...) __printflike(2, 3);
+int	if_log(struct ifnet *, int, const char *, ...) __printflike(3, 4);
 void	if_ref(struct ifnet *);
 void	if_rele(struct ifnet *);
 bool	if_try_ref(struct ifnet *);
diff --git a/sys/sys/bus.h b/sys/sys/bus.h
index fbc69ca625c1..25fb86d3c42d 100644
--- a/sys/sys/bus.h
+++ b/sys/sys/bus.h
@@ -607,6 +607,7 @@ int	device_is_quiet(device_t dev);
 device_t device_lookup_by_name(const char *name);
 int	device_print_prettyname(device_t dev);
 int	device_printf(device_t dev, const char *, ...) __printflike(2, 3);
+int	device_log(device_t dev, int pri, const char *, ...) __printflike(3, 4);
 int	device_probe(device_t dev);
 int	device_probe_and_attach(device_t dev);
 int	device_probe_child(device_t bus, device_t dev);


More information about the dev-commits-src-main mailing list