Skip to content

Commit f793136

Browse files
committed
add logcat.py
1 parent 06ce9b4 commit f793136

4 files changed

Lines changed: 270 additions & 0 deletions

File tree

‎README.md‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,22 @@ Android测试中常用到的脚本
77

88
批量安装应用(支持以中文命名的 apk)、批量卸载、截屏、录制视频、获取当前应用的 apk 文件、包名、Activity 名等。<br>
99

10+
###2016.01.21
11+
增加 `logcat.py`,windows 中在 cmd 里面运行 logcat 命令,会给输出的日志内容根据优先级添加颜色。使用前提是已配置 adb 及 python 的环境变量,在 cmd 里面可以直接运行 adb 命令和python 脚本。
12+
用法:
13+
将`logcat.py` 配置到环境变量里面,使得可以在 cmd 中可以直接执行 logcat 命令。参数与 `adb logcat` 的一样。例如:
14+
```
15+
logcat -v time
16+
```
17+
无颜色时截图:
18+
![logcat1](image/logcat1.png)
19+
20+
logcat 执行后:
21+
![logcat2](image/logcat2.png)
22+
23+
当要使用重定向时,请使用 `adb logcat`.
24+
25+
1026
###2015.06.02
1127
增加 `get_app_crash_log.py` 与 `getAppCrashLog.sh`, 应用发生 crash ,未及时从 logcat 获取到有效 log 时,可通过该脚本获取 log
1228

‎image/logcat1.png‎

89.6 KB
Loading

‎image/logcat2.png‎

84.1 KB
Loading

‎logcat/logcat.py‎

Lines changed: 254 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,254 @@
1+
# coding=utf-8
2+
3+
'''
4+
Created on 2016年1月21日
5+
6+
@author: xuxu
7+
'''
8+
9+
#用法: logcat -v time
10+
11+
import sys
12+
import os
13+
import ctypes
14+
import re
15+
16+
#输出类别标志
17+
STD_INPUT_HANDLE = -10
18+
STD_OUTPUT_HANDLE= -11
19+
STD_ERROR_HANDLE = -12
20+
21+
#颜色
22+
"""
23+
24+
0 = 黑色 8 = 灰色
25+
1 = 蓝色 9 = 淡蓝色
26+
2 = 绿色 A = 淡绿色
27+
3 = 浅绿色 B = 淡浅绿色
28+
4 = 红色 C = 淡红色
29+
5 = 紫色 D = 淡紫色
30+
6 = 黄色 E = 淡黄色
31+
7 = 白色 F = 亮白色
32+
"""
33+
BLACK = 0X00
34+
RED = 0x0c
35+
DARKRED = 0x40
36+
BLUE = 0x09
37+
GREEN = 0x0a
38+
LIGHT_GREEN = 0x0b
39+
YELLOW = 0x0e
40+
WHITE = 0x0f
41+
PINK = 0x0d
42+
43+
class Color(object):
44+
std_out_handle = ctypes.windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)
45+
46+
def set_cmd_color(self, color, handle=std_out_handle):
47+
return ctypes.windll.kernel32.SetConsoleTextAttribute(handle, color)
48+
49+
def reset_color(self):
50+
self.set_cmd_color(RED | GREEN | BLUE)
51+
52+
def show_verbose(self, msg):
53+
self.set_cmd_color(WHITE)
54+
sys.stdout.write(msg)
55+
self.reset_color()
56+
57+
def show_debug(self, msg):
58+
self.set_cmd_color(LIGHT_GREEN)
59+
sys.stdout.write(msg)
60+
self.reset_color()
61+
62+
def show_info(self, msg):
63+
self.set_cmd_color(GREEN)
64+
sys.stdout.write(msg)
65+
self.reset_color()
66+
67+
def show_warn(self, msg):
68+
self.set_cmd_color(YELLOW)
69+
sys.stdout.write(msg)
70+
self.reset_color()
71+
72+
def show_error(self, msg):
73+
self.set_cmd_color(RED)
74+
sys.stdout.write(msg)
75+
self.reset_color()
76+
77+
def show_fatal(self, msg):
78+
self.set_cmd_color(DARKRED)
79+
sys.stdout.write(msg)
80+
self.reset_color()
81+
82+
def main(argv):
83+
color = Color()
84+
command = ""
85+
if "-d" in argv:
86+
command = "adb logcat %s" %" ".join(argv[1:])
87+
else:
88+
command = "adb logcat -d %s" %" ".join(argv[1:])
89+
if "brief" in argv or "process" in argv or "time" in argv:
90+
while True:
91+
TAG = False
92+
lines = os.popen(command).readlines()
93+
os.popen("adb logcat -c")
94+
95+
for line in lines:
96+
if re.search(r"beginning of", line):
97+
pass
98+
elif re.search(r"V\/.+(\d+\):)", line):
99+
color.show_verbose(line)
100+
elif re.search(r"D\/.+(\d+\):)", line):
101+
color.show_debug(line)
102+
elif re.search(r"I\/.+(\d+\):)", line):
103+
color.show_info(line)
104+
elif re.search(r"W\/.+(\d+\):)", line):
105+
color.show_warn(line)
106+
elif re.search(r"E\/.+(\d+\):)", line):
107+
color.show_error(line)
108+
elif re.search(r"F\/.+(\d+\):)", line):
109+
color.show_fatal(line)
110+
elif re.search(r"or defaults", line):
111+
color.show_info(line)
112+
TAG = True
113+
else:
114+
color.show_info(line)
115+
if TAG:
116+
break
117+
elif "thread" in argv:
118+
while True:
119+
TAG = False
120+
lines = os.popen(command).readlines()
121+
os.popen("adb logcat -c")
122+
123+
if "invalid option" in lines[0]:
124+
for line in lines:
125+
color.show_info(line)
126+
break;
127+
128+
for line in lines:
129+
if re.search(r"beginning of", line):
130+
pass
131+
elif re.search(r"V\(.+:.+\)", line):
132+
color.show_verbose(line)
133+
elif re.search(r"D\(.+:.+\)", line):
134+
color.show_debug(line)
135+
elif re.search(r"I\(.+:.+\)", line):
136+
color.show_info(line)
137+
elif re.search(r"W\(.+:.+\)", line):
138+
color.show_warn(line)
139+
elif re.search(r"E\(.+:.+\)", line):
140+
color.show_error(line)
141+
elif re.search(r"F\(.+:.+\)", line):
142+
color.show_fatal(line)
143+
elif re.search(r"or defaults", line):
144+
color.show_info(line)
145+
TAG = True
146+
else:
147+
color.show_info(line)
148+
if TAG:
149+
break
150+
elif "threadtime" in argv:
151+
while True:
152+
TAG = False
153+
lines = os.popen(command).readlines()
154+
os.popen("adb logcat -c")
155+
156+
if "invalid option" in lines[0]:
157+
for line in lines:
158+
color.show_info(line)
159+
break;
160+
161+
for line in lines:
162+
if re.search(r"beginning of", line):
163+
pass
164+
elif re.search(r"\s+V\s+", line):
165+
color.show_verbose(line)
166+
elif re.search(r"\s+D\s+", line):
167+
color.show_debug(line)
168+
elif re.search(r"\s+I\s+", line):
169+
color.show_info(line)
170+
elif re.search(r"\s+W\s+", line):
171+
color.show_warn(line)
172+
elif re.search(r"\s+E\s+", line):
173+
color.show_error(line)
174+
elif re.search(r"\s+F\s+", line):
175+
color.show_fatal(line)
176+
elif re.search(r"or defaults", line):
177+
color.show_info(line)
178+
TAG = True
179+
else:
180+
color.show_info(line)
181+
if TAG:
182+
break;
183+
elif "tag" in argv:
184+
while True:
185+
TAG = False
186+
lines = os.popen(command).readlines()
187+
os.popen("adb logcat -c")
188+
189+
if "invalid option" in lines[0]:
190+
for line in lines:
191+
color.show_info(line)
192+
break;
193+
194+
for line in lines:
195+
if re.search(r"beginning of", line):
196+
pass
197+
elif re.search(r"^V/\w+:", line):
198+
color.show_verbose(line)
199+
elif re.search(r"^D/\w+:", line):
200+
color.show_debug(line)
201+
elif re.search(r"^I/\w+:", line):
202+
color.show_info(line)
203+
elif re.search(r"^W/\w+:", line):
204+
color.show_warn(line)
205+
elif re.search(r"^E/\w+:", line):
206+
color.show_error(line)
207+
elif re.search(r"^F/\w+:", line):
208+
color.show_fatal(line)
209+
elif re.search(r"or defaults", line):
210+
color.show_info(line)
211+
TAG = True
212+
else:
213+
color.show_info(line)
214+
if TAG:
215+
break;
216+
else:
217+
while True:
218+
TAG = False
219+
lines = os.popen(command).readlines()
220+
os.popen("adb logcat -c")
221+
222+
if "invalid option" in lines[0]:
223+
for line in lines:
224+
color.show_info(line)
225+
break;
226+
227+
for line in lines:
228+
if re.search(r"beginning of", line):
229+
pass
230+
elif re.search(r"V\/.+(\d+\):)", line):
231+
color.show_verbose(line)
232+
elif re.search(r"D\/.+(\d+\):)", line):
233+
color.show_debug(line)
234+
elif re.search(r"I\/.+(\d+\):)", line):
235+
color.show_info(line)
236+
elif re.search(r"W\/.+(\d+\):)", line):
237+
color.show_warn(line)
238+
elif re.search(r"E\/.+(\d+\):)", line):
239+
color.show_error(line)
240+
elif re.search(r"F\/.+(\d+\):)", line):
241+
color.show_fatal(line)
242+
elif re.search(r"or defaults", line):
243+
color.show_info(line)
244+
TAG = True
245+
else:
246+
color.show_info(line)
247+
if TAG:
248+
break;
249+
250+
if __name__ == "__main__":
251+
try:
252+
main(sys.argv)
253+
except:
254+
Color().reset_color()

0 commit comments

Comments
 (0)