From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS, UNPARSEABLE_RELAY,UNWANTED_LANGUAGE_BODY,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8D9F0C3A5A5 for ; Thu, 5 Sep 2019 12:43:32 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 71BB820DCE for ; Thu, 5 Sep 2019 12:43:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389471AbfIEMna (ORCPT ); Thu, 5 Sep 2019 08:43:30 -0400 Received: from mail-il-dmz.mellanox.com ([193.47.165.129]:58416 "EHLO mellanox.co.il" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S2389449AbfIEMn3 (ORCPT ); Thu, 5 Sep 2019 08:43:29 -0400 Received: from Internal Mail-Server by MTLPINE1 (envelope-from tariqt@mellanox.com) with ESMTPS (AES256-SHA encrypted); 5 Sep 2019 15:43:23 +0300 Received: from dev-l-vrt-207-011.mtl.labs.mlnx. (dev-l-vrt-207-011.mtl.labs.mlnx [10.134.207.11]) by labmailer.mlnx (8.13.8/8.13.8) with ESMTP id x85ChNGY021437; Thu, 5 Sep 2019 15:43:23 +0300 From: Tariq Toukan To: Stephen Hemminger , David Ahern Cc: netdev@vger.kernel.org, Moshe Shemesh , Aya Levin , Jiri Pirko , Tariq Toukan Subject: [PATCH iproute2 1/4] devlink: Add helper for left justification print Date: Thu, 5 Sep 2019 15:43:04 +0300 Message-Id: <1567687387-12993-2-git-send-email-tariqt@mellanox.com> X-Mailer: git-send-email 1.8.3.1 In-Reply-To: <1567687387-12993-1-git-send-email-tariqt@mellanox.com> References: <1567687387-12993-1-git-send-email-tariqt@mellanox.com> Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org From: Aya Levin Introduce a helper function which wraps code that adds a left hand side space separator unless it follows a newline. Fixes: e3d0f0c0e3d8 ("devlink: add option to generate JSON output") Signed-off-by: Aya Levin Acked-by: Jiri Pirko Signed-off-by: Tariq Toukan --- devlink/devlink.c | 45 ++++++++++++++++++++------------------------- 1 file changed, 20 insertions(+), 25 deletions(-) diff --git a/devlink/devlink.c b/devlink/devlink.c index 2f084c020765..f1b9b2da39d7 100644 --- a/devlink/devlink.c +++ b/devlink/devlink.c @@ -355,6 +355,12 @@ static bool dl_no_arg(struct dl *dl) return dl_argc(dl) == 0; } +static void __pr_out_indent_newline(struct dl *dl) +{ + if (!g_indent_newline && !dl->json_output) + pr_out(" "); +} + static const enum mnl_attr_data_type devlink_policy[DEVLINK_ATTR_MAX + 1] = { [DEVLINK_ATTR_BUS_NAME] = MNL_TYPE_NUL_STRING, [DEVLINK_ATTR_DEV_NAME] = MNL_TYPE_NUL_STRING, @@ -1799,14 +1805,11 @@ static void pr_out_port_handle_end(struct dl *dl) static void pr_out_str(struct dl *dl, const char *name, const char *val) { - if (dl->json_output) { + __pr_out_indent_newline(dl); + if (dl->json_output) jsonw_string_field(dl->jw, name, val); - } else { - if (g_indent_newline) - pr_out("%s %s", name, val); - else - pr_out(" %s %s", name, val); - } + else + pr_out("%s %s", name, val); } static void pr_out_bool(struct dl *dl, const char *name, bool val) @@ -1819,29 +1822,23 @@ static void pr_out_bool(struct dl *dl, const char *name, bool val) static void pr_out_uint(struct dl *dl, const char *name, unsigned int val) { - if (dl->json_output) { + __pr_out_indent_newline(dl); + if (dl->json_output) jsonw_uint_field(dl->jw, name, val); - } else { - if (g_indent_newline) - pr_out("%s %u", name, val); - else - pr_out(" %s %u", name, val); - } + else + pr_out("%s %u", name, val); } static void pr_out_u64(struct dl *dl, const char *name, uint64_t val) { + __pr_out_indent_newline(dl); if (val == (uint64_t) -1) return pr_out_str(dl, name, "unlimited"); - if (dl->json_output) { + if (dl->json_output) jsonw_u64_field(dl->jw, name, val); - } else { - if (g_indent_newline) - pr_out("%s %"PRIu64, name, val); - else - pr_out(" %s %"PRIu64, name, val); - } + else + pr_out("%s %"PRIu64, name, val); } static void pr_out_bool_value(struct dl *dl, bool value) @@ -5835,14 +5832,12 @@ static void pr_out_region_handle_end(struct dl *dl) static void pr_out_region_snapshots_start(struct dl *dl, bool array) { + __pr_out_indent_newline(dl); if (dl->json_output) { jsonw_name(dl->jw, "snapshot"); jsonw_start_array(dl->jw); } else { - if (g_indent_newline) - pr_out("snapshot %s", array ? "[" : ""); - else - pr_out(" snapshot %s", array ? "[" : ""); + pr_out("snapshot %s", array ? "[" : ""); } } -- 1.8.3.1