diff options
author | Quentin Monnet <quentin.monnet@netronome.com> | 2017-10-23 12:24:13 -0400 |
---|---|---|
committer | David S. Miller <davem@davemloft.net> | 2017-10-23 20:25:09 -0400 |
commit | 9a5ab8bf1d6d16ef47fdf55dba1683ec00d751ad (patch) | |
tree | bd5c00e184e771ecc21fc593454ff63203797a29 /tools/bpf/bpftool/main.c | |
parent | 3aaca6bf7a09150e4c87f2932dc8ebe82a586252 (diff) |
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>
Diffstat (limited to 'tools/bpf/bpftool/main.c')
-rw-r--r-- | tools/bpf/bpftool/main.c | 16 |
1 files changed, 8 insertions, 8 deletions
diff --git a/tools/bpf/bpftool/main.c b/tools/bpf/bpftool/main.c index 71b01bf73912..9989a77fdc4a 100644 --- a/tools/bpf/bpftool/main.c +++ b/tools/bpf/bpftool/main.c | |||
@@ -158,20 +158,20 @@ static int do_batch(int argc, char **argv) | |||
158 | int i; | 158 | int i; |
159 | 159 | ||
160 | if (argc < 2) { | 160 | if (argc < 2) { |
161 | err("too few parameters for batch\n"); | 161 | p_err("too few parameters for batch"); |
162 | return -1; | 162 | return -1; |
163 | } else if (!is_prefix(*argv, "file")) { | 163 | } else if (!is_prefix(*argv, "file")) { |
164 | err("expected 'file', got: %s\n", *argv); | 164 | p_err("expected 'file', got: %s", *argv); |
165 | return -1; | 165 | return -1; |
166 | } else if (argc > 2) { | 166 | } else if (argc > 2) { |
167 | err("too many parameters for batch\n"); | 167 | p_err("too many parameters for batch"); |
168 | return -1; | 168 | return -1; |
169 | } | 169 | } |
170 | NEXT_ARG(); | 170 | NEXT_ARG(); |
171 | 171 | ||
172 | fp = fopen(*argv, "r"); | 172 | fp = fopen(*argv, "r"); |
173 | if (!fp) { | 173 | if (!fp) { |
174 | err("Can't open file (%s): %s\n", *argv, strerror(errno)); | 174 | p_err("Can't open file (%s): %s", *argv, strerror(errno)); |
175 | return -1; | 175 | return -1; |
176 | } | 176 | } |
177 | 177 | ||
@@ -189,8 +189,8 @@ static int do_batch(int argc, char **argv) | |||
189 | while (n_argv[n_argc]) { | 189 | while (n_argv[n_argc]) { |
190 | n_argc++; | 190 | n_argc++; |
191 | if (n_argc == ARRAY_SIZE(n_argv)) { | 191 | if (n_argc == ARRAY_SIZE(n_argv)) { |
192 | err("line %d has too many arguments, skip\n", | 192 | p_err("line %d has too many arguments, skip", |
193 | lines); | 193 | lines); |
194 | n_argc = 0; | 194 | n_argc = 0; |
195 | break; | 195 | break; |
196 | } | 196 | } |
@@ -225,7 +225,7 @@ static int do_batch(int argc, char **argv) | |||
225 | perror("reading batch file failed"); | 225 | perror("reading batch file failed"); |
226 | err = -1; | 226 | err = -1; |
227 | } else { | 227 | } else { |
228 | info("processed %d lines\n", lines); | 228 | p_info("processed %d lines", lines); |
229 | err = 0; | 229 | err = 0; |
230 | } | 230 | } |
231 | err_close: | 231 | err_close: |
@@ -279,7 +279,7 @@ int main(int argc, char **argv) | |||
279 | if (json_output) { | 279 | if (json_output) { |
280 | json_wtr = jsonw_new(stdout); | 280 | json_wtr = jsonw_new(stdout); |
281 | if (!json_wtr) { | 281 | if (!json_wtr) { |
282 | err("failed to create JSON writer\n"); | 282 | p_err("failed to create JSON writer"); |
283 | return -1; | 283 | return -1; |
284 | } | 284 | } |
285 | jsonw_pretty(json_wtr, pretty_output); | 285 | jsonw_pretty(json_wtr, pretty_output); |