-
Notifications
You must be signed in to change notification settings - Fork 0
/
debug.func
143 lines (127 loc) · 3.04 KB
/
debug.func
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
slf=${slf-${0##*/}}
DEFAULT_DEBUG_LEVEL=ERROR
declare -A S2L
declare -a L2S
c=0
for svr in FATAL ERROR WARN INFO DEBUG; do
S2L[$svr]=$c
L2S[$c]=$svr
(( c++ ))
done
S2L[ERR]=${S2L[ERROR]}
S2L[DBG]=${S2L[DEBUG]}
exec 4<&2
declare -A dbgFDs=(['STDERR']=4)
declare -A fdLogs=([4]='STDERR')
flDEBUG_NoFdsOpened=1
log_open () {
local logFile svLogFile fdNum append flGoes2TTY
while [[ $#>0 ]]; do
logFile=$1; shift
! [[ $logFile =~ ^(std(err|out)|STD(ERR|OUT))$ ]]
flGoes2TTY=$?
(( flGoes2TTY )) && logFile=${logFile^^}
[[ ${dbgFDs[$logFile]} ]] && {
echo "log_open: $logFile already opened as fd #${dbgFDs[$logFile]}, close it first to reopen" >&2
continue
}
svLogFile=$logFile
if (( flGoes2TTY )); then
[[ $logFile =~ ERR ]]
logFile='&'$((2-$?))
append=''
else
[[ -f $logFile && ! -w $logFile ]] && {
echo "$logFile is not writable, cant open it, skipped" >&2
continue
}
append='>'
fi
eval "exec {fdNum}>${append}${logFile}" && {
[[ $flDEBUG_NoFdsOpened ]] && \
unset dbgFDs['STDERR'] fdLogs[4] flDEBUG_NoFdsOpened
dbgFDs+=([$svLogFile]=$fdNum)
fdLogs[$fdNum]=$svLogFile
}
done
return 0
}
log_close () {
local logFile=$1 fdNum
if [[ $#>0 ]]; then
while [[ $#>0 ]]; do
logFile=$1; shift
[[ $logFile =~ ^(std(err|out)|STD(ERR|OUT))$ ]] && logFile=${logFile^^}
fdNum=${dbgFDs[$logFile]}
[[ $fdNum ]] || {
echo "log_close: $logFile was not opened, so cant close it" >&2
continue
}
[[ -e /proc/$BASHPID/fd/$fdNum ]] && exec {fdNum}<&-
unset fdLogs[$fdNum] dbgFDs[$logFile]
done
else
for fdNum in ${dbgFDs[@]}; do
[[ -e /proc/$BASHPID/fd/$fdNum ]] && exec {fdNum}<&-
unset dbgFDs[${fdLogs[$fdNum]}] fdLogs[$fdNum]
done
fi
}
getDebugLevel () {
[[ $TRACE ]] && set +x
local dbgLevel dbgLevelNum dbgLevelSvr
if [[ $1 ]]; then
dbgLevel=$1
else
dbgLevel=${DEBUG_LEVEL:-$DEFAULT_DEBUG_LEVEL}
fi
dbgLevel=${dbgLevel^^}
if [[ ${S2L[$dbgLevel]} ]]; then
dbgLevelNum=${S2L[$dbgLevel]}
dbgLevelSvr=$dbgLevel
elif [[ $dbgLevel =~ ^[0-9]+$ ]] && (( dbgLevel<${#L2S[@]} )); then
dbgLevelNum=$dbgLevel
dbgLevelSvr=${L2S[$dbgLevel]}
else
dbgLevelNum=${S2L[$DEFAULT_DEBUG_LEVEL]}
dbgLevelSvr=$DEFAULT_DEBUG_LEVEL
fi
echo -n "$dbgLevelSvr"
[[ $TRACE ]] && set -x
return $dbgLevelNum
}
log_ () {
[[ $1 ]] || return 1
[[ $TRACE ]] && set +x
local SVR="$1"; shift
local STATE_DESCR="${STATE:+\"$STATE\"}"
local cur_dbgLevelSvr cur_dbgLevelNum rq_dbgLevelSvr rq_dbgLevelNum
local fdNum
cur_dbgLevelSvr=$(getDebugLevel)
cur_dbgLevelNum=$?
rq_dbgLevelSvr=$(getDebugLevel $SVR)
rq_dbgLevelNum=$?
if (( rq_dbgLevelNum <= cur_dbgLevelNum )); then
for fdNum in ${dbgFDs[@]}; do
echo "$(date +%H\:%M\:%S\ %d/%m/%Y)| ${slf:-${0##*/}}[$$${PPID:+/$PPID}]| ${rq_dbgLevelSvr}${STATE_DESCR:+| ST=$STATE_DESCR}| $@" >&$fdNum
done
fi
[[ $TRACE ]] && set -x
return 0
}
for svr in ${!S2L[@]}; do
eval "
${svr,,}_ () {
log_ $svr \$@
return $?
}
${svr}_ () {
log_ $svr \$@
return $?
}
"
done
try () {
{ STDERR=$( eval "$(cat -)" 2>&1 1>&3 ); } 3>&1
return $?
}