#!/usr/bin/env bash

# exec &> >(tee -a "${SYSLOG}")
# exec &> >(tee -a "${APPLOG}")

trap 'exit 1' HUP INT QUIT TERM

# trap 'on_error' EXIT

log::prefix() {
    date=$(/opt/homebrew/bin/gdate '+%Y-%m-%d %H:%M:%S,%3N')
    printf '%s %s\n' "${date}"  "${*}"
}

log::notice() {
    log::info "==> ${*}"
}

log::info() {
    log::prefix "INFO ${*}"
}

log::debug() {
    log::prefix "DEBUG ${*}"
}

log::warning() {
    log::prefix "WARNING ${*}"
}

log::error() {
    log::prefix "ERROR ${*}" >&2
}

log::fatal() {
    reason="${1-Unknown or not specified}"
    log::error "Fatal error. Reason: ${reason}"
    kill -s TERM $$
}

on_error() {
    if [ $? -ne 0 ]; then
        log::fatal "Unhandled script failure"
    fi
}
