Separated File-Logging Concern

This commit is contained in:
s-ayush2903 2020-06-20 01:48:16 +05:30
parent e8c68a7eb2
commit e985bab6f6
No known key found for this signature in database
GPG Key ID: A09DB233DBE49A8E
5 changed files with 68 additions and 38 deletions

View File

@ -29,7 +29,7 @@ import org.kiwix.kiwixmobile.core.data.local.KiwixDatabase
import org.kiwix.kiwixmobile.core.di.components.CoreComponent
import org.kiwix.kiwixmobile.core.di.components.DaggerCoreComponent
import org.kiwix.kiwixmobile.core.downloader.DownloadMonitor
import org.kiwix.kiwixmobile.core.utils.files.FileUtils.writeLogFile
import org.kiwix.kiwixmobile.core.utils.files.FileLogger
import javax.inject.Inject
abstract class CoreApp : Application() {
@ -61,6 +61,8 @@ abstract class CoreApp : Application() {
@Inject
internal lateinit var jniInitialiser: JNIInitialiser
private lateinit var fileLogger: FileLogger
override fun attachBaseContext(base: Context) {
super.attachBaseContext(base)
if (BuildConfig.DEBUG) {
@ -75,7 +77,7 @@ abstract class CoreApp : Application() {
.context(this)
.build()
AndroidThreeTen.init(this)
writeLogFile()
fileLogger.writeLogFile()
coreComponent.inject(this)
kiwixDatabase.forceMigration()
downloadMonitor.init()

View File

@ -60,8 +60,7 @@ import org.kiwix.kiwixmobile.core.reader.ZimReaderContainer;
import org.kiwix.kiwixmobile.core.utils.AlertDialogShower;
import org.kiwix.kiwixmobile.core.utils.KiwixDialog;
import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil;
import static org.kiwix.kiwixmobile.core.utils.files.FileUtils.isExternalStorageWritable;
import org.kiwix.kiwixmobile.core.utils.files.FileLogger;
/**
* Created by @author Aditya-Sood (21/05/19) as a part of GSoC 2019
@ -100,6 +99,7 @@ public class AddNoteDialog extends DialogFragment {
@Inject SharedPreferenceUtil sharedPreferenceUtil;
@Inject ZimReaderContainer zimReaderContainer;
@Inject protected AlertDialogShower alertDialogShower;
FileLogger fileLogger;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
@ -326,7 +326,7 @@ public class AddNoteDialog extends DialogFragment {
* "{External Storage}/Kiwix/Notes/ZimFileTitle/ArticleTitle.txt"
* */
if (isExternalStorageWritable()) {
if (fileLogger.isExternalStorageWritable()) {
if (ContextCompat.checkSelfPermission(getContext(),
Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {

View File

@ -50,11 +50,11 @@ import org.kiwix.kiwixmobile.core.utils.DialogShower;
import org.kiwix.kiwixmobile.core.utils.KiwixDialog;
import org.kiwix.kiwixmobile.core.utils.LanguageUtils;
import org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil;
import org.kiwix.kiwixmobile.core.utils.files.FileLogger;
import static org.kiwix.kiwixmobile.core.utils.ConstantsKt.RESULT_RESTART;
import static org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil.PREF_NIGHT_MODE;
import static org.kiwix.kiwixmobile.core.utils.SharedPreferenceUtil.PREF_STORAGE;
import static org.kiwix.kiwixmobile.core.utils.files.FileUtils.isExternalStorageWritable;
public abstract class CorePrefsFragment extends PreferenceFragmentCompat implements
SettingsContract.View,
@ -77,6 +77,7 @@ public abstract class CorePrefsFragment extends PreferenceFragmentCompat impleme
protected NightModeConfig nightModeConfig;
@Inject
protected DialogShower alertDialogShower;
FileLogger fileLogger;
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
@ -218,7 +219,7 @@ public abstract class CorePrefsFragment extends PreferenceFragmentCompat impleme
}
private void clearAllNotes() {
if (isExternalStorageWritable()) {
if (fileLogger.isExternalStorageWritable()) {
if (ContextCompat.checkSelfPermission(getActivity(),
Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {

View File

@ -0,0 +1,58 @@
/*
* Kiwix Android
* Copyright (c) 2020 Kiwix <android.kiwix.org>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package org.kiwix.kiwixmobile.core.utils.files
import android.os.Environment
import android.util.Log
import java.io.File
import java.io.IOException
class FileLogger {
fun writeLogFile() {
if (isExternalStorageWritable) {
val appDirectory =
File(Environment.getExternalStorageDirectory().toString() + "/Kiwix")
val logFile = File(appDirectory, "logcat.txt")
Log.d("KIWIX", "Writing all logs into [" + logFile.path + "]")
// create a new app folder
if (!appDirectory.exists()) {
appDirectory.mkdir()
}
if (logFile.exists() && logFile.isFile) {
logFile.delete()
}
// clear the previous logcat and then write the new one to the file
try {
logFile.createNewFile()
Runtime.getRuntime().exec("logcat -c")
Runtime.getRuntime().exec("logcat -f " + logFile.path + " -s kiwix")
} catch (e: IOException) {
Log.e("KIWIX", "Error while writing logcat.txt", e)
}
}
}
/* Checks if external storage is available for read and write */
val isExternalStorageWritable: Boolean
/** private modifier is temp */
get() = Environment.MEDIA_MOUNTED == Environment.getExternalStorageState()
}

View File

@ -31,7 +31,6 @@ import org.kiwix.kiwixmobile.core.entity.LibraryNetworkEntity.Book
import org.kiwix.kiwixmobile.core.extensions.get
import java.io.File
import java.io.IOException
import java.lang.Runtime.getRuntime
import java.util.ArrayList
object FileUtils {
@ -220,34 +219,4 @@ object FileUtils {
File("$fileName.part").exists() -> "$fileName.part"
else -> "${fileName}aa"
}
@JvmStatic fun writeLogFile() {
if (isExternalStorageWritable) {
val appDirectory =
File(Environment.getExternalStorageDirectory().toString() + "/Kiwix")
val logFile = File(appDirectory, "logcat.txt")
Log.d("KIWIX", "Writing all logs into [" + logFile.path + "]")
// create a new app folder
if (!appDirectory.exists()) {
appDirectory.mkdir()
}
if (logFile.exists() && logFile.isFile) {
logFile.delete()
}
// clear the previous logcat and then write the new one to the file
try {
logFile.createNewFile()
getRuntime().exec("logcat -c")
Runtime.getRuntime().exec("logcat -f " + logFile.path + " -s kiwix")
} catch (e: IOException) {
Log.e("KIWIX", "Error while writing logcat.txt", e)
}
}
}
/* Checks if external storage is available for read and write */
@JvmStatic val isExternalStorageWritable: Boolean
get() = Environment.MEDIA_MOUNTED == Environment.getExternalStorageState()
}