-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.sh
executable file
·80 lines (65 loc) · 1.38 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
#!/bin/bash
MYSELF="$(readlink -f "$0")"
MYDIR="${MYSELF%/*}"
ME=$(basename $MYSELF)
logf="$LOGF"
if [[ -z "$logf" ]]; then
>&2 echo "env var LOGF must be defined"
exit 1
fi
logd="$(dirname $logf)"
if [ ! -d $logd ]; then
mkdir -p "$logd"
fi
function debugging() {
verbose=${1}
confd="/tmp/log-$(basename $logf).conf.d"
debugf="$confd/debug"
if [ ! -f $debugf ]; then
mkdir -p "$(dirname $debugf)"
echo off > $debugf
debug "all logs are saved to $LOGF"
fi
if [[ -n "$verbose" ]]; then
echo $verbose > $debugf
else
cat $debugf
fi
}
function log() {
level="$1"
shift
indicator="$1"
shift
if [[ "$1" == '-n' ]]; then
echo ""
shift
fi
if [[ $level == DEBUG && $(debugging) == on || $level != DEBUG ]]; then
echo -e "$indicator $(now.sh -t) - ${FUNCNAME[2]}@${BASH_LINENO[1]}/$level: $@"
fi
echo -e "$MYSELF - $indicator $(now.sh -dt) - ${FUNCNAME[2]}@${BASH_LINENO[1]}/$level: $@" >> $logf
}
function info() {
>&2 log INFO '###' "$@"
}
function err() {
>&2 log ERROR '!!!' "$@"
}
function debug() {
>&2 log DEBUG '>>>' "$@"
}
for var in "$@"
do
case "$var" in
--verbose|--debug|-v)
shift
echo "debug is on"
debugging on
;;
--quiet|-q)
shift
debugging off
;;
esac
done