How to read and write logcat logs problematically - android

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

Related

Logs in my code do not display when getting logcat programmatically

My android code contains Log.e. When getting logcat programmatically, those logs are not displayed. My code for logcat is
String command = "logcat MyApp:V";
Process process = Runtime.getRuntime().exec(command);
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
StringBuilder result = new StringBuilder();
String currentLine;
while ((currentLine = reader.readLine()) != null) {
result.append(currentLine);
result.append("\n");
}
Log.e(TAG, "printLog: " + result.toString());
Dont use result.toString.. Instead use resultLog.e(TAG, "printLog: " + result);

How to send Command prompt output to android

I am creating an app in which it will send some command to server and i want to get the output of that command on client (android).
Basically i am sending command "systeminfo" and the output is too big to handle, so is there any way to get that big output on android as text view or anything else?
Code is as below
Process process = Runtime.getRuntime().exec("systeminfo");
and for get the output i have used
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
I am confused how to use it as too much of string. Any reference material would be appreciated.
I'd recommend using a StringBuilder to reconstruct each line from the BufferedReader
StringBuilder builder = new StringBuilder();
String line = null;
while ((line = br.readLine()) != null) {
builder.append(line).append("\n"); // Remove the \n if you don't want newlines
}
final String execOutput = builder.toString();

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.

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.

Categories

Resources