-
Notifications
You must be signed in to change notification settings - Fork 0
/
vnmik.message.utils
executable file
·156 lines (140 loc) · 2.65 KB
/
vnmik.message.utils
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
# $Id$
# This file is part of VnMiK 4.0.0 <http://vnmik.sf.net/>
##############################################################################
# color utils
##############################################################################
# set text color : $1 = color number
set_color()
{
if [ -z "$1" ]; then
echo -en \\0033\[1\;30m
else
if [ "$1" == "0" ]; then
echo -en \\0033\[1\;30m
else
echo -en \\0033\[1\;$1m
fi
fi
}
# list of all colors
test_color()
{
# list all color (from 1 to 80)
n=6
set_color 0
while [ $n -le 80 ]; do
echo -en \\0033\[1\;"$n"m
echo $n: xxxxxx
n=$((n+1))
done
set_color 0
}
##############################################################################
# messge utils
##############################################################################
# change stat_* prefix. Useful for package installation
# $1: prefix; empty means ' '
stat_prefix()
{
if [ "x$1" == "x" ]; then
export STAT_PREFIX='> '
else
export STAT_PREFIX="$1"
fi
}
# RAW write something to log file
stat_log()
{
echo "$@" >> $LOGFILE 2>/dev/null
return 0
}
# append a file to LOG file
stat_log_file()
{
if [ "x$1" == "x" ]; then
stat_log "($FUNCNAME) missing parameter"
return 1
fi
if [ ! -f "$1" ]; then
stat_log "($FUNCAME) file unreadable: $1"
fi
stat_log "file content: $1 <<\EOF"
cat "$1" >> $LOGFILE 2>/dev/null
stat_log "EOF"
}
# write time/date information to log file
stat_date()
{
stat_log
stat_log "=== `date` ==="
stat_log
}
# notify that something has done
stat_done()
{
echo "0$STAT_PREFIX$@"
stat_log "0$STAT_PREFIX$@"
return 0
}
# expose the warning ( stdout and $LOGFILE )
stat_warn()
{
set_color 35
echo "1$STAT_PREFIX$@"
stat_log "1$STAT_PREFIX$@"
set_color 0
return 2
}
# program failed as some critical errors ( stdout and $LOGFILE )
# require `cleanup' (see below)
stat_fail()
{
local EXIT_IF_FAIL=0
local CLEANUP=0
while [ "x${1:0:2}" == "x--" ]; do
case "$1" in
"--no-clean") CLEANUP=0;;
"--exit") EXIT_IF_FAIL=1;;
*) continue;;
esac
shift
done
set_color 32
echo "2$STAT_PREFIX$@"
stat_log "2$STAT_PREFIX$@"
set_color 0
[ $CLEANUP -eq 1 ] && cleanup
if [ $EXIT_IF_FAIL -eq 1 ]; then
pause
exit 1
else
return 1
fi
}
# print what we are going to do
stat_msg()
{
set_color 34
echo "0$STAT_PREFIX$@"
stat_log "0$STAT_PREFIX$@"
set_color 0
}
# notify that something has done
stat_msh()
{
echo "0$STAT_PREFIX$@"
stat_log "0$STAT_PREFIX$@"
return 0
}
msg()
{
echo -e $@
}
# initialize something
# ok. let's intialize something
[ -d $LOGDIR ] || mkdir -p $LOGDIR
stat_prefix
stat_log
stat_log "=== BEGIN NEW SESSION `date` ==="
stat_log
stat_log "library loaded: vnmik.message.utils"