-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlog.sh
85 lines (72 loc) · 1.41 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
#
# logging of shell script activity
#
#
# log_process_options
# to be used when parsing options
#
# usage:
#
# if log_process_options $1 ; then shift ; continue ; fi
#
#
log_quiet=0
log_process_options()
{
if [ "$1" = "--debug" -o "$1" = "-d" ] ; then
log_debug=1
return 0
fi
if [ "$1" = "--quiet" -o "$1" = "-q" ] ; then
log_quiet=1
return 0
fi
return 1
}
_log_impl()
{
if [ -z "${SCRIPT_NAME-}" -a -n "${PNAME-}" ] ; then
# my legacy scripts all used PNAME
SCRIPT_NAME=$PNAME
fi
if [ -z "${SCRIPT_NAME-}" ] ; then
local idx=$((${#BASH_SOURCE[*]} -1))
local source="${BASH_SOURCE[$idx]}"
SCRIPT_NAME="`basename $source`"
fi
echo -e "$SCRIPT_NAME: $*" >&2
}
log_debug()
{
if [ "$log_debug" = 1 ] ; then
_log_impl "$*"
fi
}
log_error()
{
_log_impl "$*"
}
log_info()
{
if [ "$log_quiet" != 1 ] ; then
_log_impl "$*"
fi
}
log_run()
{
log '!' "$@"
# TBD: wishlist
# we shall support composing of log_run with other
# "run modifiers"
#
# log_run quiet git pull
# shall log "git pull"
# not "quiet git pull"
# we shall shift all "first elements of $@" which are functions
"$@"
local r=$?
if [ "$r" != 0 ] ; then
log_error "-> failed, exit_code=$r"
fi
return $r
}