bat实现固定时间循环抓取设备log

时间:2021-07-11 09:15:17

背景:

测试时需要实时抓取android设备log,但是一份log抓取过来非常庞大(有时超过500M+,编辑器都打不开,还得找工具进行分割,甚是蛋疼),查看也非常不方便。

解决:

基于上述情况,与其之后进行分割,不如采取分段式抓取log方案。文件名以时间命名还很好进行log查看的。

实现:

由于是在win上执行,所以使用批处理实现较为方便,脚本如下:

@echo off

rem +++++++++++++++++++++++++++++++++++++++++++
rem LogcatLoop.bat
rem By zhzw @2018/12/30
rem
rem Version: 1.0
rem +++++++++++++++++++++++++++++++++++++++++++ adb wait-for-device rem counter
set count=1
set a=1111111
rem Create folder in current path
mkdir log >nul
:Start
echo ---------start %count%--------- set sdate=%date:~0,4%%date:~5,2%%date:~8,2%
set stime=%time:~0,2%%time:~3,2%%time:~6,2%
if "%stime:~0,1%"==" " set "stime=0%stime:~1%" call :Process if not %a%==000000 set a=000000 & start "logcat-win" /min cmd /c "adb logcat -c & adb logcat -b all>log\%sdate%-%stime%.txt"
rem wait 10s to the next loop
timeout /t 10 >nul
set /a count+=1
if %count%==5 goto Process
goto Start :Process
rem kill adb logcat process
for /f "skip=1 tokens=2" %%i in ('tasklist /nh /fi "WINDOWTITLE eq logcat-win"') do (
set a=%%i
@rem 强制终止线程及子线程
taskkill /f /t /pid %%i >nul
)