forked from LordAmit/mobile-monkey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log_analyzer.py
289 lines (259 loc) · 10.7 KB
/
log_analyzer.py
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
'''
log analyzer module, has Analyzer class.
xInstantiated by file path of log file
'''
from pathlib import Path
import time
import util
from enum import Enum
from typing import List
class Priority(Enum):
Verbose = 'V'
Debug = 'D'
Info = 'I'
Warning = 'W'
Error = 'E'
Fatal = 'F'
Silent = 'S'
class Log():
'''
Log class is instantiated by single logs generated by Logcat
in `threadtime` format. It cleans the line, and assigns the
proper values to the following properties:
log_time: time
log_pid: str
log_tid: str
log_priority: str
log_tag: str
log_message: str
eg. "06-14 22:08:27.424 2987 3046 W System.err:
at java.lang.Thread.run(Thread.java:761)"
will be used to create:
```
log_time = time.struct_time(tm_year=1900, tm_mon=6, tm_mday=14,
tm_hour=22, tm_min=8, tm_sec=27, tm_wday=3,tm_yday=165, tm_isdst=-1)
log_pid = "2987"
log_tid = "3046"
log_priority = "W"
log_tag = "System.err"
log_message = "at java.lang.Thread.run(Thread.java:761)"
```
'''
def __init__(self, log_line: str, mode: int=None)-> None:
if len(log_line) < 1:
raise ValueError("Error: Empty Line Detected from Log.")
# "06-14 22:08:27.424 2987 3046 W System.err: at java.lang.Thread.run(Thread.java:761) # noqa
log_line_split = log_line.split(' ', maxsplit=8)
current_line = log_line_split[0] + " " + log_line_split[1][:-4]
# print(log_line[1][:-4])
try:
self.log_time = time.strptime(current_line, "%m-%d %H:%M:%S")
except ValueError:
self.log_time = None
self.log_pid = None
self.log_tid = None
self.log_priority = None
self.log_tag = None
self.log_message = None
return None
if mode is 1:
try:
self.log_pid = log_line_split[3]
self.log_tid = log_line_split[5]
self.log_priority = log_line_split[6]
self.log_tag = log_line_split[7]
self.log_message = log_line_split[8].strip().strip(":").strip()
except IndexError:
self.log_time = None
self.log_pid = None
self.log_tid = None
self.log_priority = None
self.log_tag = None
self.log_message = None
return None
if mode is 2:
log_line_split = log_line.split(' ', maxsplit=6)
self.log_pid = log_line_split[2]
self.log_tid = log_line_split[3]
self.log_priority = log_line_split[4]
self.log_tag = log_line_split[5].strip('\t')
self.log_message = log_line_split[6].strip().strip(":").strip()
def __str__(self):
return "{}, {}, {}, {}, {}, {}".format(self.log_time,
self.log_pid,
self.log_tid,
self.log_priority,
self.log_tag,
self.log_message)
__repr__ = __str__
class Analyzer():
'''
Analyzer class, instantiated by file path of log file
when object is returned as string, it gives output in following format:
```
return "Unique Warnings: {}\nUnique Errors: {}\
\nTotal Warnings: \
{}\nTotal Errors: {}\n".format(self.count_unique_warnings,
self.count_unique_errors,
self.count_warnings,
self.count_errors)
```
'''
def __init__(self, file_path):
self.file_path = file_path
if not util.check_file_directory_exists(file_path, False):
raise ValueError("File path does not exist.")
self.file_contents = Path(file_path).read_text().split('\n')
self.unique_warnings = set()
self.all_warnings = list()
self.unique_errors = set()
self.all_errors = list()
self.unique_fatals = set()
self.all_fatals = list()
self.count_warnings = None
self.count_errors = None
self.count_fatals = None
self.count_unique_warnings = None
self.count_unique_errors = None
self.count_unique_fatals = None
self.logs = self.__logfile_to_logs(mode=1)
self.__calculate_stats()
if self.count_errors == 0 and\
self.count_unique_errors == 0 and\
self.count_unique_warnings == 0 and self.count_warnings == 0:
print("something went wrong. recalculating")
self.logs = self.__logfile_to_logs(mode=2)
self.__calculate_stats()
def return_unique_stats(self):
'''
Returns a tuple containing numbers of unique
`(warnings, errors, fatals)`
'''
return (self.count_unique_warnings, self.count_unique_errors,
self.count_unique_fatals)
def __logfile_to_logs(self, mode: int=None) -> List[Log]:
'''
converts logfile entries to a `List[Log]`
'''
log_list = []
for line in self.file_contents:
if len(line) < 1:
continue
if mode is 1:
log_list.append(Log(line, mode))
else:
log_list.append(Log(line, 2))
return log_list
def return_stats(self):
'''
Returns a tuple containing numbers of `(warnings, errors, fatals)`
'''
return (self.count_warnings, self.count_errors, self.count_fatals)
def __calculate_stats(self):
avc_counter = 0
non_jni_counter = 0
text_speech_counter = 0
long_monitor_counter = 0
ad_counter = 0
# failed_load_counter = 0
# time_out_ad = 0
facebook_katana_counter = 0
for log in self.logs:
if log.log_message is None or log.log_pid is None:
continue
if log.log_message.startswith('at ') or\
log.log_message.startswith('*') or\
log.log_message.endswith("more"):
continue
if "avc: denied" in log.log_message.lower():
if avc_counter > 0:
continue
avc_counter += 1
if "long monitor contention" in log.log_message.lower():
if long_monitor_counter > 0:
continue
long_monitor_counter += 1
if "remove non-jni" in log.log_message.lower():
if non_jni_counter > 0:
continue
non_jni_counter += 1
if "texttospeech" in log.log_tag.lower():
if text_speech_counter > 0:
continue
text_speech_counter += 1
# if "failed to load ad" in log.log_message.lower():
# failed_load_counter += 1
# if failed_load_counter > 0:
# continue
# if "timed out waiting for ad response" in log.log_message.lower():# noqa
# time_out_ad += 1
# if time_out_ad > 0:
# continue
if "Ads".lower() is log.log_tag.lower() or(
"MMSDK-PlayList".lower() in log.log_tag.lower()):
if ad_counter > 0:
continue
ad_counter += 1
if "com.facebook.katana.provider.AttributionIdProvider".lower() \
in log.log_message.lower():
if facebook_katana_counter > 0:
continue
facebook_katana_counter += 1
# print(log.log_message)
if "attempted to finish an input event" in log.log_message.lower()\
or "bluetooth" in log.log_message.lower() \
or "dropping event" in log.log_message.lower() \
or "cancelling event" in log.log_message.lower() \
or "discarding hit" in log.log_message.lower():
# print("dropping/cancelling event")
continue
# print(log.log_message)
if log.log_priority is 'E':
self.unique_errors.add(log.log_message)
self.all_errors.append(log.log_message)
if log.log_priority is 'W':
self.unique_warnings.add(log.log_message)
self.all_warnings.append(log.log_message)
if log.log_priority is 'F':
self.unique_fatals.add(log.log_message)
self.all_fatals.append(log.log_message)
self.count_unique_errors = len(self.unique_errors)
self.count_unique_warnings = len(self.unique_warnings)
self.count_unique_fatals = len(self.unique_fatals)
self.count_errors = len(self.all_errors)
self.count_warnings = len(self.all_warnings)
self.count_fatals = len(self.all_fatals)
# for line in self.file_contents:
# if len(line) < 1 or ':' not in line:
# continue
# line = str(line)
# message = line.split(':', maxsplit=1)[1].strip()
# if message.startswith('at '):
# continue
# if line[0] == 'W':
# self.unique_warnings.add(message)
# self.all_warnings.append(message)
# if line[0] == 'E':
# self.unique_errors.add(message)
# self.all_errors.append(message)
# self.count_unique_errors = len(self.unique_errors)
# self.count_unique_warnings = len(self.unique_warnings)
# self.count_errors = len(self.all_errors)
# self.count_warnings = len(self.all_warnings)
def return_file_contents(self) -> List:
'''
returns the contents of the log file in List
'''
return self.file_contents
# def print_file_contents_experimental(self):
# for line in self.file_contents:
# Log(line)
def __str__(self):
return "Total Warnings: {}\nUnique Warnings: {}\
\nTotal Errors: {}\nUnique Errors: {}\
\nTotal Fatals: {}\nUnique Fatals: {}".format(self.count_warnings,
self.count_unique_warnings, # noqa
self.count_errors,
self.count_unique_errors,
self.count_fatals,
self.count_unique_fatals)