Collect logcat for specified amount of time - android

My app is a root app and I have access to root shell on android phone. My requirement is to write logcat output for one minute into a file and then send the file to server. My app handles the file sending part but I am having trouble with collecting the log for specified amount of time.! I do not see any option in logcat command to specify the time duration. Is there any other way.! The following are the limitations I have
Command needs to be executed only on android phone shell. Since the device is not with me I can not use a laptop to run/schedule the command.
Command has to run only specified amount of time. I looked at -d of logcat options but it gives me log for around 5 seconds which is not sufficient for me to look at crashes.

Related

Use grep with python subproccess.check_output and adb logcat

I am trying to write a python function that captures the time it takes for an android device to get gps fix. I want to do this by capturing the timestamp of the logcat record which mentions that system time is syncronised with GPS. So I submit an adb logcat with timestamp and grep for "Setting system time at". The I want to give the timestamp value of this line to a varialble. Specifically I want to capture the characters 10-18 of this line as this reflects the exact time from device boot. Once the string has been detected once I want the function to exit and my program to continue.
I try in the following:
cmdtimegps='adb logcat -v time| grep "Setting system time at"'
w=subprocess.check_output(cmdtimegps.split())
print w
My main problem is making the command (adb logcat -v time| grep SetTimeFromGPSService) work. I get a "/system/bin/sh: grep: not found" error. Any help with this or advice on the following steps would be very welcome. Many thanks in advance,
You have a couple of problems. First, this is a piped shell command so you need to pass the string without splitting it into a list and set shell=True. Second, check_output is a bad choice here because it only returns after the program terminates but your particular command runs forever.
A better solution is to stop grepping and have python process the command. Then you can kill it when you see the data you want.
proc = subprocess.Popen(['adb', 'logcat', '-v', 'time'], stdout=subprocess.PIPE)
for line in proc.stdout:
if "Setting system time at" in line:
proc.kill()
break
proc.wait()

How to use adb socket to get the result from the phone?

I have written an executable file, and push it into /system/bin.
After run the file, it will give a result in float.
Now on PC side, I want to get this result.
At first I write this float number into a file and use 'adb pull' to pull this file, then read file.
Because I need to do this operation frequently, may 2 times per sec. This cause bad performance of the phone.
Wheather it will be little influence when I use adb socket?
Where my executable file should output?
How adb socket get the result?
Thanks.
James.
If you're leaving the phone connected, you could probably just do
adb shell /system/bin/myexecutable
and just have your binary print its output to stdout. As long as your program runs quickly, twice per second shouldn't be too fast. Otherwise, you could do
adb shell cat /somewhere/myoutfile
to see what's in a file currently.
You could potentially use logcat as a medium for getting data from your Android app to your desktop machine provided there is an ADB connection available.
My thinking is that there are two pieces:
Log your app output with logcat to a unique TAG on the Android side. For example,
Log.d("MyAppOutput", "This is the output I am looking for");
On the desktop side, you could run a command line that looks specifically for that TAG, something like:
adb logcat -s MyAppOutput
I believe this would allow you to read the results from the Android app in near realtime. If you need to know the timestamp of the log message, you could add the -v time parameter to prefix each message with a timestamp.
I had the same question before, you don't need output result to file, just output your result to a socket port, and use adb forward to get the result on your pc by adb socket. this is what you need solution.
adb forward tcp:18000 tcp:19000
this command means,pc's tcp port 18000 bind to device's tcp port 19000, if you send data to 18000 port on pc, you can get data from 19000 on device.vice versa.

Can I execute logcat on the mobile phone itself by issung a command in the shell?

How can I see timestamp in Logcat? Is there any application which shows me the log along with the timestamp? Also, how can I increase the size of the Log in andriod phone?
To get timestamps: logcat -v time
To run logcat on the device at a shell prompt: logcat
There are several free logcat viewers for Android. I've heard that aLogCat is pretty good.
EDIT
You can set your preferences in aLogCat to see the time.
Regarding your second question, I found an answer here:
The logs are held by a kernel device; the entries are in /dev/log.
The buffer is currently 64KB, and there is no way to change the size
on a production device.
The easiest way to keep more of the log is to run "logcat" and send
the output to a file. logcat runs on the device, so you could run it
there and redirect it to (say) /sdcard/log.txt.

How to sync Kernel time and logcat time?

I am working on an Android phone based on the Linux Kernel. I'm using kmsg for kernel logs and adb logcat -v time for platform logs. The problem is Kernel logs shows time from 0.000000 and logcat is showing from the start of system time ( For example, if time on the phone is 10.43.00, it'll show the first log from this time )
Now I am unable to compare events from these 2 logs as the time base ( reference) is different. Can anyone kindly point out how to sync these 2 times?
Another solution would be similar to jpg's answer, but in the other direction, redirect
the kernel messages into logcat. This is better, because too many logcat messages might overload the serial console (if you have it active).
you can run this in an android shell:
cat /proc/kmsg | while read LINE; do echo '\06kernel\0'$LINE'\0' > /dev/log/main; done
or this in a host shell:
adb shell '(cat /proc/kmsg | while read LINE; do echo \\06kernel\\0$LINE\\0 > /dev/log/main; done)'
The first time you start the command you will see all of the current dmesg messages in one place, but any further messages will be interleaved, when they appear.
and then examine logcat in a different shell. If you examine logcat with -v time, then the kernel messages will contain both the logcat and the kernel timestamps. Of course there may be delays between the two.
Another, even simpler way to see messages interleaved would be:
adb shell '(logcat & cat /proc/kmsg) > /path/to/log/file'
But in this case it's a little harder to identify messages coming from the kernel, and you can't tell how kernel timestamps relate to logcat timestamps.
you can create a single file containing both kernel and platform logs as follows:
$adb shell
$logcat -v time -f /dev/kmsg | cat /proc/kmsg > /data/klog_plog_log.txt
you can differentiate both the logs using time stamps in the platform logs. And then pull the file to your local drive using
adb pull /data/klog_plog_log.txt > sample.txt
Refer http://jai-tech.blogspot.com/2012/01/taking-kernel-and-platform-logs-of.html . Hope this helps.
Regards,
JP
Pavan,
Perhaps with you can tag your logcat prints with the time since last boot? This should be closer to the time shown in kmsg.
Time since last boot can be retrieved with elapsedRealtime().
The single file method described above is nice but may not always be useful as the input from logcat and kmsg might be delayed due to buffering. So you will most likely see a block of kmsg entries and then a block of logcat entries which might still be interleaved in real time.
That said, you might be able to sync kmsg time and logcat time by looking for device suspend messages in kmsg (grep UTC):
<6>[249485.550811] suspend: exit suspend, ret = 0 (2012-12-27 16:16:46.300872527 UTC)
As you can see those entries in kmsg report the current system wallclock time which can then be synchronized with the kmsg time value. Note however that the kmsg time value does not increase when the device is asleep while the wallclock time obviously does. To that end you will have to resynchronize on the wallclock time on every suspend entry and exit in order to get the correct wallclock time.
I think it can use like this:
adb shell logcat -v time -f /dev/kmsg | adb shell cat /proc/kmsg >sample.txt

How to keep android device log from not being cleared?

I want to dump all logs out to test a long usage of my app, maybe 2-3 days, but the logs seem to be cleared out while running after a long time.
How can I keep the logs?
I do not want to connect my device with eclipse all the day, I want the log just keep for me, like store it in the sd card for further check.
I had same requirement for my application. By default there is logcat process that tracks logs up to around 64kb (depends on device).
You can start your own logcat process using adb shell with some custom arguments. For example:
adb shell logcat -f /mnt/sdcard/large.log -r 100 -n 10
This will save up to 1mb of logs evenly distributed among 10 files (large.log, large.log.0, larget.log.1, etc.);
The next step is to launch logcat from your application:
String [] args = new String[] { Logcat, "-v", "threadtime",
"-f", logFile.getAbsolutePath(),
"-r", Integer.toString(sizePerFile),
"-n", Integer.toString(rotationCount),
"*:"+filter};
Runtime.getRuntime().exec(args);
So now your app may start saving logs. But you should be careful not to enable this in your production versions of application (or at least make this opt-in feature).
Instead of using the eclipse log use the apps like 'log viewer'. I think that app can solve your issue. I think the log will remain until you clear the log manually in 'log viewer'. I just downloaded it today morning and still has the logs of actions I performed just after I switched on the device.
Did you try dumping the log to a file?
adb logcat > log.txt

Categories

Resources