added check to logfile

This commit is contained in:
alanw 2023-05-10 10:07:09 -07:00
parent 14e4a6139d
commit 86c10738ce

View file

@ -410,20 +410,24 @@ run-and-log(){
}
logger(){
if [ ! -f $logfile ] ; then
run mkdir -p ${rundir%/*}
touch $logfile
if [ ! -z $logfile ] ; then
if [ ! -f $logfile ] ; then
run mkdir -p ${rundir%/*}
touch $logfile
fi
$@ 2>&1 | tee >(
while IFS= read -r line; do
# strip any escaped strings for logging output
_line=$(echo $line | sed "s,\x1B\[[0-9;]*[a-zA-Z],,g" | tr -dc '[:print:]')
# only print out non-empty lines to log
if [[ "$_line" != "" ]] ; then
printf '[%s] %s\n' "${scriptname}][${pretty_date}" "$_line"
fi
done >> $logfile
)
else
$@
fi
$@ 2>&1 | tee >(
while IFS= read -r line; do
# strip any escaped strings for logging output
_line=$(echo $line | sed "s,\x1B\[[0-9;]*[a-zA-Z],,g" | tr -dc '[:print:]')
# only print out non-empty lines to log
if [[ "$_line" != "" ]] ; then
printf '[%s] %s\n' "${scriptname}][${pretty_date}" "$_line"
fi
done >> $logfile
)
}
# File handling functions