git: cc50ec0289bc - stable/13 - Add device and ifnet logging methods, similar to device_printf / if_printf
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Tue, 24 Jan 2023 04:46:36 UTC
The branch stable/13 has been updated by jhb: URL: https://cgit.FreeBSD.org/src/commit/?id=cc50ec0289bca8f9e906f5c46e0c56b4681ce8f2 commit cc50ec0289bca8f9e906f5c46e0c56b4681ce8f2 Author: Adrian Chadd <adrian@FreeBSD.org> AuthorDate: 2021-03-21 18:49:05 +0000 Commit: John Baldwin <jhb@FreeBSD.org> CommitDate: 2023-01-24 04:42:53 +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 (cherry picked from commit 25bfa448602cac74723115d0b0bd145ac795b685) --- 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 c1383add7e47..4512a9c83bb2 100644 --- a/sys/kern/subr_bus.c +++ b/sys/kern/subr_bus.c @@ -2443,6 +2443,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 4df376cbc7a6..9de81b7da248 100644 --- a/sys/net/if.c +++ b/sys/net/if.c @@ -3975,15 +3975,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 c0f7de44dc55..db399d7c7485 100644 --- a/sys/net/if_var.h +++ b/sys/net/if_var.h @@ -660,6 +660,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 __result_use_check if_try_ref(struct ifnet *); diff --git a/sys/sys/bus.h b/sys/sys/bus.h index a308810f158f..2179e718c668 100644 --- a/sys/sys/bus.h +++ b/sys/sys/bus.h @@ -624,6 +624,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);