Subject: Re: [PATCH iproute2-next v2 03/11] lib: utils: Add print_on_off_bool()

From: Leon Romanovsky <[email-masked]>
Date: Tue, 3 Nov 2020 08:24:06 +0200

On Tue, Nov 03, 2020 at 12:05:20AM +0100, Petr Machata wrote:
>
> Leon Romanovsky <[email-masked]> writes:
>
> > On Sun, Nov 01, 2020 at 04:55:42PM -0700, David Ahern wrote:
> >> On 10/31/20 3:23 PM, Petr Machata wrote:
> >> >
> >> > David Ahern <[email-masked]> writes:
> >> >
> >> >> On 10/30/20 6:29 AM, Petr Machata wrote:
> >> >>> diff --git a/lib/utils.c b/lib/utils.c
> >> >>> index 930877ae0f0d..8deec86ecbcd 100644
> >> >>> --- a/lib/utils.c
> >> >>> +++ b/lib/utils.c
> >> >>> @@ -1763,3 +1763,11 @@ int parse_on_off(const char *msg, const char *realval, int *p_err)
> >> >>>
> >> >>>  	return parse_one_of(msg, realval, values_on_off, ARRAY_SIZE(values_on_off), p_err);
> >> >>>  }
> >> >>> +
> >> >>> +void print_on_off_bool(FILE *fp, const char *flag, bool val)
> >> >>> +{
> >> >>> +	if (is_json_context())
> >> >>> +		print_bool(PRINT_JSON, flag, NULL, val);
> >> >>> +	else
> >> >>> +		fprintf(fp, "%s %s ", flag, val ? "on" : "off");
> >> >>> +}
> >> >>>
> >> >>
> >> >> I think print_on_off should be fine and aligns with parse_on_off once it
> >> >> returns a bool.
> >> >
> >> > print_on_off() is already used in the RDMA tool, and actually outputs
> >> > "on" and "off", unlike this. So I chose this instead.
> >> >
> >> > I could rename the RDMA one though -- it's used in two places, whereas
> >> > this is used in about two dozen instances across the codebase.
> >> >
> >>
> >> yes, the rdma utils are using generic function names. The rdma version
> >> should be renamed; perhaps rd_print_on_off. That seems to be once common
> >> prefix. Added Leon.
> >
> > I made fast experiment and the output for the code proposed here and existed
> > in the RDMAtool - result the same. So the good thing will be to delete the
> > function from the RDMA after print_on_off_bool() will be improved.
>
> The RDMAtool uses literal "on" and "off" as values in JSON, not
> booleans. Moving over to print_on_off_bool() would be a breaking change,
> which is problematic especially in JSON output.

Nothing prohibits us from adding extra parameter to this new
function/json logic/json type that will control JSON behavior. Personally,
I don't think that json and stdout outputs should be different, e.g. 1/0 for
the json and on/off for the stdout.

>
> > However I don't understand why print_on_off_bool() is implemented in
> > utils.c and not in lib/json_print.c that properly handles JSON context,
>
> There's a whole lot of print_X functions for printing non-fundamental
> data types in utils.c. Seemed obvious to put it there. I can move it to
> json_print.c, no problem.

It looks like it is worth to cleanup util.c and make sure that all
prints are gathered in one place. Out of the scope for this series.

>
> I think the current function does handle JSON context, what else do
> you have in mind?a

It handles, but does it twice, first time for is_json_context() and
second time inside print_bool.

>
> > provide colorized output and doesn't require to supply FILE *fp.
>
> Stephen Hemminger already pointed out the FILE *fp bit, I'll be removing
> it.