Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Add log_all_output #84

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1031,6 +1031,48 @@ echo -n >file
printf '' >file
```

## Use `exec` to redirect script output to different file descriptors

`exec` can be used to redirect script output to file descriptors. In the example below, `stdout` and `stderr` are redirected to `myoutput.log` (a file).

**Example Function:**

```sh
log_all_output() {
# Usage: log_all_output log_filename
exec 6>&1 # save stdout
mnp marked this conversation as resolved.
Show resolved Hide resolved
exec 7>&2 # save stderr
exec &>"$1"
}

stop_logging_output() {
exec 1>&6 6>&- # Restore stdout
exec 2>&7 7>&- # Restore stderr
}
```

**Example Usage:**

```shell
# These are *not* logged.
printf 'This is a normal message.\n'
mnp marked this conversation as resolved.
Show resolved Hide resolved
printf 'This is a normal error message.\n' >&2

# All script output will go to 'myoutput.log'.
log_all_output myoutput.log

# These are now logged.
printf 'This is a logged message.\n'
printf 'This is a logged error message.\n' >&2

# Return to normal.
stop_logging_output

# These are not logged.
printf 'This is a normal message.\n'
printf 'This is a normal error message.\n' >&2
```

## Extract lines between two markers

**Example Function:**
Expand Down
42 changes: 42 additions & 0 deletions manuscript/chapter4.txt
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,48 @@ echo -n >file
printf '' >file
```

## Use `exec` to redirect script output to different file descriptors

`exec` can be used to redirect script output to file descriptors. In the example below, `stdout` and `stderr` are redirected to `myoutput.log` (a file).

**Example Function:**

```sh
log_all_output() {
# Usage: log_all_output log_filename
exec 6>&1 # save stdout
exec 7>&2 # save stderr
exec &>"$1"
}

stop_logging_output() {
exec 1>&6 6>&- # Restore stdout
exec 2>&7 7>&- # Restore stderr
}
```

**Example Usage:**

```shell
# These are *not* logged.
printf 'This is a normal message.\n'
printf 'This is a normal error message.\n' >&2

# All script output will go to 'myoutput.log'.
log_all_output myoutput.log

# These are now logged.
printf 'This is a logged message.\n'
printf 'This is a logged error message.\n' >&2

# Return to normal.
stop_logging_output

# These are not logged.
printf 'This is a normal message.\n'
printf 'This is an normal error message.\n' >&2
```

## Extract lines between two markers

**Example Function:**
Expand Down
15 changes: 14 additions & 1 deletion test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,19 @@ test_split() {
assert_equals "${result[*]}" "hello world my name is john"
}

test_log_all_output() {
(
log_all_output test.log
printf stdout
printf stderr >&2
stop_logging_output
printf stdout
printf stderr >&2
) &> /dev/null
contents="$(<test.log)"
assert_equals "$contents" "stdoutstderr"
}

assert_equals() {
if [[ "$1" == "$2" ]]; then
((pass+=1))
Expand All @@ -219,7 +232,7 @@ assert_equals() {
}

main() {
trap 'rm readme_code test_file' EXIT
trap 'rm readme_code test_file test.log' EXIT

# Extract code blocks from the README.
while IFS=$'\n' read -r line; do
Expand Down