Skip to content

Commit 54e8b95

Browse files
* Delete stale log files post the zip file's size reaches 1000kb
1 parent 20f0703 commit 54e8b95

File tree

1 file changed

+60
-38
lines changed

1 file changed

+60
-38
lines changed

app/src/main/java/fr/free/nrw/commons/CommonsApplication.java

Lines changed: 60 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -66,21 +66,21 @@
6666
import static org.acra.ReportField.USER_COMMENT;
6767

6868
@AcraCore(
69-
buildConfigClass = BuildConfig.class,
70-
resReportSendSuccessToast = R.string.crash_dialog_ok_toast,
71-
reportFormat = StringFormat.KEY_VALUE_LIST,
72-
reportContent = {USER_COMMENT, APP_VERSION_CODE, APP_VERSION_NAME, ANDROID_VERSION, PHONE_MODEL, STACK_TRACE}
69+
buildConfigClass = BuildConfig.class,
70+
resReportSendSuccessToast = R.string.crash_dialog_ok_toast,
71+
reportFormat = StringFormat.KEY_VALUE_LIST,
72+
reportContent = {USER_COMMENT, APP_VERSION_CODE, APP_VERSION_NAME, ANDROID_VERSION, PHONE_MODEL, STACK_TRACE}
7373
)
7474

7575
@AcraMailSender(
76-
mailTo = "[email protected]"
76+
mailTo = "[email protected]"
7777
)
7878

7979
@AcraDialog(
80-
resTheme = R.style.Theme_AppCompat_Dialog,
81-
resText = R.string.crash_dialog_text,
82-
resTitle = R.string.crash_dialog_title,
83-
resCommentPrompt = R.string.crash_dialog_comment_prompt
80+
resTheme = R.style.Theme_AppCompat_Dialog,
81+
resText = R.string.crash_dialog_text,
82+
resTitle = R.string.crash_dialog_title,
83+
resCommentPrompt = R.string.crash_dialog_comment_prompt
8484
)
8585

8686
public class CommonsApplication extends Application {
@@ -135,9 +135,9 @@ public void onCreate() {
135135
Mapbox.getInstance(this, getString(R.string.mapbox_commons_app_token));
136136

137137
ApplicationlessInjection
138-
.getInstance(this)
139-
.getCommonsApplicationComponent()
140-
.inject(this);
138+
.getInstance(this)
139+
.getCommonsApplicationComponent()
140+
.inject(this);
141141

142142
AppAdapter.set(new CommonsAppAdapter(sessionManager, defaultPrefs));
143143

@@ -155,8 +155,8 @@ public void onCreate() {
155155

156156
// Set DownsampleEnabled to True to downsample the image in case it's heavy
157157
ImagePipelineConfig config = ImagePipelineConfig.newBuilder(this)
158-
.setDownsampleEnabled(true)
159-
.build();
158+
.setDownsampleEnabled(true)
159+
.build();
160160
try {
161161
Fresco.initialize(this, config);
162162
} catch (Exception e) {
@@ -185,30 +185,52 @@ public void onCreate() {
185185
*
186186
*/
187187
private void initTimber() {
188-
boolean isBeta = ConfigUtils.isBetaFlavour();
189-
String logFileName = isBeta ? "CommonsBetaAppLogs" : "CommonsAppLogs";
190-
String logDirectory = LogUtils.getLogDirectory();
191-
FileLoggingTree tree = new FileLoggingTree(
192-
Log.VERBOSE,
193-
logFileName,
194-
logDirectory,
195-
1000,
196-
getFileLoggingThreadPool());
197-
198-
Timber.plant(tree);
199-
Timber.plant(new Timber.DebugTree());
188+
boolean isBeta = ConfigUtils.isBetaFlavour();
189+
String logFileName =
190+
isBeta ? "CommonsBetaAppLogs" : "CommonsAppLogs";
191+
String logDirectory = LogUtils.getLogDirectory();
192+
//Delete stale logs if they have exceeded the specified size
193+
deleteStaleLogs(logFileName, logDirectory);
194+
195+
FileLoggingTree tree = new FileLoggingTree(
196+
Log.VERBOSE,
197+
logFileName,
198+
logDirectory,
199+
1000,
200+
getFileLoggingThreadPool());
201+
202+
Timber.plant(tree);
203+
Timber.plant(new Timber.DebugTree());
200204
}
201205

206+
/**
207+
* Deletes the logs zip file at the specified directory and file locations specified in the
208+
* params
209+
*
210+
* @param logFileName
211+
* @param logDirectory
212+
*/
213+
private void deleteStaleLogs(String logFileName, String logDirectory) {
214+
try {
215+
File file = new File(logDirectory + "/zip/" + logFileName + ".zip");
216+
if (file.exists() && file.getTotalSpace() > 1000000) {// In Kbs
217+
file.delete();
218+
}
219+
} catch (Exception e) {
220+
Timber.e(e);
221+
}
222+
}
223+
202224
public static boolean isRoboUnitTest() {
203225
return "robolectric".equals(Build.FINGERPRINT);
204226
}
205227

206228
private ThreadPoolService getFileLoggingThreadPool() {
207229
return new ThreadPoolService.Builder("file-logging-thread")
208-
.setPriority(Process.THREAD_PRIORITY_LOWEST)
209-
.setPoolSize(1)
210-
.setExceptionHandler(new BackgroundPoolExceptionHandler())
211-
.build();
230+
.setPriority(Process.THREAD_PRIORITY_LOWEST)
231+
.setPoolSize(1)
232+
.setExceptionHandler(new BackgroundPoolExceptionHandler())
233+
.build();
212234
}
213235

214236
public static void createNotificationChannel(@NonNull Context context) {
@@ -217,7 +239,7 @@ public static void createNotificationChannel(@NonNull Context context) {
217239
NotificationChannel channel = manager.getNotificationChannel(NOTIFICATION_CHANNEL_ID_ALL);
218240
if (channel == null) {
219241
channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID_ALL,
220-
context.getString(R.string.notifications_channel_name_all), NotificationManager.IMPORTANCE_DEFAULT);
242+
context.getString(R.string.notifications_channel_name_all), NotificationManager.IMPORTANCE_DEFAULT);
221243
manager.createNotificationChannel(channel);
222244
}
223245
}
@@ -238,7 +260,7 @@ protected RefWatcher setupLeakCanary() {
238260
return LeakCanary.install(this);
239261
}
240262

241-
/**
263+
/**
242264
* Provides a way to get member refWatcher
243265
*
244266
* @param context Application context
@@ -269,12 +291,12 @@ public void clearApplicationData(Context context, LogoutListener logoutListener)
269291

270292
sessionManager.logout()
271293
.andThen(Completable.fromAction(() ->{
272-
Timber.d("All accounts have been removed");
273-
clearImageCache();
274-
//TODO: fix preference manager
275-
defaultPrefs.clearAll();
276-
defaultPrefs.putBoolean("firstrun", false);
277-
updateAllDatabases();
294+
Timber.d("All accounts have been removed");
295+
clearImageCache();
296+
//TODO: fix preference manager
297+
defaultPrefs.clearAll();
298+
defaultPrefs.putBoolean("firstrun", false);
299+
updateAllDatabases();
278300
}
279301
))
280302
.subscribeOn(Schedulers.io())

0 commit comments

Comments
 (0)