Skip to content

Commit

Permalink
feat: Define debug/1 in terms of debug/0
Browse files Browse the repository at this point in the history
ie. so it can be used for logging debug messages.

This modifies `debug` to `tee` the `stdin` to `stdout` without consuming it,
while printing the debug messages to `stderr` if `LOG_LEVEL=DEBUG` is set in `$ENV`.

Example usage:
```
seq 2 | LOG_LEVEL=DEBUG jq '
  debug({$ENV})                    |
  debug({line: input_line_number}) |
  debug("here1: .=\(.)")           |
  . * 10                           |
  debug("here2: .=\(.)")           |
  . + 1
'
```
=>
```
["DEBUG:",{"ENV":{"PWD":"/tmp","SHELL":"/bin/zsh","PATH":"/usr/bin:/bin", ...}}]
["DEBUG:",{"line":1}]
["DEBUG:","here1: .=1"]
["DEBUG:","here2: .=10"]
11
["DEBUG:",{"ENV":{"PWD":"/tmp","SHELL":"/bin/zsh","PATH":"/usr/bin:/bin", ...}}]
["DEBUG:",{"line":2}]
["DEBUG:","here1: .=2"]
["DEBUG:","here2: .=20"]
21
```

Or without `LOG_LEVEL=DEBUG`:
```
seq 2 | jq '
  debug({$ENV})                    |
  debug({line: input_line_number}) |
  debug("here1: .=\(.)")           |
  . * 10                           |
  debug("here2: .=\(.)")           |
  . + 1
'
```
=>
```
11
21
```
  • Loading branch information
yertto committed Sep 3, 2022
1 parent cff5336 commit ab5c5b0
Showing 1 changed file with 1 addition and 0 deletions.
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 | if env.LOG_LEVEL == "DEBUG" then msg | debug | $dot else . end;
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 ab5c5b0

Please sign in to comment.