Skip to content

Commit

Permalink
Revert "fix(JENKINS-73604): Remove xterm escape codes from log file"
Browse files Browse the repository at this point in the history
This reverts commit 89292e5.
  • Loading branch information
mydeveloperday committed Nov 27, 2024
1 parent 89292e5 commit e831be2
Showing 1 changed file with 79 additions and 60 deletions.
139 changes: 79 additions & 60 deletions src/main/java/hudson/plugins/logparser/LogParserParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,6 +23,7 @@
import java.util.regex.Pattern;

public class LogParserParser {

final private HashMap<String, Integer> statusCount = new HashMap<>();
final private HashMap<String, BufferedWriter> writers = new HashMap<>();
final private HashMap<String, String> linkFiles = new HashMap<>();
Expand All @@ -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;
Expand All @@ -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());

Expand Down Expand Up @@ -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
Expand All @@ -134,30 +140,29 @@ public LogParserResult parseLog(final Run<?, ?> build)
// file.

// Create dummy header and section for beginning of log
final String shortLink =
" <a target=\"content\" href=\"log_content.html\">Beginning of log</a>";
LogParserWriter.writeHeaderTemplateToAllLinkFiles(
writers, sectionCounter); // This enters a line which will later be
final String shortLink = " <a target=\"content\" href=\"log_content.html\">Beginning of log</a>";
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);
writer.write(LogParserConsts.getHtmlOpeningTags());

// write styles for log body
final String styles = "<style>\n"
+ " body {margin-left:.5em; }\n"
+ " pre {font-family: Consolas, \"Courier New\"; word-wrap: break-word; }\n"
+ " pre span {word-wrap: break-word; } \n"
+ "</style>\n";
+ " body {margin-left:.5em; }\n"
+ " pre {font-family: Consolas, \"Courier New\"; word-wrap: break-word; }\n"
+ " pre span {word-wrap: break-word; } \n"
+ "</style>\n";
writer.write(styles);

if (this.preformattedHtml)
writer.write("<pre>");
// 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)
Expand All @@ -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);

Expand Down Expand Up @@ -204,6 +211,7 @@ public LogParserResult parseLog(final Run<?, ?> build)
result.setExtraTags(this.extraTags);

return result;

}

public String parseLine(final String line) throws IOException {
Expand All @@ -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) {
Expand All @@ -232,18 +241,21 @@ public String parseLine(final String line, final String status) throws IOExcepti
parsedLine = parsedLine.replaceAll(">", "&gt;");

// 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);
Expand All @@ -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) {
Expand All @@ -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(" <a target=\"content\" href=\"log_content.html#");
final StringBuffer shortLink = new StringBuffer(
" <a target=\"content\" href=\"log_content.html#");
shortLink.append(marker);
shortLink.append("\">");
shortLink.append(line);
Expand All @@ -302,7 +318,8 @@ private String addMarkerAndLink(
link.append(shortLink);
link.append("</li>");

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.

Expand All @@ -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("<br/>");
brShortLink.append(shortLink);
Expand All @@ -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<String, String> lineStatusMatches = channel.call(new LogParserStatusComputer(
log, parsingRulesArray, compiledPatterns, signature, charset));
final HashMap<String, String> 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;
Expand All @@ -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.");

}

}

0 comments on commit e831be2

Please sign in to comment.