From bec9d1dfcd7f9df244abb4ca10d43d0b345f57d3 Mon Sep 17 00:00:00 2001 From: Marc Hartmayer Date: Tue, 1 Aug 2023 15:13:52 +0000 Subject: [PATCH] pvattest: pvattest_hexdump: add `beautify` parameter MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add `beautify` parameter to `pvattest_hexdump`. If the parameter is set to true, a offset and whitespaces will be added for better readability. With beautify set to FALSE: 14141414141414141414141414141414 With beautify set to TRUE: 0x0000 1414 1414 1414 1414 1414 1414 1414 1414 Reviewed-by: Marc Hartmayer Reviewed-by: Jan Höppner Signed-off-by: Steffen Eiden Signed-off-by: Jan Höppner --- pvattest/src/exchange_format.c | 2 +- pvattest/src/log.c | 19 +++++++++++++------ pvattest/src/log.h | 4 ++-- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/pvattest/src/exchange_format.c b/pvattest/src/exchange_format.c index 293872cf..2f94c4bf 100644 --- a/pvattest/src/exchange_format.c +++ b/pvattest/src/exchange_format.c @@ -455,7 +455,7 @@ static void print_entry(const char *name, GBytes *data, const gboolean print_dat fprintf(stream, _("%s (%#lx bytes)"), name, g_bytes_get_size(data)); if (print_data) { fprintf(stream, ":\n"); - pvattest_hexdump(stream, data, 16, " "); + pvattest_hexdump(stream, data, 16, " ", TRUE); } fprintf(stream, "\n"); } diff --git a/pvattest/src/log.c b/pvattest/src/log.c index 61703fa4..65df2b5e 100644 --- a/pvattest/src/log.c +++ b/pvattest/src/log.c @@ -159,7 +159,8 @@ void pvattest_log_bytes(const void *data, size_t size, size_t width, const char g_log(PVATTEST_BYTES_LOG_DOMAIN, log_lvl, "\n"); } -void pvattest_hexdump(FILE *stream, GBytes *bytes, const size_t width, const char *prefix) +void pvattest_hexdump(FILE *stream, GBytes *bytes, const size_t width, const char *prefix, + const gboolean beautify) { const uint8_t *data; size_t size; @@ -170,19 +171,25 @@ void pvattest_hexdump(FILE *stream, GBytes *bytes, const size_t width, const cha data = g_bytes_get_data(bytes, &size); pv_wrapped_g_assert(data); - fprintf(stream, "%s0x0000 ", prefix); + if (beautify) + fprintf(stream, "%s0x0000 ", prefix); + else + fprintf(stream, "%s", prefix); for (size_t i = 0; i < size; i++) { fprintf(stream, "%02x", data[i]); - if (i % 2 == 1) + if (i % 2 == 1 && beautify) fprintf(stream, " "); if (i == size - 1) break; if (width == 0) continue; - if (i % width == width - 1) - fprintf(stream, "\n%s0x%04lx ", prefix, i + 1); + if (i % width == width - 1) { + if (beautify) + fprintf(stream, "\n%s0x%04lx ", prefix, i + 1); + else + fprintf(stream, "\n%s", prefix); + } } - fprintf(stream, "\n"); } void pvattest_log_GError(const char *info, GError *error) diff --git a/pvattest/src/log.h b/pvattest/src/log.h index 0d15c4cf..ce520b5d 100644 --- a/pvattest/src/log.h +++ b/pvattest/src/log.h @@ -60,8 +60,8 @@ void pvattest_log_plain_logger(const char *log_domain, GLogLevelFlags level, con } void pvattest_log_bytes(const void *data, size_t size, size_t width, const char *prefix, gboolean beautify, GLogLevelFlags log_lvl) PV_NONNULL(1); -void pvattest_hexdump(FILE *stream, GBytes *bytes, const size_t width, const char *prefix) - PV_NONNULL(1, 2); +void pvattest_hexdump(FILE *stream, GBytes *bytes, const size_t width, const char *prefix, + const gboolean beautify) PV_NONNULL(1, 2); void pvattest_log_GError(const char *info, GError *error) PV_NONNULL(1); #endif /* PVATTEST_LOG_H */