Skip to content

Commit

Permalink
Discard the oldest error rather than the newest
Browse files Browse the repository at this point in the history
  • Loading branch information
Duncan Hewett committed Jan 19, 2016
1 parent 8ac99ac commit 712aa07
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 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 Down Expand Up @@ -80,9 +82,16 @@ void write(Error error) {

// Limit number of saved errors to prevent disk space issues
File exceptionDir = new File(path);
if (exceptionDir.isDirectory() && exceptionDir.listFiles().length >= MAX_STORED_ERRORS) {
Logger.warn("Discarding error without saving to disk as stored error limit reached");
return;
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());
Expand Down

0 comments on commit 712aa07

Please sign in to comment.