blob: 58fbd7ab7ce3eb0d2b306c46f93b61b0c304f40e (
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
shopt -s nullglob
tput() {
# If tput fails, it just means less colors
command tput "$@" 2> /dev/null || true
}
NORMAL="$(tput sgr0)"
# shellcheck disable=SC2034
readonly NORMAL
BOLD="$(tput bold)"
# shellcheck disable=SC2034
readonly BOLD
RED="$(tput setaf 1)"
# shellcheck disable=SC2034
readonly RED
GREEN="$(tput setaf 2)"
# shellcheck disable=SC2034
readonly GREEN
YELLOW="$(tput setaf 3)"
# shellcheck disable=SC2034
readonly YELLOW
BLUE="$(tput setaf 4)"
# shellcheck disable=SC2034
readonly BLUE
PURPLE="$(tput setaf 5)"
# shellcheck disable=SC2034
readonly PURPLE
CYAN="$(tput setaf 6)"
# shellcheck disable=SC2034
readonly CYAN
WHITE="$(tput setaf 7)"
# shellcheck disable=SC2034
readonly WHITE
readonly BASH_LIB_LOG_VAR="${BASH_LIB_NAME:-BASH}_LOG"
is_debug() {
[ "${!BASH_LIB_LOG_VAR:-0}" -ge 1 ]
}
is_trace() {
[ "${!BASH_LIB_LOG_VAR:-0}" -ge 2 ]
}
declare -a VERBOSE_ARG
if is_debug; then
VERBOSE_ARG=("--verbose")
else
VERBOSE_ARG=()
fi
# shellcheck disable=SC2034
readonly VERBOSE_ARG
echoe() {
IFS=" " echo "$@" >&2
}
log() {
local level="$1"
local color="$2"
local message="$3"
shift 3
local -a rest=("$@")
echoe "${BOLD}${color}${level} ${WHITE}${message}${NORMAL}" "${rest[@]}"
}
trace() {
if is_trace; then
log "Trace: " "${PURPLE}" "$@"
fi
}
debug() {
if is_debug; then
log "Debug: " "${GREEN}" "$@"
fi
}
info() {
log "Info: " "${CYAN}" "$@"
}
warn() {
log "Warning:" "${YELLOW}" "$@"
}
error() {
log "Error: " "${RED}" "$@"
}
critical() {
error "$@"
# Useful for triggering the error trap system
return 1
}
fatal() {
error "$@"
exit 1
}
|