-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_util_log
More file actions
executable file
·147 lines (128 loc) · 3.91 KB
/
ft_util_log
File metadata and controls
executable file
·147 lines (128 loc) · 3.91 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
#!/bin/bash
declare -a arrayData
declare -a pathArray
usage() {
echo -e "\nUsage: $0 \
([-p <logfile_path>] \
[-s <alert|crit|err|warn|info|debug>] \
[-d <pipe-separated data>] [-i] <message> \n \
- You can supply several [-p path] \n \
- If no path are suppled script will look into \$LOG_FILE for path. Several path can be supplied separated by space. \n \
- If a file doesn't exist it will be created but not folder structure (if folder not existing, the path will be ignored).
- Default severity is \"info\"\n \
- Severity can be passed as int 0=info and 1-259=err
- You can supply several data either [-d data1 -d data2 -d data3] or [-d \"data1|data2|data3\"] \n \
- STDIN is accepted instead of <message> with [-i] option. This option must be last!\n \
" 1>&2
exit 1
}
while getopts "ip::s::d::" o; do
case "${o}" in
i)
acceptStdin=true
;;
p)
if [ ! -d "$(dirname "${OPTARG}")" ]; then
echo -e "\nft_util_log : Incorrect \$LOG_FILE path (folder missing): \"${pathArray[k]}\"\n" 1>&2
else
pathArray+=("${OPTARG}")
fi
;;
s)
case ${OPTARG} in
alert | \
crit | \
err | \
warn | \
info)
s=${OPTARG}
;;
debug)
if [ "$LOG_DEBUG" = true ]; then s=${OPTARG}; else exit 0; fi
;;
2[0-5][0-9] | 1[0-9][0-9] | [1-9][0-9] | [1-9])
s=err
;;
0)
s=info
;;
*)
echo -e "\nft_util_log : Incorrect severity ${OPTARG}\nAvailable alert|crit|err|warn|info|debug\n" 1>&2
usage
;;
esac
;;
d)
IFSx=$IFS
IFS='|'
for arg in $OPTARG; do
arrayData+=("${arg}")
done
IFS=$IFSx
;;
*)
usage
;;
esac
[[ $acceptStdin ]] && break
done
shift "$((OPTIND - 1))"
echo_log() {
if [ -n "${log_msg}" ]; then
timestamp="$(date +\%Y-\%m-\%d\ \%H:\%M:\%S)"
# Check if terminal support colors
if command -v tput >/dev/null; then
term_colors=$(tput colors -T $TERM 2>/dev/null)
else
term_colors=2
fi
# First output to stdout with or formatting formatting
if [ -n "${term_colors}" ] && [ $term_colors -ge 8 ]; then
# Let's put some color
# https://misc.flogisoft.com/bash/tip_colors_and_formatting
case ${s} in
alert) color="\e[41;93m" ;;
crit) color="\e[41m" ;;
err) color="\e[91m" ;;
warn) color="\e[33m" ;;
info) color="\e[94m" ;;
debug) color="\e[92m" ;;
esac
echo -e "${color}${timestamp} ||\e[7m ${s} \e[27m|${data}|\e[0m ${log_msg}"
else
echo -e "${timestamp} || ${s} |${data}| ${log_msg}"
fi
# Output to file(s)
for f in ${pathArray[@]}; do
echo -e "${timestamp} || ${s} |${data}| ${log_msg}" >>"${f}"
done
fi
}
if [ -z ${s} ]; then
s="info"
fi
if [[ ${#pathArray[@]} = 0 ]]; then
pathArray=($LOG_FILE) # load the variable in an array
for k in ${!pathArray[@]}; do # check path
if [ ! -d "$(dirname "${pathArray[k]}")" ]; then
echo -e "\nft_util_log : Incorrect \$LOG_FILE path (folder missing): ${pathArray[k]}\n" 1>&2
unset pathArray[$k]
fi
done
if [[ ${#pathArray[@]} = 0 ]]; then # exit if there are still no path in the array
echo -e "\nft_util_log : No log file path found." 1>&2
usage
fi
fi
for i in ${!arrayData[@]}; do
data="$data ${arrayData[i]} |"
done
if [ ! -t 0 ] && [[ $acceptStdin ]]; then # check if STDIN is open and loop it
while read line; do
log_msg="${line}"
echo_log
done <&0
elif [ -n "$*" ]; then # check if normal argument input
log_msg="$*"
echo_log
fi