zdump/zg: Convert print macros to functions which can be mocked

This change allows mocking of print macros in unit tests.
Being able to do this in unit tests, enables us to catch output from
zgetdump functions and test it.

Signed-off-by: Alexander Egorenkov <egorenar@linux.ibm.com>
Reviewed-by: Marc Hartmayer <mhartmay@linux.ibm.com>
Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
This commit is contained in:
Alexander Egorenkov
2021-11-24 15:17:22 +01:00
committed by Jan Höppner
parent 2238eb820f
commit 0d407904dd
3 changed files with 62 additions and 18 deletions

View File

@@ -41,7 +41,7 @@ check_dep_zlib:
all: check_dep_fuse check_dep_zlib zgetdump all: check_dep_fuse check_dep_zlib zgetdump
OBJECTS = zgetdump.o opts.o zg.o zg_error.o \ OBJECTS = zgetdump.o opts.o zg.o zg_error.o zg_print.o \
dfi.o dfi_mem_chunk.o dfi_vmcoreinfo.o \ dfi.o dfi_mem_chunk.o dfi_vmcoreinfo.o \
dfi_lkcd.o dfi_elf.o \ dfi_lkcd.o dfi_elf.o \
dfi_s390.o dfi_s390_ext.o\ dfi_s390.o dfi_s390_ext.o\

View File

@@ -82,24 +82,13 @@ void zg_abort(const char *fmt, ...);
#define ERR_EXIT_ERRNO(fmt, ...) zg_err_exit_errno(fmt, ## __VA_ARGS__) #define ERR_EXIT_ERRNO(fmt, ...) zg_err_exit_errno(fmt, ## __VA_ARGS__)
#define ABORT(fmt, ...) zg_abort(fmt, ## __VA_ARGS__) #define ABORT(fmt, ...) zg_abort(fmt, ## __VA_ARGS__)
#define STDERR(x...) \ void zg_stderr(const char *fmt, ...);
do { \ void zg_stderr_pr(const char *fmt, ...);
fprintf(stderr, x); \ void zg_stdout(const char *fmt, ...);
fflush(stderr); \
} while (0)
#define STDERR_PR(x...) \
do { \
fprintf(stderr, "\r%s: ", "zgetdump"); \
fprintf(stderr, x); \
} while (0)
#define STDOUT(x...) \
do { \
fprintf(stdout, x); \
fflush(stdout); \
} while (0)
#define STDERR(fmt, ...) zg_stderr(fmt, ## __VA_ARGS__)
#define STDERR_PR(fmt, ...) zg_stderr_pr(fmt, ## __VA_ARGS__)
#define STDOUT(fmt, ...) zg_stdout(fmt, ## __VA_ARGS__)
/* /*
* Misc * Misc
*/ */

55
zdump/zg_print.c Normal file
View File

@@ -0,0 +1,55 @@
/*
* Copyright IBM Corp. 2001, 2017, 2021
*
* s390-tools is free software; you can redistribute it and/or modify
* it under the terms of the MIT license. See LICENSE for details.
*/
#include <stdarg.h>
#include "zg.h"
static inline void _zg_stderr(const char *fmt, va_list ap)
{
vfprintf(stderr, fmt, ap);
fflush(stderr);
}
static inline void _zg_stderr_pr(const char *fmt, va_list ap)
{
fprintf(stderr, "\r%s: ", "zgetdump");
vfprintf(stderr, fmt, ap);
}
static inline void _zg_stdout(const char *fmt, va_list ap)
{
vfprintf(stdout, fmt, ap);
fflush(stdout);
}
void zg_stderr(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
_zg_stderr(fmt, ap);
va_end(ap);
}
void zg_stderr_pr(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
_zg_stderr_pr(fmt, ap);
va_end(ap);
}
void zg_stdout(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
_zg_stdout(fmt, ap);
va_end(ap);
}