Skip to content

Commit

Permalink
Properly escape log message additions
Browse files Browse the repository at this point in the history
Also ensure that the proper format style is preserved. Avoids various NPE, IAE, etc. exceptions when rendering log messages that have been decorated.
  • Loading branch information
dmlloyd authored and gsmet committed Sep 4, 2024
1 parent 4df321c commit 7e6c529
Showing 1 changed file with 35 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -41,6 +42,7 @@
import org.jboss.jandex.DotName;
import org.jboss.jandex.IndexView;
import org.jboss.logging.Logger;
import org.jboss.logmanager.ExtLogRecord;
import org.jboss.logmanager.LogContextInitializer;
import org.jboss.logmanager.LogManager;
import org.objectweb.asm.Opcodes;
Expand Down Expand Up @@ -416,9 +418,41 @@ public void accept(LogRecord logRecord, Consumer<LogRecord> logRecordConsumer) {

if (launchMode.getLaunchMode().equals(LaunchMode.DEVELOPMENT)
&& logBuildTimeConfig.decorateStacktraces) {

String decoratedString = DecorateStackUtil.getDecoratedString(srcMainJava, elem);
if (decoratedString != null) {
logRecord.setMessage(logRecord.getMessage() + "\n\n" + decoratedString + "\n\n");
if (logRecord instanceof ExtLogRecord elr) {
switch (elr.getFormatStyle()) {
case MESSAGE_FORMAT -> {
Object[] p = elr.getParameters(); // can be null
Object[] np = p != null ? Arrays.copyOf(p, p.length + 1) : new Object[1];
np[np.length - 1] = decoratedString;
elr.setParameters(np);
elr.setMessage(elr.getMessage() + "\n\n{" + (np.length - 1) + "}\n\n");
}
case PRINTF -> {
Object[] p = elr.getParameters(); // can be null
Object[] np = p != null ? Arrays.copyOf(p, p.length + 1) : new Object[1];
np[np.length - 1] = decoratedString;
elr.setParameters(np);
elr.setMessage(elr.getMessage() + "\n\n%" + (np.length - 1) + "$s",
ExtLogRecord.FormatStyle.PRINTF);
}
case NO_FORMAT -> {
elr.setParameters(new Object[] {
elr.getMessage(),
decoratedString
});
elr.setMessage("{0}\n\n{1}\n\n");
}
}
} else {
Object[] p = logRecord.getParameters(); // can be null
Object[] np = p != null ? Arrays.copyOf(p, p.length + 1) : new Object[1];
np[np.length - 1] = decoratedString;
logRecord.setParameters(np);
logRecord.setMessage(logRecord.getMessage() + "\n\n{" + (np.length - 1) + "}\n\n");
}
}
}

Expand Down

0 comments on commit 7e6c529

Please sign in to comment.