-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolves #23
- Loading branch information
Showing
10 changed files
with
168 additions
and
0 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
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,10 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"> | ||
|
||
<application> | ||
<provider | ||
android:name=".internal.ContextProvider" | ||
android:authorities="${applicationId}.context_provider" | ||
android:exported="false" /> | ||
</application> | ||
</manifest> |
8 changes: 8 additions & 0 deletions
8
log4k/src/androidMain/kotlin/saschpe/log4k/FileLogger.android.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,8 @@ | ||
package saschpe.log4k | ||
|
||
import kotlinx.io.files.Path | ||
import saschpe.log4k.internal.ContextProvider.Companion.applicationContext | ||
|
||
internal actual val defaultLogPath: Path | ||
get() = Path(applicationContext.cacheDir.path) | ||
|
42 changes: 42 additions & 0 deletions
42
log4k/src/androidMain/kotlin/saschpe/log4k/internal/ContextProvider.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,42 @@ | ||
package saschpe.log4k.internal | ||
|
||
import android.annotation.SuppressLint | ||
import android.content.ContentProvider | ||
import android.content.ContentValues | ||
import android.content.Context | ||
import android.database.Cursor | ||
import android.net.Uri | ||
|
||
/** | ||
* Hidden initialization content provider. | ||
* | ||
* Does not provide real content but hides initialization boilerplate from the library user. | ||
* | ||
* @link https://firebase.googleblog.com/2016/12/how-does-firebase-initialize-on-android.html | ||
*/ | ||
internal class ContextProvider : ContentProvider() { | ||
/** | ||
* Called exactly once before Application.onCreate() | ||
*/ | ||
override fun onCreate(): Boolean { | ||
applicationContext = context?.applicationContext ?: throw Exception("Need the context") | ||
return true | ||
} | ||
|
||
override fun insert(uri: Uri, values: ContentValues?): Uri? = null | ||
|
||
override fun query( | ||
uri: Uri, projection: Array<out String>?, selection: String?, args: Array<out String>?, sortOrder: String? | ||
): Cursor? = null | ||
|
||
override fun update(uri: Uri, values: ContentValues?, selection: String?, args: Array<out String>?): Int = 0 | ||
|
||
override fun delete(uri: Uri, selection: String?, args: Array<out String>?): Int = 0 | ||
|
||
override fun getType(uri: Uri): String? = null | ||
|
||
companion object { | ||
@SuppressLint("StaticFieldLeak") | ||
lateinit var applicationContext: Context | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
log4k/src/androidUnitTest/kotlin/saschpe/log4k/ConsoleLoggerTest.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,10 @@ | ||
package saschpe.log4k | ||
|
||
import org.junit.Test | ||
|
||
class ConsoleLoggerTest { | ||
@Test | ||
fun foo() { | ||
Log.debug { "hello" } | ||
} | ||
} |
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,7 @@ | ||
package saschpe.log4k | ||
|
||
import kotlinx.io.files.Path | ||
import kotlinx.io.files.SystemTemporaryDirectory | ||
|
||
internal actual val defaultLogPath: Path | ||
get() = SystemTemporaryDirectory |
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,72 @@ | ||
package saschpe.log4k | ||
|
||
import kotlinx.io.files.Path | ||
import kotlinx.io.files.SystemFileSystem | ||
|
||
internal expect val defaultLogPath: Path | ||
|
||
class FileLogger( | ||
private val rotation: Rotate = Rotate.Daily, | ||
private val logPath: Path = defaultLogPath | ||
) : Logger() { | ||
override fun print(level: Log.Level, tag: String, message: String?, throwable: Throwable?) { | ||
var logTag = tag.ifEmpty { getTraceTag() } | ||
|
||
val logFile: Path = rotation.logFile(logPath) | ||
// logFile.writeText("${level.name.first()}/$logTag: $message") | ||
// throwable?.let { logFile.writeText("$throwable") } | ||
} | ||
|
||
private fun getTraceTag(): String { | ||
// val trace = Exception().stackTrace[6] | ||
// val className = trace.className.split(".").last() | ||
// return "$className.${trace.methodName}" | ||
return "foo" | ||
} | ||
|
||
sealed class Rotate { | ||
internal abstract fun logFile(logPath: Path): Path | ||
|
||
data object Daily : Rotate() { | ||
override fun logFile(logPath: Path): Path { | ||
val logFile = "daily.txt" // "daily.${DateFormat.format("yyyy-MM-dd", Date().time)}.txt" | ||
return Path(logPath, logFile).apply { SystemFileSystem.createDirectories(this) } | ||
} | ||
} | ||
|
||
class After(private val lines: Int = 10000) : Rotate() { | ||
override fun logFile(logPath: Path): Path { | ||
val logFile = Path(logPath, "log.txt") | ||
val foes = if (SystemFileSystem.exists(logFile)/* && logFile.lines > lines*/) { | ||
// val now = DateFormat.format("yyyy-MM-dd_HH-mm-ss", Date().time) | ||
// logFile.renameTo(File(logPath, "log.$now.txt")) | ||
Path(logPath, "log.txt") | ||
} else { | ||
logFile | ||
} | ||
|
||
return foes.apply { SystemFileSystem.createDirectories(this) } | ||
} | ||
|
||
// private val File.lines: Int | ||
// get() { | ||
// var lines = 0 | ||
// val fis = FileInputStream(this) | ||
// val buffer = ByteArray(BUFFER_SIZE) | ||
// var read: Int | ||
// while (fis.read(buffer).also { read = it } != -1) { | ||
// for (i in 0 until read) { | ||
// if (buffer[i] == '\n'.code.toByte()) | ||
// lines++ | ||
// } | ||
// } | ||
// fis.close() | ||
// return lines | ||
// } | ||
|
||
companion object { | ||
private const val BUFFER_SIZE = 8 * 1024 | ||
} | ||
} | ||
} | ||
} |
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,7 @@ | ||
package saschpe.log4k | ||
|
||
import kotlinx.io.files.Path | ||
import kotlinx.io.files.SystemTemporaryDirectory | ||
|
||
internal actual val defaultLogPath: Path | ||
get() = SystemTemporaryDirectory |
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,7 @@ | ||
package saschpe.log4k | ||
|
||
import kotlinx.io.files.Path | ||
import kotlinx.io.files.SystemTemporaryDirectory | ||
|
||
internal actual val defaultLogPath: Path | ||
get() = SystemTemporaryDirectory |