-
Notifications
You must be signed in to change notification settings - Fork 6
/
message.lib.sh
170 lines (148 loc) · 5.82 KB
/
message.lib.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
166
167
168
169
170
#
# Copyright (c) 2010-2011 Linagora
# Patrick Guiran <pguiran@linagora.com>
# http://github.com/Tauop/ScriptHelper
#
# ScriptHelper is free software, you can redistribute it and/or modify
# it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version.
#
# ScriptHelper is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# README ---------------------------------------------------------------------
# This library purpose is to print and log messages (posix compatibility is
# the real purpose)
#
# Global variables ===========================================================
# IMPORTANT: Please to write to those variables
# __LIB_MESSAGE__ : Indicate that this lib is loaded
# __MSG_INDENT__ : indentation for messages
# __IN_DOTHIS__ : used to know if we are in a DOTHIS - OK/KO block
# ----------------------------------------------------------------------------
# don't source several times this file
if [ "${__LIB_MESSAGE__:-}" != 'Loaded' ]; then
__LIB_MESSAGE__='Loaded'
__MSG_INDENT__=''
__IN_DOTHIS__='false'
# Utility functions -------------------------------------
# usage: MSG_INDENT_INC
# desc: increment indentation of MESSAGE() display by two spaces
MSG_INDENT_INC () { __MSG_INDENT__="${__MSG_INDENT__:-} "; }
# usage: MSG_INDENT_DEC
# desc: decrement indentation of MESSAGE() display by two spaces
MSG_INDENT_DEC () { __MSG_INDENT__="${__MSG_INDENT__% }"; }
# usage: MESSAGE [<options>] "<message>"
# desc: This function is used to display messages on standard output
# and/or write messages into the output log file.
# note: Message are display with indentation if MSG_INDENT_* functions
# are called, or if the script call DOTHIS() and related functions
# options:
# --no-break : don't print a line break
# --no-date : Don't add the date in message written into the output log file
# --no-print : Don't print message on standard output
# --no-log : Don't write message into output log file
MESSAGE () {
local do_print='true' do_log='true' do_indent='true' cbreak='\n'
local msg= date=
date=$(date +"[%D %T]")
# parse arguments
while [ true ]; do
[ $# -eq 0 ] && break
case "$1" in
--no-break ) shift; cbreak='' ;;
--no-date ) shift; date='' ;;
--no-print ) shift; do_print='false' ;;
--no-log ) shift; do_log='false' ;;
--no-indent ) shift; do_indent='false' ;;
-- ) shift; break; ;;
--* ) shift; ;; # ignore
* ) break; ;;
esac
done
msg="$*"
[ "${do_indent}" = 'true' ] && msg="${__MSG_INDENT__:-}${msg}"
# don't put the date on standard output (STDOUT)
[ "${do_print}" = 'true' ] && printf "%s${cbreak}" "${msg}"
if [ "${do_log}" = 'true' -a -f "${__OUTPUT_LOG_FILE__:-}" ]; then
[ -n "${date}" ] && msg="${date} ${msg}"
printf "%s${cbreak}" "${msg}" >> "${__OUTPUT_LOG_FILE__:-}"
fi
}
# usage: MSG [<option>] <string>
# desc: alias of MESSAGE [<option>] <string>
MSG () { MESSAGE $@; }
# usage: LOG [<option>] <string>
# desc: alias of MESSAGE --no-indent --no-print [<option>] <string>
LOG () { MESSAGE --no-indent --no-print $@; }
# usage: NOTICE <string>
# desc: alias of MESSAGE --no-indent <string> prefixed by "NOTICE: "
NOTICE () { MESSAGE --no-indent "${__MSG_INDENT__:-}NOTICE: $*"; }
# usage: BR
# desc: print a break return
BR () { MESSAGE --no-log ''; }
# usage: ERROR <string>
# desc: close the current DOTHIS if needed and print a message prefixed by "ERROR: "
ERROR () {
if [ "${__IN_DOTHIS__:-}" = 'true' ]; then
MESSAGE --no-date --no-indent -- 'Err';
__IN_DOTHIS__='false'
fi
MESSAGE --no-indent "${__MSG_INDENT__:-}ERROR: $*"
}
# usage: WARNING <string>
# desc: close the current DOTHIS if needed and print a message prefixed by "WARNING: "
WARNING () {
if [ "${__IN_DOTHIS__:-}" = 'true' ]; then
MESSAGE --no-date --no-indent -- 'Warn';
__IN_DOTHIS__='false'
fi
MESSAGE --no-indent "${__MSG_INDENT__:-}WARNING: $*"
}
# usage: DOTHIS <string>
# desc: print "- <string> ... "
DOTHIS () {
MESSAGE --no-break --no-indent -- "${__MSG_INDENT__:-}- $* ... "
__IN_DOTHIS__='true'
}
# usage: OK
# desc: close the current DOTHIS and print "OK"
OK () {
[ "${__IN_DOTHIS__:-}" = 'true' ] && MESSAGE --no-date --no-indent -- 'OK'
__IN_DOTHIS__='false'
}
# usage: KO <message>
# desc: close the current DOTHIS and print "KO" and call FATAL with <message>
KO () {
if [ "${__IN_DOTHIS__:-}" = 'true' ]; then
MESSAGE --no-date --no-indent -- 'KO';
__IN_DOTHIS__='false'
fi
FATAL "$*"
}
# usage: ROLLBACK
# desc: by default, it's done nothing. Its purpose is to be
# override by the parent script
ROLLBACK () { cat /dev/null >/dev/null; }
# usage: FATAL <error message>
# desc: prefix message with "FATAL:" and call MESSAGE().
# Then it call ROLLBACK() and exit(1).
# note: close DOTHIS if needed
FATAL () {
if [ "${__IN_DOTHIS__:-}" = 'true' ]; then
MESSAGE --no-date --no-indent -- 'Fatal';
__IN_DOTHIS__='false'
fi
MESSAGE --no-indent "${__MSG_INDENT__:-}FATAL: $*"
type DISPLAY_LOG_FILES >/dev/null 2>/dev/null
[ $? -eq 0 ] && DISPLAY_LOG_FILES
ROLLBACK
exit 1
}
fi # end of: if [ "${__LIB_MESSAGE__}" != 'Loaded' ]; then