fix: Log file is not being created

This commit is contained in:
Ahmad Ansori Palembani 2024-11-22 15:31:15 +07:00
parent b1766ebb94
commit 53ea5bafee
Signed by: null2264
GPG key ID: BA64F8B60AF3EFB6
3 changed files with 43 additions and 36 deletions

View file

@ -23,8 +23,6 @@ import androidx.lifecycle.ProcessLifecycleOwner
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import androidx.multidex.MultiDex import androidx.multidex.MultiDex
import co.touchlab.kermit.Logger import co.touchlab.kermit.Logger
import co.touchlab.kermit.io.RollingFileLogWriter
import co.touchlab.kermit.io.RollingFileLogWriterConfig
import coil3.ImageLoader import coil3.ImageLoader
import coil3.PlatformContext import coil3.PlatformContext
import coil3.SingletonImageLoader import coil3.SingletonImageLoader
@ -59,13 +57,9 @@ import eu.kanade.tachiyomi.util.system.launchIO
import eu.kanade.tachiyomi.util.system.localeContext import eu.kanade.tachiyomi.util.system.localeContext
import eu.kanade.tachiyomi.util.system.notification import eu.kanade.tachiyomi.util.system.notification
import eu.kanade.tachiyomi.util.system.setToDefault import eu.kanade.tachiyomi.util.system.setToDefault
import eu.kanade.tachiyomi.util.system.setupFileLog
import java.security.Security import java.security.Security
import java.text.SimpleDateFormat
import java.util.Date
import java.util.Locale
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.flow.onEach
import kotlinx.io.files.Path import kotlinx.io.files.Path
@ -96,18 +90,10 @@ open class App : Application(), DefaultLifecycleObserver, SingletonImageLoader.F
private fun buildWritersToAdd( private fun buildWritersToAdd(
logPath: Path?, logPath: Path?,
logFileName: String?,
) = buildList { ) = buildList {
if (!BuildConfig.DEBUG) Logger.addLogWriter(CrashlyticsLogWriter()) if (!BuildConfig.DEBUG) add(CrashlyticsLogWriter())
if (logPath != null && logFileName != null) add( if (logPath != null) add(Logger.setupFileLog(logPath, BuildConfig.BUILD_TYPE))
RollingFileLogWriter(
config = RollingFileLogWriterConfig(
logFileName,
logFilePath = logPath,
)
)
)
} }
@SuppressLint("LaunchActivityFromNotification") @SuppressLint("LaunchActivityFromNotification")
@ -133,24 +119,8 @@ open class App : Application(), DefaultLifecycleObserver, SingletonImageLoader.F
val scope = ProcessLifecycleOwner.get().lifecycleScope val scope = ProcessLifecycleOwner.get().lifecycleScope
combine( val logPath = storageManager.getLogsDirectory()?.filePath?.let { path -> Path(path) }
storageManager.changes, Logger.setToDefault(buildLogWritersToAdd(logPath))
// Just in case we have more things to add in the future...
) { _ ->
listOf(storageManager.getLogsDirectory())
}
.distinctUntilChanged()
.onEach {
val logPath = it[0]?.filePath?.let { path -> Path(path) }
val date = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(Date())
Logger.setToDefault(
buildWritersToAdd(
logPath = logPath,
logFileName = "$date-${BuildConfig.BUILD_TYPE}.log"
)
)
}
.launchIn(scope)
basePreferences.crashReport().changes() basePreferences.crashReport().changes()
.onEach { .onEach {
@ -320,4 +290,12 @@ open class App : Application(), DefaultLifecycleObserver, SingletonImageLoader.F
} }
} }
fun buildLogWritersToAdd(
logPath: Path?,
) = buildList {
if (!BuildConfig.DEBUG) add(CrashlyticsLogWriter())
if (logPath != null) add(Logger.setupFileLog(logPath, BuildConfig.BUILD_TYPE))
}
private const val ACTION_DISABLE_INCOGNITO_MODE = "tachi.action.DISABLE_INCOGNITO_MODE" private const val ACTION_DISABLE_INCOGNITO_MODE = "tachi.action.DISABLE_INCOGNITO_MODE"

View file

@ -2,8 +2,11 @@ package yokai.domain.storage
import android.content.Context import android.content.Context
import androidx.core.net.toUri import androidx.core.net.toUri
import co.touchlab.kermit.Logger
import com.hippo.unifile.UniFile import com.hippo.unifile.UniFile
import eu.kanade.tachiyomi.buildLogWritersToAdd
import eu.kanade.tachiyomi.util.storage.DiskUtil import eu.kanade.tachiyomi.util.storage.DiskUtil
import eu.kanade.tachiyomi.util.system.setToDefault
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.channels.Channel
@ -14,6 +17,7 @@ import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.receiveAsFlow import kotlinx.coroutines.flow.receiveAsFlow
import kotlinx.coroutines.flow.shareIn import kotlinx.coroutines.flow.shareIn
import kotlinx.io.files.Path
class StorageManager( class StorageManager(
private val context: Context, private val context: Context,
@ -42,7 +46,15 @@ class StorageManager(
} }
parent.createDirectory(COVERS_PATH) parent.createDirectory(COVERS_PATH)
parent.createDirectory(PAGES_PATH) parent.createDirectory(PAGES_PATH)
parent.createDirectory(LOGS_PATH) parent.createDirectory(LOGS_PATH)?.also {
try {
Logger.setToDefault(buildLogWritersToAdd(it.filePath?.let { path -> Path(path) }))
} catch (e: Exception) {
// Just in case something went horribly wrong
Logger.setToDefault(buildLogWritersToAdd(null))
Logger.e(e) { "Something went wrong while trying to setup log file" }
}
}
} }
_changes.send(Unit) _changes.send(Unit)
} }

View file

@ -2,13 +2,30 @@ package eu.kanade.tachiyomi.util.system
import co.touchlab.kermit.LogWriter import co.touchlab.kermit.LogWriter
import co.touchlab.kermit.Logger import co.touchlab.kermit.Logger
import co.touchlab.kermit.io.RollingFileLogWriter
import co.touchlab.kermit.io.RollingFileLogWriterConfig
import co.touchlab.kermit.platformLogWriter import co.touchlab.kermit.platformLogWriter
import kotlinx.datetime.Clock
import kotlinx.datetime.TimeZone
import kotlinx.datetime.todayIn
import kotlinx.io.files.Path
fun Logger.w(e: Throwable) = w(e) { "Something is not right..." } fun Logger.w(e: Throwable) = w(e) { "Something is not right..." }
fun Logger.e(e: Throwable) = e(e) { "Something went wrong!" } fun Logger.e(e: Throwable) = e(e) { "Something went wrong!" }
fun Logger.setToDefault( fun Logger.setToDefault(
writersToAdd: List<LogWriter>, writersToAdd: List<LogWriter>,
) { ) {
Logger.setLogWriters(listOf(platformLogWriter()) + writersToAdd) Logger.setLogWriters(listOf(platformLogWriter()) + writersToAdd)
Logger.setTag("Yokai") Logger.setTag("Yokai")
} }
fun Logger.setupFileLog(logPath: Path, buildType: String? = null): LogWriter {
val date = Clock.System.todayIn(TimeZone.currentSystemDefault())
return RollingFileLogWriter(
config = RollingFileLogWriterConfig(
logFileName = date.toString() + if (buildType != null) "-$buildType-" else "" + ".log",
logFilePath = logPath,
)
)
}