Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MNG-7899] Various memory usage improvements 5 #1270

Merged
merged 3 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,11 @@ public interface MessageBuilder {
*/
@Nonnull
String build();

/**
* Set the buffer length.
*
* @param length the new length
*/
void setLength(int length);
gnodet marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,9 @@ public String build() {
public String toString() {
return build();
}

@Override
public void setLength(int length) {
buffer.setLength(length);
}
}
26 changes: 13 additions & 13 deletions maven-embedder/src/main/java/org/apache/maven/cli/MavenCli.java
Original file line number Diff line number Diff line change
Expand Up @@ -497,19 +497,6 @@ void logging(CliRequest cliRequest) {
cliRequest.quiet = !cliRequest.verbose && commandLine.hasOption(CLIManager.QUIET);
cliRequest.showErrors = cliRequest.verbose || commandLine.hasOption(CLIManager.ERRORS);

slf4jLoggerFactory = LoggerFactory.getILoggerFactory();
gnodet marked this conversation as resolved.
Show resolved Hide resolved
Slf4jConfiguration slf4jConfiguration = Slf4jConfigurationFactory.getConfiguration(slf4jLoggerFactory);

if (cliRequest.verbose) {
cliRequest.request.setLoggingLevel(MavenExecutionRequest.LOGGING_LEVEL_DEBUG);
slf4jConfiguration.setRootLoggerLevel(Slf4jConfiguration.Level.DEBUG);
} else if (cliRequest.quiet) {
cliRequest.request.setLoggingLevel(MavenExecutionRequest.LOGGING_LEVEL_ERROR);
slf4jConfiguration.setRootLoggerLevel(Slf4jConfiguration.Level.ERROR);
}
// else fall back to default log level specified in conf
// see https://issues.apache.org/jira/browse/MNG-2570

// LOG COLOR
String styleColor = cliRequest.getUserProperties().getProperty(STYLE_COLOR_PROPERTY, "auto");
styleColor = commandLine.getOptionValue(COLOR, styleColor);
Expand All @@ -528,6 +515,19 @@ void logging(CliRequest cliRequest) {
}
}

slf4jLoggerFactory = LoggerFactory.getILoggerFactory();
Slf4jConfiguration slf4jConfiguration = Slf4jConfigurationFactory.getConfiguration(slf4jLoggerFactory);

if (cliRequest.verbose) {
cliRequest.request.setLoggingLevel(MavenExecutionRequest.LOGGING_LEVEL_DEBUG);
slf4jConfiguration.setRootLoggerLevel(Slf4jConfiguration.Level.DEBUG);
} else if (cliRequest.quiet) {
cliRequest.request.setLoggingLevel(MavenExecutionRequest.LOGGING_LEVEL_ERROR);
slf4jConfiguration.setRootLoggerLevel(Slf4jConfiguration.Level.ERROR);
}
// else fall back to default log level specified in conf
// see https://issues.apache.org/jira/browse/MNG-2570

// LOG STREAMS
if (commandLine.hasOption(CLIManager.LOG_FILE)) {
File logFile = new File(commandLine.getOptionValue(CLIManager.LOG_FILE));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,16 @@
@Experimental
public class JansiMessageBuilder implements MessageBuilder {
private final Ansi ansi;
private StringBuilder sb;

@SuppressWarnings("magicnumber")
public JansiMessageBuilder() {
this.sb = new StringBuilder(80);
this.ansi = Ansi.ansi();
}

public JansiMessageBuilder(StringBuilder sb) {
this.sb = sb;
this.ansi = Ansi.ansi(sb);
}

Expand Down Expand Up @@ -159,4 +163,9 @@ public String build() {
public String toString() {
return build();
}

@Override
public void setLength(int length) {
sb.setLength(length);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
import java.io.PrintStream;

import org.apache.maven.api.services.MessageBuilder;
import org.apache.maven.cli.jansi.MessageUtils;

import static org.apache.maven.cli.jansi.MessageUtils.builder;

/**
* Logger for Maven, that support colorization of levels and stacktraces. This class implements 2 methods introduced in
Expand All @@ -30,6 +31,13 @@
* @since 3.5.0
*/
public class MavenSimpleLogger extends SimpleLogger {

private final String traceRenderedLevel = builder().trace("TRACE").build();
private final String debugRenderedLevel = builder().debug("DEBUG").build();
private final String infoRenderedLevel = builder().info("INFO").build();
private final String warnRenderedLevel = builder().warning("WARNING").build();
private final String errorRenderedLevel = builder().error("ERROR").build();

MavenSimpleLogger(String name) {
super(name);
}
Expand All @@ -38,16 +46,16 @@ public class MavenSimpleLogger extends SimpleLogger {
protected String renderLevel(int level) {
switch (level) {
case LOG_LEVEL_TRACE:
return builder().trace("TRACE").build();
return traceRenderedLevel;
case LOG_LEVEL_DEBUG:
return builder().debug("DEBUG").build();
return debugRenderedLevel;
case LOG_LEVEL_INFO:
return builder().info("INFO").build();
return infoRenderedLevel;
case LOG_LEVEL_WARN:
return builder().warning("WARNING").build();
return warnRenderedLevel;
case LOG_LEVEL_ERROR:
default:
return builder().error("ERROR").build();
return errorRenderedLevel;
}
}

Expand All @@ -56,24 +64,30 @@ protected void writeThrowable(Throwable t, PrintStream stream) {
if (t == null) {
return;
}
stream.print(builder().failure(t.getClass().getName()));
MessageBuilder builder = builder().failure(t.getClass().getName());
if (t.getMessage() != null) {
stream.print(": ");
stream.print(builder().failure(t.getMessage()));
builder.a(": ").failure(t.getMessage());
}
stream.println();
stream.println(builder);

printStackTrace(t, stream, "");
}

private void printStackTrace(Throwable t, PrintStream stream, String prefix) {
MessageBuilder builder = builder();
for (StackTraceElement e : t.getStackTrace()) {
stream.print(prefix);
stream.print(" ");
stream.print(builder().strong("at"));
stream.print(" " + e.getClassName() + "." + e.getMethodName());
stream.print(builder().a(" (").strong(getLocation(e)).a(")"));
stream.println();
builder.a(prefix);
builder.a(" ");
builder.strong("at");
builder.a(" ");
builder.a(e.getClassName());
builder.a(".");
builder.a(e.getMethodName());
builder.a(" (");
builder.strong(getLocation(e));
builder.a(")");
stream.println(builder);
builder.setLength(0);
}
for (Throwable se : t.getSuppressed()) {
writeThrowable(se, stream, "Suppressed", prefix + " ");
Expand All @@ -85,12 +99,12 @@ private void printStackTrace(Throwable t, PrintStream stream, String prefix) {
}

private void writeThrowable(Throwable t, PrintStream stream, String caption, String prefix) {
stream.print(builder().a(prefix).strong(caption).a(": ").a(t.getClass().getName()));
MessageBuilder builder =
builder().a(prefix).strong(caption).a(": ").a(t.getClass().getName());
if (t.getMessage() != null) {
stream.print(": ");
stream.print(builder().failure(t.getMessage()));
builder.a(": ").failure(t.getMessage());
}
stream.println();
stream.println(builder);

printStackTrace(t, stream, prefix);
}
Expand All @@ -103,13 +117,9 @@ protected String getLocation(final StackTraceElement e) {
} else if (e.getFileName() == null) {
return "Unknown Source";
} else if (e.getLineNumber() >= 0) {
return String.format("%s:%s", e.getFileName(), e.getLineNumber());
return e.getFileName() + ":" + e.getLineNumber();
gnodet marked this conversation as resolved.
Show resolved Hide resolved
} else {
return e.getFileName();
}
}

private MessageBuilder builder() {
return MessageUtils.builder();
}
}