2015-05-11 12:47:02 +02:00
|
|
|
#!/bin/bash
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; version 2 of the License.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
|
|
|
|
error_function() {
|
|
|
|
if [[ -p $logpipe ]]; then
|
|
|
|
rm "$logpipe"
|
|
|
|
fi
|
|
|
|
# first exit all subshells, then print the error
|
|
|
|
if (( ! BASH_SUBSHELL )); then
|
|
|
|
error "A failure occurred in %s()." "$1"
|
|
|
|
plain "Aborting..."
|
|
|
|
fi
|
|
|
|
exit 2
|
|
|
|
}
|
|
|
|
|
2016-02-15 16:31:38 +01:00
|
|
|
# $1: function
|
|
|
|
run_log(){
|
2016-02-23 22:00:59 +01:00
|
|
|
local func="$1"
|
2016-02-23 18:23:28 +01:00
|
|
|
if ${is_log};then
|
2016-02-24 22:06:34 +01:00
|
|
|
local tmpfile=/tmp/$(gen_iso_fn).$func.log logfile=${iso_dir}/$func.log
|
2016-02-23 18:23:28 +01:00
|
|
|
logpipe=$(mktemp -u "/tmp/logpipe.XXXXXXXX")
|
|
|
|
mkfifo "$logpipe"
|
2016-02-24 22:06:34 +01:00
|
|
|
tee "$tmpfile" < "$logpipe" &
|
2016-02-23 18:23:28 +01:00
|
|
|
local teepid=$!
|
2016-02-23 22:00:59 +01:00
|
|
|
$func &> "$logpipe"
|
2016-02-23 18:23:28 +01:00
|
|
|
wait $teepid
|
|
|
|
rm "$logpipe"
|
2016-02-24 22:06:34 +01:00
|
|
|
cat $tmpfile | perl -pe 's/\e\[?.*?[\@-~]//g' > $logfile
|
|
|
|
rm "$tmpfile"
|
2016-02-23 18:23:28 +01:00
|
|
|
else
|
2016-02-23 22:00:59 +01:00
|
|
|
"$func"
|
2016-02-23 18:23:28 +01:00
|
|
|
fi
|
2016-02-15 16:31:38 +01:00
|
|
|
}
|
|
|
|
|
2015-05-11 12:47:02 +02:00
|
|
|
run_safe() {
|
2016-02-23 22:00:59 +01:00
|
|
|
local restoretrap func="$1"
|
2015-05-11 12:47:02 +02:00
|
|
|
set -e
|
|
|
|
set -E
|
|
|
|
restoretrap=$(trap -p ERR)
|
2016-02-23 22:00:59 +01:00
|
|
|
trap 'error_function $func' ERR
|
|
|
|
run_log "$func"
|
2015-05-11 12:47:02 +02:00
|
|
|
eval $restoretrap
|
|
|
|
set +E
|
|
|
|
set +e
|
|
|
|
}
|