44 */
55package scala_maven_executions ;
66
7+ import java .util .regex .Pattern ;
8+
79public class LogProcessorUtils {
810
911 public enum Level {
@@ -17,16 +19,24 @@ public static class LevelState {
1719 public String untilContains = null ;
1820 }
1921
22+ /**
23+ * Matches "warning" or "warnings" as complete words, but not filenames e.g. "Warnings.scala".
24+ */
25+ private static final Pattern WARNING_WORD = Pattern .compile ("\\ bwarnings?\\ b(?!\\ .scala)" );
26+
27+ /** Matches "error" or "errors" as complete words, but not filenames e.g. "Errors.scala". */
28+ private static final Pattern ERROR_WORD = Pattern .compile ("\\ berrors?\\ b(?!\\ .scala)" );
29+
2030 public static LevelState levelStateOf (String line , LevelState previous ) {
2131 LevelState back = new LevelState ();
2232 String lineLowerCase = line .toLowerCase ();
23- if (lineLowerCase . contains ( "error" )) {
24- back .level = Level .ERROR ;
33+ if (isWarningLine ( lineLowerCase )) {
34+ back .level = Level .WARNING ;
2535 if (lineLowerCase .contains (".scala" )) {
2636 back .untilContains = "^" ;
2737 }
28- } else if (lineLowerCase . contains ( "warn" )) {
29- back .level = Level .WARNING ;
38+ } else if (isErrorLine ( lineLowerCase )) {
39+ back .level = Level .ERROR ;
3040 if (lineLowerCase .contains (".scala" )) {
3141 back .untilContains = "^" ;
3242 }
@@ -40,4 +50,30 @@ public static LevelState levelStateOf(String line, LevelState previous) {
4050 }
4151 return back ;
4252 }
53+
54+ private static boolean isWarningLine (String line ) {
55+ return hasSeverityMarker (line , "warning" ) || WARNING_WORD .matcher (line ).find ();
56+ }
57+
58+ private static boolean isErrorLine (String line ) {
59+ return hasSeverityMarker (line , "error" ) || ERROR_WORD .matcher (line ).find ();
60+ }
61+
62+ /**
63+ * Checks whether the line contains a severity marker e.g. "error:" or "warning:".
64+ *
65+ * <p>Matches common compiler and build-tool formats, including:
66+ *
67+ * <ul>
68+ * <li>{@code path:lineNum: error: message}
69+ * <li>{@code error: message} at the start of the line
70+ * </ul>
71+ *
72+ * @param lineLowerCase the lowercase line to check
73+ * @param marker the severity marker to look for e.g. "error" or "warning"
74+ * @return true if the line starts with {@code marker:} or contains {@code " marker:"}
75+ */
76+ private static boolean hasSeverityMarker (String lineLowerCase , String marker ) {
77+ return lineLowerCase .startsWith (marker + ":" ) || lineLowerCase .contains (" " + marker + ":" );
78+ }
4379}
0 commit comments