Skip to content

Instantly share code, notes, and snippets.

@jpluscplusm
Created December 3, 2019 21:39
Show Gist options
  • Save jpluscplusm/70583dae7060155c17753effeebb26ff to your computer and use it in GitHub Desktop.
Save jpluscplusm/70583dae7060155c17753effeebb26ff to your computer and use it in GitHub Desktop.
Shell log formatter with increment/decrement indentation thingy
#!/usr/bin/env bash
set -ueo pipefail
outofbandlogstring="This string must NEVER appear in the script's stdout/err ..." # or, like, simply "§" ...
function enter() {
echo "$@"
echo "${outofbandlogstring}+2"
}
function leave() {
echo "${outofbandlogstring}-2"
echo "$@"
}
function func1() {
echo "Some stdout stuff in func1 ..."
echo "Some stderr stuff in func1 ..." >&2
}
function func2() {
enter "Starting func2"
echo "Some stderr stuff in func2 ..." >&2
echo "$(echo "Some uncontrolled stuff in func2")"
echo "Some uncontrolled stuff in func2" >&2
echo "Some stdout stuff in func2 ..."
leave "Leaving func2"
}
function main() {
enter "Starting main ..."
echo "Some stdout stuff in main."
echo "Some stderr stuff in main." >&2
func1
func2
func1
echo "Some stderr stuff in main." >&2
echo "Some stdout stuff in main."
leave "Leaving main"
}
function timestamp_stream() {
awk -F"$outofbandlogstring" 'BEGIN{rep=1}$2{rep+=$2}rep<1{rep=1}!$2{"(date)"|getline d;print d sprintf("%*s",rep,"") $0;close(cmd)}'
}
set -o pipefail
main 2>&1 | timestamp_stream | tee output.$(date +%s).log
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment