blob: 689bf4e5ff7d8faf7c7caf05f6efd0be887d6cc7 (
plain)
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
#---------------change history-----------------------------
# nucca@20170412: merge several logs into a single log. log files are rotated
# when they grow bigger than 5MB ($LOG_ROTATE_SIZE). Each log
# is separated by $LOG_LINEBREAK. Some definitions are modified
# for this change
# LOG_PREFIX : RouteInfo --> routeinfo
# LOG_LIMIT : 900 --> 36
# nucca@20161230: change temporary directory to avoid file explorer UI flickering
# because it is monitoring root folder of /sdcard
# TMPDIR: /sdcard --> /sdcard/htclog/routeinfo/tmp
# nucca@20161125: 1. change definition
# maximum number of log files: 300 --> 900
# log path: /sdcard/htclog --> /data/htclog/routeinfo
# 2. change log permission to 777 for adb pull
# 3. when high watermark of log amount ($LOG_LIMIT * 1.1) is reached,
# the oldest log files are deleted until log amount is less than
# $LOG_LIMIT.
# nucca@20160815: initial release
#
#---------------release note-------------------------------
#For user:
# This script invokes dump_route_info.cmd to get route
# information repeatedly. The oldest logs will be deleted
# when log amount limit is reached
#
#For developer:
# 1. use dos2unix to remove newline before release
#
#---------------definition---------------------------------
# LOG_INTERVAL : time interval, in seconds, of log capture
# LOG_LIMIT : maximum number of log files
# LOG_PATH : log path
# LOG_PREFIX : prefix of log file name
# LOG_ROTATE_SIZE : log rotate size by bytes
# LOG_LINEBREAK : signifying the end of log and the start of a new log
#
LOG_INTERVAL=5
LOG_LIMIT=36
LOG_PATH=/data/htclog/routeinfo
LOG_PREFIX=routeinfo
LOG_ROTATE_SIZE=5242880
LOG_LINEBREAK=********************************************************************************
#---------------redirect temporary directory---------------
# dump_route_info.cmd need access temporary folder but it
# may not have permission to create temporary file on
# default temporary folder /data/local/. Besides system_app and system_server,
# other apps are partially limited to access \data by "system_data_file:file no_w_file_perms"
# in system\sepolicy\domain.te
#
# workaround: redirect temporary folder to /sdcard
#
export TMPDIR=/sdcard/htclog/routeinfo/tmp
#---------------main---------------------------------------
# main script entry
#
# Function : main
# Input : (none)
# Output : (none)
#
function main(){
create_temporary_directory
create_log_directory
while true
do
dump_route_info
rotate_log_files
sleep $LOG_INTERVAL
done
}
#---------------create temporary directory-----------------
# Create temporary directory $TMPDIR for generated temporary files
#
# Function : create_temporary_directory
# Input : (none)
# Output : (none)
#
function create_temporary_directory(){
if [ -d $TMPDIR ]
then
echo "Directory $TMPDIR has existed."
else
echo "Directory $TMPDIR does not exist. Creating $TMPDIR.."
mkdir -p $TMPDIR
fi
}
#---------------create log directory-----------------------
# Create log directory $LOG_PATH to save route information logs
#
# Function : create_log_directory
# Input : (none)
# Output : (none)
#
function create_log_directory(){
if [ -d $LOG_PATH ]
then
echo "Directory $LOG_PATH has existed."
else
echo "Directory $LOG_PATH does not exist. Creating $LOG_PATH.."
mkdir -p $LOG_PATH
chmod 777 $LOG_PATH
restorecon $LOG_PATH
fi
}
#---------------count log amount---------------------------
# because some sku doesn't have command "wc", the following line
# can't be invoked
# log count: $(find ./ -maxdepth 1 -name "${LOG_PREFIX}*.txt" | wc -l)
#
# workaround: count file amount manually by loop
#
# Function : count_log_amount
# Input : (none)
# Output : $RET: current log amount
#
function count_log_amount(){
RET=0
for name in $(ls $LOG_PATH | grep $LOG_PREFIX)
do
echo "file #${RET}: $name"
RET=$(( $RET + 1 ))
done
}
#---------------dump routing information-------------------
# invoke "dump_route_info.cmd" to dump routing information and
# save logs to $LOG_PATH. Log files are rotated when they grow
# bigger than size bytes $LOG_ROTATE_SIZE
#
# Function : dump_route_info
# Input : (none)
# Output : (none)
#
function dump_route_info(){
local need_create_file=0
count_log_amount
local log_count=${RET}
# creating new log decision
# when no log exists or log rotate size is reached, create a new log file.
# if the last log file doesn't reach log rotate size, attach current log to
# it.
if [ $log_count -eq 0 ]; then
need_create_file=1;
else
local lastlog_path=$(ls $LOG_PATH/${LOG_PREFIX}*.txt -t 2>&1 | head -1)
local lastlog_size=$(stat -c %s $lastlog_path)
echo "last log inforamtion: $lastlog_path ($lastlog_size bytes)"
if [ $lastlog_size -gt $LOG_ROTATE_SIZE ]; then
need_create_file=1;
else
need_create_file=0;
fi
fi
# dump route inforamtion
dump_route_info.cmd
# save route inforamtion
if [ $need_create_file -eq 1 ]; then
local current_date=`date +%Y%m%d_%H%M%S`
local newlog_path=$LOG_PATH/${LOG_PREFIX}_$current_date.txt
echo "$LOG_LINEBREAK" >> $newlog_path
cat $TMPDIR/RouteInfo.txt >> $newlog_path
chmod 777 $newlog_path
echo "create file: $newlog_path"
else
echo "$LOG_LINEBREAK" >> $lastlog_path
cat $TMPDIR/RouteInfo.txt >> $lastlog_path
echo "attach file: $lastlog_path"
fi
}
#---------------rotate log files---------------------------
# when high watermark of log amount ($LOG_LIMIT * 1.1) is reached,
# the oldest log files are deleted until log amount is less than $LOG_LIMIT.
# the high watermark is used for reducing the frequency of file deletion
# which may cause "adb pull" failed during pulling files.
#
# Function : rotate_log_files
# Input : (none)
# Output : (none)
#
function rotate_log_files(){
local high_watermark=$(($LOG_LIMIT*11/10))
echo "high watermark of log amount is $high_watermark"
count_log_amount
local log_count=${RET}
echo "log count: $log_count"
if [ $log_count -gt $high_watermark ]; then
echo "deleting the oldest logs because file amount is over high watermark ($high_watermark).."
echo "log files will be deleted until log amount is under log limit ($LOG_LIMIT)"
ls $LOG_PATH/${LOG_PREFIX}*.txt -t | sed -e "1,${LOG_LIMIT}d" | xargs rm -rf
else
echo "log amount is under high watermark ($high_watermark)"
fi
}
#---------------main entry---------------------------------
#
main $*
exit
|