-
Notifications
You must be signed in to change notification settings - Fork 1
/
logging.sh
169 lines (152 loc) · 3.38 KB
/
logging.sh
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/bin/bash
if [ -n "${LOGGING_DEFINED}" ]; then
return
else
export LOGGING_DEFINED="yes"
fi
BASE_DIR="$(dirname "${BASH_SOURCE}")"
. "${BASE_DIR}/colors.sh"
# Debug logging
TRACE=4
DEBUG=3
INFO=2
WARN=1
ERROR=0
LOG_LEVEL="${LOG_LEVEL:-${INFO}}"
_LOG_TARGETS=( STDERR )
trace(){ [ ${LOG_LEVEL} -ge ${TRACE} ]; }
debug(){ [ ${LOG_LEVEL} -ge ${DEBUG} ]; }
info(){ [ ${LOG_LEVEL} -ge ${INFO} ]; }
warn(){ [ ${LOG_LEVEL} -ge ${WARN} ]; }
error(){ [ ${LOG_LEVEL} -ge ${ERROR} ]; }
trace_do(){ trace && eval "${@}" >&3 || return 0; }
debug_do(){ debug && eval "${@}" >&3 || return 0; }
info_do(){ info && eval "${@}" >&3 || return 0; }
warn_do(){ warn && eval "${@}" >&3 || return 0; }
error_do(){ error && eval "${@}" >&3 || return 0; }
escape(){ [ -n "${*}" ] && printf '%q ' "${@}"; }
trace_log(){
trace || return
if [ $# -eq 0 ]; then
while IFS= read _LINE; do
_echo -e "${BOLD_WHITE}[TRACE] ${BOLD_BLACK}${BASH_SOURCE[1]}:${FUNCNAME[1]}:${BASH_LINENO[0]}: ${_LINE}${RESET_COLOR}"
done
return
fi
_echo -e "${BOLD_WHITE}[TRACE] ${BOLD_BLACK}${BASH_SOURCE[1]}:${FUNCNAME[1]}:${BASH_LINENO[0]}: ${@}${RESET_COLOR}"
}
debug_log(){
debug || return
if [ $# -eq 0 ]; then
while IFS= read _LINE; do
_echo -e "${BOLD_WHITE}[DEBUG] ${BOLD_BLUE}${BASH_SOURCE[1]}:${FUNCNAME[1]}:${BASH_LINENO[0]}: ${_LINE}${RESET_COLOR}"
done
return
fi
_echo -e "${BOLD_WHITE}[DEBUG] ${BOLD_BLUE}${BASH_SOURCE[1]}:${FUNCNAME[1]}:${BASH_LINENO[0]}: ${@}${RESET_COLOR}"
}
info_log(){
info || return
if [ $# -eq 0 ]; then
while IFS= read _LINE; do
_echo -e "${BOLD_WHITE}[INFO ] ${RESET_COLOR}${_LINE}${RESET_COLOR}"
done
return
fi
_echo -e "${BOLD_WHITE}[INFO ] ${RESET_COLOR}${@}${RESET_COLOR}"
}
warn_log(){
warn || return
if [ $# -eq 0 ]; then
while IFS= read _LINE; do
_echo -e "${BOLD_WHITE}[WARN ] ${BOLD_YELLOW}${_LINE}${RESET_COLOR}"
done
return
fi
_echo -e "${BOLD_WHITE}[WARN ] ${BOLD_YELLOW}${@}${RESET_COLOR}"
}
error_log(){
error || return
if [ $# -eq 0 ]; then
while IFS= read _LINE; do
_echo -e "${BOLD_WHITE}[ERROR] ${BOLD_WHITE_RED}${_LINE}${RESET_COLOR}"
done
return
fi
_echo -e "${BOLD_WHITE}[ERROR] ${BOLD_WHITE_RED}${@}${RESET_COLOR}"
}
log(){
case "${1}" in
${TRACE})
shift
trace_log "${*}"
;;
${DEBUG})
shift
debug_log "${*}"
;;
${INFO})
shift
info_log "${*}"
;;
${WARN})
shift
warn_log "${*}"
;;
${ERROR})
shift
error_log "${*}"
;;
*)
error_log "Unknown log level \"${*}\""
shift
error_log "${*}"
;;
esac
}
value_of() {
while [ $# -ge 1 ]; do
echo "${1}=\"${!1}\""
shift
done
}
print_stack_trace() {
local i=0
local FRAMES=${#BASH_LINENO[@]}
# FRAMES-2 skips main, the last one in arrays
echo 'Stack Trace:'
for ((i=FRAMES-2; i>=0; i--)); do
echo ' File' \"${BASH_SOURCE[i+1]}\", line ${BASH_LINENO[i]}, in ${FUNCNAME[i+1]}
# Grab the source code of the line
sed -n "${BASH_LINENO[i]}{s/^/ /;p}" "${BASH_SOURCE[i+1]}"
# It requires `shopt -s extdebug'
done
echo
}
enable_syslog() {
add_log_target SYSLOG
}
_echo() {
for TARGET in ${_LOG_TARGETS[@]}; do
case "${TARGET}" in
"STDOUT")
echo "${@}"
;;
"STDERR")
echo "${@}" >&2
;;
"SYSLOG")
echo "${@}" > >(logger -t "$(basename "${0}")")
;;
*)
echo "${@}" >>"${TARGET}"
;;
esac
done
}
add_log_target() {
while [ $# -ge 1 ]; do
_LOG_TARGETS[${#_LOG_TARGETS[*]}]="${1}"
shift
done
}