-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathLog
197 lines (164 loc) · 3.74 KB
/
Log
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#!/bin/bash (source)
Import Terminal
logLevelError=10
logLevelTerse=20
logLevelNormal=30
logLevelVerbose=40
logLevelDebug=50
[ "$logMode" ] || logMode="Normal"
export verboseFD=3
export normalFD=4
export terseFD=5
export errorFD=6
export debugFD=7
export logFD=8
export questionFD=9
[ -z "${logfile}" ] && logfile="/dev/null"
if [ -n "$(echo -e)" ]
then
echo=echo
else
echo="echo -e"
fi
Is_Log_Mode() {
[ "$logMode" = "$1" ]
}
Log_Function() {
local message
local echoflags
local color
local FD
Parameters "$@" message color FD echoflags
$echo $echoflags "${colorGray}${scriptName}:${colorNormal} ${color}$message${colorNormal}" >&$FD
$echo $echoflags "$scriptName: $message" >&$logFD
}
Log_Error() {
Log_Function "$*" "${colorBoldRed}" "${errorFD}"
}
Log_Normal() {
Log_Function "$*" "${colorCyan}" "${normalFD}"
}
Log_Terse() {
Log_Function "$*" "${colorBoldCyan}" "${terseFD}"
}
Log_Verbose() {
Log_Function "$*" "${colorNormal}" "${verboseFD}"
}
Log_Debug() {
Log_Function "$*" "${colorRedWhite}" "${debugFD}"
}
Quiet() { "$@" &> /dev/null ;}
Verbose() { "$@" 1>&$verboseFD 2>&$verboseFD ;}
# Ask a question. If the answer is "n", quit.
Ask_Continue() {
if [ "$logLevel" -lt "$logLevelNormal" ]
then return 0
fi
if [ "$1" ]
then Log_Question "$1"
fi
Log_Question "Press Enter to continue or Ctrl-C to cancel."
read
if [ "$REPLY" = "n" ]
then
exit 1
fi
if [ "$?" != "0" ]
then
exit $?
fi
}
Log_Question() {
# Manager expects ${colorNormal}${colorNormal} in questions
Log_Function "$*" "${colorNormal}${colorNormal}${colorBoldCyan}" "${questionFD}"
}
Ask_Option() {
local question=$1
local defaultanswer=$2
local lambda=$3
Log_Question "${question}"
# detsch, maybe this loop should be continued until some valid
# response is received
while true
do
read
#if [ "$REPLY" != "" -o "${questionStream}" = "" ]
if [ -z "$REPLY" -a -n "${defaultanswer}" ]
then
Log_Verbose "Using default answer (${defaultanswer})"
REPLY="${defaultanswer}"
fi
if [ "$REPLY" != "" ] && [ -n "$lambda" ]
then
if eval "${lambda}"
then break
fi
elif [ -n "$REPLY" ]
then
break
fi
done
if [ "$?" != "0" ]
then
exit $?
fi
}
Ask() {
local question
Parameters "$@" question
REPLY=foo
Ask_Option "$question [Y/n]" "yes" '[ -n "$REPLY" -a -z "$(echo $REPLY | sed -r -e "s/((Y|y)(ES|es)?|(N|n)(O|o)?)//")" ]'
if expr "$REPLY" : "N" >/dev/null || expr "$REPLY" : "n" >/dev/null
then return 1
else return 0
fi
}
Progress_Start() {
local message
Parameters "$@" message
Log_Function "$message *" "${colorCyan}" "${normalFD}" "-n"
spinControl=0
}
Progress_Move() {
$echo -n "${colorCyan}"
case "$spinControl" in
0) echo -n "|" >&${normalFD} ; spinControl=1 ;;
1) echo -n "/" >&${normalFD} ; spinControl=2 ;;
2) echo -n "-" >&${normalFD} ; spinControl=3 ;;
3) echo -n "\\" >&${normalFD} ; spinControl=0 ;;
esac # esac is ridiculous.
$echo -n ${colorNormal}
}
Progress_End() {
$echo "${colorNormal}" 1>&2
}
# Prints an error message and exits
Die() {
[ "" = "$*" ] || Log_Error "$@"
exit 1
}
# Error constants
export errorNotFound=2
# Prints an error message and exits with a given code
Die_With() {
status=$1
shift
[ "" = "$*" ] || Log_Error "$@"
exit $status
}
# Prints a message and exits successfully
Pass_Away() {
[ "" = "$*" ] || Log_Error "$@"
exit 0
}
Exit_With() {
ret=$1
shift
if [ 0 -eq $ret ]
then
[ -n "$*" ] && Log_Terse "$@"
else
[ -n "$*" ] && Log_Error "$@"
fi
exit $ret
}