Skip to content

Commit e54b2c0

Browse files
committed
Feature/logging (#26)
* Timber impl patch 1 * Timber impl patch 2 * Timber impl patch 3
1 parent 67d0c24 commit e54b2c0

File tree

51 files changed

+385
-166
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+385
-166
lines changed

app/app.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ dependencies {
8181
implementation di.koinCompose
8282
implementation reactive.rxkotlin
8383
implementation reactive.rxandroid
84+
implementation log.timber
8485

8586
playstoreImplementation platform(proprietary.firebaseBom)
8687
playstoreImplementation proprietary.firebaseAnalytics

app/src/main/java/com/shifthackz/aisdv1/app/AiStableDiffusionClientApp.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import android.app.Application
44
import com.shifthackz.aisdv1.app.di.featureModule
55
import com.shifthackz.aisdv1.app.di.preferenceModule
66
import com.shifthackz.aisdv1.app.di.providersModule
7+
import com.shifthackz.aisdv1.core.common.log.FileLoggingTree
78
import com.shifthackz.aisdv1.core.imageprocessing.di.imageProcessingModule
89
import com.shifthackz.aisdv1.core.validation.di.validatorsModule
910
import com.shifthackz.aisdv1.data.di.dataModule
@@ -14,12 +15,14 @@ import com.shifthackz.aisdv1.presentation.di.presentationModule
1415
import com.shifthackz.aisdv1.storage.di.databaseModule
1516
import org.koin.android.ext.koin.androidContext
1617
import org.koin.core.context.startKoin
18+
import timber.log.Timber
1719

1820
class AiStableDiffusionClientApp : Application() {
1921

2022
override fun onCreate() {
2123
super.onCreate()
2224
initializeKoin()
25+
initializeLogging()
2326
}
2427

2528
private fun initializeKoin() = startKoin {
@@ -38,4 +41,11 @@ class AiStableDiffusionClientApp : Application() {
3841
*presentationModule,
3942
)
4043
}
44+
45+
private fun initializeLogging() {
46+
Timber.plant(FileLoggingTree())
47+
if (BuildConfig.DEBUG) {
48+
Timber.plant(Timber.DebugTree())
49+
}
50+
}
4151
}

app/src/main/java/com/shifthackz/aisdv1/app/di/ProvidersModule.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package com.shifthackz.aisdv1.app.di
22

33
import com.shifthackz.aisdv1.app.BuildConfig
4+
import com.shifthackz.aisdv1.core.common.appbuild.BuildInfoProvider
5+
import com.shifthackz.aisdv1.core.common.appbuild.BuildType
6+
import com.shifthackz.aisdv1.core.common.appbuild.BuildVersion
47
import com.shifthackz.aisdv1.core.common.file.FileProviderDescriptor
58
import com.shifthackz.aisdv1.core.common.links.LinksProvider
69
import com.shifthackz.aisdv1.core.common.schedulers.SchedulersProvider
7-
import com.shifthackz.aisdv1.domain.appbuild.BuildInfoProvider
8-
import com.shifthackz.aisdv1.domain.appbuild.BuildType
9-
import com.shifthackz.aisdv1.domain.entity.AppVersion
1010
import com.shifthackz.aisdv1.network.qualifiers.ApiUrlProvider
1111
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
1212
import io.reactivex.rxjava3.core.Scheduler
@@ -43,7 +43,7 @@ val providersModule = module {
4343
object : BuildInfoProvider {
4444
override val isDebug: Boolean = BuildConfig.DEBUG
4545
override val buildNumber: Int = BuildConfig.VERSION_CODE
46-
override val version: AppVersion = AppVersion(BuildConfig.VERSION_NAME)
46+
override val version: BuildVersion = BuildVersion(BuildConfig.VERSION_NAME)
4747
override val buildType: BuildType = BuildType.parse(BuildConfig.BUILD_FLAVOR_TYPE)
4848

4949
override fun toString(): String = buildString {
@@ -68,6 +68,7 @@ val providersModule = module {
6868
object : FileProviderDescriptor {
6969
override val providerPath: String = "${androidApplication().packageName}.fileprovider"
7070
override val imagesCacheDirPath: String = "${androidApplication().cacheDir}/images"
71+
override val logsCacheDirPath: String = "${androidApplication().cacheDir}/logs"
7172
}
7273
}
7374
}

app/src/main/res/xml/file_provider_paths.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
name="folder_images"
55
path="images/" />
66

7+
<cache-path
8+
name="folder_logs"
9+
path="logs/" />
10+
711
<external-path name="media" path="." />
812
<external-path name="external_files" path="."/>
913
</paths>

core/common/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,7 @@ android {
1010
}
1111

1212
dependencies {
13+
implementation di.koinCore
1314
implementation reactive.rxjava
15+
implementation log.timber
1416
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.shifthackz.aisdv1.core.common.appbuild
2+
3+
interface BuildInfoProvider {
4+
val isDebug: Boolean
5+
val buildNumber: Int
6+
val version: BuildVersion
7+
val buildType: BuildType
8+
}

domain/src/main/java/com/shifthackz/aisdv1/domain/appbuild/BuildType.kt renamed to core/common/src/main/java/com/shifthackz/aisdv1/core/common/appbuild/BuildType.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.shifthackz.aisdv1.domain.appbuild
1+
package com.shifthackz.aisdv1.core.common.appbuild
22

33
enum class BuildType {
44
FOSS,

domain/src/main/java/com/shifthackz/aisdv1/domain/entity/AppVersion.kt renamed to core/common/src/main/java/com/shifthackz/aisdv1/core/common/appbuild/BuildVersion.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
package com.shifthackz.aisdv1.domain.entity
1+
package com.shifthackz.aisdv1.core.common.appbuild
22

3-
class AppVersion : Comparable<AppVersion> {
3+
class BuildVersion : Comparable<BuildVersion> {
44
private var major: Int = 0
55
private var minor: Int = 0
66
private var patch: Int = 0
@@ -37,7 +37,7 @@ class AppVersion : Comparable<AppVersion> {
3737
}
3838
}
3939

40-
override fun compareTo(other: AppVersion): Int {
40+
override fun compareTo(other: BuildVersion): Int {
4141
if (this.major > other.major) return 1
4242
if (this.major < other.major) return -1
4343
if (this.minor > other.minor) return 1
@@ -51,7 +51,7 @@ class AppVersion : Comparable<AppVersion> {
5151
if (this === other) return true
5252
if (javaClass != other?.javaClass) return false
5353

54-
other as AppVersion
54+
other as BuildVersion
5555

5656
if (major != other.major) return false
5757
if (minor != other.minor) return false

core/common/src/main/java/com/shifthackz/aisdv1/core/common/file/FileProviderDescriptor.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ package com.shifthackz.aisdv1.core.common.file
33
interface FileProviderDescriptor {
44
val providerPath: String
55
val imagesCacheDirPath: String
6+
val logsCacheDirPath: String
67
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package com.shifthackz.aisdv1.core.common.log
2+
3+
import android.util.Log
4+
import com.shifthackz.aisdv1.core.common.appbuild.BuildInfoProvider
5+
import com.shifthackz.aisdv1.core.common.file.FileProviderDescriptor
6+
import com.shifthackz.aisdv1.core.common.schedulers.SchedulersProvider
7+
import org.koin.core.component.KoinComponent
8+
import org.koin.core.component.inject
9+
import timber.log.Timber
10+
import java.io.File
11+
import java.io.FileOutputStream
12+
import java.text.SimpleDateFormat
13+
import java.util.*
14+
15+
class FileLoggingTree : Timber.Tree(), KoinComponent {
16+
17+
private val fileProviderDescriptor: FileProviderDescriptor by inject()
18+
private val schedulersProvider: SchedulersProvider by inject()
19+
private val buildInfoProvider: BuildInfoProvider by inject()
20+
21+
private val formatDate: String
22+
get() = "[${SimpleDateFormat(LOGGER_TIMESTAMP_FORMAT, Locale.ROOT).format(Date())}]"
23+
24+
private val formatPriority: (Int) -> String = {
25+
when (it) {
26+
Log.ASSERT -> "[A]"
27+
Log.DEBUG -> "[D]"
28+
Log.ERROR -> "[E]"
29+
Log.INFO -> "[I]"
30+
Log.VERBOSE -> "[V]"
31+
Log.WARN -> "[W]"
32+
else -> "[U]"
33+
}
34+
}
35+
36+
private val formatTag: (String?) -> String = { tag ->
37+
tag?.let { "[$it]" } ?: LOGGER_DEFAULT_TAG
38+
}
39+
40+
init {
41+
writeLine(buildString {
42+
appendLine("=== APP SESSION STARTED ===")
43+
appendLine()
44+
appendLine("Version : $buildInfoProvider")
45+
appendLine("Type : ${buildInfoProvider.buildType}")
46+
appendLine()
47+
})
48+
}
49+
50+
override fun log(priority: Int, tag: String?, message: String, t: Throwable?) {
51+
val log = buildString {
52+
append(formatDate)
53+
append(formatPriority(priority))
54+
append(" ")
55+
append(formatTag(tag))
56+
append(" : ")
57+
append(message)
58+
t?.stackTraceToString()?.let { stacktrace ->
59+
appendLine()
60+
append(stacktrace)
61+
}
62+
appendLine()
63+
}
64+
writeLine(log)
65+
}
66+
67+
private fun writeLine(message: String) = schedulersProvider.singleThread.execute {
68+
run {
69+
val cacheDirectory = File(fileProviderDescriptor.logsCacheDirPath)
70+
if (!cacheDirectory.exists()) cacheDirectory.mkdirs()
71+
val outFile = File(cacheDirectory, LOGGER_FILENAME)
72+
if (!outFile.exists()) outFile.createNewFile()
73+
FileOutputStream(outFile, true).use { fos ->
74+
val payload = message.toByteArray()
75+
fos.write(payload)
76+
fos.close()
77+
}
78+
}
79+
}
80+
81+
companion object {
82+
private const val LOGGER_TIMESTAMP_FORMAT = "dd.MM.yyyy HH:mm:SS"
83+
private const val LOGGER_DEFAULT_TAG = "[SDAI]"
84+
85+
const val LOGGER_FILENAME = "sdaiv1.log"
86+
87+
fun clearLog(fileProviderDescriptor: FileProviderDescriptor) {
88+
val cacheDirectory = File(fileProviderDescriptor.logsCacheDirPath)
89+
cacheDirectory.deleteRecursively()
90+
}
91+
}
92+
}

0 commit comments

Comments
 (0)