Skip to content

Commit

Permalink
[MNG-7899] Various memory usage improvements
Browse files Browse the repository at this point in the history
Multiple optimizations :

- renderLevel() method use static constants instead of rebuilding the
strings on each call
- replace + operator usage with more PrintStream.print() calls to reduce
temporary strings creation
- reduce usage of MessageBuilder.a() method usage with more
PrintStream.print() calls to reduce temporary strings creation
- replace the builder() method with a static import
  • Loading branch information
sebastien-doyon committed Sep 27, 2023
1 parent 14d1606 commit c03db56
Showing 1 changed file with 25 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@

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 +29,14 @@
* @since 3.5.0
*/
public class MavenSimpleLogger extends SimpleLogger {

private static final String TRACE_RENDERED_LEVEL = builder().trace("TRACE").build();
private static final String DEBUG_RENDERED_LEVEL = builder().debug("DEBUG").build();
private static final String INFO_RENDERED_LEVEL = builder().info("INFO").build();
private static final String WARN_RENDERED_LEVEL =
builder().warning("WARNING").build();
private static final String ERROR_RENDERED_LEVEL = builder().error("ERROR").build();

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

Expand All @@ -71,8 +78,13 @@ private void printStackTrace(Throwable t, PrintStream stream, String prefix) {
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.print(" ");
stream.print(e.getClassName());
stream.print(".");
stream.print(e.getMethodName());
stream.print(" (");
stream.print(builder().strong(getLocation(e)));
stream.print(")");
stream.println();
}
for (Throwable se : t.getSuppressed()) {
Expand All @@ -85,7 +97,10 @@ 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()));
stream.print(prefix);
stream.print(builder().strong(caption));
stream.print(": ");
stream.print(t.getClass().getName());
if (t.getMessage() != null) {
stream.print(": ");
stream.print(builder().failure(t.getMessage()));
Expand All @@ -108,8 +123,4 @@ protected String getLocation(final StackTraceElement e) {
return e.getFileName();
}
}

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

0 comments on commit c03db56

Please sign in to comment.