From f416decf84bce92250b94577dc8b53c95af202a5 Mon Sep 17 00:00:00 2001 From: Etienne Champetier Date: Thu, 9 Mar 2017 02:48:03 +0000 Subject: [PATCH 1/2] Introduce -E argument (don't exit on error) This introduce a new ERROR macro, similar to FATAL By default ERROR has the behaviour of FATAL If you use -E, it will not exit Signed-off-by: Etienne Champetier --- kafkacat.c | 27 ++++++++++++++++++++++++++- kafkacat.h | 6 ++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/kafkacat.c b/kafkacat.c index d9632291..d76677cf 100644 --- a/kafkacat.c +++ b/kafkacat.c @@ -53,6 +53,7 @@ struct conf conf = { .run = 1, .verbosity = 1, + .exitonerror = 1, .partition = RD_KAFKA_PARTITION_UA, .msg_size = 1024*1024, .null_str = "NULL", @@ -94,6 +95,26 @@ void RD_NORETURN fatal0 (const char *func, int line, exit(1); } +/** + * Print error and exit if needed + */ +void error0 (int exitonerror, const char *func, int line, const char *fmt, ...) { + va_list ap; + char buf[1024]; + + va_start(ap, fmt); + vsnprintf(buf, sizeof(buf), fmt, ap); + va_end(ap); + + if (exitonerror) + INFO(2, "Error at %s:%i:\n", func, line); + + fprintf(stderr, "%% ERROR: %s%s\n", buf, exitonerror ? " : terminating":""); + + if (exitonerror) + exit(1); +} + /** @@ -887,6 +908,7 @@ static void RD_NORETURN usage (const char *argv0, int exitcode, " -D Message delimiter character:\n" " a-z.. | \\r | \\n | \\t | \\xNN\n" " Default: \\n\n" + " -E Do not exit on non fatal error\n" " -K Key delimiter (same format as -D)\n" " -c Limit message count\n" " -X list List available librdkafka configuration " @@ -1116,7 +1138,7 @@ static void argparse (int argc, char **argv, int do_conf_dump = 0; while ((opt = getopt(argc, argv, - "PCG:LQt:p:b:z:o:eD:K:Od:qvX:c:Tuf:ZlVh" + "PCG:LQt:p:b:z:o:eED:K:Od:qvX:c:Tuf:ZlVh" #if ENABLE_JSON "J" #endif @@ -1178,6 +1200,9 @@ static void argparse (int argc, char **argv, case 'e': conf.exit_eof = 1; break; + case 'E': + conf.exitonerror = 0; + break; case 'f': fmt = optarg; break; diff --git a/kafkacat.h b/kafkacat.h index 05b13595..b1c62616 100644 --- a/kafkacat.h +++ b/kafkacat.h @@ -63,6 +63,7 @@ struct conf { int run; int verbosity; int exitcode; + int exitonerror; char mode; int flags; #define CONF_F_FMT_JSON 0x1 /* JSON formatting */ @@ -108,8 +109,13 @@ extern struct conf conf; void RD_NORETURN fatal0 (const char *func, int line, const char *fmt, ...); +void error0 (int erroronexit, const char *func, int line, + const char *fmt, ...); + #define FATAL(.../*fmt*/) fatal0(__FUNCTION__, __LINE__, __VA_ARGS__) +#define ERROR(.../*fmt*/) error0(conf.exitonerror, __FUNCTION__, __LINE__, __VA_ARGS__) + /* Info printout */ #define INFO(VERBLVL,.../*fmt*/) do { \ if (conf.verbosity >= (VERBLVL)) \ From a1e0eaaa134273a05715cb7d82cd1a3ec1cd587d Mon Sep 17 00:00:00 2001 From: Etienne Champetier Date: Thu, 9 Mar 2017 03:10:32 +0000 Subject: [PATCH 2/2] Make ALL_BROKERS_DOWN a non fatal error Signed-off-by: Etienne Champetier --- kafkacat.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/kafkacat.c b/kafkacat.c index d76677cf..9b035587 100644 --- a/kafkacat.c +++ b/kafkacat.c @@ -1035,12 +1035,13 @@ static void term (int sig) { static void error_cb (rd_kafka_t *rk, int err, const char *reason, void *opaque) { - if (err == RD_KAFKA_RESP_ERR__ALL_BROKERS_DOWN) - FATAL("%s: %s: terminating", rd_kafka_err2str(err), + if (err == RD_KAFKA_RESP_ERR__ALL_BROKERS_DOWN) { + ERROR("%s: %s", rd_kafka_err2str(err), reason ? reason : ""); - - INFO(1, "ERROR: %s: %s\n", rd_kafka_err2str(err), - reason ? reason : ""); + } else { + INFO(1, "ERROR: %s: %s\n", rd_kafka_err2str(err), + reason ? reason : ""); + } }