From patchwork Tue Dec 29 23:04:11 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Leonardo_M=C3=B6rlein?= X-Patchwork-Id: 1421262 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.openwrt.org (client-ip=2001:8b0:10b:1231::1; helo=merlin.infradead.org; envelope-from=openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=irrelefant.net Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=merlin.20170209 header.b=weIvHNk0; dkim-atps=neutral Received: from merlin.infradead.org (merlin.infradead.org [IPv6:2001:8b0:10b:1231::1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4D596M6Rl2z9sVs for ; Wed, 30 Dec 2020 10:07:07 +1100 (AEDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:MIME-Version:Message-Id:Date:Subject:To:From: Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender :Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References:List-Owner; bh=/p0vSnBbyyv2Os4XPjTVVuK1OFQ522GNqfNLR1Dg3IQ=; b=weIvHNk0+/xh6NGIjWIVrl+n1K oUIzNTI7v8WLDbTooa6r9zSmqy99QGgYkmr+oCzBDKw0q0ryjOWx9zxP4N0cI0oMKzofdKnZLxaG9 BoP5e22wkXG4CH0WN9naIbzJ6S0KSoxE4JXvNVipTGtbtHcq41dNvmzoq+iO473kmMqVTjeGpfcVU PukeYXJi8nXtLhhLAX3FT9ui3DxV1qY2bekRva55oVUoR/yuuidL3Kp/P/iAQglzOUCnEoaF5AQ0x Di31NpdkAXXJ5WqVJKVyXcU2MJsuqVcOSF57LJ9zeay0HgLFGQKXgsejJfGJGHGgutJYR6CSDbrYp Q/5oJxpg==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1kuO2s-00022d-O4; Tue, 29 Dec 2020 23:04:50 +0000 Received: from smtprelay03.ispgateway.de ([80.67.18.15]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1kuO2o-000220-FF for openwrt-devel@lists.openwrt.org; Tue, 29 Dec 2020 23:04:47 +0000 Received: from [81.3.6.94] (helo=orange.ffh.zone) by smtprelay03.ispgateway.de with esmtpsa (TLSv1.2:ECDHE-RSA-AES128-GCM-SHA256:128) (Exim 4.92.3) (envelope-from ) id 1kuO2h-0000ny-9P; Wed, 30 Dec 2020 00:04:39 +0100 From: =?utf-8?q?Leonardo_M=C3=B6rlein?= To: openwrt-devel@lists.openwrt.org Subject: [PATCH libubox] sh/jshn.sh: Add JSHN_DEBUG flag Date: Wed, 30 Dec 2020 00:04:11 +0100 Message-Id: <20201229230411.161748-1-me@irrelefant.net> X-Mailer: git-send-email 2.29.2 MIME-Version: 1.0 X-Df-Sender: bWVAaXJyZWxlZmFudC5uZXQ= X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20201229_180446_531058_9FEF762B X-CRM114-Status: GOOD ( 14.28 ) X-Spam-Score: -0.0 (/) X-Spam-Report: SpamAssassin version 3.4.4 on merlin.infradead.org summary: Content analysis details: (-0.0 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_MSPIKE_H2 RBL: Average reputation (+2) [80.67.18.15 listed in wl.mailspike.net] -0.0 SPF_HELO_PASS SPF: HELO matches SPF record 0.0 SPF_NONE SPF: sender does not publish an SPF Record X-BeenThere: openwrt-devel@lists.openwrt.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: OpenWrt Development List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: =?utf-8?q?Leonardo_M=C3=B6rlein?= , Felix Fietkau Sender: "openwrt-devel" Errors-To: openwrt-devel-bounces+incoming=patchwork.ozlabs.org@lists.openwrt.org This flag can be set to JSHN_DEBUG=1 to print all calls to json_* to stderr. The variable JSHN_DEBUG_PREFIX can be used optionally to add a string in front of every debug print. The json_* commands internally use other json_* commands. As it is not desired that this inner commands are also printed, a variable JSHN_DEBUG_DEPTH is used. It is initialized to JSHN_DEBUG_DEPTH=0, increased on any json_* call and decreased when leaving the json_* call. Debug prints are only fired if JSHN_DEBUG_DEPTH=0. Other scripts may abuse this variable to their own needs, if they do it carefully. ---snip--- JSHN_DEBUG=1 JSHN_DEBUG_PREFIX="json_debug: " json_init json_add_string "foo" "bar" json_add_string "val" "12" json_dump ---stderr--- json_debug: json_init json_debug: json_add_string foo bar json_debug: json_add_string val 12 json_debug: json_dump ---stdout--- { "foo": "bar", "val": "12" } ---snap--- This is a preparatory commit to eventually allow debugging netifd protos in a better way. Signed-off-by: Leonardo Mörlein --- sh/jshn.sh | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) diff --git a/sh/jshn.sh b/sh/jshn.sh index 7b0155d..50ec96f 100644 --- a/sh/jshn.sh +++ b/sh/jshn.sh @@ -1,5 +1,18 @@ # functions for parsing and generating json +JSHN_DEBUG_DEPTH=0 + +_json_debug() { + JSHN_DEBUG_DEPTH="$((JSHN_DEBUG_DEPTH+1))" + if [ -n "$JSHN_DEBUG" ] && [ "$JSHN_DEBUG" -eq 1 ] && [ "$JSHN_DEBUG_DEPTH" -eq 1 ]; then + echo "${JSHN_DEBUG_PREFIX}$*" 1>&2 + fi +} + +_json_debug_end() { + JSHN_DEBUG_DEPTH="$((JSHN_DEBUG_DEPTH-1))" +} + _json_get_var() { # dest=$1 # var=$2 @@ -93,14 +106,17 @@ _json_close_table() { } json_set_namespace() { + _json_debug json_set_namespace "$@" local _new="$1" local _old="$2" [ -n "$_old" ] && _set_var "$_old" "$JSON_PREFIX" JSON_PREFIX="$_new" + _json_debug_end } json_cleanup() { + _json_debug json_cleanup "$@" local unset tmp _json_get_var unset JSON_UNSET @@ -118,86 +134,116 @@ json_cleanup() { ${JSON_PREFIX}JSON_SEQ \ ${JSON_PREFIX}JSON_CUR \ ${JSON_PREFIX}JSON_UNSET + _json_debug_end } json_init() { + _json_debug json_init "$@" json_cleanup export -n ${JSON_PREFIX}JSON_SEQ=0 export -- \ ${JSON_PREFIX}JSON_CUR="J_V" \ ${JSON_PREFIX}K_J_V= + _json_debug_end } json_add_object() { + _json_debug json_add_object "$@" _json_add_table "$1" object T + _json_debug_end } json_close_object() { + _json_debug json_close_object "$@" _json_close_table + _json_debug_end } json_add_array() { + _json_debug json_add_array "$@" _json_add_table "$1" array A + _json_debug_end } json_close_array() { + _json_debug json_close_array "$@" _json_close_table + _json_debug_end } json_add_string() { + _json_debug json_add_string "$@" local cur _json_get_var cur JSON_CUR _json_add_generic string "$1" "$2" "$cur" + _json_debug_end } json_add_int() { + _json_debug json_add_int "$@" local cur _json_get_var cur JSON_CUR _json_add_generic int "$1" "$2" "$cur" + _json_debug_end } json_add_boolean() { + _json_debug json_add_boolean "$@" local cur _json_get_var cur JSON_CUR _json_add_generic boolean "$1" "$2" "$cur" + _json_debug_end } json_add_double() { + _json_debug json_add_double "$@" local cur _json_get_var cur JSON_CUR _json_add_generic double "$1" "$2" "$cur" + _json_debug_end } json_add_null() { + _json_debug json_add_null "$@" local cur _json_get_var cur JSON_CUR _json_add_generic null "$1" "" "$cur" + _json_debug_end } # functions read access to json variables json_load() { + _json_debug json_load "$@" eval "`jshn -r "$1"`" + _json_debug_end } json_load_file() { + _json_debug json_load_file "$@" eval "`jshn -R "$1"`" + _json_debug_end } json_dump() { - jshn "$@" ${JSON_PREFIX:+-p "$JSON_PREFIX"} -w + _json_debug json_dump "$@" + jshn "$@" ${JSON_PREFIX:+-p "$JSON_PREFIX"} -w + _json_debug_end } json_get_type() { + _json_debug json_get_type "$@" local __dest="$1" local __cur _json_get_var __cur JSON_CUR local __var="${JSON_PREFIX}T_${__cur}_${2//[^a-zA-Z0-9_]/_}" eval "export -- \"$__dest=\${$__var}\"; [ -n \"\${$__var+x}\" ]" + _json_debug_end } json_get_keys() { + _json_debug json_get_keys "$@" local __dest="$1" local _tbl_cur @@ -208,9 +254,11 @@ json_get_keys() { fi local __var="${JSON_PREFIX}K_${_tbl_cur}" eval "export -- \"$__dest=\${$__var}\"; [ -n \"\${$__var+x}\" ]" + _json_debug_end } json_get_values() { + _json_debug json_get_values "$@" local _v_dest="$1" local _v_keys _v_val _select= local _json_no_warning=1 @@ -230,19 +278,23 @@ json_get_values() { done [ -n "$_select" ] && json_select .. + _json_debug_end return 0 } json_get_var() { + _json_debug json_get_var "$@" local __dest="$1" local __cur _json_get_var __cur JSON_CUR local __var="${JSON_PREFIX}${__cur}_${2//[^a-zA-Z0-9_]/_}" eval "export -- \"$__dest=\${$__var:-$3}\"; [ -n \"\${$__var+x}\${3+x}\" ]" + _json_debug_end } json_get_vars() { + _json_debug json_get_vars "$@" while [ "$#" -gt 0 ]; do local _var="$1"; shift if [ "$_var" != "${_var#*:}" ]; then @@ -251,21 +303,25 @@ json_get_vars() { json_get_var "$_var" "$_var" fi done + _json_debug_end } json_select() { + _json_debug json_select "$@" local target="$1" local type local cur [ -z "$1" ] && { _json_set_var JSON_CUR "J_V" + _json_debug_end return 0 } [[ "$1" == ".." ]] && { _json_get_var cur JSON_CUR _json_get_var cur "U_$cur" _json_set_var JSON_CUR "$cur" + _json_debug_end return 0 } json_get_type type "$target" @@ -277,19 +333,24 @@ json_select() { *) [ -n "$_json_no_warning" ] || \ echo "WARNING: Variable '$target' does not exist or is not an array/object" + _json_debug_end return 1 ;; esac + _json_debug_end } json_is_a() { + _json_debug json_is_a "$@" local type json_get_type type "$1" [ "$type" = "$2" ] + _json_debug_end } json_for_each_item() { + _json_debug json_for_each_item "$@" [ "$#" -ge 2 ] || return 0 local function="$1"; shift local target="$1"; shift @@ -312,4 +373,5 @@ json_for_each_item() { eval "$function \"\$val\" \"\" \"\$@\"" ;; esac + _json_debug_end }