Search in sources :

Example 1 with ScopedLogHandlersManager

use of net.sourceforge.pmd.util.log.ScopedLogHandlersManager in project pmd by pmd.

the class PMD method run.

/**
 * Parses the command line arguments and executes PMD.
 *
 * @param args
 *            command line arguments
 * @return the exit code, where <code>0</code> means successful execution,
 *         <code>1</code> means error, <code>4</code> means there have been
 *         violations found.
 */
public static int run(String[] args) {
    int status = 0;
    long start = System.nanoTime();
    final PMDParameters params = PMDCommandLineInterface.extractParameters(new PMDParameters(), args, "pmd");
    final PMDConfiguration configuration = params.toConfiguration();
    final Level logLevel = params.isDebug() ? Level.FINER : Level.INFO;
    final Handler logHandler = new ConsoleLogHandler();
    final ScopedLogHandlersManager logHandlerManager = new ScopedLogHandlersManager(logLevel, logHandler);
    final Level oldLogLevel = LOG.getLevel();
    // Need to do this, since the static logger has already been initialized
    // at this point
    LOG.setLevel(logLevel);
    try {
        int violations = PMD.doPMD(configuration);
        if (violations > 0 && configuration.isFailOnViolation()) {
            status = PMDCommandLineInterface.VIOLATIONS_FOUND;
        } else {
            status = 0;
        }
    } catch (Exception e) {
        System.out.println(PMDCommandLineInterface.buildUsageText());
        System.out.println();
        System.err.println(e.getMessage());
        status = PMDCommandLineInterface.ERROR_STATUS;
    } finally {
        logHandlerManager.close();
        LOG.setLevel(oldLogLevel);
        if (params.isBenchmark()) {
            long end = System.nanoTime();
            Benchmarker.mark(Benchmark.TotalPMD, end - start, 0);
            // TODO get specified report format from config
            TextReport report = new TextReport();
            report.generate(Benchmarker.values(), System.err);
        }
    }
    return status;
}
Also used : PMDParameters(net.sourceforge.pmd.cli.PMDParameters) LanguageVersionHandler(net.sourceforge.pmd.lang.LanguageVersionHandler) ConsoleLogHandler(net.sourceforge.pmd.util.log.ConsoleLogHandler) Handler(java.util.logging.Handler) Level(java.util.logging.Level) TextReport(net.sourceforge.pmd.benchmark.TextReport) ConsoleLogHandler(net.sourceforge.pmd.util.log.ConsoleLogHandler) ScopedLogHandlersManager(net.sourceforge.pmd.util.log.ScopedLogHandlersManager) URISyntaxException(java.net.URISyntaxException) SQLException(java.sql.SQLException) IOException(java.io.IOException)

Example 2 with ScopedLogHandlersManager

use of net.sourceforge.pmd.util.log.ScopedLogHandlersManager in project pmd by pmd.

the class PMDTaskImpl method execute.

public void execute() throws BuildException {
    final Handler antLogHandler = new AntLogHandler(project);
    final ScopedLogHandlersManager logManager = new ScopedLogHandlersManager(Level.FINEST, antLogHandler);
    try {
        doTask();
    } finally {
        logManager.close();
        // exceptions
        if (configuration.getClassLoader() instanceof ClasspathClassLoader) {
            IOUtil.tryCloseClassLoader(configuration.getClassLoader());
        }
    }
}
Also used : ClasspathClassLoader(net.sourceforge.pmd.util.ClasspathClassLoader) AntLogHandler(net.sourceforge.pmd.util.log.AntLogHandler) Handler(java.util.logging.Handler) AntLogHandler(net.sourceforge.pmd.util.log.AntLogHandler) ScopedLogHandlersManager(net.sourceforge.pmd.util.log.ScopedLogHandlersManager)

Aggregations

Handler (java.util.logging.Handler)2 ScopedLogHandlersManager (net.sourceforge.pmd.util.log.ScopedLogHandlersManager)2 IOException (java.io.IOException)1 URISyntaxException (java.net.URISyntaxException)1 SQLException (java.sql.SQLException)1 Level (java.util.logging.Level)1 TextReport (net.sourceforge.pmd.benchmark.TextReport)1 PMDParameters (net.sourceforge.pmd.cli.PMDParameters)1 LanguageVersionHandler (net.sourceforge.pmd.lang.LanguageVersionHandler)1 ClasspathClassLoader (net.sourceforge.pmd.util.ClasspathClassLoader)1 AntLogHandler (net.sourceforge.pmd.util.log.AntLogHandler)1 ConsoleLogHandler (net.sourceforge.pmd.util.log.ConsoleLogHandler)1