[PATCH 3/6] nss: Make logging conditional on an envvar

Michal Privoznik via Devel posted 6 patches 5 months, 1 week ago
[PATCH 3/6] nss: Make logging conditional on an envvar
Posted by Michal Privoznik via Devel 5 months, 1 week ago
From: Michal Privoznik <mprivozn@redhat.com>

As promised in previous commit, make NSS modules silent by
default and enable debug printings if LIBVIRT_NSS_DEBUG envvar is
set.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
---
 docs/nss.rst                | 13 +++++++++++++
 tools/nss/libvirt_nss_log.c |  4 ++++
 tools/nss/libvirt_nss_log.h |  2 ++
 3 files changed, 19 insertions(+)

diff --git a/docs/nss.rst b/docs/nss.rst
index be137d987b..3df85cee0a 100644
--- a/docs/nss.rst
+++ b/docs/nss.rst
@@ -111,6 +111,19 @@ their IP addresses in any other way (usermode networking, assigned network
 devices and so on) will not be able to have their hostnames resolved through
 it.
 
+Debugging
+---------
+
+:since:`Since 11.5.0` both NSS modules check for ``LIBVIRT_NSS_DEBUG``
+environment variable¸ which if set to any value turns on printing of debug and
+error messages onto standard error output. This can be useful when debugging
+either of the module.
+
+::
+
+  $ LIBVIRT_NSS_DEBUG=1 getent hosts mydomain
+
+
 Alternatives
 ------------
 
diff --git a/tools/nss/libvirt_nss_log.c b/tools/nss/libvirt_nss_log.c
index 0863897c07..9a28a91800 100644
--- a/tools/nss/libvirt_nss_log.c
+++ b/tools/nss/libvirt_nss_log.c
@@ -24,6 +24,7 @@
 #include <stdio.h>
 #include <string.h>
 #include <stdarg.h>
+#include <stdlib.h>
 
 #include "libvirt_nss_log.h"
 #include "libvirt_nss.h"
@@ -54,6 +55,9 @@ nssLog(nssLogPriority prio,
     g_autofree char *ebuf = NULL;
     va_list ap;
 
+    if (!getenv(NSS_LOG_ENV_VAR))
+        return;
+
     fprintf(stderr, "%s %s:%d : ", nssLogPriorityToString(prio), func, linenr);
 
     va_start(ap, fmt);
diff --git a/tools/nss/libvirt_nss_log.h b/tools/nss/libvirt_nss_log.h
index bac64d4bc7..25926fb6de 100644
--- a/tools/nss/libvirt_nss_log.h
+++ b/tools/nss/libvirt_nss_log.h
@@ -31,6 +31,8 @@ typedef enum {
 #define ERROR(...) \
     nssLog(NSS_ERROR, __FUNCTION__, __LINE__, __VA_ARGS__)
 
+#define NSS_LOG_ENV_VAR "LIBVIRT_NSS_DEBUG"
+
 void
 nssLog(nssLogPriority prio,
        const char *func,
-- 
2.49.0
Re: [PATCH 3/6] nss: Make logging conditional on an envvar
Posted by Ján Tomko via Devel 5 months ago
On a Friday in 2025, Michal Privoznik via Devel wrote:
>From: Michal Privoznik <mprivozn@redhat.com>
>
>As promised in previous commit, make NSS modules silent by
>default and enable debug printings if LIBVIRT_NSS_DEBUG envvar is
>set.
>
>Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
>---
> docs/nss.rst                | 13 +++++++++++++
> tools/nss/libvirt_nss_log.c |  4 ++++
> tools/nss/libvirt_nss_log.h |  2 ++
> 3 files changed, 19 insertions(+)
>
>diff --git a/docs/nss.rst b/docs/nss.rst
>index be137d987b..3df85cee0a 100644
>--- a/docs/nss.rst
>+++ b/docs/nss.rst
>@@ -111,6 +111,19 @@ their IP addresses in any other way (usermode networking, assigned network
> devices and so on) will not be able to have their hostnames resolved through
> it.
>
>+Debugging
>+---------
>+
>+:since:`Since 11.5.0` both NSS modules check for ``LIBVIRT_NSS_DEBUG``

11.6.0

Jano

>+environment variable¸ which if set to any value turns on printing of debug and
>+error messages onto standard error output. This can be useful when debugging
>+either of the module.
>+
>+::
>+
>+  $ LIBVIRT_NSS_DEBUG=1 getent hosts mydomain
>+
>+