-
Notifications
You must be signed in to change notification settings - Fork 1
/
log.bash
150 lines (128 loc) · 3.04 KB
/
log.bash
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/usr/bib/env bash
log__debug=0
log__colors=-1
log::detect_colors() {
if [ -t 1 ];then
log__colors=1
else
log__colors=0
fi
}
log::enable_debug() {
log__debug=1
}
log::disable_debug() {
log__debug=0
}
log::disable_colors() {
log__colors=0
log::debug "Colors forcefuly disabled"
}
log::enable_colors() {
log__colors=1
log::debug "Colors forcefuly enabled"
}
log::debug() {
local pid=$BASHPID
if [ "$log__debug" = "1" ] || [ "$DEBUG" = "1" ];then
if [ "$log__colors" = "1" ];then
echo -e "$(tput el)\e[90m$(printf "%-6s %-30s" "$pid" "${FUNCNAME[1]}"): $*\e[0m" >&2
elif [ "$log__colors" = "-1" ];then
log::detect_colors
log::debug "$@"
else
echo -e "[DEBUG ] $(printf "%-6s %-30s" "$pid" "${FUNCNAME[1]}"): $*" >&2
fi
fi
}
log::panic() {
log::error "Panic: $*"
stack::print 1 1
}
log::fatal() {
log::error "$*"
wait
exit 1
}
log::success() {
if [ "$log__colors" = "1" ];then
echo -e "$(tput el)\e[32m$*\e[0m" >&2
elif [ "$log__colors" = "-1" ];then
log::detect_colors
log::success "$@"
else
echo -e "[SUCCESS] $*" >&2
fi
}
log::info() {
if [ "$log__colors" = "1" ];then
echo -e "$(tput el)\e[36m$*\e[0m" >&2
elif [ "$log__colors" = "-1" ];then
log::detect_colors
log::info "$@"
else
echo -e "[INFO ] $*" >&2
fi
}
log::error() {
if [ "$log__colors" = "1" ];then
echo -e "$(tput el)\e[31m$*\e[0m" >&2
elif [ "$log__colors" = "-1" ];then
log::detect_colors
log::error "$@"
else
echo -e "[ERROR ] $*" >&2
fi
}
log::warn() {
if [ "$log__colors" = "1" ];then
echo -e "$(tput el)\e[33m$*\e[0m" >&2
elif [ "$log__colors" = "-1" ];then
log::detect_colors
log::warn "$@"
else
echo -e "[WARN ] $*" >&2
fi
}
log::progress_bar() {
local done=$1
local total=${2:-100}
if [ "$total" = "0" ];then
log::debug "Total cannot be 0!"
total=1
fi
if [ "$done" -gt "$total" ];then
log::debug "Requested progress $done from $total"
done="$total"
fi
cols="$(tput cols)"
local filled="$((done * (cols - 6) / total))"
local percent="$((done * 100 / total))"
local a
local bar
bar="$(printf "%4s " "$percent%")"
bar+="$(echo -e "\e[42m")"
for ((a=0;a<filled;a++));do
bar+=" "
done
bar+="$(echo -e "\e[49m")"
TRUSTED_CONTENT=1 log::status "$bar"
}
log::status() {
local line=$1
if [ "$DEBUG" = "1" ] || [ "$log__colors" != "1" ];then
return
fi
local cols
cols="$(tput cols)"
if [ "$TRUSTED_CONTENT" != "1" ];then
line=$(echo -n "$line" | tr -d '[:cntrl:]' | cut -c "-$cols")
fi
local civis cuu el cr cnorm
civis="$(tput civis)"
cuu="$(tput cuu1)"
el="$(tput el)"
cr="$(tput cr)"
cnorm="$(tput cnorm)"
echo -n "${civis}"$'\n'"${cr}${el}${line}${cuu}${cr}${el}${cnorm}" >&2
}