-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpprint
More file actions
118 lines (99 loc) · 2.43 KB
/
pprint
File metadata and controls
118 lines (99 loc) · 2.43 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
#!/bin/bash -e
export LOGFILE='./log'
#This function displays information messages using a green font.
#param: The message to display.
#usage: info "This is some information"
function info {
echo -e "\033[1;32m$@\033[0;49m"
}
#This function displays warnings in a yellow font.
#param: The message to display.
#usage: warn "This is a warning"
function warn {
echo -e "\033[1;33mWARNING: $@\033[0;49m"
}
#This function is used to exit the script do to an unrecoverable error
#param: The error message to display.
#usage: die "This is an error"
function die {
error $@
exit 1
}
#This function prints out an error, unlike die, it does not exit
#param: The message to display.
#usage: error "This is an error."
function error {
echo -e "\033[1;31;47m"
echo "***************************************************"
echo "$@"
echo "***************************************************"
echo -e "\033[0;49m"
}
#Adds a log message to the log file
#param: Log level name
#param: The message to add
#usage: mkmsg INFO This is a log message
function mkmsg {
local level=$1
shift
echo "$level $(date +"%F@%T") $@" >> $LOGFILE
}
#These function add the message to a log file,
#and then call the associated message function
#function.
function info_log {
mkmsg INFO "$@"
info "$@"
}
function warn_log {
mkmsg WARN "$@"
warn "$@"
}
function error_log {
mkmsg ERROR "$@"
error "$@"
}
function die_log {
mkmsg FATAL "$@"
die "$@"
}
#Prints who to send mail to. It does this via cat.
function get_to {
cat "./etc/email"
}
#This function mails the user the given message. The
#user can be found in ./etc/email
#param: Subject of the message
#param: The messge to send
#usage: mail_msg "The subject" An email message.
function mail_msg {
local subject="$1"
local user="$(get_to)"
shift
echo "$@" | mail -s "$subject" $user
}
#This function will mail the log file.
#param: The message's subject
#param: A message to prefix to the log
#usage: mail_log "Script failed" "The xxx script failed"
function mail_log {
local subject="$1"
local user=$(get_to)
shift
mail -s "$subject" $user <<EOF
$@
-------------------
$(cat $LOGFILE)
EOF
}
#This function sends the log via email then dies
#param: Subject of the message
#param: The die message
#usage: die_mail "Script failed" The xxx script failed!
function die_mail {
local subect=$1
shift
error_log $@
mail_log "$1" $@
exit 1
}