logcat -d is giving incomplete logs - android

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.

Related

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.

How to read and write logcat logs problematically

I know there are several similar questions however, I couldn't figure out what is wrong with my code. I am trying to read the program logs and write them in an activity. I basically wanted to write only my tagged Debug and Error messages to show but it does still show logs I don't want.
Here it is my code:
String[] command=new String[]{"logcat", "-d","-s","*:D"};
Process process = Runtime.getRuntime().exec(command);
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
StringBuilder log=new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
log.append("\n"+line);
}
TextView txtLogs = (TextView)findViewById(R.id.txtLogs);
txtLogs.setMovementMethod(new ScrollingMovementMethod());
txtLogs.setText(log.toString());
and i have tags like ;
Log.e(TAG, "Error onCreate() " + ex.getMessage());
and I am getting result like
D/ScanFragment(18675): message
Why do I get a log like 'message' ?
thank you

Programmatically get log cat data

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");

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();
}

How to read output of android process command

I am trying to get the output of android shell command 'getprop' with java since getprop() always returns null no matter what.
I tried this from developer.android.com:
Process process = null;
try {
process = new ProcessBuilder()
.command("/system/bin/getprop", "build.version")
.redirectErrorStream(true)
.start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
InputStream in = process.getInputStream();
//String prop = in.toString();
System.out.println(in);
process.destroy();
However what is printed is not the output but a bunch of characters and numbers (dont have the exact output right now).
How can i get the output of the process?
Thanks!
Is there any particular reason why you want to run the command as an external process?
There is a simpler way:
String android_rel_version = android.os.Build.VERSION.RELEASE;
However, if you really want to do it via a shell command, here is the way I got it to work:
try {
// Run the command
Process process = Runtime.getRuntime().exec("getprop");
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
// Grab the results
StringBuilder log = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
log.append(line + "\n");
}
// Update the view
TextView tv = (TextView)findViewById(R.id.my_text_view);
tv.setText(log.toString());
} catch (IOException e) {
}

Categories

Resources