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
This commit is contained in:
Adrian Chadd 2021-03-21 18:49:05 +00:00
parent 2595d78f3d
commit 25bfa44860
4 changed files with 66 additions and 3 deletions

View File

@ -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
*/

View File

@ -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);
}

View File

@ -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 *);

View File

@ -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);