Android testing using logcat for event capturing - android

I have been googling about this issue for hours, and I'm stumped.
Im trying to write a comprehensive test suite for android devices, and my first idea was to validate test results using logcat output. Logcat has all the event info I need from both the OS and our apps.
Using Robotium or Monkeyrunner, I found no way to access logcat on the fly.
Creating an external app to monitor logcat is out of the question (hard to sync events to results).
Any ideas guys?

Try this. Add the following permission to your Robotium manifest file:
<uses-permission android:name="android.permission.READ_LOGS" />
Then create a thread in your setup method, and have it perform the following:
Process proc = Runtime.getRuntime().exec("logcat -d");
BufferedReader reader = new BufferedReader(new
InputStreamReader(proc.getInputStream()));
Then read using:
reader.readLine()

Use logcat -d -v time option at regular checkpoints,parse it then proceed.
will this help?

Related

Any way to grab a snapshot of current logcat output without opening up the output stream?

I'm trying to grab some filtered output from logcat through command line, but would prefer to not have a logcat stream opened up for automation purposes.
Here's my use case: I want to clear logcat, perform some instrumentation tests with AccessibilityChecks enabled, then check logcat for Accessibility errors that the class has found in my UI.
Currently, here's what I can do via command line:
//clear logcat history:
adb logcat -c
// Run instrumentation tests at this time.
// Run following command when tests done:
adb logcat AccessibilityValidator:E *:S
// ctrl-c to close stream
This does what it is supposed to. I will see just the Accessibility errors printed out. The problem is that I do not want to open up a logcat stream. I'd like to get the results as is, right at the point that my instrumentation tests have completed. Opening up a stream is not very ideal as I would then need to pipe in a ctrl-C to my pipeline to close the stream. I would really like to get a snapshot of the output in plain text. Is such a thing possible?
Thank you!
Edit: I was able to find what I believe is a working solution - I just needed to add the -d flag. This will not open a stream and will print the contents of the logcat file. Combine with my filtering, I am able to get exactly what I was looking for.
I was able to find what I believe is a working solution - I just needed to add the -d flag. -d will dump the logs and exit. This will not open a stream and will print the contents of the logcat file to stdout. Combine with my filtering, I am able to get exactly what I was looking for.
The command I needed is:
adb logcat -d AccessibilityValidator:E *:S

Sending AT Commands in Android Using atinout library

As the question title suggest, I'm going to use atinout library in my android application to be able to send AT Commands to my device's gsm modem and get the response back.
I've searched the SO and other blogs and the best code I got is something like this, which does not cauese anything to be written to output :
Runtime r = Runtime.getRuntime();
Process process = r.exec(new String[] {"su", "-c", "echo -n -e 'AT\r' > /dev/smd0"});
BufferedReader in = new BufferedReader( new InputStreamReader(process.getInputStream()));
String line = "";
while ((line = in.readLine()) != null)
{
output+= line ;
}
tv.setText(output);
So my main concerns are :
1- How to send a simple AT commmad (like AT Ok) to the device's gsm modem and "print" the response back ?
2- I have no idea how to use the atinout library in an android application project, since it is written in C language.
By the way :
I have successfully rooted my Sony Xperia M Dual (C2005) phone and have SuperUser installed on it, so this prerequisite is met.
Also, I think I need some fundamental training on Unix commands and Serial communication and I really love to learn any needed material.
Finally, I'm on a tight deadline ! So the more straightforward , the better!
P.S. I don't want someone to write the code for me, I just need the guidelines.
Thanks for your help.
Step 0. Compile atinout for android and install it. For this I have no experience and cannot give any help other than some general guidelines that you need to replace CC = gcc in the Makefile with an appropriate cross compiler.
Step 1. Write all the AT commands you want to run into a file input.txt (using normal '\n' line endings, there is no need to mess with '\r' here).
Step 2. Change the exec line to
Process process = r.exec(new String[] {"su", "-c",
"atinout input.txt /dev/smd0 output.txt"});
Wait for the process to finish execution (process.waitFor() I think).
Step 3. Open the file output.txt and read the output from this instead of process.getInputStream().

How can I get logcat on my device to show logs from all processes

I'm trying to write an app that reads all logs on my device. I've got a client/service architecture, and I see log messages from both the client and service processes but I don't see any messages from any other applications on the phone (I do see other messages using the desktop logcat).
Do I need root?
Code Snippets
Manifest
<uses-permission android:name="android.permission.READ_LOGS" />
Log Reader
Runtime.getRuntime().exec("logcat -c").waitFor();
Process process = Runtime.getRuntime().exec("logcat -v long *:*");
BufferedReader reader =
new BufferedReader(new InputStreamReader(process.getInputStream()));
while (true) {
String nextLine = reader.readLine();
if (!nextLine.contains("LogWatcher-D")) {
Log.w("LogWatcher-D", "See: " + nextLine);
}
// Process line
}
On Android 4.1+, you can only access log messages logged by your process, unless you hold the READ_LOGS permission. That permission requires either that your app be signed by the same signing key that signed the device's firmware, or that your app is installed on the system partition.
To read logs from other applications programmatically, you will need to assign the same android:sharedUserId in all apks' manifest files.
There is another way to access all logs for all applications without root. It requires enabling remote debugging. Take a look at the open source rootless Logcat app.

Log Just My App and View it Remotely

I have an application/phone which I am developing to be given to people who will be using it in remote areas and not so tech-savvy. I want a mechanism to be able to read their log files remotely. The android app is making use of the logcat logging. I want to be able to just read my logs for my app by some remote mechanism. I was thinking more along the lines of reading the logcat then posting that up periodically to a REST service where I can database these. So question is how do I read the Logcat files programatically from my app?
If you invoke:
Proccess p = Runtime.getRuntime().exec("logcat");
InputStream in = p.getInputStream();
//code to process the InputStream
I honestly do not know the entire process of getting the input stream back, but the code above will allow your app to read the logcat file. You can go here for possible command-line paramaters for "logcat". The exec method runs the string through a linux shell, btw.
You must have the permission READ_LOGS

Android device's log file

using logcat I can see the log messages generated by my app in the emulator. How can I read/retrieve the same log file but this time from the device the app is running on ? The device is not attached to any computer and the log file has to be sent via email.
To read the log from within your application you need to have the android.permission.READ_LOGS permission.
Once you have that, you could start up a process to read logcat with something like
Process logcatProcess = Runtime.getRuntime().exec("logcat");
Then you can create a buffered reader from it:
BufferedReader logcat = new BufferedReader(new InputStreamReader(logcatProcess.getInputStream()), 8192);
From here you can String s = logcat.readLine(); to read the log.
There is a log collector project that you can use from your code/application. If you want an already existing application, try Log Collector.
Check out this app on the market. It does what you need and it is open source!

Categories

Resources