Skip to content

Commit 0273615

Browse files
author
yertto
committed
feat: Add debug/1 to log debug messages without consuming stdin
Define `debug/1` to call `debug/0`, but send `stdin` to `stdout` without consuming it, while printing the debug messages to `stderr`. Example usage: ```sh seq 2 | LOG_LEVEL=DEBUG jq ' debug({$ENV}) | debug({line: input_line_number}) | debug("\(now | todate) here1: .=\(.)") | . * 10 | debug("\(now | todate) here2: .=\(.)") | . + 1 ' ``` => ``` ["DEBUG:",{"ENV":{"PWD":"/tmp","SHELL":"/bin/zsh","PATH":"/usr/bin:/bin", ...}}] ["DEBUG:",{"line":1}] ["DEBUG:","2022-09-03T10:52:24Z here1: .=1"] ["DEBUG:","2022-09-03T10:52:24Z here2: .=10"] 11 ["DEBUG:",{"ENV":{"PWD":"/tmp","SHELL":"/bin/zsh","PATH":"/usr/bin:/bin", ...}}] ["DEBUG:",{"line":2}] ["DEBUG:","2022-09-03T10:52:24Z here1: .=2"] ["DEBUG:","2022-09-03T10:52:24Z here2: .=20"] 21 ``` ie. a *slightly* less efficient way of achieving: ```sh seq 2 | jq ' . * 10 | . + 1 ' ``` => ``` 11 21 ```
1 parent cff5336 commit 0273615

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

docs/content/manual/manual.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2984,6 +2984,17 @@ sections:
29842984
`["DEBUG:", <input-value>]` and prints that and a newline on
29852985
stderr, compactly. This may change in the future.
29862986
2987+
- title: "`debug(msg)`"
2988+
body: |
2989+
2990+
Calls `debug/0` to print the debug `msg` to stderr, but this
2991+
version sends stdin to stdout without consuming it.
2992+
2993+
examples:
2994+
- program: 'debug("here1") | . + 1'
2995+
input: '1'
2996+
output: ['2']
2997+
29872998
- title: "`stderr`"
29882999
body: |
29893000

src/builtin.jq

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
def halt_error: halt_error(5);
22
def error(msg): msg|error;
3+
def debug(msg): . as $dot | msg | debug | $dot;
34
def map(f): [.[] | f];
45
def select(f): if f then . else empty end;
56
def sort_by(f): _sort_by_impl(map([f]));

0 commit comments

Comments
 (0)