Skip to content

Commit 7ffeae3

Browse files
authored
Merge pull request #43 from square/py/logcat_filter_tags
isLoggable takes a priority and a tag, allows skipping logging based on tags
2 parents 3e63d2b + 3c84ee6 commit 7ffeae3

File tree

7 files changed

+46
-11
lines changed

7 files changed

+46
-11
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
Change Log
22
==========
33

4+
Next version
5+
-------------
6+
7+
_20XX-XX-XX_
8+
9+
* `isLoggable` now takes a priority and a tag, which allows skipping logging based on tags.
10+
11+
412
Version 0.3
513
-------------
614

logcat/api/android/logcat.api

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ public final class logcat/AndroidLogcatLogger : logcat/LogcatLogger {
55
public synthetic fun <init> (Llogcat/LogPriority;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
66
public static final fun installOnDebuggableApp (Landroid/app/Application;Llogcat/LogPriority;)V
77
public fun isLoggable (Llogcat/LogPriority;)Z
8+
public fun isLoggable (Llogcat/LogPriority;Ljava/lang/String;)Z
89
public fun log (Llogcat/LogPriority;Ljava/lang/String;Ljava/lang/String;)V
910
}
1011

@@ -47,6 +48,7 @@ public abstract interface class logcat/LogcatLogger {
4748
public static fun install ()V
4849
public static fun isInstalled ()Z
4950
public abstract fun isLoggable (Llogcat/LogPriority;)Z
51+
public abstract fun isLoggable (Llogcat/LogPriority;Ljava/lang/String;)Z
5052
public abstract fun log (Llogcat/LogPriority;Ljava/lang/String;Ljava/lang/String;)V
5153
public static fun setObserver (Llogcat/LogcatObserver;)V
5254
public static fun uninstall ()V
@@ -65,6 +67,7 @@ public final class logcat/LogcatLogger$Companion {
6567

6668
public final class logcat/LogcatLogger$DefaultImpls {
6769
public static fun isLoggable (Llogcat/LogcatLogger;Llogcat/LogPriority;)Z
70+
public static fun isLoggable (Llogcat/LogcatLogger;Llogcat/LogPriority;Ljava/lang/String;)Z
6871
}
6972

7073
public abstract interface class logcat/LogcatObserver {
@@ -75,6 +78,7 @@ public abstract interface class logcat/LogcatObserver {
7578
public final class logcat/PrintLogger : logcat/LogcatLogger {
7679
public static final field INSTANCE Llogcat/PrintLogger;
7780
public fun isLoggable (Llogcat/LogPriority;)Z
81+
public fun isLoggable (Llogcat/LogPriority;Ljava/lang/String;)Z
7882
public fun log (Llogcat/LogPriority;Ljava/lang/String;Ljava/lang/String;)V
7983
}
8084

logcat/api/jvm/logcat.api

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public abstract interface class logcat/LogcatLogger {
2828
public static fun install ()V
2929
public static fun isInstalled ()Z
3030
public abstract fun isLoggable (Llogcat/LogPriority;)Z
31+
public abstract fun isLoggable (Llogcat/LogPriority;Ljava/lang/String;)Z
3132
public abstract fun log (Llogcat/LogPriority;Ljava/lang/String;Ljava/lang/String;)V
3233
public static fun setObserver (Llogcat/LogcatObserver;)V
3334
public static fun uninstall ()V
@@ -46,6 +47,7 @@ public final class logcat/LogcatLogger$Companion {
4647

4748
public final class logcat/LogcatLogger$DefaultImpls {
4849
public static fun isLoggable (Llogcat/LogcatLogger;Llogcat/LogPriority;)Z
50+
public static fun isLoggable (Llogcat/LogcatLogger;Llogcat/LogPriority;Ljava/lang/String;)Z
4951
}
5052

5153
public abstract interface class logcat/LogcatObserver {
@@ -56,6 +58,7 @@ public abstract interface class logcat/LogcatObserver {
5658
public final class logcat/PrintLogger : logcat/LogcatLogger {
5759
public static final field INSTANCE Llogcat/PrintLogger;
5860
public fun isLoggable (Llogcat/LogPriority;)Z
61+
public fun isLoggable (Llogcat/LogPriority;Ljava/lang/String;)Z
5962
public fun log (Llogcat/LogPriority;Ljava/lang/String;Ljava/lang/String;)V
6063
}
6164

logcat/src/androidMain/kotlin/logcat/AndroidLogcatLogger.kt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ class AndroidLogcatLogger(minPriority: LogPriority = DEBUG) : LogcatLogger {
2727

2828
private val minPriorityInt: Int = minPriority.priorityInt
2929

30-
override fun isLoggable(priority: LogPriority): Boolean =
31-
priority.priorityInt >= minPriorityInt
30+
override fun isLoggable(
31+
priority: LogPriority,
32+
tag: String
33+
): Boolean = priority.priorityInt >= minPriorityInt
3234

3335
override fun log(
3436
priority: LogPriority,
@@ -77,7 +79,10 @@ class AndroidLogcatLogger(minPriority: LogPriority = DEBUG) : LogcatLogger {
7779

7880
companion object {
7981
@JvmStatic
80-
fun installOnDebuggableApp(application: Application, minPriority: LogPriority = VERBOSE) {
82+
fun installOnDebuggableApp(
83+
application: Application,
84+
minPriority: LogPriority = VERBOSE
85+
) {
8186
if (!LogcatLogger.isInstalled && application.isDebuggableApp) {
8287
LogcatLogger.install()
8388
LogcatLogger.loggers += AndroidLogcatLogger(minPriority)

logcat/src/commonMain/kotlin/logcat/Logcat.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@ inline fun Any.logcat(
6161
if (!LogcatLogger.isInstalled) {
6262
return
6363
}
64-
val loggers = LogcatLogger.loggers.filter { it.isLoggable(priority) }
6564
@OptIn(InternalLogcatApi::class)
65+
val tagOrCaller = tag ?: outerClassSimpleName()
66+
val loggers = LogcatLogger.loggers.filter { it.isLoggable(priority, tagOrCaller) }
6667
if (loggers.isNotEmpty()) {
67-
val tagOrCaller = tag ?: outerClassSimpleName()
6868
val observer = LogcatLogger.observer
6969
observer?.beforeLog(priority, tagOrCaller)
7070
val evaluatedMessage = message()
@@ -88,7 +88,7 @@ inline fun logcat(
8888
if (!LogcatLogger.isInstalled) {
8989
return
9090
}
91-
val loggers = LogcatLogger.loggers.filter { it.isLoggable(priority) }
91+
val loggers = LogcatLogger.loggers.filter { it.isLoggable(priority, tag) }
9292
if (loggers.isNotEmpty()) {
9393
val observer = LogcatLogger.observer
9494
observer?.beforeLog(priority, tag)

logcat/src/commonMain/kotlin/logcat/LogcatLogger.kt

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,20 @@ import kotlin.concurrent.Volatile
1717
*/
1818
interface LogcatLogger {
1919

20+
@Deprecated(
21+
"Override the method that takes priority and tag instead",
22+
ReplaceWith("isLoggable(priority, tag)")
23+
)
24+
fun isLoggable(priority: LogPriority) = true
25+
2026
/**
21-
* Whether a log with the provided priority should be logged and the corresponding message
27+
* Whether a log with the provided priority and tag should be logged and the corresponding message
2228
* providing lambda evaluated. Called by [logcat].
2329
*/
24-
fun isLoggable(priority: LogPriority) = true
30+
fun isLoggable(
31+
priority: LogPriority,
32+
tag: String
33+
) = isLoggable(priority)
2534

2635
/**
2736
* Write a log to its destination. Called by [logcat].
@@ -96,11 +105,14 @@ interface LogcatLogger {
96105

97106
private lateinit var localLoggers: List<LogcatLogger>
98107

99-
override fun isLoggable(priority: LogPriority): Boolean {
108+
override fun isLoggable(
109+
priority: LogPriority,
110+
tag: String
111+
): Boolean {
100112
if (!isInstalled) {
101113
return false
102114
}
103-
val filteredLoggers = loggers.filter { it.isLoggable(priority) }
115+
val filteredLoggers = loggers.filter { it.isLoggable(priority, tag) }
104116
localLoggers = filteredLoggers
105117
return filteredLoggers.isNotEmpty()
106118
}

logcat/src/commonTest/kotlin/logcat/TestLogcatLogger.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package logcat
22

33
class TestLogcatLogger : LogcatLogger {
4-
override fun isLoggable(priority: LogPriority): Boolean {
4+
override fun isLoggable(
5+
priority: LogPriority,
6+
tag: String
7+
): Boolean {
58
latestPriority = priority
69
return shouldLog
710
}

0 commit comments

Comments
 (0)