Skip to content

Commit

Permalink
Print not mapped API messages to the log
Browse files Browse the repository at this point in the history
Currently if a message can't be mapped to the logfile it is simply
ignored.

This now always print them to the log if they can't be mapped and print
a waring about the issue as it is usually some configuration problem.
  • Loading branch information
laeubi committed Feb 6, 2024
1 parent e1d4df4 commit e933ba5
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -195,10 +196,6 @@ public void execute() throws MojoExecutionException, MojoFailureException {
.collect(Collectors.groupingBy(IApiProblem::getSeverity));
List<IApiProblem> errors = problems.getOrDefault(ApiPlugin.SEVERITY_ERROR, List.of());
List<IApiProblem> warnings = problems.getOrDefault(ApiPlugin.SEVERITY_WARNING, List.of());
if (printSummary) {
log.info(errors.size() + " API ERRORS");
log.info(warnings.size() + " API warnings");
}
if (printProblems) {
for (IApiProblem problem : errors) {
printProblem(problem, "API ERROR", log::error);
Expand All @@ -209,11 +206,31 @@ public void execute() throws MojoExecutionException, MojoFailureException {
}
if (enhanceLogs && logDirectory != null && logDirectory.isDirectory()) {
try {
LogFileEnhancer.enhanceXml(logDirectory, analysisResult);
AtomicInteger notMapped = new AtomicInteger();
LogFileEnhancer.enhanceXml(logDirectory, analysisResult, notfound -> {
notMapped.incrementAndGet();
if (printProblems) {
// it was already printed before...
return;
}
if (ApiPlugin.SEVERITY_ERROR == notfound.getSeverity()) {
printProblem(notfound, "API ERROR", log::error);
} else if (ApiPlugin.SEVERITY_WARNING == notfound.getSeverity()) {
printProblem(notfound, "API WARNING", log::warn);
}
});
int count = notMapped.get();
if (count > 0) {
log.warn(count + " API problems can't be mapped to the compiler log!");
}
} catch (IOException e) {
log.warn("Can't enhance logs in directory " + logDirectory);
}
}
if (printSummary) {
log.info(errors.size() + " API ERRORS");
log.info(warnings.size() + " API warnings");
}
if (errors.size() > 0 && failOnError) {
String msg = errors.stream().map(problem -> {
if (problem.getResourcePath() == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Set;
import java.util.function.Consumer;
import java.util.stream.Collectors;

import org.eclipse.pde.api.tools.internal.provisional.ApiPlugin;
Expand All @@ -46,23 +48,27 @@ public class LogFileEnhancer {
private static final String ATTRIBUTES_INFOS = "infos";
private static final String ATTRIBUTES_ERRORS = "errors";

public static void enhanceXml(File logDirectory, ApiAnalysisResult analysisResult) throws IOException {
public static void enhanceXml(File logDirectory, ApiAnalysisResult analysisResult,
Consumer<IApiProblem> notFoundConsumer) throws IOException {
Map<String, List<IApiProblem>> problems = analysisResult.problems()
.collect(Collectors.groupingBy(IApiProblem::getResourcePath));
.collect(Collectors.groupingBy(problem -> Objects.requireNonNullElse(problem.getResourcePath(),
"no-path-" + System.identityHashCode(problem))));
if (problems.isEmpty()) {
return;
}
Set<File> needsUpdate = new HashSet<>();
Map<File, Document> documents = readDocuments(logDirectory);
for (Entry<String, List<IApiProblem>> problemEntry : problems.entrySet()) {
String path = problemEntry.getKey();
boolean found = false;
for (Entry<File, Document> documentEntry : documents.entrySet()) {
Document document = documentEntry.getValue();
Element statsElement = getStatsElement(document);
for (Element sources : document.getRootElement().getChildren("sources")) {
for (Element source : sources.getChildren("source")) {
String pathAttribute = source.getAttributeValue("path");
if (pathAttribute != null && !pathAttribute.isEmpty() && pathAttribute.endsWith(path)) {
found = true;
needsUpdate.add(documentEntry.getKey());
Element problemsElement = getProblemsElement(source);
List<IApiProblem> list = problemEntry.getValue();
Expand Down Expand Up @@ -90,7 +96,9 @@ public static void enhanceXml(File logDirectory, ApiAnalysisResult analysisResul
}
}
}

if (!found) {
problemEntry.getValue().forEach(notFoundConsumer);
}
}
writeDocuments(needsUpdate, documents);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public enum InjectP2MavenMetadataHandling {
private String resolver;

private List<TargetEnvironment> environments = new ArrayList<>();
private List<TargetEnvironment> filteredEnvironments = new ArrayList<>();

private boolean implicitTargetEnvironment = true;

Expand Down Expand Up @@ -340,4 +341,12 @@ public void addTargetLocation(Xpp3Dom locationDom) {
xmlFragments.add(locationDom);
}

public void addFilteredEnvironment(TargetEnvironment environment) {
filteredEnvironments.add(environment);
}

public List<TargetEnvironment> getFilteredEnvironments() {
return filteredEnvironments;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@
import org.eclipse.tycho.helper.PluginRealmHelper;
import org.eclipse.tycho.model.project.EclipseProject;
import org.eclipse.tycho.targetplatform.TargetDefinition;
import org.osgi.framework.Filter;

import aQute.bnd.osgi.Processor;

Expand Down Expand Up @@ -185,10 +184,10 @@ public TargetPlatformConfiguration getTargetPlatformConfiguration(ReactorProject
public Collection<TargetEnvironment> getTargetEnvironments(MavenProject project) {
TychoProject tychoProject = projectTypes.get(project.getPackaging());
if (tychoProject != null) {
Filter environmentFilter = tychoProject.getTargetEnvironmentFilter(project);
return getTargetPlatformConfiguration(project).getEnvironments().stream()
.filter(te -> te.match(environmentFilter)).toList();
//these will already be filtered at reading the target configuration
return getTargetPlatformConfiguration(project).getEnvironments();
}
//if no tycho project, just assume the default running environment
return List.of(TargetEnvironment.getRunningEnvironment());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -332,8 +331,8 @@ private void setBREEHeaderSelectionPolicy(TargetPlatformConfiguration result, Xp
/**
* Take the constraints of the configured execution environment into account when resolving
* dependencies or target definitions. These constraints include the list of system packages and
* the <code>Bundle-RequiredExecutionEnvironment</code> header. When set to <code>true</code>, the
* dependency resolution verifies that the bundle and all required bundles can be used in an
* the <code>Bundle-RequiredExecutionEnvironment</code> header. When set to <code>true</code>,
* the dependency resolution verifies that the bundle and all required bundles can be used in an
* OSGi container with the configured execution environment.
*/
private void setResolveWithEEContraints(TargetPlatformConfiguration result, Xpp3Dom resolverDom) {
Expand Down Expand Up @@ -372,20 +371,20 @@ private void addTargetEnvironments(TargetPlatformConfiguration result, MavenProj
Xpp3Dom environmentsDom = configuration.getChild(ENVIRONMENTS);
if (environmentsDom != null) {
Filter filter = getTargetEnvironmentFilter(tychoProject, project);
List<TargetEnvironment> skipped = new ArrayList<>();
for (Xpp3Dom environmentDom : environmentsDom.getChildren("environment")) {
TargetEnvironment environment = newTargetEnvironment(environmentDom);
if (environment.match(filter)) {
result.addEnvironment(environment);
} else {
skipped.add(environment);
result.addFilteredEnvironment(environment);
}
}
if (!skipped.isEmpty()) {
List<TargetEnvironment> filteredEnvironments = result.getFilteredEnvironments();
if (!filteredEnvironments.isEmpty()) {
logger.debug(MessageFormat.format(
"Declared TargetEnvironment(s) {0} are skipped for {1} as they do not match the project filter {2}.",
skipped.stream().map(TargetEnvironment::toFilterProperties).map(String::valueOf)
.collect(Collectors.joining(", ")),
filteredEnvironments.stream().map(TargetEnvironment::toFilterProperties)
.map(String::valueOf).collect(Collectors.joining(", ")),
project.getId(), filter));
}
}
Expand Down

0 comments on commit e933ba5

Please sign in to comment.