-
Notifications
You must be signed in to change notification settings - Fork 205
Commit
Limit number of stored errors
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
||
|
@@ -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; | ||
|
@@ -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); | ||
This comment has been minimized.
Sorry, something went wrong. |
||
Logger.warn(String.format("Discarding oldest error as stored error limit reached (%s)", files[0].getPath())); | ||
if (!files[0].delete()) { | ||
files[0].deleteOnExit(); | ||
This comment has been minimized.
Sorry, something went wrong.
d4rken
Contributor
|
||
} | ||
} | ||
} | ||
|
||
String filename = String.format("%s%d.json", path, System.currentTimeMillis()); | ||
Writer out = null; | ||
try { | ||
|
What guarantees that this always sorts such that index 0 is the oldest one?