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

Limit number of stored errors #97

Merged
merged 2 commits into from
Jan 20, 2016
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
17 changes: 17 additions & 0 deletions src/main/java/com/bugsnag/android/ErrorStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.util.Arrays;
import java.util.List;

import android.content.Context;

Expand All @@ -12,6 +14,7 @@
*/
class ErrorStore {
private static final String UNSENT_ERROR_PATH = "/bugsnag-errors/";
private static final int MAX_STORED_ERRORS = 100;

final Configuration config;
final String path;
Expand Down Expand Up @@ -77,6 +80,20 @@ public void run() {
void write(Error error) {
if(path == null) return;

// Limit number of saved errors to prevent disk space issues
File exceptionDir = new File(path);
if (exceptionDir.isDirectory()) {
File[] files = exceptionDir.listFiles();
if (files.length >= MAX_STORED_ERRORS) {
// Sort files then delete the first one (oldest timestamp)
Arrays.sort(files);
Logger.warn(String.format("Discarding oldest error as stored error limit reached (%s)", files[0].getPath()));
if (!files[0].delete()) {
files[0].deleteOnExit();
}
}
}

String filename = String.format("%s%d.json", path, System.currentTimeMillis());
Writer out = null;
try {
Expand Down