Skip to content

Commit

Permalink
feat: Add debug/1 to log debug messages without consuming stdin
Browse files Browse the repository at this point in the history
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
```
  • Loading branch information
yertto committed Sep 3, 2022
1 parent cff5336 commit 0273615
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
11 changes: 11 additions & 0 deletions docs/content/manual/manual.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2984,6 +2984,17 @@ sections:
`["DEBUG:", <input-value>]` and prints that and a newline on
stderr, compactly. This may change in the future.
- title: "`debug(msg)`"
body: |
Calls `debug/0` to print the debug `msg` to stderr, but this
version sends stdin to stdout without consuming it.
examples:
- program: 'debug("here1") | . + 1'
input: '1'
output: ['2']

- title: "`stderr`"
body: |
Expand Down
1 change: 1 addition & 0 deletions src/builtin.jq
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
def halt_error: halt_error(5);
def error(msg): msg|error;
def debug(msg): . as $dot | msg | debug | $dot;
def map(f): [.[] | f];
def select(f): if f then . else empty end;
def sort_by(f): _sort_by_impl(map([f]));
Expand Down

0 comments on commit 0273615

Please sign in to comment.