-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.sh
executable file
·166 lines (156 loc) · 4.55 KB
/
logger.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/bin/bash
# shellcheck disable=SC2128,SC1094
. "$(cd "$(dirname "$(readlink "$BASH_SOURCE" || echo "$BASH_SOURCE")")" && pwd)"/colors.sh
display_help() {
local s=" "
# shellcheck disable=SC2059
printf "\v${s}${Green}Usage:${s}source ./logger.sh [Flags...]${Color_Off}\n${Blue}" >&2
printf "%4s-c, --colors%10susing colors for output (optional)\n" "$s" "$s"
printf "%-4s-h, --help%12shelp information${Color_Off}" "$s" "$s"
}
Colors=true
while [ $# -gt 0 ]; do
case "$1" in
--help*|-h*)
if [[ "$1" != *=* ]]; then shift; fi
display_help
exit 0
;;
--colors*|-c*)
if [[ "$1" != *=* ]]; then shift; fi
Colors="${1#*=}"
;;
*)
>&2 printf "%s Invalid argument\n" "$(RedStr "Error:")"
exit 1
;;
esac
shift
done
# Define a timestamp function
function timestamp {
DATE=$(date +"%Y-%m-%d")
TIME=$(date +"%H:%M:%S")
ZONE=$(date +"%Z %z")
printf "%s%s%s" "$DATE" "$TIME" "$ZONE"
}
function printfuncName {
local fn=""
local postfix="=>"
local space=" "
local color=$1
for value in "${@:3}"
do
if [ "${value}" != "" ]
then
if [ "${fn}" != "" ]
then
fn+=" "
fi
fn="${fn}${color}${value}${Color_Off} $postfix"
fi
done
tmp="${fn%$postfix}"
if [[ "${Colors}" == true ]]
then
# shellcheck disable=SC2059
if [[ -n "${tmp%$space}" ]]; then printf "${tmp%$space}"; else printf "${color}$2${Color_Off}"; fi
else
# shellcheck disable=SC2059
if [[ -n "${tmp%$space}" ]]; then printf "${tmp%$space}"; else printf "$2"; fi
fi
}
function execStr() {
local whoRun
whoRun="${1}"
local fName
fName="${2}"
whoRun="${whoRun#[$'\r\t\n -']}"
whoRun="${whoRun%[$'\r\t\n -']}"
local shell
# shellcheck disable=SC2059
shell="$(if [[ -n "$(which bash)" ]]; then printf "$(which bash)"; else printf "$(which sh)"; fi)"
# shellcheck disable=SC2059
if [[ "${whoRun}" != "" && "${whoRun}" != "$fName" ]]; then printf "${whoRun}"; else printf "${shell}"; fi
}
function INFO {
local stack
stack=""
local fName
fName=INFO
local result
result="$(printfuncName "${Blue}" "${FUNCNAME[@]}" "$fName")"
local msg
msg="$1"
local timeAndDate
timeAndDate="$(timestamp)"
local whoRun
whoRun=$(execStr "${0}" "$fName")
local line
# shellcheck disable=SC2059,SC2030,SC2031
line="$(if [[ -n $BASH_LINENO && "$BASH_LINENO" != "$fName" ]]; then printf " $BASH_LINENO"; fi)"
if [[ "${Colors}" == true ]]
then
stack="${whoRun}${Cyan}${line}"
# shellcheck disable=SC2059
printf "[${Yellow}$timeAndDate${Color_Off}] [${result}] [${BIWhite}${stack#$fName}${Color_Off}] $msg\n"
else
stack="${whoRun}${line}"
# shellcheck disable=SC2059
printf "[$timeAndDate] [${result}] [${stack#$fName}] $msg\n"
fi
}
function DEBUG(){
local stack
stack=""
local fName
fName=DEBUG
local result
result="$(printfuncName "${Purple}" "${FUNCNAME[@]}" $fName)"
local msg
msg="$1"
local timeAndDate
timeAndDate="$(timestamp)"
local whoRun
whoRun=$(execStr "${0}" "$fName")
local line
# shellcheck disable=SC2059,SC2030,SC2031
line="$(if [[ -n $BASH_LINENO && "$BASH_LINENO" != "$fName" ]]; then printf " $BASH_LINENO"; fi)"
if [[ "${Colors}" == true ]]
then
stack="${whoRun}${Cyan}${line}"
# shellcheck disable=SC2059
printf "[${Yellow}$timeAndDate${Color_Off}] [${result}] [${BIWhite}${stack#$fName}${Color_Off}] $msg\n"
else
stack="${whoRun}${line}"
# shellcheck disable=SC2059
printf "[$timeAndDate] [${result}] [${stack#$fName}] $msg\n"
fi
}
function ERROR(){
local stack
stack=""
local fName
fName=ERROR
local result
result="$(printfuncName "${Red}" "${FUNCNAME[@]}" "$fName")"
local msg
msg="$1"
local timeAndDate
timeAndDate="$(timestamp)"
local whoRun
whoRun=$(execStr "${0}" "$fName")
local line
# shellcheck disable=SC2059,SC2030,SC2031
line="$(if [[ -n $BASH_LINENO && "$BASH_LINENO" != "$fName" ]]; then printf " $BASH_LINENO"; fi)"
if [[ "${Colors}" == true ]]
then
stack="${whoRun}${Cyan}${line}"
# shellcheck disable=SC2059
printf "[${Yellow}$timeAndDate${Color_Off}] [${result}] [${BIWhite}${stack#$fName}${Color_Off}] $msg\n"
else
stack="${whoRun}${line}"
# shellcheck disable=SC2059
printf "[$timeAndDate] [${result}] [${stack#$fName}] $msg\n"
fi
}