8279821: JFR: Log warnings properly when loading a misconfigured .jfc file

Reviewed-by: mgronlun
This commit is contained in:
Erik Gahlin 2022-01-12 05:16:36 +00:00
parent 1c688f4137
commit f16f6a95df
3 changed files with 10 additions and 9 deletions

View File

@ -240,7 +240,7 @@ final class DCmdStart extends AbstractDCmd {
paths.add(JFC.createSafePath(setting));
}
try {
JFCModel model = new JFCModel(paths);
JFCModel model = new JFCModel(paths, l -> logWarning(l));
Set<String> jfcOptions = new HashSet<>();
for (XmlInput input : model.getInputs()) {
jfcOptions.add(input.getName());

View File

@ -32,6 +32,7 @@ import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import jdk.jfr.internal.SecuritySupport.SafePath;
@ -42,19 +43,19 @@ public final class JFCModel {
private final Map<String, List<ControlElement>> controls = new LinkedHashMap<>();
private final XmlConfiguration configuration;
public JFCModel(SafePath file) throws ParseException, IOException {
public JFCModel(SafePath file, Consumer<String> logger) throws ParseException, IOException {
this.configuration = createConfiguration(file);
this.configuration.validate();
addControls();
wireConditions();
wireSettings();
wireSettings(logger);
}
public JFCModel(List<SafePath> files) throws IOException, ParseException {
public JFCModel(List<SafePath> files, Consumer<String> logger) throws IOException, ParseException {
this.configuration = new XmlConfiguration();
this.configuration.setAttribute("version", "2.0");
for (SafePath file : files) {
JFCModel model = new JFCModel(file);
JFCModel model = new JFCModel(file, logger);
for (var entry : model.controls.entrySet()) {
String name = entry.getKey();
// Fail-fast checks that prevents an ambiguous file to be written later
@ -183,14 +184,14 @@ public final class JFCModel {
}
}
private void wireSettings() {
private void wireSettings(Consumer<String> logger) {
for (XmlEvent event : configuration.getEvents()) {
for (XmlSetting setting : event.getSettings()) {
var controlName = setting.getControl();
if (controlName.isPresent()) {
List<ControlElement> controls = getControlElements(controlName.get());
if (controls.isEmpty()) {
System.out.println("Warning! Setting '" + setting.getFullName() + "' refers to missing control '" + controlName.get() + "'");
logger.accept("Setting '" + setting.getFullName() + "' refers to missing control '" + controlName.get() + "'");
}
for (ControlElement ce : controls) {
XmlElement control = (XmlElement) ce;

View File

@ -127,7 +127,7 @@ final class Configure extends Command {
}
private void displayParameters(PrintStream stream, SafePath path, String name) throws ParseException, IOException {
JFCModel parameters = new JFCModel(path);
JFCModel parameters = new JFCModel(path, l -> stream.println("Warning! " + l));
stream.println();
stream.println("Options for " + name + ":");
stream.println();
@ -195,7 +195,7 @@ final class Configure extends Command {
output = new SafePath(Path.of("custom.jfc"));
}
UserInterface ui = new UserInterface();
JFCModel model = new JFCModel(inputFiles);
JFCModel model = new JFCModel(inputFiles, l -> ui.println("Warning! " + l));
model.setLabel("Custom");
if (log) {
SettingsLog.enable();