Skip to content

Instantly share code, notes, and snippets.

@mpapis
Created July 22, 2011 10:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mpapis/1099240 to your computer and use it in GitHub Desktop.
Save mpapis/1099240 to your computer and use it in GitHub Desktop.
log with levels
#!/usr/bin/env bash
export LEVEL=0
function log() {
printf "%$(( LEVEL * 2 ))s$*\n" ""
}
function fail() {
local ret=$1
shift
printf "FAIL: $*\n"
exit $ret
}
function cf()
{
"$@"
local ret=$?
if (( $ret == 0 ))
then
return $ret
else
fail $ret "$@"
fi
}
function go()
{
: $(( LEVEL++ ))
"$@"
local ret=$?
: $(( LEVEL-- ))
return $ret
}
test2()
{
log 'a 2nd level'
go test3 x
log 'b 2nd level'
cf go test3 y
log 'c 2nd level'
}
test3()
{
log "$1 a 3nd level"
log "$1 b 3nd level"
return 1
}
log '1st level'
go test2
log 'finish'
1st level
a 2nd level
x a 3nd level
x b 3nd level
b 2nd level
y a 3nd level
y b 3nd level
FAIL: go test3 y
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment