From nobody Tue Jan 24 04:46:36 2023 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4P1Dvc5yHtz3bGMy; Tue, 24 Jan 2023 04:46:36 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4P1Dvc5Cdnz4Kj7; Tue, 24 Jan 2023 04:46:36 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1674535596; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=gWG9KIbYgkewSEkv6E+N1dYVwAwcm93CohcT7Ln+CBg=; b=KirBYQ79CZBDA8hBFBkHVTy1vh2QVNUROyjBYhuux63UOvlDIc83o49oNdzM5ELackAhsB r/Qc47KgfPCBy5Y7HJF4BT2CkvoV+l4IDS4G7GXiZTo+T2nASVcTzRYpf3G5F9ZnqAIWN5 IHgYIMTl67QmxzJxpaguRK9+7l0s08pQKkB1XWQzt1kl9XFThMh/q8vIa17UOcsrD8yBVz nn7k5Qd2cqicXlZTsNcM2PCy18FX+9T1fIbx/ipo/IGDisf4tWql/jT6npea1TYHLVh3XC FyUfkQK9vEgDWiJJD7ee2FyZiQajg9Lw+rGIDAGy2a5O9KZVR7XOQiK981il5w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1674535596; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=gWG9KIbYgkewSEkv6E+N1dYVwAwcm93CohcT7Ln+CBg=; b=wyUQbqUy+e2F3vY1ylRI0tm00U9G06lxLGoBsP4DiBs/WMxnzSx7J2uq3uJs8khsu+jvNB 7bEDzNwiSXOD40WkzrgX6k+FIQJyec341LSoLitzwob1XJXelQ8GnhImAXRno3Fz4+qEn/ 7h9V2ZPBEAbpllwNOJ+vlYv02vacsCgXVQNoniJnwiEOJWcKcYU3/Jyl0+HRnp/rYdXl7o Ul15V+VFnbtXLYKrSkgGJiSnLK/pkCFEaxKTo9EStZ/gCucvwswXWohLQxs86JhdlDgvQT qKUjkYeRQWd+W5KfoYR8Ncqd0VKADqkZalUVRoFLbIj9FNVbiovJDmLf0oni9g== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1674535596; a=rsa-sha256; cv=none; b=gi4wA/w8/6YtEEG0+pjuwsQOMOKYjxy2eDYos9uMU4k3c3xIJBtFkk3L8QT9e6BKJLvk9L 8K0+8hNTNg+hVXHxJOTCUJL47CxXhWeHjz3Iiy4mEnPSW7Wojd8DAYZpjc0OY4QRvuVDd7 tpozH+vP6qMBarKzDQEzYJml3PSRTorLHzfo9B2/pJCRPKjObWFL7AMf6gR2PmFkeY2E61 iQx8ql6vODxlRZu0ZFNju9SmaXxy/FJc5aSFr6iZnqnEvyN+HUKKLY0/XOEf69402Tm16w RFHJWs9LGMbOs4hYQmdtovKkselGsVJQrYogI+cLeJ4ok6D3W3YYCzBKRdLPKw== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4P1Dvc4CtHzwbd; Tue, 24 Jan 2023 04:46:36 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 30O4ka0K076138; Tue, 24 Jan 2023 04:46:36 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 30O4kadI076137; Tue, 24 Jan 2023 04:46:36 GMT (envelope-from git) Date: Tue, 24 Jan 2023 04:46:36 GMT Message-Id: <202301240446.30O4kadI076137@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: John Baldwin Subject: git: cc50ec0289bc - stable/13 - Add device and ifnet logging methods, similar to device_printf / if_printf List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: jhb X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: cc50ec0289bca8f9e906f5c46e0c56b4681ce8f2 Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch stable/13 has been updated by jhb: URL: https://cgit.FreeBSD.org/src/commit/?id=cc50ec0289bca8f9e906f5c46e0c56b4681ce8f2 commit cc50ec0289bca8f9e906f5c46e0c56b4681ce8f2 Author: Adrian Chadd AuthorDate: 2021-03-21 18:49:05 +0000 Commit: John Baldwin 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);