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"})
Related
I'm trying to save only the important logs of my app to keep a record that should be saved in a cloud service but I do not know how to do it properly. Currently saving all the logs and filtering them by the package name of my app, but there are some logs belonging to my app that are not shown with this method, any recommendations? thank you!
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) ) {
if(line.contains("<NAMEOFPACKAGEAPP>")) {
log.append(line);
log.append(System.getProperty("line.separator"));
}
}
} catch (IOException e) {
e.printStackTrace();
}
You can use JLog library: it supports different log levels and allows you to generate a file for your logs.
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 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.
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");
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" />