-
Notifications
You must be signed in to change notification settings - Fork 0
/
poor.mans.logger.sh
executable file
·52 lines (45 loc) · 1.11 KB
/
poor.mans.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
#!/usr/bin/env bash
#
# A poor logger
#
# @author: Konstantinos Pechlivanis
# @version 0.1
#
# Colours for logging messages
red='\033[0;31m'
green='\033[0;32m'
orange='\033[0;33m'
cyan='\033[0;36m'
noColour='\033[0m'
# param $1: info message
log_info(){
if [ -f $LOG ]; then
echo -e "${cyan}"`date`" ${green}[ INFO ] $1 ${noColour}" | tee -a $LOG
else
echo -e "${cyan}"`date`" ${green}[ INFO ] $1 ${noColour}"
fi
}
# param $1: warning message
log_warn(){
if [ -f $LOG ]; then
echo -e "${cyan}"`date`" ${orange}[ WARN ] $1 ${noColour}" | tee -a $LOG
else
echo -e "${cyan}"`date`" ${orange}[ WARN ] $1 ${noColour}"
fi
}
# param $1: error message
log_error(){
if [ -f $LOG ]; then
echo -e "${cyan}"`date`" ${red}[ ERROR ] $1 ${noColour}" | tee -a $LOG
else
echo -e "${cyan}"`date`" ${red}[ ERROR ] $1 ${noColour}"
fi
}
# param $1: error message (default is "An error occured, exiting...")
# param $2: error code (default is 1)
exit_error(){
if [ $? -ne 0 ]; then
log_error ${1:-"An error occured, exiting..."}
exit ${2:-1}
fi
}