summaryrefslogtreecommitdiff
path: root/lib/log.source.sh
blob: a93a35d92ec812c5867ccfda86c446b8299dce71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# Log a message.
log () {
    local -r level="$1"; shift
    local message

    for message in "$@"; do
        echo "$message"
    done | log::_pipe "$level" $$
}

# Log a stream through a pipe.
log::pipe () {
    log::_pipe "$1" $$
}

# Internal log implementation.
log::_pipe () {
    local -r level="$1"; shift
    local -r pid="$1"; shift

    if [[ "$level" == VERBOSE && -z "$LOG_VERBOSE" ]]; then
        return
    fi

    local -r callee=${FUNCNAME[2]}
    local -r stamp=$($DATE +%Y%m%d-%H%M%S)

    while read -r line; do
        echo "$level|$stamp|$pid|$callee|$line" >&2
    done

    if [ "$level" = PANIC ]; then
        exit 2
    fi
}