Skip to content

Commit

Permalink
Introduce -E argument (don't exit on error)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
champtar committed Mar 13, 2017
1 parent 9deecb5 commit f416dec
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
27 changes: 26 additions & 1 deletion kafkacat.c
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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);
}



/**
Expand Down Expand Up @@ -887,6 +908,7 @@ static void RD_NORETURN usage (const char *argv0, int exitcode,
" -D <delim> Message delimiter character:\n"
" a-z.. | \\r | \\n | \\t | \\xNN\n"
" Default: \\n\n"
" -E Do not exit on non fatal error\n"
" -K <delim> Key delimiter (same format as -D)\n"
" -c <cnt> Limit message count\n"
" -X list List available librdkafka configuration "
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions kafkacat.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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)) \
Expand Down

0 comments on commit f416dec

Please sign in to comment.