Search in sources :

Example 1 with DefaultLogger

use of com.puppycrawl.tools.checkstyle.DefaultLogger in project maven-plugins by apache.

the class CheckstyleViolationCheckMojo method getListener.

private AuditListener getListener() throws MojoFailureException, MojoExecutionException {
    AuditListener listener = null;
    if (StringUtils.isNotEmpty(outputFileFormat)) {
        File resultFile = outputFile;
        OutputStream out = getOutputStream(resultFile);
        if ("xml".equals(outputFileFormat)) {
            listener = new XMLLogger(out, true);
        } else if ("plain".equals(outputFileFormat)) {
            try {
                // Write a plain output file to the standard output file,
                // and write an XML output file to the temp directory that can be used to count violations
                outputXmlFile = File.createTempFile("checkstyle-result", ".xml");
                outputXmlFile.deleteOnExit();
                OutputStream xmlOut = getOutputStream(outputXmlFile);
                CompositeAuditListener compoundListener = new CompositeAuditListener();
                compoundListener.addListener(new XMLLogger(xmlOut, true));
                compoundListener.addListener(new DefaultLogger(out, true));
                listener = compoundListener;
            } catch (IOException e) {
                throw new MojoExecutionException("Unable to create temporary file", e);
            }
        } else {
            throw new MojoFailureException("Invalid output file format: (" + outputFileFormat + "). Must be 'plain' or 'xml'.");
        }
    }
    return listener;
}
Also used : XMLLogger(com.puppycrawl.tools.checkstyle.XMLLogger) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) MojoFailureException(org.apache.maven.plugin.MojoFailureException) IOException(java.io.IOException) AuditListener(com.puppycrawl.tools.checkstyle.api.AuditListener) File(java.io.File) DefaultLogger(com.puppycrawl.tools.checkstyle.DefaultLogger)

Example 2 with DefaultLogger

use of com.puppycrawl.tools.checkstyle.DefaultLogger in project maven-plugins by apache.

the class AbstractCheckstyleReport method getListener.

/**
     * Creates and returns the report generation listener.
     *
     * @return The audit listener.
     * @throws MavenReportException If something goes wrong.
     */
protected AuditListener getListener() throws MavenReportException {
    AuditListener listener = null;
    if (StringUtils.isNotEmpty(outputFileFormat)) {
        File resultFile = outputFile;
        OutputStream out = getOutputStream(resultFile);
        if ("xml".equals(outputFileFormat)) {
            listener = new XMLLogger(out, true);
        } else if ("plain".equals(outputFileFormat)) {
            listener = new DefaultLogger(out, true);
        } else {
            // TODO: failure if not a report
            throw new MavenReportException("Invalid output file format: (" + outputFileFormat + "). Must be 'plain' or 'xml'.");
        }
    }
    return listener;
}
Also used : XMLLogger(com.puppycrawl.tools.checkstyle.XMLLogger) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) AuditListener(com.puppycrawl.tools.checkstyle.api.AuditListener) File(java.io.File) DefaultLogger(com.puppycrawl.tools.checkstyle.DefaultLogger) MavenReportException(org.apache.maven.reporting.MavenReportException)

Example 3 with DefaultLogger

use of com.puppycrawl.tools.checkstyle.DefaultLogger in project maven-plugins by apache.

the class AbstractCheckstyleReport method getConsoleListener.

/**
     * Creates and returns the console listener.
     *
     * @return The console listener.
     * @throws MavenReportException If something goes wrong.
     */
protected DefaultLogger getConsoleListener() throws MavenReportException {
    DefaultLogger consoleListener;
    if (useFile == null) {
        stringOutputStream = new ByteArrayOutputStream();
        consoleListener = new DefaultLogger(stringOutputStream, false);
    } else {
        OutputStream out = getOutputStream(useFile);
        consoleListener = new DefaultLogger(out, true);
    }
    return consoleListener;
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DefaultLogger(com.puppycrawl.tools.checkstyle.DefaultLogger)

Example 4 with DefaultLogger

use of com.puppycrawl.tools.checkstyle.DefaultLogger in project checkstyle by checkstyle.

the class CheckstyleAntTask method getListeners.

/**
     * Return the list of listeners set in this task.
     * @return the list of listeners.
     */
private AuditListener[] getListeners() {
    final int formatterCount = Math.max(1, formatters.size());
    final AuditListener[] listeners = new AuditListener[formatterCount];
    // formatters
    try {
        if (formatters.isEmpty()) {
            final OutputStream debug = new LogOutputStream(this, Project.MSG_DEBUG);
            final OutputStream err = new LogOutputStream(this, Project.MSG_ERR);
            listeners[0] = new DefaultLogger(debug, true, err, true);
        } else {
            for (int i = 0; i < formatterCount; i++) {
                final Formatter formatter = formatters.get(i);
                listeners[i] = formatter.createListener(this);
            }
        }
    } catch (IOException ex) {
        throw new BuildException(String.format(Locale.ROOT, "Unable to create listeners: " + "formatters {%s}.", formatters), ex);
    }
    return listeners;
}
Also used : LogOutputStream(org.apache.tools.ant.taskdefs.LogOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) BuildException(org.apache.tools.ant.BuildException) AuditListener(com.puppycrawl.tools.checkstyle.api.AuditListener) DefaultLogger(com.puppycrawl.tools.checkstyle.DefaultLogger) LogOutputStream(org.apache.tools.ant.taskdefs.LogOutputStream)

Example 5 with DefaultLogger

use of com.puppycrawl.tools.checkstyle.DefaultLogger in project checkstyle by checkstyle.

the class CheckstyleAntTaskTest method testDefaultLoggerListenerWithToFile.

@Test
public void testDefaultLoggerListenerWithToFile() throws IOException {
    final CheckstyleAntTask.Formatter formatter = new CheckstyleAntTask.Formatter();
    formatter.setUseFile(false);
    formatter.setTofile(new File("target/"));
    assertTrue(formatter.createListener(null) instanceof DefaultLogger);
}
Also used : File(java.io.File) DefaultLogger(com.puppycrawl.tools.checkstyle.DefaultLogger) Test(org.junit.Test)

Aggregations

DefaultLogger (com.puppycrawl.tools.checkstyle.DefaultLogger)6 FileOutputStream (java.io.FileOutputStream)5 OutputStream (java.io.OutputStream)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 AuditListener (com.puppycrawl.tools.checkstyle.api.AuditListener)3 File (java.io.File)3 XMLLogger (com.puppycrawl.tools.checkstyle.XMLLogger)2 IOException (java.io.IOException)2 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1 MojoFailureException (org.apache.maven.plugin.MojoFailureException)1 MavenReportException (org.apache.maven.reporting.MavenReportException)1 BuildException (org.apache.tools.ant.BuildException)1 LogOutputStream (org.apache.tools.ant.taskdefs.LogOutputStream)1 Test (org.junit.Test)1