Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 22 - Better logcat replace support #23

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions logcat/api/logcat.api
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public final class logcat/LogcatLogger$Companion {
public final fun getLogger ()Llogcat/LogcatLogger;
public final fun install (Llogcat/LogcatLogger;)V
public final fun isInstalled ()Z
public final fun replace (Llogcat/LogcatLogger;)V
public final fun uninstall ()V
}

Expand Down
12 changes: 11 additions & 1 deletion logcat/src/main/java/logcat/LogcatLogger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,16 @@ interface LogcatLogger {
logger = NoLog
}
}

/**
* Replaces the current logger (if any) with a new logger.
*/
fun replace(logger: LogcatLogger) {
synchronized(this) {
installedThrowable = RuntimeException("Previous logger installed here")
Companion.logger = logger
}
}
}

private object NoLog : LogcatLogger {
Expand All @@ -78,6 +88,6 @@ interface LogcatLogger {
priority: LogPriority,
tag: String,
message: String
) = error("Should never receive any log")
) = Unit
}
}
31 changes: 31 additions & 0 deletions logcat/src/test/java/logcat/LogcatTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,37 @@ class LogcatTest {
assertThat(logger.latestLog!!.tag).isEqualTo(LogcatTest::class.java.simpleName)
}

@Test fun `installing a new Logger when one was already present, logs an error`() {
TestLogcatLogger().apply { LogcatLogger.install(this) }.latestLog.let { latestLog ->
// Expect no logcat messages to start.
assertThat(latestLog).isNull()
}
TestLogcatLogger().apply { LogcatLogger.install(this) }.latestLog.let { latestLog ->
latestLog!!.let {
assertThat(it.tag).isEqualTo("LogcatLogger")
assertThat(it.priority).isEqualTo(LogPriority.ERROR)
assertThat(it.message).isNotEmpty()
}
}
}

@Test fun `replacing a Logger when one was already present, this is fine`() {
TestLogcatLogger().apply { LogcatLogger.install(this) }.latestLog.let { latestLog ->
// Expect no logcat messages to start.
assertThat(latestLog).isNull()
}
TestLogcatLogger().apply { LogcatLogger.replace(this) }.latestLog.let { latestLog ->
assertThat(latestLog).isNull()
}
}

@Test fun `replace is lenient, it is fine to replace when no Logger previously installed`() {
TestLogcatLogger().apply { LogcatLogger.replace(this) }.latestLog.let { latestLog ->
// Expect no logcat messages to start.
assertThat(latestLog).isNull()
}
}

companion object {
fun companionFunctionLog(
message: () -> String
Expand Down
Loading