-
-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #317 from gotify/log
Migrate to tinylog
- Loading branch information
Showing
23 changed files
with
166 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
app/src/main/kotlin/com/github/gotify/GotifyApplication.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.github.gotify | ||
|
||
import android.app.Application | ||
import android.app.NotificationManager | ||
import android.os.Build | ||
import androidx.preference.PreferenceManager | ||
import com.github.gotify.log.LoggerHelper | ||
import com.github.gotify.log.UncaughtExceptionHandler | ||
import com.github.gotify.settings.ThemeHelper | ||
import org.tinylog.kotlin.Logger | ||
|
||
class GotifyApplication : Application() { | ||
override fun onCreate() { | ||
LoggerHelper.init(this) | ||
UncaughtExceptionHandler.registerCurrentThread() | ||
Logger.info("${javaClass.simpleName}: onCreate") | ||
|
||
val theme = PreferenceManager.getDefaultSharedPreferences(this) | ||
.getString(getString(R.string.setting_key_theme), getString(R.string.theme_default))!! | ||
ThemeHelper.setTheme(this, theme) | ||
|
||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | ||
NotificationSupport.createForegroundChannel( | ||
this, | ||
this.getSystemService(NotificationManager::class.java) | ||
) | ||
} | ||
|
||
super.onCreate() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.github.gotify.log | ||
|
||
import android.content.Context | ||
import java.io.File | ||
import org.tinylog.kotlin.Logger | ||
|
||
class LoggerHelper { | ||
companion object { | ||
fun read(ctx: Context): String = folder(ctx) | ||
.listFiles() | ||
.orEmpty() | ||
.flatMap { it.readLines() } | ||
.fold(mutableListOf<String>()) { newLines, line -> groupExceptions(newLines, line) } | ||
.takeLast(200) | ||
.reversed() | ||
.joinToString(separator = "\n") | ||
|
||
private fun groupExceptions( | ||
newLines: MutableList<String>, | ||
line: String | ||
): MutableList<String> { | ||
if (newLines.isNotEmpty() && (line.startsWith('\t') || line.startsWith("Caused"))) { | ||
newLines[newLines.lastIndex] += '\n' + line | ||
} else { | ||
newLines.add(line) | ||
} | ||
return newLines | ||
} | ||
|
||
fun clear(ctx: Context) { | ||
folder(ctx).listFiles()?.forEach { it.writeText("") } | ||
Logger.info("Logs cleared") | ||
} | ||
|
||
fun init(ctx: Context) { | ||
val file = folder(ctx) | ||
file.mkdirs() | ||
System.setProperty("tinylog.directory", file.absolutePath) | ||
} | ||
|
||
private fun folder(ctx: Context): File = File(ctx.filesDir, "log") | ||
} | ||
} |
Oops, something went wrong.