Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling poorly configured application log file. #279

Merged
merged 1 commit into from
Jun 2, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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])));
}
}
}