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.
Related
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?
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
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"})
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.
I would like to parse all logmessages (from other apps too) from inside my app at runtime. Is this somehow possible ?
Just to clarify, I want to get the logmessages without the use of eclipse, adb or any other external tool but my app only.
PS: I am using Android 2.3.
To read logs.. I do something like,
String readLogs()
{
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);
}
} catch (IOException e) {
return null;
}
return log.toString();
}
In manifest file add this permission..
<uses-permission android:name="android.permission.READ_LOGS" />