From e831be24e039588309abdb09a35e3bcbdc3f5e81 Mon Sep 17 00:00:00 2001 From: mydeveloperday Date: Wed, 27 Nov 2024 18:03:03 +0000 Subject: [PATCH] Revert "fix(JENKINS-73604): Remove xterm escape codes from log file" This reverts commit 89292e5f8b5792111cf9957dd04435db693c1c83. --- .../plugins/logparser/LogParserParser.java | 139 ++++++++++-------- 1 file changed, 79 insertions(+), 60 deletions(-) diff --git a/src/main/java/hudson/plugins/logparser/LogParserParser.java b/src/main/java/hudson/plugins/logparser/LogParserParser.java index 92248b3..3902243 100755 --- a/src/main/java/hudson/plugins/logparser/LogParserParser.java +++ b/src/main/java/hudson/plugins/logparser/LogParserParser.java @@ -5,12 +5,13 @@ import hudson.model.AbstractBuild; import hudson.model.Run; import hudson.remoting.VirtualChannel; + +import java.io.InputStreamReader; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; -import java.io.InputStreamReader; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Calendar; @@ -22,6 +23,7 @@ import java.util.regex.Pattern; public class LogParserParser { + final private HashMap statusCount = new HashMap<>(); final private HashMap writers = new HashMap<>(); final private HashMap linkFiles = new HashMap<>(); @@ -41,18 +43,22 @@ public class LogParserParser { final private VirtualChannel channel; final private boolean preformattedHtml; - public LogParserParser(final FilePath parsingRulesFile, final boolean preformattedHtml, - final VirtualChannel channel) throws IOException { + public LogParserParser(final FilePath parsingRulesFile, + final boolean preformattedHtml, final VirtualChannel channel) + throws IOException { + // init logger final Logger logger = Logger.getLogger(getClass().getName()); - this.parsingRulesArray = LogParserUtils.readParsingRules(parsingRulesFile); + this.parsingRulesArray = LogParserUtils + .readParsingRules(parsingRulesFile); // This causes each regular expression to be compiled once for better // performance - this.compiledPatternsPlusError = - LogParserUtils.compilePatterns(this.parsingRulesArray, logger); - this.compiledPatterns = this.compiledPatternsPlusError.getCompiledPatterns(); + this.compiledPatternsPlusError = LogParserUtils.compilePatterns( + this.parsingRulesArray, logger); + this.compiledPatterns = this.compiledPatternsPlusError + .getCompiledPatterns(); this.extraTags = this.compiledPatternsPlusError.getExtraTags(); this.preformattedHtml = preformattedHtml; @@ -74,13 +80,12 @@ public LogParserParser(final FilePath parsingRulesFile, final boolean preformatt * errorLinks.html, warningLinks.html, infoLinks.html */ @Deprecated - public LogParserResult parseLog(final AbstractBuild build) - throws IOException, InterruptedException { + public LogParserResult parseLog(final AbstractBuild build) throws IOException, InterruptedException { return this.parseLog((Run) build); } - public LogParserResult parseLog(final Run build) - throws IOException, InterruptedException { + public LogParserResult parseLog(final Run build) throws IOException, InterruptedException { + // init logger final Logger logger = Logger.getLogger(getClass().getName()); @@ -112,18 +117,19 @@ public LogParserResult parseLog(final Run build) // Open console log for reading and all other files for writing try (BufferedWriter writer = new BufferedWriter(new FileWriter(parsedFilePath))) { + // Record writers to links files in hash - writers.put( - LogParserConsts.ERROR, new BufferedWriter(new FileWriter(errorLinksFilePath))); - writers.put( - LogParserConsts.WARNING, new BufferedWriter(new FileWriter(warningLinksFilePath))); - writers.put( - LogParserConsts.INFO, new BufferedWriter(new FileWriter(infoLinksFilePath))); - writers.put( - LogParserConsts.DEBUG, new BufferedWriter(new FileWriter(debugLinksFilePath))); + writers.put(LogParserConsts.ERROR, new BufferedWriter(new FileWriter( + errorLinksFilePath))); + writers.put(LogParserConsts.WARNING, new BufferedWriter(new FileWriter( + warningLinksFilePath))); + writers.put(LogParserConsts.INFO, new BufferedWriter(new FileWriter( + infoLinksFilePath))); + writers.put(LogParserConsts.DEBUG, new BufferedWriter(new FileWriter( + debugLinksFilePath))); for (String extraTag : this.extraTags) { - writers.put(extraTag, - new BufferedWriter(new FileWriter(linksFilePathByExtraTags.get(extraTag)))); + writers.put(extraTag, new BufferedWriter(new FileWriter( + linksFilePathByExtraTags.get(extraTag)))); } // Loop on the console log as long as there are input lines and parse @@ -134,10 +140,8 @@ public LogParserResult parseLog(final Run build) // file. // Create dummy header and section for beginning of log - final String shortLink = - " Beginning of log"; - LogParserWriter.writeHeaderTemplateToAllLinkFiles( - writers, sectionCounter); // This enters a line which will later be + final String shortLink = " Beginning of log"; + LogParserWriter.writeHeaderTemplateToAllLinkFiles(writers, sectionCounter); // This enters a line which will later be // replaced by the actual header and count for // this header headerForSection.add(shortLink); @@ -145,19 +149,20 @@ public LogParserResult parseLog(final Run build) // write styles for log body final String styles = "\n"; + + " body {margin-left:.5em; }\n" + + " pre {font-family: Consolas, \"Courier New\"; word-wrap: break-word; }\n" + + " pre span {word-wrap: break-word; } \n" + + "\n"; writer.write(styles); if (this.preformattedHtml) writer.write("
");
             // Read bulks of lines, parse
-            parseLogBody(build, writer, log, logger);
+            parseLogBody(build, writer, log,
+                    logger);
 
             // Write parsed output, links, etc.
-            // writeLogBody();
+            //writeLogBody();
 
             // Close html footer
             if (this.preformattedHtml)
@@ -171,9 +176,11 @@ public LogParserResult parseLog(final Run build)
 
         // Build the reference html from the warnings/errors/info html files
         // created in the loop above
-        LogParserWriter.writeReferenceHtml(buildRefPath, headerForSection, statusCountPerSection,
-            displayConstants.getIconTable(), displayConstants.getLinkListDisplay(),
-            displayConstants.getLinkListDisplayPlural(), statusCount, linkFiles, extraTags);
+        LogParserWriter.writeReferenceHtml(buildRefPath, headerForSection,
+                statusCountPerSection, displayConstants.getIconTable(),
+                displayConstants.getLinkListDisplay(),
+                displayConstants.getLinkListDisplayPlural(), statusCount,
+                linkFiles, extraTags);
         // Write the wrapping html for the reference page and the parsed log page
         LogParserWriter.writeWrapperHtml(buildWrapperPath);
 
@@ -204,6 +211,7 @@ public LogParserResult parseLog(final Run build)
         result.setExtraTags(this.extraTags);
 
         return result;
+
     }
 
     public String parseLine(final String line) throws IOException {
@@ -215,7 +223,8 @@ public static String convertEscapeSequencesToHtml(String text) {
         return text.replaceAll("\u001B\\[(\\d{1,2})(;\\d{1,2})?(;\\d{1,2})?m", "");
     }
 
-    public String parseLine(final String line, final String status) throws IOException {
+    public String parseLine(final String line, final String status)
+            throws IOException {
         String parsedLine = line;
         String effectiveStatus = status;
         if (status == null) {
@@ -232,18 +241,21 @@ public String parseLine(final String line, final String status) throws IOExcepti
         parsedLine = parsedLine.replaceAll(">", ">");
 
         // Remove xterm color escape sequence with an empty space.
+        //parsedLine = parsedLine.replaceAll("\u001B\\[\\d+m", "");
         parsedLine = convertEscapeSequencesToHtml(parsedLine);
 
-        if (effectiveStatus != null && !effectiveStatus.equals(LogParserConsts.NONE)) {
+        if (effectiveStatus != null
+                && !effectiveStatus.equals(LogParserConsts.NONE)) {
             // Increment count of the status
             incrementCounter(effectiveStatus);
             incrementCounterPerSection(effectiveStatus, sectionCounter);
             // Color line according to the status
-            final String parsedLineColored = colorLine(parsedLine, effectiveStatus);
+            final String parsedLineColored = colorLine(parsedLine,
+                    effectiveStatus);
 
             // Mark line and add to left side links of highlighted lines
-            final String parsedLineColoredAndMarked =
-                addMarkerAndLink(parsedLineColored, effectiveStatus, status);
+            final String parsedLineColoredAndMarked = addMarkerAndLink(
+                    parsedLineColored, effectiveStatus, status);
             parsedLine = parsedLineColoredAndMarked;
         }
         final StringBuffer result = new StringBuffer(parsedLine);
@@ -257,8 +269,10 @@ public void incrementCounter(final String status) {
         statusCount.put(status, currentVal + 1);
     }
 
-    public void incrementCounterPerSection(final String status, final int sectionNumber) {
-        final String key = LogParserUtils.getSectionCountKey(status, sectionNumber);
+    public void incrementCounterPerSection(final String status,
+            final int sectionNumber) {
+        final String key = LogParserUtils.getSectionCountKey(status,
+                sectionNumber);
         Integer currentValInteger = statusCountPerSection.get(key);
         // No value - entered yet - initialize with 0
         if (currentValInteger == null) {
@@ -283,15 +297,17 @@ private String colorLine(final String line, final String status) {
         return result.toString();
     }
 
-    private String addMarkerAndLink(
-        final String line, final String effectiveStatus, final String status) throws IOException {
+    private String addMarkerAndLink(final String line,
+            final String effectiveStatus, final String status)
+            throws IOException {
         // Add marker
-        final String statusCountStr = statusCount.get(effectiveStatus).toString();
+        final String statusCountStr = statusCount
+                .get(effectiveStatus).toString();
         final String marker = effectiveStatus + statusCountStr;
 
         // Add link
-        final StringBuffer shortLink =
-            new StringBuffer(" ");
         shortLink.append(line);
@@ -302,7 +318,8 @@ private String addMarkerAndLink(
         link.append(shortLink);
         link.append("");
 
-        final BufferedWriter linkWriter = (BufferedWriter) writers.get(effectiveStatus);
+        final BufferedWriter linkWriter = (BufferedWriter) writers
+                .get(effectiveStatus);
         linkWriter.write(link.toString());
         linkWriter.newLine(); // Write system dependent end of line.
 
@@ -317,7 +334,7 @@ private String addMarkerAndLink(
             sectionCounter++;
             // This enters a line which will later be replaced by the actual
             // header and count for this header
-            LogParserWriter.writeHeaderTemplateToAllLinkFiles(writers, sectionCounter);
+            LogParserWriter.writeHeaderTemplateToAllLinkFiles(writers, sectionCounter); 
 
             final StringBuffer brShortLink = new StringBuffer("
"); brShortLink.append(shortLink); @@ -327,21 +344,22 @@ private String addMarkerAndLink( return markedLine.toString(); } - private void parseLogBody(final Run build, final BufferedWriter writer, - final InputStream log, final Logger logger) throws IOException, InterruptedException { + private void parseLogBody(final Run build, final BufferedWriter writer, final InputStream log, + final Logger logger) throws IOException, InterruptedException { + // Logging information - start - final String signature = build.getParent().getName() + "_build_" + build.getNumber(); + final String signature = build.getParent().getName() + "_build_" + + build.getNumber(); logger.log(Level.INFO, "LogParserParser: Start parsing : " + signature); final Calendar calendarStart = Calendar.getInstance(); Charset charset = build.getCharset(); - final HashMap lineStatusMatches = channel.call(new LogParserStatusComputer( - log, parsingRulesArray, compiledPatterns, signature, charset)); + final HashMap lineStatusMatches = channel.call( + new LogParserStatusComputer(log, parsingRulesArray, compiledPatterns, signature, charset)); // Read log file from start - line by line and apply the statuses as // found by the threads. - try (InputStreamReader streamReader = - new InputStreamReader(build.getLogInputStream(), charset); + try (InputStreamReader streamReader = new InputStreamReader(build.getLogInputStream(), charset); BufferedReader reader = new BufferedReader(streamReader)) { String line; String status; @@ -358,11 +376,12 @@ private void parseLogBody(final Run build, final BufferedWriter writer, // Logging information - end final Calendar calendarEnd = Calendar.getInstance(); - final long diffSeconds = - (calendarEnd.getTimeInMillis() - calendarStart.getTimeInMillis()) / 1000; + final long diffSeconds = (calendarEnd.getTimeInMillis() - calendarStart + .getTimeInMillis()) / 1000; final long diffMinutes = diffSeconds / 60; - logger.log(Level.INFO, - "LogParserParser: Parsing took " + diffMinutes + " minutes (" + diffSeconds - + ") seconds."); + logger.log(Level.INFO, "LogParserParser: Parsing took " + diffMinutes + + " minutes (" + diffSeconds + ") seconds."); + } + }