I want to get the whole log (Log.d(...)), after pressing a button to analyse some parts of our app (count something...). I'm able to do this by the following code:
HashMap<String, Integer> hashMapToSaveStuff = new HashMap<String, Integer>();
int count= 0;
String toCount= "";
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("MYSTRING")) {
toCount = line.substring(line.indexOf(":") + 1);
if (hashMapToSaveStuff.containsKey(toCount)) {
count = hashMapToSaveStuff.get(toCount);
count++;
} else {
count= 1;
}
hashMapToSaveStuff.put(toCount, count);
}
}
} catch (Exception e) {
}
After that I'll send the result to our server and save it on database. Because of that I want to clear all the logs, I've already send. Trying to do this with the following code didn't work:
try {
Process process = Runtime.getRuntime().exec("logcat -c");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()), 1024);
String line = bufferedReader.readLine();
} catch (Exception e) {
}
How can I clear the log?
This code has worked for me in the past:
Process process = new ProcessBuilder()
.command("logcat", "-c")
.redirectErrorStream(true)
.start();
Related
I want to know how to capture logs with in mobile phone
Does this network logs can be used to measure page load time ?
I tried capturing device logs using adb logcat in desktop but I want to capture those logs with in device
Assuming your logs are located in a String file, you can save them to a text file like this:
private void saveLogsToFile(String logs) {
try {
OutputStreamWriter out = new OutputStreamWriter(openFileOutput("saved_logs.txt", Context.MODE_PRIVATE));
out.write(logs);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
And then read them back from the file like this:
private String readSavedLogs() {
String logs = "";
try {
InputStream inputStream = openFileInput("saved_logs.txt");
if(inputStream != null) {
InputStreamReader in = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(in);
String line;
StringBuilder stringBuilder = new StringBuilder();
while((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line).append("\n");
}
inputStream.close();
logs = stringBuilder.toString();
}
} catch (IOException e) {
e.printStackTrace();
}
return logs;
}
i try read the log generated from "CallNotifier" and "PhoneUtils" but i can't,
i use two ways for this: both are thread (AsyncTask)
1:
try {
Process process = Runtime.getRuntime().exec("logcat");
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
String line = "";
while ((line = bufferedReader.readLine()) != null) {
if (line.contains("CallNotifier")) {
publishProgress(line);
}
}
}
catch (IOException e) {
}
on this way i read all catlog but i filter if the line contains "CallNotifier"
2:
try {
Process process = Runtime.getRuntime().exec("logcat -s CallNotifier");
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
String line = "";
while ((line = bufferedReader.readLine()) != null) {
publishProgress(line);
}
}
catch (IOException e) {
}
This way i filter in
exec("logcat -s CallNotifier");
Don't work
When I filter tag generated from my app the code works. if i filter from PC in terminal i can see log from CallNotifier the problem its when i filter from Android.
I am using the following code to call android equipment ping command:
public static String pingServer() {
PingResult result = new PingResult();
String jsonString = null;
try {
String command = "ping -c 3 192.168.8.185";
Process p = Runtime.getRuntime().exec(command);
int status = p.waitFor();
InputStream input = p.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(input));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = in.readLine()) != null) {
buffer.append(line);
buffer.append("\n");
}
String bufferStr = buffer.toString();
System.out.println(bufferStr);
} catch (Exception e) {
System.out.println("---------------exception-----------ping");
System.out.println(e.getMessage());
// e.printStackTrace();
}
}
BufferStr always obtain the last line data.
3 packets transmitted, 3 received, 0% packet loss, time 2000ms
The details result can be get.
It may be simpler and easier to use InetAddress.isReachable:
if (InetAddress.getByName("192.168.8.185").isReachable(6000))
...
I want to save my app logcat events in a text file on sd card.
my alarming app work properly in my and my friends devices, but other have error on my app.
for example they say alarms in app are in wrong time, but i dont see this error in my and my friends devices.
Because of this issue and other issues, i want save all events logcat related my app, atomatically. so they send log file to me to solve issues.
how can i do this?
thanks
sorry for my bad english
You can get logcat via the following:
static final int BUFFER_SIZE = 1024;
public String getLogCat() {
String[] logcatArgs = new String[] {"logcat", "-v", "time"};
Process logcatProc = null;
try {
logcatProc = Runtime.getRuntime().exec(logcatArgs);
}
catch (IOException e) {
return null;
}
BufferedReader reader = null;
String response = null;
try {
String separator = System.getProperty("line.separator");
StringBuilder sb = new StringBuilder();
reader = new BufferedReader(new InputStreamReader(logcatProc.getInputStream()), BUFFER_SIZE);
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
sb.append(separator);
}
response = sb.toString();
}
catch (IOException e) {
}
finally {
if (reader != null) {
try {
reader.close();
}
catch (IOException e) {}
}
}
return response;
}
You can then save this String to the sdcard.
You can get logcat via the following:
static final int BUFFER_SIZE = 1024;
public String getLogCat() {
String[] logcatArgs = new String[] {"logcat", "-v", "time"};
Process logcatProc = null;
try {
logcatProc = Runtime.getRuntime().exec(logcatArgs);
}
catch (IOException e) {
return null;
}
BufferedReader reader = null;
String response = null;
try {
String separator = System.getProperty("line.separator");
StringBuilder sb = new StringBuilder();
reader = new BufferedReader(new InputStreamReader(logcatProc.getInputStream()), BUFFER_SIZE);
String line;
while ((line = reader.readLine()) != null) {
sb.append(line);
sb.append(separator);
}
response = sb.toString();
}
catch (IOException e) {
}
finally {
if (reader != null) {
try {
reader.close();
}
catch (IOException e) {}
}
}
return response;
}
You can then save this String to the sdcard.
This answer from "Dororo" didn't work for me since it always got stuck in the while due to to many lines, but i have no idea how to fix that.
the logcat will block for reading new logs unless you specify the '-d' arg.
try
String[] logcatArgs = new String[] {"logcat", "-d", "-v", "time"};
This type of functionality is already implemented by the ACRA Android library. The library detects crashes, and send the crash information to either a Google Docs spreadsheet, or your own destination.
Execute within a thread to avoid ANRs
as an error report I want to send the device and/or application log to a email account. For that I prepared a button in my view. Can someone help me with that, how can I retrieve the log files?
Thanks
String separator = System.getProperty("line.separator");
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(separator);
}
} catch (Exception e) {
}
Here's a slightly improved version, adapted from Ramesh (thanks). The main difference is to do your own buffering. It's fast.
private void extractLogToFile()
{
// Make file name.
String fullName = ...;
// Extract to file.
File file = new File (fullName);
InputStreamReader reader = null;
FileWriter writer = null;
try
{
// get input stream
String cmd = "logcat -d -v time";
Process process = Runtime.getRuntime().exec(cmd);
reader = new InputStreamReader (process.getInputStream());
// write output stream
writer = new FileWriter (file);
char[] buffer = new char[10000];
do
{
int n = reader.read (buffer, 0, buffer.length);
if (n == -1)
break;
writer.write (buffer, 0, n);
} while (true);
reader.close();
writer.close();
}
catch (IOException e)
{
if (writer != null)
try {
writer.close();
} catch (IOException e1) {
}
if (reader != null)
try {
reader.close();
} catch (IOException e1) {
}
e.printStackTrace();
return;
}
}