# -*-Shell-script-*-
# 
# logflow: functions for recording script call flow
#
# Copyright (c) 2016 by Cisco Systems, Inc.
# All rights reserved.
#
#
function unlogflow {
    set +x
    if [ -n "$logflow_pid" ]; then
        exec 2>&109-
        kill -HUP $logflow_pid
        while [ -e /proc/$logflow_pid ]; do :; done
        logflow_pid=""
    fi
}
readonly -f unlogflow

function logflow {
    set +x
    unlogflow
    exec 109>&2
    exec 2> >(exec cat >> ${1})
    logflow_pid=$!
    echo "Logging started at $(date)..."
    set -x
}
readonly -f logflow

function unlogflowall {
    set +x
    local selfid=$$
    ls /proc/$selfid/fd 2>/dev/null | grep 107 >/dev/null 2>&1 && exec >&107-
    ls /proc/$selfid/fd 2>/dev/null | grep 100 >/dev/null 2>&1 && exec 1>&100-
    ls /proc/$selfid/fd 2>/dev/null | grep 101 >/dev/null 2>&1 && exec 2>&101-
}
readonly -f unlogflowall

function logflowall {
    set +x
    unlogflowall
    exec 100>&1
    exec 101>&2
    mkdir -p /var/log/bootlogs
    exec 107>/var/log/bootlogs/`basename $0`.$$.log
    exec >/var/log/bootlogs/`basename $0`.flow.$$.log 2>&1
    echo "Logging flow  started at $(date)..."
    echo "Loggin started at $(date)..." >&107
    set -x
}
readonly -f logflowall

#trap 'echo "--TRAP--${LINENO}-`basename $0`" >&2 ' ERR
