-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_log.sh
executable file
·91 lines (86 loc) · 1.86 KB
/
_log.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
# @bekcpear
# @VARIABLE: LOGLEVEL
# #DEFAULT: 2
# @DESCRIPTION:
# Used to control output level of messages. Should only be setted
# by shell itself.
# 0 -> DEBUG; 1 -> INFO; 2 -> NORMAL; 3 -> WARNNING; 4 -> ERROR
LOGLEVEL=2
# @VARIABLE: FD_STD_OUT
# #DEFAULT: 1
# @DESCRIPTION:
# The STDOUT file descriptor
FD_STD_OUT=1
# @VARIABLE: FD_STD_OUT
# #DEFAULT: 2
# @DESCRIPTION:
# The STDERR file descriptor
FD_STD_ERR=2
# @FUNCTION: _log
# @USAGE: <[dinwe]> <message>
# @INTERNAL
# @DESCRIPTION:
# Echo messages with a unified format.
# 'd' means showing in DEBUG level;
# 'i' means showing in INFO level;
# 'n' means showing in NORMAL level;
# 'w' means showing in WARNNING level;
# 'e' means showing in ERROR level;
# Msg will be printed to the standard output normally
# when this function is called without any option.
function _log() {
local color='\e[0m'
local reset='\e[0m'
local ofd="&${FD_STD_OUT}"
local -i lv=2
if [[ ! ${1} =~ ^[dinwe]+$ ]]; then
echo "UNRECOGNIZED OPTIONS OF INTERNAL <_log> FUNCTION!" >&2
exit 1
fi
case ${1} in
*e*)
lv=4
color='\e[31m'
ofd="&${FD_STD_ERR}"
;;
*w*)
lv=3
color='\e[33m'
ofd="&${FD_STD_ERR}"
;;
*n*)
lv=2
color='\e[36m'
;;
*i*)
lv=1
;;
*d*)
lv=0
;;
esac
if [[ ${lv} -ge ${LOGLEVEL} ]]; then
shift
local prefix=""
local msg="${@}"
if [[ ${lv} != 2 ]]; then
prefix="[$(date '+%Y-%m-%d %H:%M:%S')] " || true
fi
eval ">${ofd} echo -e '${color}${prefix}${msg//\'/\'\\\'\'}${reset}'"
fi
}
# @FUNCTION: _fatal
# @USAGE: <exit-code> <message>
# @INTERNAL
# @DESCRIPTION:
# Print an error message and exit shell.
function _fatal() {
if [[ ${1} =~ ^[[:digit:]]+$ ]]; then
local exit_code=${1}
shift
else
local exit_code=1
fi
_log e "${@}"
exit ${exit_code}
}