Last active
September 19, 2018 09:16
-
-
Save antonagestam/c528db4c8ff14e202bc56531f68f3948 to your computer and use it in GitHub Desktop.
helper functions for formatting log output
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| # The logging functions all take input both as arguments and as stdin so that | |
| # logging can be piped from other commands. The commands use this themselves: | |
| # `log_sub` pipes into `log` which pipes into `prefix`. | |
| prefix () { | |
| local replace="s/^/$1/" | |
| if (( $# > 1 )); then | |
| echo "${@:2}" | sed "$replace" | |
| else | |
| tee | sed "$replace" | |
| fi | |
| } | |
| log () { | |
| local date_fmt='--iso-8601=seconds' | |
| if (( $# > 0 )); then | |
| echo "$*" | prefix "[$(date ${date_fmt})] " | |
| else | |
| tee | prefix "[$(date ${date_fmt})] " | |
| fi | |
| } | |
| log_sub () { | |
| local p=" ---> " | |
| if (( $# > 0 )); then | |
| echo "$*" | prefix "$p" | log | |
| else | |
| tee | prefix "$p" | log | |
| fi | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment