Skip to content

Commit

Permalink
chore: rework highlighing for regular files
Browse files Browse the repository at this point in the history
  • Loading branch information
Raphael Vullriede committed Nov 8, 2024
1 parent 35fad0f commit 2cc12aa
Showing 1 changed file with 12 additions and 24 deletions.
36 changes: 12 additions & 24 deletions src/main/java/net/osslabz/loggazer/Highlighter.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package net.osslabz.loggazer;

import com.fasterxml.jackson.core.JsonFactory;
import org.fxmisc.richtext.model.StyleSpans;
import org.fxmisc.richtext.model.StyleSpansBuilder;
import org.slf4j.Logger;
Expand Down Expand Up @@ -79,7 +78,7 @@ private static StyleSpans<Collection<String>> highlightLogLevelJson(String text)
if (insideJson) {
logSegmentLength += fullLineLength;
if (logLevelForCurrentSegment == null) {
logLevelForCurrentSegment = determineLogLevelForSegment(line);
logLevelForCurrentSegment = determineLogLevelForLine(line);
}
} else {
spansBuilder.add(Collections.emptyList(), fullLineLength);
Expand All @@ -94,13 +93,11 @@ private static StyleSpans<Collection<String>> highlightLogLevelJson(String text)
e.printStackTrace();
}

StyleSpans<Collection<String>> styleSpans = spansBuilder.create();

return styleSpans;
return spansBuilder.create();
}


private static String determineLogLevelForSegment(String line) {
private static String determineLogLevelForLine(String line) {
for (String logLevel : LOG_LEVEL) {
if (line.contains(logLevel)) {
return logLevel;
Expand All @@ -118,38 +115,29 @@ static StyleSpans<Collection<String>> highlightLogLevelRegularFile(String text)
try (BufferedReader reader = new BufferedReader(new StringReader(text))) {

String lastLogLevel = null;

String line = null;

while ((line = reader.readLine()) != null) {
numLines++;

boolean matchedLine = false;

List<String> styles = new ArrayList<>(2);

for (String logLevel : LOG_LEVEL) {
if (line.contains(logLevel)) {
styles.add(logLevel.toLowerCase());
lastLogLevel = logLevel;
matchedLine = true;
break;
}
}
if (!matchedLine) {
String logLevel = determineLogLevelForLine(line);
if (logLevel != null) {
styles.add(logLevel.toLowerCase());
lastLogLevel = logLevel;
} else if (lastLogLevel != null) {
log.debug("No log level found on line {}, using previous level {}", numLines, lastLogLevel);
styles.add(lastLogLevel.toLowerCase());
} else {
log.trace("No log level found on line {} and no previous logLevel set, no highlighting", numLines);
}

spansBuilder.add(styles, line.length() + 1);
}
} catch (IOException e) {
e.printStackTrace();
}

StyleSpans<Collection<String>> styleSpans = spansBuilder.create();

log.debug("file contains {} lines, {} spans calculated", numLines, styleSpans.length());

return styleSpans;
return spansBuilder.create();
}
}

0 comments on commit 2cc12aa

Please sign in to comment.