-
Notifications
You must be signed in to change notification settings - Fork 22
/
err_common.sh
executable file
·151 lines (135 loc) · 3.82 KB
/
err_common.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
#!/bin/sh
#------------------------------------------------------------------
# err_common.sh
#
# Common error handling routines.
#
# (c) Kaiwan N Billimoria
# kaiwan -at- kaiwantech -dot- com
# MIT / GPL v2
#------------------------------------------------------------------
export TOPDIR=$(pwd)
export VERBOSE_MSG=0
export DEBUG=0
# Replace with log filename
export LOGFILE_COMMON=/dev/null
export COLOR=1
#--- Icons
# src: /usr/share/icons/Humanity/actions/
ICON_NEXT=go-next
ICON_BACK=go-previous
ICON_YES=add #go-next
ICON_NO=remove #gtk-remove
ICON_ADD=add #gtk-add
ICON_REGISTER=player_record
ICON_SIGNIN=format-text-direction-ltr
ICON_EXIT=stock_mark #system-log-out
# QP
# QuickPrint ;-)
# Print timestamp, script name, line#. Useful for debugging.
QP()
{
_ERR_HDR_FMT="%.23s %s[%s]: "
_ERR_MSG_FMT="${_ERR_HDR_FMT}%s\n"
[ ${COLOR} -eq 1 ] && fg_blue
printf " QP: $_ERR_MSG_FMT" $(date +%F.%T.%N) " ${BASH_SOURCE[1]##*/}:${FUNCNAME[2]}" |tee -a ${LOGFILE_COMMON}
dumpstack
#printf " QP: $_ERR_MSG_FMT" $(date +%F.%T.%N) " ${BASH_SOURCE[1]##*/}:${BASH_LINENO[0]}" |tee -a ${LOGFILE_COMMON}
[ ${COLOR} -eq 1 ] && color_reset
unset _ERR_HDR_FMT
unset _ERR_MSG_FMT
true
}
STACK_MAXDEPTH=32 # arbit?
dumpstack()
{
#for frame in $(seq 1 $1)
local frame=1
local funcname
ShowTitle " Stack Call-trace:"
[ ${COLOR} -eq 1 ] && fg_blue
while [ true ]
do
funcname=${FUNCNAME[${frame}]}
printf " [frame #${frame}] ${BASH_SOURCE[${frame}]}:${funcname}:${BASH_LINENO[${frame}]}"
#printf " [frame #${frame}] ${funcname}"
[ ${frame} -ne 1 ] && printf "\n" || {
[ ${COLOR} -eq 1 ] && fg_magenta
printf " <-- top of stack\n"
[ ${COLOR} -eq 1 ] && fg_blue
}
[ "${funcname}" = "main" ] && break # stop, reached 'main'
[ ${frame} -ge ${STACK_MAXDEPTH} ] && break # just in case ...
let frame=frame+1
done |tee -a ${LOGFILE_COMMON}
[ ${COLOR} -eq 1 ] && color_reset
true
}
# params: the error message
cli_handle_error()
{
#QP
if [ $# -lt 1 ] ; then
cecho "FatalError :: <no error msg>"
else
cecho "FatalError :: $@"
fi
dumpstack
[ ${COLOR} -eq 1 ] && color_reset
exit 1
}
#--------------------- F a t a l E r r o r ----------------------------
# Exits with exit status 1 !
# Parameters:
# $1 : error message [optional]
FatalError()
{
#set -x
SEALS_REPORT_ERROR_URL=""
local msgpre="<b><span foreground='Crimson'>Sorry, we've encountered a fatal error.</span></b>\n\n"
local errmsg="<i>Details:</i>\n$(date):${name}:${FUNCNAME[ 1 ]}()"
local msgpost="\n<span foreground='Crimson'>\
If you feel this is a bug / issue, kindly report it here:</span>
${SEALS_REPORT_ERROR_URL}\n
Many thanks.
"
[ $# -ne 1 ] && {
local msg="${msgpre}<span foreground='NavyBlue'>${errmsg}</span>\n${msgpost}"
} || {
local msg="${msgpre}<span foreground='NavyBlue'>${errmsg}\n ${1}</span>\n${msgpost}"
}
#cecho "Fatal Error! Details: ${errmsg} ${1}"
#local LN=$(echo "${MSG}" |wc -l)
#local calht=$(($LN*10))
local title=" FATAL ERROR!"
yad --title="${title}" --image=dialog-warning --text="${msg}" \
--button="Close!${ICON_NO}:0" \
--wrap --text-align=center --button-layout=center --center \
--selectable-labels --no-escape --dialog-sep --sticky --on-top --skip-taskbar 2>/dev/null || true
cli_handle_error "$@"
exit 1
} # end FatalError()
# $1 = warning msg
warn()
{
[ $# -eq 0 ] && return
fg_yellow
ShowTitle "!WARNING! $@"
#QP
color_reset
}
# Prompt
# Interactive: prompt the user to continue by pressing ENTER or
# abort by pressing Ctrl-C
# Parameter(s):
# $1 : string to display (string)
# $2 : string to display on signal trap [optional]
Prompt()
{
local msg="*** User Abort detected! ***"
trap 'wecho "${msg}" ; dumpstack ; color_reset ; exit 3' INT QUIT
[ ${COLOR} -eq 1 ] && fg_magenta
echo "Press ENTER to continue, or Ctrl-C to abort now..."
read
[ ${COLOR} -eq 1 ] && color_reset
} # end Prompt()