How to write logcat from emulator (device) to external file. Or trick with Continuous Integration

Published by Igor Khrupin on

Let me describe my problem.

I have Android project which is under Continuous Integration (CI) server. In my case it is Jenkins.

Then I run my tests I just can see JUnit output in Jenkins console.

If some unexpected error happens I can’t see logcat from Android emulator.

Here I want to show you how I grab logcat from Android emulator. Now If something strange happens I can see logcat from emulator.

I made it using BASH script which is bellow. This script grab logcat and periodically check logcat filesize AND can create new file for logcat if it is greater than 5MB. Script can add time postfix to file if new one file was created.

PLEASE, TELL ME IF YOU KNOW THE BETTER SOLUTION !!!

HERE the BACH script:

#!/bin/sh
LOGCATFILE=$1/logcat.out
touch $LOGCATFILE
adb -s $2 logcat -v time > $LOGCATFILE&
PID=$!

while true; do
FILESIZE=$(stat -c%s "$LOGCATFILE")
echo "Size of $LOGCATFILE = $FILESIZE bytes."
echo "PID = $PID"
if [ "$FILESIZE" -gt 5242880 ]
then
echo "$LOGCATFILE is too large = $FILESIZE bytes."
now=$(date +"%Y_%m_%d_%H_%M_%S")
NEWLOGFILENAME=/home/igor/logcat_$now.out
cp $LOGCATFILE $NEWLOGFILENAME
kill -9 $PID
adb -s $2 logcat -v time > $LOGCATFILE&
PID=$!
fi
sleep 10
done

To run just create file with executable permission and as parameter to script write path to dir with log
SECOND parameter is your device name which you can get when run adb devices in your terminal

Example:

igor@igor-laptop:~$ adb devices
List of devices attached
HT10WPL00853 device

The script parameters in my case will be next:

igor@igor-laptop:~$ ./write_logcat.sh ~/Temp HT10WPL00853

Hope, It help you!


0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.