lint/lib/log.sh

54 lines
1.5 KiB
Bash
Raw Normal View History

2020-07-27 17:11:33 -04:00
#!/usr/bin/env bash
declare -Agr B=(
2020-07-30 16:39:05 -04:00
[B]=$(echo -e "\e[44m")
[C]=$(echo -e "\e[46m")
[G]=$(echo -e "\e[42m")
[K]=$(echo -e "\e[40m")
[M]=$(echo -e "\e[45m")
[R]=$(echo -e "\e[41m")
[W]=$(echo -e "\e[47m")
[Y]=$(echo -e "\e[43m")
2020-07-27 17:11:33 -04:00
)
declare -Agr F=(
2020-07-30 16:39:05 -04:00
[B]=$(echo -e "\e[0;34m")
[C]=$(echo -e "\e[0;36m")
[G]=$(echo -e "\e[0;32m")
[K]=$(echo -e "\e[0;30m")
[M]=$(echo -e "\e[0;35m")
[R]=$(echo -e "\e[0;31m")
[W]=$(echo -e "\e[0;37m")
[Y]=$(echo -e "\e[0;33m")
2020-07-27 17:11:33 -04:00
)
readonly NC=$(echo -e "\e[0m")
export B
export F
export NC
# Log Functions
LOG_TEMP=$(mktemp) || echo "Failed to create temporary log file."
export LOG_TEMP
2020-11-06 17:10:09 -05:00
echo "super-linter Log" >"${LOG_TEMP}"
2020-07-27 17:11:33 -04:00
log() {
2020-07-30 16:39:05 -04:00
local TOTERM=${1:-}
local MESSAGE=${2:-}
echo -e "${MESSAGE:-}" | (
if [[ -n ${TOTERM} ]]; then
tee -a "${LOG_TEMP}" >&2
else
2020-11-06 17:10:09 -05:00
cat >>"${LOG_TEMP}" 2>&1
2020-07-30 16:39:05 -04:00
fi
)
2020-07-27 17:11:33 -04:00
}
2020-10-28 20:08:01 -04:00
trace() { log "${LOG_TRACE:-}" "${NC}$(date +"%F %T") ${F[B]}[TRACE]${NC} $*${NC}"; }
debug() { log "${LOG_DEBUG:-}" "${NC}$(date +"%F %T") ${F[B]}[DEBUG]${NC} $*${NC}"; }
info() { log "${LOG_VERBOSE:-}" "${NC}$(date +"%F %T") ${F[B]}[INFO]${NC} $*${NC}"; }
notice() { log "${LOG_NOTICE:-}" "${NC}$(date +"%F %T") ${F[G]}[NOTICE]${NC} $*${NC}"; }
warn() { log "${LOG_WARN:-}" "${NC}$(date +"%F %T") ${F[Y]}[WARN]${NC} $*${NC}"; }
error() { log "${LOG_ERROR:-}" "${NC}$(date +"%F %T") ${F[R]}[ERROR]${NC} $*${NC}"; }
2020-07-27 17:11:33 -04:00
fatal() {
2020-10-28 20:08:01 -04:00
log "true" "${NC}$(date +"%F %T") ${B[R]}${F[W]}[FATAL]${NC} $*${NC}"
2020-07-30 16:39:05 -04:00
exit 1
2020-07-27 17:11:33 -04:00
}