Skip to content

Commit 07ef97c

Browse files
authored
debug/1 (#2710)
* debug/1 This def ensures the output of debug(m1,m2) is kept together. Closes #2709 #2111 #2112
1 parent 4b1ac7c commit 07ef97c

File tree

3 files changed

+75
-7
lines changed

3 files changed

+75
-7
lines changed

docs/content/manual/manual.yml

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3075,13 +3075,32 @@ sections:
30753075
to invoke jq with the -n command-line option, otherwise
30763076
the first entity will be lost.
30773077
3078-
- title: "`debug`"
3078+
- title: "`debug`, `debug(msgs)`"
30793079
body: |
30803080
3081-
Causes a debug message based on the input value to be
3082-
produced. The jq executable wraps the input value with
3083-
`["DEBUG:", <input-value>]` and prints that and a newline on
3084-
stderr, compactly. This may change in the future.
3081+
These two filters are like `.` but have as a side-effect the
3082+
production of one or more messages on stderr.
3083+
3084+
The message produced by the `debug` filter has the form
3085+
3086+
["DEBUG:",<input-value>]
3087+
3088+
where `<input-value>` is a compact rendition of the input
3089+
value. This format may change in the future.
3090+
3091+
The `debug(msgs)` filter is defined as `(msgs | debug | empty), .`
3092+
thus allowing great flexibility in the content of the message,
3093+
while also allowing multi-line debugging statements to be created.
3094+
3095+
For example, the expression:
3096+
3097+
1 as $x | 2 | debug("Entering function foo with $x == \($x)", .) | (.+1)
3098+
3099+
would produce the value 3 but with the following two lines
3100+
being written to stderr:
3101+
3102+
["DEBUG:","Entering function foo with $x == 1"]
3103+
["DEBUG:",2]
30853104
30863105
- title: "`stderr`"
30873106
body: |

jq.1.prebuilt

Lines changed: 48 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/builtin.jq

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,9 @@ def pick(pathexps):
266266
| reduce path(pathexps) as $a (null;
267267
setpath($a; $in|getpath($a)) );
268268

269+
# ensure the output of debug(m1,m2) is kept together:
270+
def debug(msgs): (msgs | debug | empty), .;
271+
269272
# SQL-ish operators here:
270273
def INDEX(stream; idx_expr):
271274
reduce stream as $row ({}; .[$row|idx_expr|tostring] = $row);

0 commit comments

Comments
 (0)