Improve logged exec output readability (#36217)

* Improve logged exec output readability

- Split error and out streams and log them separately
- Log everything in a single call to prevent interference from
  other log messages
This commit is contained in:
Alpar Torok 2018-12-10 08:23:03 +02:00 committed by GitHub
parent a42502df8b
commit e160346b9a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 12 additions and 8 deletions

View File

@ -21,16 +21,18 @@ public class LoggedExec extends Exec {
public LoggedExec() { public LoggedExec() {
ByteArrayOutputStream output = new ByteArrayOutputStream(); ByteArrayOutputStream output = new ByteArrayOutputStream();
ByteArrayOutputStream error = new ByteArrayOutputStream();
if (getLogger().isInfoEnabled() == false) { if (getLogger().isInfoEnabled() == false) {
setStandardOutput(output); setStandardOutput(output);
setErrorOutput(output); setErrorOutput(error);
setIgnoreExitValue(true); setIgnoreExitValue(true);
doLast((unused) -> { doLast((unused) -> {
if (getExecResult().getExitValue() != 0) { if (getExecResult().getExitValue() != 0) {
try { try {
for (String line : output.toString("UTF-8").split("\\R")) { getLogger().error("Standard output:");
getLogger().error(line); getLogger().error(output.toString("UTF-8"));
} getLogger().error("Standard error:");
getLogger().error(error.toString("UTF-8"));
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
throw new GradleException("Failed to read exec output", e); throw new GradleException("Failed to read exec output", e);
} }
@ -65,17 +67,19 @@ public class LoggedExec extends Exec {
return function.apply(action); return function.apply(action);
} }
ByteArrayOutputStream output = new ByteArrayOutputStream(); ByteArrayOutputStream output = new ByteArrayOutputStream();
ByteArrayOutputStream error = new ByteArrayOutputStream();
try { try {
return function.apply(spec -> { return function.apply(spec -> {
spec.setStandardOutput(output); spec.setStandardOutput(output);
spec.setErrorOutput(output); spec.setErrorOutput(error);
action.execute(spec); action.execute(spec);
}); });
} catch (Exception e) { } catch (Exception e) {
try { try {
for (String line : output.toString("UTF-8").split("\\R")) { project.getLogger().error("Standard output:");
project.getLogger().error(line); project.getLogger().error(output.toString("UTF-8"));
} project.getLogger().error("Standard error:");
project.getLogger().error(error.toString("UTF-8"));
} catch (UnsupportedEncodingException ue) { } catch (UnsupportedEncodingException ue) {
throw new GradleException("Failed to read exec output", ue); throw new GradleException("Failed to read exec output", ue);
} }