Skip to content

Commit

Permalink
Merge pull request #55 from jerry-jeon/feature/support-fb-testlab
Browse files Browse the repository at this point in the history
Feature/support fb testlab
  • Loading branch information
jerry-jeon authored Aug 15, 2023
2 parents 094c104 + 80923a3 commit 1fecfda
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,13 @@ fun parseToStringRepresentation(input: String): Map<String, Any?>? {
val className = matchResult.groupValues[1]
val propertiesString = matchResult.groupValues[2]

resultMap.putAll(parseProperties(propertiesString))
// If the data class representation has no arguments, return null
if (propertiesString.isBlank()) {
if (resultMap.isEmpty()) {
return null
}

resultMap["class"] = className
resultMap.putAll(parseProperties(propertiesString))

return resultMap
}
Expand Down
1 change: 0 additions & 1 deletion src/main/kotlin/com/jerryjeon/logjerry/log/Log.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ data class Log(
null
}
} catch (e: Exception) {
e.printStackTrace()
null
}

Expand Down
30 changes: 27 additions & 3 deletions src/main/kotlin/com/jerryjeon/logjerry/logview/LogAnnotation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ object LogAnnotation {

fun separateAnnotationStrings(log: Log, detectionResults: List<Detection>): List<LogContent> {
val sortedDetections = detectionResults.sortedBy { it.range.first }
val overlapRemovedDetections = removeOverlappingDetections(sortedDetections)
val originalLog = log.log

var lastEnded = 0
val logContents = mutableListOf<LogContent>()
val jsonDetections = mutableListOf<JsonDetection>()
sortedDetections.forEach {
overlapRemovedDetections.forEach {
val newStart = it.range.first
val newEnd = it.range.last
// Assume that there are no overlapping areas.
Expand All @@ -36,7 +36,6 @@ object LogAnnotation {
json.encodeToString(JsonObject.serializer(), it.json)
)
)
jsonDetections.clear()
lastEnded = newEnd + 1
}

Expand All @@ -55,6 +54,31 @@ object LogAnnotation {
return logContents
}

private fun removeOverlappingDetections(detections: List<Detection>): List<Detection> {
// Sort the detections by start range
val sortedDetections = detections.sortedBy { it.range.first }
.filter { it is JsonDetection || it is DataClassDetection } // Only consider Json and DataClass detections
// TODO Refactor to use shownAsBlock field in detector

val result = mutableListOf<Detection>()
var lastEnd = -1

for (detection in sortedDetections) {
// If this detection doesn't overlap with the previous one, add it to the result
if (detection.range.first > lastEnd) {
result.add(detection)
lastEnd = detection.range.last
} else if (detection is JsonDetection && result.last() is DataClassDetection) {
// If current detection is of type JsonDetection and the last added is of type DataClassDetection, replace the last one
result.removeAt(result.size - 1)
result.add(detection)
lastEnd = detection.range.last
}
}

return result
}

fun annotate(log: Log, logContents: List<LogContent>, detectors: List<Detector<*>>): List<LogContentView> {
val result = logContents.map { logContent ->
when (logContent) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ data class AdbLogcatDefaultFormatParser(
}

override fun toString(): String {
return "StudioLogcatAboveDolphinParser(includeDate=$includeDate, includeTime=$includeTime, includePid=$includePid, includeTid=$includeTid, includeTag=$includeTag, includePackageName=$includePackageName, number=$number)"
return "AdbLogcatDefaultFormatParser(includeDate=$includeDate, includeTime=$includeTime, includePid=$includePid, includeTid=$includeTid, includeTag=$includeTag, includePackageName=$includePackageName, number=$number)"
}

companion object : ParserFactory {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.jerryjeon.logjerry.parse

import com.jerryjeon.logjerry.log.Log
import java.util.concurrent.atomic.AtomicInteger

class FirebaseTestLabLogcatFormatParser : LogParser {

private val number = AtomicInteger(1)

override fun parse(rawLines: List<String>): ParseResult {
val logs = mutableListOf<Log>()
val invalidSentences = mutableListOf<Pair<Int, String>>()
var lastLog: Log? = null
rawLines.forEachIndexed { index, s ->
lastLog = try {
val log = parseSingleLineLog(s)

// Custom continuation
if (log.log.startsWith("Cont(")) {
lastLog?.let {
it.copy(log = "${it.log}${log.log.substringAfter(") ")}")
} ?: log
} else {
lastLog?.let { logs.add(it) }
log
}
} catch (e: Exception) {
val continuedLog = if (lastLog == null) {
invalidSentences.add(index to s)
return@forEachIndexed
} else {
lastLog!!
}
continuedLog.copy(log = "${continuedLog.log}\n$s")
}
}
lastLog?.let { logs.add(it) }
return ParseResult(logs, invalidSentences)
}

fun parseSingleLineLog(raw: String): Log {
val timeAndMessage = raw.split(": ", limit = 2)

val date = timeAndMessage[0].split(" ")[0]
val time = timeAndMessage[0].split(" ")[1]

val metadataParts = timeAndMessage[1].substringBefore(":")
val priorityText = metadataParts.substringBefore("/")

val tagWithPid = metadataParts.substringAfter("/")
val tag = tagWithPid.substringBefore("(")
val pid = tagWithPid.substringAfter("(").substringBefore(")").toLong()

val originalLog = timeAndMessage[1].substringAfter(": ")

return Log(number.getAndIncrement(), date, time, pid, null, null, priorityText, tag, originalLog)
}

companion object : ParserFactory {
private val logRegex = """\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3}: [EWIDV]/[a-zA-Z0-9_\-]+\( *\d+ *\): .+""".toRegex()

override fun create(sample: String): LogParser? {
val matches = logRegex.matches(sample)
return if (matches) {
FirebaseTestLabLogcatFormatParser()
} else {
null
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,6 @@ class PreferencesViewModel {
windowSizeWhenOpened = windowSizeWhenOpened
)

println("HMMMM? ${preferencesFlow.value}")

Preferences.file.outputStream().use {
json.encodeToStream(preferencesFlow.value, it)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ class SourceManager(
private fun chooseParser(lines: List<String>): LogParser {
return lines.firstNotNullOfOrNull {
CustomParser.create(it)
?: FirebaseTestLabLogcatFormatParser.create(it)
?: StudioLogcatBelowChipmunkParser.create(it)
?: StudioLogcatAboveDolphinParser.create(it)
?: AdbLogcatDefaultFormatParser.create(it)
Expand Down

0 comments on commit 1fecfda

Please sign in to comment.