Skip to content

Commit

Permalink
#3797: better logging
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Jan 10, 2025
1 parent 41edfc2 commit 52ff65a
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 63 deletions.
2 changes: 1 addition & 1 deletion eo-maven-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ SOFTWARE.
<dependency>
<groupId>org.eolang</groupId>
<artifactId>lints</artifactId>
<version>0.0.26</version>
<version>0.0.31</version>
</dependency>
<dependency>
<groupId>org.glassfish</groupId>
Expand Down
131 changes: 71 additions & 60 deletions eo-maven-plugin/src/main/java/org/eolang/maven/LintMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.jcabi.xml.XMLDocument;
import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -143,7 +144,7 @@ private int lintOne(final ForeignTojo tojo,
final Path target = new Place(name).make(base, AssembleMojo.XMIR);
tojo.withLinted(
new FpDefault(
src -> this.counted(LintMojo.lint(xmir), counts).toString(),
src -> LintMojo.lint(xmir, counts).toString(),
this.cache.toPath().resolve(LintMojo.CACHE),
this.plugin.getVersion(),
new TojoHash(tojo),
Expand Down Expand Up @@ -175,103 +176,113 @@ private int lintAll(final ConcurrentHashMap<Severity, Integer> counts) throws IO
pkg.get(defect.program()),
new ListOf<>(defect)
);
LintMojo.logOne(defect);
}
return pkg.size();
}

/**
* Log errors of XMIR.
* @param xml XMIR.
* @param counts Counts of errors, warnings, and critical
* @return TRUE if it's good
* Log one defect.
* @param defect The defect to log
*/
private XML counted(final XML xml, final ConcurrentHashMap<Severity, Integer> counts) {
for (final XML error : xml.nodes("/program/errors/error")) {
final List<String> line = error.xpath("@line");
final StringBuilder message = new StringBuilder()
.append(Logger.format("%[file]s", xml.xpath("/program/@source").get(0)));
if (!line.isEmpty()) {
message.append(':').append(Integer.parseInt(line.get(0)));
}
message.append(' ')
.append(error.xpath("text()").get(0))
.append(" (")
.append(error.xpath("@check").get(0))
.append(' ')
.append(error.xpath("@severity").get(0))
.append(')');
final Severity severity = Severity.parsed(error.xpath("@severity").get(0));
counts.compute(severity, (sev, before) -> before + 1);
switch (severity) {
case WARNING:
Logger.warn(this, message.toString());
break;
case ERROR:
case CRITICAL:
Logger.error(this, message.toString());
break;
default:
throw new IllegalArgumentException(
String.format("Not yet supported severity: %s", severity)
);
}
private static void logOne(final Defect defect) {
final StringBuilder message = new StringBuilder()
.append(defect.program())
.append(':').append(defect.line())
.append(' ')
.append(defect.text())
.append(" (")
.append(defect.rule())
.append(' ')
.append(defect.severity())
.append(')');
switch (defect.severity()) {
case WARNING:
Logger.warn(LintMojo.class, message.toString());
break;
case ERROR:
case CRITICAL:
Logger.error(LintMojo.class, message.toString());
break;
default:
throw new IllegalArgumentException(
String.format(
"Not yet supported severity: %s",
defect.severity()
)
);
}
return xml;
}

/**
* Text in plural or singular form.
* @param count Counts of errors, warnings, and critical
* @param name Name of them
* @return Summary text
*/
private static String plural(final int count, final String name) {
final StringBuilder txt = new StringBuilder();
txt.append(count).append(' ').append(name);
if (count > 1) {
txt.append('s');
}
return txt.toString();
}
/**
* Summarize the counts.
* @param counts Counts of errors, warnings, and critical
* @return Summary text
*/
private static String summary(final ConcurrentHashMap<Severity, Integer> counts) {
final StringBuilder sum = new StringBuilder(100);
final List<String> parts = new ArrayList<>(0);
final int criticals = counts.get(Severity.CRITICAL);
if (criticals > 0) {
sum.append(criticals).append(" critical error");
if (criticals > 1) {
sum.append('s');
}
parts.add(LintMojo.plural(criticals, "critical error"));
}
final int errors = counts.get(Severity.ERROR);
if (errors > 0) {
if (sum.length() > 0) {
sum.append(", ");
}
sum.append(errors).append(" error");
if (errors > 1) {
sum.append('s');
}
parts.add(LintMojo.plural(errors, "error"));
}
final int warnings = counts.get(Severity.WARNING);
if (warnings > 0) {
if (sum.length() > 0) {
sum.append(", ");
}
sum.append(warnings).append(" warning");
if (warnings > 1) {
sum.append('s');
}
parts.add(LintMojo.plural(warnings, "warning"));
}
if (sum.length() == 0) {
sum.append("no complaints");
if (parts.isEmpty()) {
parts.add("no complaints");
}
final String sum;
if (parts.size() == 1) {
sum = parts.get(0);
} else if (parts.size() == 2) {
sum = String.join(" and ", parts);
} else {
sum = String.format(
"%s, and %s",
String.join(", ", parts.subList(0, parts.size() - 2)),
parts.get(parts.size() - 1)
);
}
return sum.toString();
return sum;
}

/**
* Find all possible linting defects and add them to the XMIR.
* @param xmir The XML before linting
* @param counts Counts of errors, warnings, and critical
* @return XML after linting
* @throws IOException If fails
*/
private static XML lint(final XML xmir) throws IOException {
private static XML lint(final XML xmir,
final ConcurrentHashMap<Severity, Integer> counts) {
final Directives dirs = new Directives();
final Collection<Defect> defects = new Program(xmir).defects();
if (!defects.isEmpty()) {
dirs.xpath("/program").addIf("errors").strict(1);
LintMojo.embed(xmir, defects);
}
for (final Defect defect : defects) {
counts.compute(defect.severity(), (sev, before) -> before + 1);
LintMojo.logOne(defect);
}
final Node node = xmir.inner();
new Xembler(dirs).applyQuietly(node);
return new XMLDocument(node);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,10 @@ FakeMaven withPlacedBinary(final Path binary) {
FakeMaven withHelloWorld() throws IOException {
return this.withProgram(
"+alias stdout org.eolang.io.stdout",
"+home https://www.eolang.org",
"+version 0.0.0",
"+package f\n",
"# No comments.",
"# No comments here, since it's just an example.",
"[x] > main",
" (stdout \"Hello!\" x).print > @"
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ void detectsWarningWithCorrespondingFlag(@Mktmp final Path temp) throws IOExcept
new XMLDocument(
maven.result().get("target/6-lint/foo/x/main.xmir")
).nodes("//errors/error[@severity='warning']"),
Matchers.hasSize(Matchers.equalTo(6))
Matchers.hasSize(Matchers.greaterThanOrEqualTo(6))
);
}

Expand Down
11 changes: 11 additions & 0 deletions eo-maven-plugin/src/test/resources/log4j.properties
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ log4j.logger.org.eolang=INFO
log4j.logger.com.yegor256.Jaxec=WARN
log4j.logger.com.yegor256.farea=WARN
log4j.logger.org.eolang.parser.Syntax=WARN
log4j.logger.org.eolang.maven.PlaceMojo=WARN
log4j.logger.org.eolang.maven.DiscoverMojo=WARN
log4j.logger.org.eolang.maven.RegisterMojo=WARN
log4j.logger.org.eolang.maven.CleanMojo=WARN
log4j.logger.org.eolang.maven.PhiMojo=WARN
log4j.logger.org.eolang.maven.ShakeMojo=WARN
log4j.logger.org.eolang.maven.UnphiMojo=WARN
log4j.logger.org.eolang.maven.LintMojo=INFO
log4j.logger.org.eolang.maven.ParseMojo=WARN
log4j.logger.org.eolang.maven.PrintMojo=WARN
log4j.logger.org.eolang.maven.OyIndexed=ERROR
log4j.logger.com.jcabi.log.VerboseProcess=WARN
log4j.logger.org.eolang.parser.EoSyntax=WARN
log4j.logger.org.eolang.parser.Program=WARN
Expand Down
1 change: 1 addition & 0 deletions eo-parser/src/test/resources/log4j.properties
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=com.jcabi.log.MulticolorLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{HH:mm:ss} [%color{%p}] %c: %m%n

log4j.logger.org.eolang.parser.StrictXmir=ERROR
log4j.logger.com.jcabi.log=WARN
log4j.logger.com.yegor256.xsline=WARN
log4j.logger.com.yegor256.xsline.StSchema=DEBUG
Expand Down

0 comments on commit 52ff65a

Please sign in to comment.