nmcli: add nmc_print()/nmc_printerr() macros

These will replace the direct calls to g_print()/g_printerr() in nmcli.
There are two purposes.

1) the new macros embody the concept of "printing something from nmcli".
   It means, we can `git grep` for those functions, and find all the
   relevant places, without hitting the irrelevant ones (e.g. tests that
   also use g_print()).

2) by having one place, we can trivially change it. That is useful for
   printf debugging. For example, "test-client.py" runs nmcli and
   captures and compares the output.  With libnm we can set
   LIBNM_CLIENT_DEBUG and LIBNM_CLIENT_DEBUG_FILE to print libnm debug
   messages to a file. But we cannot trivially synchronize the messages
   from nmcli with that output (also because they are consumed by the test
   and not immediately accessible). This would be easy, if we temporarily
   could patch nmc_print*() to also log to nm_utils_print(). The new macros
   will allow doing that at one place.

For example, patch the "#if 0" and run:

  $ LIBNM_CLIENT_DEBUG=trace \
    LIBNM_CLIENT_DEBUG_FILE='xxx.%p' \
    NMTST_USE_VALGRIND=1 \
    LIBTOOL="/bin/sh ./libtool"
    ./src/tests/client/test-client.sh -- -k monitor

(cherry picked from commit 4cf94f30c7)
This commit is contained in:
Thomas Haller 2023-02-06 16:50:50 +01:00
parent a791078798
commit 61100788e7
No known key found for this signature in database
GPG key ID: 29C2366E4DFC5728

View file

@ -369,4 +369,34 @@ gboolean nmc_print_table(const NmcConfig *nmc_config,
/*****************************************************************************/
#if 0
/* For manual testing to sync output with LIBNM_CLIENT_DEBUG/LIBNM_CLIENT_DEBUG_FILE */
#define nmc_print(...) \
G_STMT_START \
{ \
gs_free char *_ss = g_strdup_printf(__VA_ARGS__); \
gs_free char *_ss1 = g_strdup_printf("nmcli[out]: %s", _ss); \
\
nm_utils_print(0, _ss1); \
nm_utils_print(1, _ss); \
} \
G_STMT_END
#define nmc_printerr(...) \
G_STMT_START \
{ \
gs_free char *_ss = g_strdup_printf(__VA_ARGS__); \
gs_free char *_ss1 = g_strdup_printf("nmcli[err]: %s", _ss); \
\
nm_utils_print(0, _ss1); \
nm_utils_print(2, _ss); \
} \
G_STMT_END
#else
#define nmc_print(...) g_print(__VA_ARGS__)
#define nmc_printerr(...) g_printerr(__VA_ARGS__)
#endif
/*****************************************************************************/
#endif /* NMC_UTILS_H */