pvattest: pvattest_hexdump: add beautify parameter

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 <mhartmay@linux.ibm.com>
Reviewed-by: Jan Höppner <hoeppner@linux.ibm.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
This commit is contained in:
Marc Hartmayer
2023-08-01 15:13:52 +00:00
committed by Jan Höppner
parent 694d5d4638
commit bec9d1dfcd
3 changed files with 16 additions and 9 deletions

View File

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

View File

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

View File

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