Programmatically get log cat data - android

i want to get logcat data programmatically i used the below code
StringBuilder debuglog=new StringBuilder();
try {
Process process = Runtime.getRuntime().exec("logcat -d");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = bufferedReader.readLine()) != null) {
debuglog.append(line);
debuglog.append("\r\n");
}
} catch (IOException e) {
e.printStackTrace();
}
Above code is working fine
This line "logcat -d" shows -d,-w,-i messages
But,my problem is i want to show -e messages

To get only ERROR use this:
Process process = Runtime.getRuntime().exec("logcat -d *:E");

Related

Logcat is only showing my PID

I am trying to get the logcat programmatically from my application. There are plenty of tutorials online on how to do this and I am doing it in one of my fragments as such:
Process process = Runtime.getRuntime().exec("logcat -v time -d");
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
StringBuilder log=new StringBuilder();
String line = "";
while ((line = bufferedReader.readLine()) != null) {
log.append(line);
log.append("\n");
}
bufferedReader.close();
This works great; the only problem is that what is returned are only logcat entries from my PID. I would like all logcat entries. I do have READ_LOGS permission. Any ideas what is going on here?

How to fetch logs continuously from logcat programmatically?

I am fetching android logs using logcat with following piece of program:
Process process=runtime.exec("logcat");
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(process.getInputStream()));
FileOutputStream fos=new FileOutputStream("/sdcard/Pictures/logs.txt");
String line; while((line=bufferedReader.readLine())!=null){
fos.write(line.getBytes());
}
But the problem is it will fetch all logs at once and will write to the file.
However, I want to keep polling logcat for new logs and want to write the log to the file whenever there is a new log available.
Can you please suggest a way to do this ?
To fetch logcat
try {
Process process = Runtime.getRuntime().exec("logcat");
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
StringBuilder log=new StringBuilder();
String line = "";
while ((line = bufferedReader.readLine()) != null) {
log.append(line);
}
TextView tv = (TextView)findViewById(R.id.textView1);
tv.setText(log.toString());
}
catch (IOException e) {}
make sure
you run this on a separate thread
apply filters to the log , as the output can be exhaustive
http://developer.android.com/tools/debugging/debugging-log.html#outputFormat
Try to add fos.flush(); into your while cycle.
Your cycle could appear like this:
while((line=bufferedReader.readLine())!=null){
fos.write(line.getBytes());
fos.flush();
}
It is untested solution, so let me know if it would not work.

Runtime.getRuntime().exec("logcat -d") returns null

I'm trying to get the log of my app. Android 4.1.2 (Huawei tablet)
This is the code that I'm using
StringBuilder log=new StringBuilder();
try {
Process process = Runtime.getRuntime().exec("logcat -d");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = bufferedReader.readLine()) != null) {
log.append(line);
log.append(System.getProperty("line.separator"));
}
System.out.println(line == null);
} catch (IOException e) {
System.out.println("error");
e.printStackTrace();
}
I have added ther permissions to manifest
<uses-permission android:name="android.permission.READ_LOGS" />
When I try to read it bufferedReader is null and nothing is added to log variable.
EDIT:
I've founded this info
INFO
Apparently Huawei devices have service loggin disabled by default. I've tried the solution of the post but it doesn't work for me because "service loggin" option doesn't appears
Can someone help me?
Thanks
untime.getRuntime().exec(new String[]{"logcat", "-d"})

logcat -d is giving incomplete logs

I am using logcat -d to flush logs to a file programmatically. Below is the piece of code I wrote in the onCreate() to do the same:
Log.d(TAG, "First");
Log.d(TAG, "Second");
Log.d(TAG, "Third");
try {
Process process = Runtime.getRuntime().exec("logcat -d");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
StringBuilder log = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
log.append(line);
}
PrintWriter printWriter = new PrintWriter(new FileWriter(FILE_PATH));
printWriter.println(log.toString());
printWriter.close();
TextView tv = (TextView)findViewById(R.id.textView);
tv.setText(log.toString());
} catch (IOException e) {
}
The logs are not flushed completely. Sometimes, it dumps First, Second and Third, while at other times, it shows only few of the three. The worst case is, there are times when it dumps none.
To check if there are any File write issues, I also used TextView. Even the textView follows the same behavior.
Why logcat -d is behaving unexpectedly?
Restart your adb and try to see directly the ddms logcat.

How to get response of shell execution

All I want to achieve is to get the response from the execution.
For example if I execute command like this "ls"
I want to get string with all the files and directories
For example like this
Runtime.getRuntime().exec("ls");
But I do not know how to get the response.
I run something like this, I thought that I will redirect the output but still nothing
Runtime.getRuntime().exec("ls",null,new File("/sdcard/myFile") );
I tried also something like this but still nothing
Runtime.getRuntime().exec("echo | ls > /sdcard/myfile");
Any ideas ?
Have you tried this?
String cmd = "commands";
Process process = Runtime.getRuntime().exec(cmd);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
StringBuilder log = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()).length() > -1)
{
log.append(line);
}
try this code
try {
String[] commands = {"dumpstate > /sdcard/log1.txt"};
Process p = Runtime.getRuntime().exec("/system/bin/sh -");
DataOutputStream os = new DataOutputStream(p.getOutputStream());
for (String tmpCmd : commands) {
os.writeBytes(tmpCmd+"\n");
}
} catch (IOException e) {
e.printStackTrace();
}

Categories

Resources