add stop_logging_output function and test
This commit is contained in:
parent
4b34f7220f
commit
6738fc6653
22
README.md
22
README.md
|
@ -1040,23 +1040,37 @@ printf '' >file
|
|||
```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 message.\n'
|
||||
printf 'This is an error message.\n' >&2
|
||||
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 message.\n'
|
||||
printf 'This is an error message.\n' >&2
|
||||
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
|
||||
|
|
|
@ -173,23 +173,37 @@ printf '' >file
|
|||
```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 message.\n'
|
||||
printf 'This is an error message.\n' >&2
|
||||
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 message.\n'
|
||||
printf 'This is an error message.\n' >&2
|
||||
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
|
||||
|
|
Loading…
Reference in New Issue