Android device's log file - android

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!

Related

Android App continous receiving data via USB

I'm developing an Android App which i want to communicate with a device connected via USB. This device is delivering data all the time. These data are visible with a programm on the android linux shell.
My goal is to see those data in my App and after that deliver them via a Service to other Apps.
So my question is: Can i "open" a connection from my Android App to the USB-Port so im continuesly receiveing the data in my app, which are sent by the usb-device? And if yes, how would the code look like?
*Edit
Thank you for your answers, the app itself doesn't run on the commandline any more. So its no executable anymore but a shared library getting load by my android app.
It did before, but i want to be able to initiate the connection using NDK methods in my App to be able to see the data in my App itself. So I've tried to see if a connection is allready open.
I've added some functions to my code, so i can see if the usb-connection is open and i have the permission to that usb device.
UsbManager.hasPermission(device)
returns true, because I'm using an itent filter.
UsbManager.openDevice(device)
returns an UsbDeviceConnection, so i seem to have the access to use that device.
What im not capable of so far is receiving either iniciating the bus connection to by usb-device and of course either getting data input of that device.
Since my native code allready has a while(true)-method which is only using callbacks to send data to my app when actually data getting sent trough my usb-device i want to keep the work done in my c-code.
The only job my app should do is open the bus-connection once and after that be ready for callback from the c-code.
Is that possible?
If you have the app that runs on the command line, you can actually run a command line from within your app. Take a look at the Process class and the ProcessBuilder docs for full details. The example given is below
To run /system/bin/ping to ping android.com:
Process process = new ProcessBuilder()
.command("/system/bin/ping", "android.com")
.redirectErrorStream(true)
.start();
try {
InputStream in = process.getInputStream();
OutputStream out = process.getOutputStream();
readStream(in);
finally {
process.destroy();
}
}

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.

Android testing using logcat for event capturing

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?

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

wrong usage of BufferedReader

s=new Scanner(new BufferedReader(new InputStreamReader(this.clientSocket.getInputStream())));
while(s.hasNext()){
System.out.println("Am intrat in bucla s:");
longitude=Integer.parseInt(s.next());
System.out.println("Valoare longitudine:"+longitude);
latitude=Integer.parseInt(s.next());
System.out.println(latitude);
I'm using the lines above to read some data from a client-server connection;this is the server side.The data are read in scanner s and after that I try to display it,but when I look in logcat I have nothing display but this exception:
04-18 00:07:56.138: INFO/global(295):
Default buffer size used in
BufferedReader constructor. It would
be better to be explicit if an 8k-char
buffer is required.
Both my client and server are on android!Does anyone have any idea what I'm doing wrong?
This is how I read the data,I send latitude and longitude,I assume that is blank spaces delimited,the strange thing is that sometimes is working:
Cursor c=db.getAllData();
if(c.moveToFirst())
{
do{
longitude=Integer.parseInt(c.getString(1));
out.println(longitude);
latitude=Integer.parseInt(c.getString(2));
out.println(latitude);
}while(c.moveToNext());
}
The message seems to be for the BufferedReader construct.
First, I do not think you are doing anything "wrong", since you are saying that the code works as expected and the message is "INFO", not "ERROR" or even "WARNING".
Second, if you look at the BufferedReader constructor, you will see:
BufferedReader(Reader in, int size)
Constructs a new BufferedReader, providing in with size characters of buffer.
http://developer.android.com/reference/java/io/BufferedReader.html
Use that constructor instead and you should not see that message.
BTW, the logcat is full of output, some lines are more relevant than others.
Use Log.d instead of System.out.println(). Regarding System.out: http://developer.android.com/guide/developing/tools/adb.html
Viewing stdout and stderr
By default, the Android system sends
stdout and stderr (System.out and
System.err) output to /dev/null. In
processes that run the Dalvik VM, you
can have the system write a copy of
the output to the log file. In this
case, the system writes the messages
to the log using the log tags stdout
and stderr, both with priority I.
To route the output in this way, you
stop a running emulator/device
instance and then use the shell
command setprop to enable the
redirection of output. Here's how you
do it:
$ adb shell stop $ adb shell setprop
log.redirect-stdio true $ adb shell
start
The system retains this setting until
you terminate the emulator/device
instance. To use the setting as a
default on the emulator/device
instance, you can add an entry to
/data/local.prop on the device.
Is your data white-space delimited? If not, you will need to specify delimiter for your Scanner.
Do you have some exception handling code you are not showing... e.g. hiding a NumberFormatException if parseInt failed?
While you debug this issue (unless you can attach a debugger), you could log messages at points like when you accept new client connection and when you enter and exit your worker thread. This might help you see whether you are getting as far as scanning the data when you think you are receiving.

Categories

Resources