Skip to content

Commit 4d7b6f3

Browse files
committed
Issue mozilla-mobile#2404: Print logcat to stdout while running unit tests.
1 parent 91c0e4d commit 4d7b6f3

File tree

2 files changed

+29
-0
lines changed
  • components/support/base/src/main/java/mozilla/components/support/base/log

2 files changed

+29
-0
lines changed

build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ subprojects {
7272

7373
// Format test output
7474
tasks.matching {it instanceof Test}.all {
75+
systemProperty "robolectric.logging", "stdout"
76+
systemProperty "logging.test-mode", "true"
77+
7578
testLogging.events = []
7679

7780
def out = services.get(StyledTextOutputFactory).create("an-ouput")

components/support/base/src/main/java/mozilla/components/support/base/log/Log.kt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ object Log {
2525

2626
private val sinks = mutableListOf<LogSink>()
2727

28+
private val testMode: Boolean = System.getProperty("logging.test-mode") == "true"
29+
2830
/**
2931
* Adds a sink that will receive log calls.
3032
*/
@@ -56,6 +58,10 @@ object Log {
5658
}
5759
}
5860
}
61+
62+
if (testMode) {
63+
printTestModeMessage(priority, tag, throwable, message)
64+
}
5965
}
6066

6167
// Only for testing
@@ -67,6 +73,26 @@ object Log {
6773
}
6874
}
6975

76+
private fun printTestModeMessage(
77+
priority: Priority,
78+
tag: String?,
79+
throwable: Throwable?,
80+
message: String?
81+
) {
82+
val printMessage = StringBuilder()
83+
printMessage.append(priority.name[0])
84+
printMessage.append(" ")
85+
if (tag != null) {
86+
printMessage.append("[$tag] ")
87+
}
88+
if (message != null) {
89+
printMessage.append(message)
90+
}
91+
92+
println(printMessage.toString())
93+
throwable?.printStackTrace()
94+
}
95+
7096
/**
7197
* Priority constants for logging calls.
7298
*/

0 commit comments

Comments
 (0)