tools: bpftool: turn err() and info() macros into functions

Turn err() and info() macros into functions.

In order to avoid naming conflicts with variables in the code, rename
them as p_err() and p_info() respectively.

The behavior of these functions is similar to the one of the macros for
plain output. However, when JSON output is requested, these macros
return a JSON-formatted "error" object instead of printing a message to
stderr.

To handle error messages correctly with JSON, a modification was brought
to their behavior nonetheless: the functions now append a end-of-line
character at the end of the message. This way, we can remove end-of-line
characters at the end of the argument strings, and not have them in the
JSON output.

All error messages are formatted to hold in a single call to p_err(), in
order to produce a single JSON field.

Signed-off-by: Quentin Monnet <quentin.monnet@netronome.com>
Acked-by: Jakub Kicinski <jakub.kicinski@netronome.com>
Acked-by: Daniel Borkmann <daniel@iogearbox.net>
Signed-off-by: David S. Miller <davem@davemloft.net>
This commit is contained in:
Quentin Monnet 2017-10-23 09:24:13 -07:00 committed by David S. Miller
parent 3aaca6bf7a
commit 9a5ab8bf1d
5 changed files with 126 additions and 91 deletions

View file

@ -158,20 +158,20 @@ static int do_batch(int argc, char **argv)
int i;
if (argc < 2) {
err("too few parameters for batch\n");
p_err("too few parameters for batch");
return -1;
} else if (!is_prefix(*argv, "file")) {
err("expected 'file', got: %s\n", *argv);
p_err("expected 'file', got: %s", *argv);
return -1;
} else if (argc > 2) {
err("too many parameters for batch\n");
p_err("too many parameters for batch");
return -1;
}
NEXT_ARG();
fp = fopen(*argv, "r");
if (!fp) {
err("Can't open file (%s): %s\n", *argv, strerror(errno));
p_err("Can't open file (%s): %s", *argv, strerror(errno));
return -1;
}
@ -189,8 +189,8 @@ static int do_batch(int argc, char **argv)
while (n_argv[n_argc]) {
n_argc++;
if (n_argc == ARRAY_SIZE(n_argv)) {
err("line %d has too many arguments, skip\n",
lines);
p_err("line %d has too many arguments, skip",
lines);
n_argc = 0;
break;
}
@ -225,7 +225,7 @@ static int do_batch(int argc, char **argv)
perror("reading batch file failed");
err = -1;
} else {
info("processed %d lines\n", lines);
p_info("processed %d lines", lines);
err = 0;
}
err_close:
@ -279,7 +279,7 @@ int main(int argc, char **argv)
if (json_output) {
json_wtr = jsonw_new(stdout);
if (!json_wtr) {
err("failed to create JSON writer\n");
p_err("failed to create JSON writer");
return -1;
}
jsonw_pretty(json_wtr, pretty_output);