-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbashtasklog.sh
More file actions
executable file
·345 lines (284 loc) · 7.34 KB
/
bashtasklog.sh
File metadata and controls
executable file
·345 lines (284 loc) · 7.34 KB
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
<<HEAD
Name: bashtasklog.sh
Company: Zulius
Author: Timbo White
(OOP implementation derived from
code by Pim van Riezen)
Version: 0.1.0
Website: http://www.zulius.com
Description: Bash function library for displaying formatted
messages to the console and log files.
The output imitates the display format of
Linux boot messages.
Copyright: 2011 Zulius
License: GPL v2 (http://www.gnu.org/licenses/gpl-2.0.html)
HEAD
# ---------------------------------------------------------------------------
# OO support functions
# Kludged by Pim van Riezen <pi@madscience.nl>
# ---------------------------------------------------------------------------
DEFCLASS=""
CLASS=""
THIS=0
class() {
DEFCLASS="$1"
eval CLASS_${DEFCLASS}_VARS=""
eval CLASS_${DEFCLASS}_FUNCTIONS=""
}
static() {
return 0
}
func() {
local varname="CLASS_${DEFCLASS}_FUNCTIONS"
eval "$varname=\"\${$varname}$1 \""
}
var() {
local varname="CLASS_${DEFCLASS}_VARS"
eval $varname="\"\${$varname}$1 \""
}
loadvar() {
eval "varlist=\"\$CLASS_${CLASS}_VARS\""
for var in $varlist; do
eval "$var=\"\$INSTANCE_${THIS}_$var\""
done
}
loadfunc() {
eval "funclist=\"\$CLASS_${CLASS}_FUNCTIONS\""
for func in $funclist; do
eval "${func}() { ${CLASS}::${func} \"\$@\"; return \$?; }"
done
}
savevar() {
eval "varlist=\"\$CLASS_${CLASS}_VARS\""
for var in $varlist; do
eval "INSTANCE_${THIS}_$var=\"\$$var\""
done
}
typeof() {
eval echo \$TYPEOF_$1
}
new() {
local class="$1"
local cvar="$2"
shift
shift
local id=$(uuidgen | tr A-F a-f | sed -e "s/-//g")
eval TYPEOF_${id}=$class
eval $cvar=$id
local funclist
eval "funclist=\"\$CLASS_${class}_FUNCTIONS\""
for func in $funclist; do
eval "${cvar}.${func}() { local t=\$THIS; THIS=$id; local c=\$CLASS; CLASS=$class; loadvar; loadfunc; ${class}::${func} \"\$@\"; rt=\$?; savevar; CLASS=\$c; THIS=\$t; return $rt; }"
done
eval "${cvar}.${class} \"\$@\" || true"
}
# bashtasklog class definition
class bashtasklog
func bashtasklog
func print
func printTask
func printOk
func printFail
func printWarn
func printInfo
func setWidth
func setQuiet
func setTimestamp
func setLogfile
func printStatus
var timestamp
var logfile
var quiet
var width
<<BTL
NAME
bashtasklog constructor
SYNOPSIS
new bashtasklog your_instance_var_here [OPTIONS]
OPTIONS:
-t flag to automatically prepend all task messages with 14 a digit timestamp
-l specify a log file to write to
-q quiet mode flag. Do not output to stdout, only write to log file if supplied
-w width of padded message column. Defaults to 80 characters.
EXAMPLE:
1) Instantiate a bashtasklog object that will append a timestamp
to each message, have a message column 50 chars in width,
and will write to log file /tmp/foo.log:
new bashtasklog logger -t -w 50 -l "/tmp/foo.log"
BTL
bashtasklog::bashtasklog() {
# get options
OPTARG=""
OPTIND=1
while getopts ":tql:w:" OPT; do
case $OPT in
t) setTimestamp 1 ;;
q) setQuiet 1 ;;
l) setLogfile $OPTARG ;;
w) setWidth $OPTARG ;;
:)
esac
done
# get remaining arguments
shift $(($OPTIND - 1))
OPTIND=1
# defaults
if [ -z "$width" ]; then setWidth 80; fi
}
# sets the width of the task column
bashtasklog::setWidth() { width="%-${1}s"; }
# sets timestamp flag (1 = on, anything else = off)
bashtasklog::setTimestamp() {
if [ ! -z $1 ] && [ $1 == "1" ]; then
timestamp="1";
else
timestamp="";
fi
}
# sets quiet mode flag (1 = on, anything else = off)
bashtasklog::setQuiet() {
if [ ! -z $1 ] && [ $1 == "1" ]; then
quiet="1";
else
quiet="";
fi
}
# sets the logfile path
bashtasklog::setLogfile() { logfile="$1"; }
bashtasklog::printStatus() {
local COLOR_RESET="\x1b[39;49;00m"
local STATUS=''
local COLOR=''
case $1 in
1) STATUS="[ OK ]"; COLOR="\x1b[33;32m";;
2) STATUS="[ FAIL ]"; COLOR="\x1b[31;31m";;
3) STATUS="[ WARN ]"; COLOR="\x1b[33;33m";;
4) STATUS="[ INFO ]"; COLOR="\x1b[36;01m";;
*) STATUS="[ $1 ]"; COLOR="";;
esac
# quiet mode, print only to log file
if [ ! -z "$quiet" ] && [ ! -z "$logfile" ]; then
echo -e "$STATUS" >> "$logfile"
# supplementary message
if [ ! -z "$2" ]; then
echo -e "\n$2\n" >> "$logfile"
fi
return 0
fi
# non-quiet mode with log file
if [ -z "$quiet" ] && [ ! -z "$logfile" ]; then
echo -e "${COLOR}${STATUS}${COLOR_RESET}"
echo -e "${STATUS}" >> "$logfile"
# supplementary message
if [ ! -z "$2" ]; then
echo -e "\n${COLOR}${2}${COLOR_RESET}\n"
echo -e "\n$2\n" >> "$logfile"
fi
return 0
fi
# non-quiet mode, no log file
echo -e "${COLOR}${STATUS}${COLOR_RESET}"
# supplementary message
if [ ! -z "$2" ]; then
echo -e "\n${COLOR}${2}${COLOR_RESET}\n"
fi
return 0
}
# Echo's plain string to console and/or logfile.
# No padding or timestamp.
bashtasklog::print() {
# quiet mode, print only to log file
if [ ! -z "$quiet" ] && [ ! -z "$logfile" ]; then
echo -e "$1" >> "$logfile"
return 0
fi
# non-quiet mode with log file
if [ -z "$quiet" ] && [ ! -z "$logfile" ]; then
echo -e "$1" | tee -a "$logfile"
return 0
fi
# no log file
echo -e "$1"
}
<<BTL
NAME
bashtasklog.printTask
SYNOPSIS
instance_var.printTask [MESSAGE]
DESCRIPTION
Prints a message to the console and/or task log.
EXAMPLE:
1) Print a message:
logger.printTask "foo bar baz"
BTL
bashtasklog::printTask() {
local TIMESTAMP=''
if [ ! -z "$timestamp" ]; then
TIMESTAMP=$(date +"%Y%m%d%H%M%S ");
fi
# quiet mode, print only to log file
if [ ! -z "$quiet" ] && [ ! -z "$logfile" ]; then
printf "$width" "${TIMESTAMP}${1}" >> "$logfile"
return 0
fi
# non-quiet mode with log file
if [ -z "$quiet" ] && [ ! -z "$logfile" ]; then
printf "$width" "${TIMESTAMP}${1}" | tee -a "$logfile"
return 0
fi
# no log file
printf "$width" "${TIMESTAMP}${1}"
return 0
}
<<BTL
NAME
bashtasklog.printOk
SYNOPSIS
instance_var.printOk [MESSAGE]
DESCRIPTION
Prints a green "OK" in square brackets. If optional MESSAGE is
passed, it is printed on the next line in green text.
BTL
bashtasklog::printOk() {
printStatus "1" "$1"
return 0
}
<<BTL
NAME
bashtasklog.printFail
SYNOPSIS
instance_var.printFail [MESSAGE]
DESCRIPTION
Prints a red "FAIL" in square brackets. If optional MESSAGE is
passed, it is printed on the next line in red text.
BTL
bashtasklog::printFail() {
printStatus "2" "$1"
return 0
}
<<BTL
NAME
bashtasklog.printWarn
SYNOPSIS
instance_var.printWarn [MESSAGE]
DESCRIPTION
Prints a orange "WARN" in square brackets. If optional MESSAGE is
passed, it is printed on the next line in orange text.
BTL
bashtasklog::printWarn() {
printStatus "3" "$1"
return 0
}
<<BTL
NAME
bashtasklog.printInfo
SYNOPSIS
instance_var.printInfo [MESSAGE]
DESCRIPTION
Prints a blue "INFO" in square brackets. If optional MESSAGE is
passed, it is printed on the next line in blue text.
BTL
bashtasklog::printInfo() {
printStatus "4" "$1"
return 0
}