From 61100788e721e4d135740229a3e4f3ada7f425a3 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Mon, 6 Feb 2023 16:50:50 +0100 Subject: [PATCH] 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 4cf94f30c72bce47c5079e8534dda7f9b3c49869) --- src/nmcli/utils.h | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/nmcli/utils.h b/src/nmcli/utils.h index 3897cd2b87..5d9449f54b 100644 --- a/src/nmcli/utils.h +++ b/src/nmcli/utils.h @@ -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 */