Skip to content

Commit

Permalink
Merge pull request #279 from ACRA/issue_278
Browse files Browse the repository at this point in the history
Handling poorly configured application log file.
  • Loading branch information
william-ferguson-au committed Jun 2, 2015
2 parents 543f876 + 0f2865d commit 90d118d
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions src/main/java/org/acra/collector/LogFileCollector.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,20 @@
package org.acra.collector;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

import org.acra.ACRA;
import org.acra.util.BoundedLinkedList;

import android.app.Application;
import android.content.Context;

import static org.acra.ACRA.LOG_TAG;

/**
* Collects the N last lines of a text stream. Use this collector if your
* application handles its own logging system.
Expand Down Expand Up @@ -54,12 +59,7 @@ private LogFileCollector() {
*/
public static String collectLogFile(Context context, String fileName, int numberOfLines) throws IOException {
final BoundedLinkedList<String> resultBuffer = new BoundedLinkedList<String>(numberOfLines);
final BufferedReader reader;
if (fileName.contains("/")) {
reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)), 1024);
} else {
reader = new BufferedReader(new InputStreamReader(context.openFileInput(fileName)), 1024);
}
final BufferedReader reader = getReader(context, fileName);
try {
String line = reader.readLine();
while (line != null) {
Expand All @@ -71,4 +71,17 @@ public static String collectLogFile(Context context, String fileName, int number
}
return resultBuffer.toString();
}

private static BufferedReader getReader(Context context, String fileName) {
try {
if (fileName.contains("/")) {
return new BufferedReader(new InputStreamReader(new FileInputStream(fileName)), 1024);
} else {
return new BufferedReader(new InputStreamReader(context.openFileInput(fileName)), 1024);
}
} catch (FileNotFoundException e) {
ACRA.log.e(LOG_TAG, "Cannot find application log file : '" + ACRA.getConfig().applicationLogFile() + "'");
return new BufferedReader(new InputStreamReader(new ByteArrayInputStream(new byte[0])));
}
}
}

0 comments on commit 90d118d

Please sign in to comment.