Skip to content

Commit

Permalink
resolves asciidoctor#536 configure the failure level and fail the tas…
Browse files Browse the repository at this point in the history
…k accordingly
  • Loading branch information
ggrossetie committed Mar 23, 2020
1 parent a14cfa1 commit 64223f1
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2013-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.asciidoctor.gradle.base.process

import groovy.transform.CompileStatic

/**
* Logging severity (Ruby).
*/
@CompileStatic
enum LoggerSeverity implements Serializable {
DEBUG(0),
ERROR(3),
FATAL(4), // also know as QUIET in ExecutorLogLevel
INFO(1),
WARN(2),

final int level

private LoggerSeverity(int level) {
this.level = level
}

static LoggerSeverity of(int level) {
for (value in values()) {
if (value.level == level) {
return value
}
}
return null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package org.asciidoctor.gradle.internal

import groovy.transform.CompileStatic
import groovy.transform.TupleConstructor
import org.asciidoctor.gradle.base.process.LoggerSeverity

import java.util.regex.Pattern

Expand Down Expand Up @@ -53,6 +54,7 @@ class ExecutorConfiguration implements Serializable, Cloneable {
List<Object> asciidoctorExtensions

ExecutorLogLevel executorLogLevel
LoggerSeverity failureLevel

String toString() {
"""backend(s) = ${backendName}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import org.asciidoctor.gradle.base.AbstractAsciidoctorBaseTask
import org.asciidoctor.gradle.base.AsciidoctorAttributeProvider
import org.asciidoctor.gradle.base.Transform
import org.asciidoctor.gradle.base.internal.Workspace
import org.asciidoctor.gradle.base.process.LoggerSeverity
import org.asciidoctor.gradle.base.process.ProcessMode
import org.asciidoctor.gradle.internal.ExecutorConfiguration
import org.asciidoctor.gradle.internal.ExecutorConfigurationContainer
Expand Down Expand Up @@ -69,10 +70,16 @@ class AbstractAsciidoctorTask extends AbstractAsciidoctorBaseTask {
public final static ProcessMode OUT_OF_PROCESS = ProcessMode.OUT_OF_PROCESS
public final static ProcessMode JAVA_EXEC = ProcessMode.JAVA_EXEC

public final static LoggerSeverity FATAL = LoggerSeverity.FATAL
public final static LoggerSeverity ERROR = LoggerSeverity.ERROR
public final static LoggerSeverity WARN = LoggerSeverity.WARN
public final static LoggerSeverity INFO = LoggerSeverity.INFO

protected final static GradleVersion LAST_GRADLE_WITH_CLASSPATH_LEAKAGE = GradleVersion.version(('5.99'))

protected final AsciidoctorJExtension asciidoctorj
private ProcessMode inProcess = JAVA_EXEC
private LoggerSeverity failureLevel = LoggerSeverity.FATAL
private final WorkerExecutor worker
private final List<Object> asciidocConfigurations = []

Expand Down Expand Up @@ -107,6 +114,32 @@ class AbstractAsciidoctorTask extends AbstractAsciidoctorBaseTask {
this.inProcess
}

/** Set the minimum logging level that will fail the task.
*
* @param severity {@link #FATAL}, {@link #ERROR} or {@link #WARN} or {@link #INFO}.
*/
void setFailureLevel(LoggerSeverity severity) {
this.failureLevel = severity
}

/** Set the minimum logging level that will fail the task.
*
* @param severity Case-insensitive string from of {@link #FATAL}, {@link #ERROR} or {@link #WARN} or {@link #INFO}.
*/
void setFailureLevel(String severity) {
this.failureLevel = LoggerSeverity.valueOf(severity.toUpperCase(Locale.US))
}

/** Get the minimum logging level that will fail the task.
*
* Valid options are {@link #FATAL}, {@link #ERROR} or {@link #WARN} or {@link #INFO}.
* The default mode is {@link #FATAL}.
*/
@Internal
LoggerSeverity getFailureLevel() {
this.failureLevel
}

/** Set the mode for running conversions sequential or in parallel.
* For instance a task that has multiple backends can have the
* conversion in parallel.
Expand Down Expand Up @@ -393,6 +426,7 @@ class AbstractAsciidoctorTask extends AbstractAsciidoctorBaseTask {
projectDir: project.projectDir,
rootDir: project.rootProject.projectDir,
options: evaluateProviders(options),
failureLevel: failureLevel,
attributes: preparePreserialisedAttributes(workingSourceDir, lang),
backendName: backendName,
logDocuments: logDocuments,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ import groovy.util.logging.Log4j
import org.apache.log4j.Level
import org.apache.log4j.LogManager
import org.asciidoctor.Asciidoctor
import org.asciidoctor.gradle.base.process.LoggerSeverity
import org.asciidoctor.gradle.internal.ExecutorConfiguration
import org.asciidoctor.gradle.internal.ExecutorConfigurationContainer
import org.asciidoctor.gradle.internal.ExecutorLogLevel
import org.asciidoctor.groovydsl.AsciidoctorExtensions
import org.asciidoctor.log.LogHandler
import org.gradle.api.GradleException

import javax.inject.Inject

Expand All @@ -39,6 +41,9 @@ import static org.asciidoctor.jruby.AsciidoctorJRuby.Factory.create
@Log4j
class AsciidoctorJExecuter extends ExecutorBase implements Runnable {

private LoggerSeverity maxSeverity = null
private LoggerSeverity failureLevel = null

@Inject
AsciidoctorJExecuter(
final ExecutorConfigurationContainer execConfig
Expand All @@ -50,12 +55,16 @@ class AsciidoctorJExecuter extends ExecutorBase implements Runnable {
void run() {
Thread.currentThread().contextClassLoader = asciidoctorClassLoader
logLevel = findHighestLogLevel(runConfigurations*.executorLogLevel)
failureLevel = findHighestFailureLevel(runConfigurations*.failureLevel)
logClasspath(Thread.currentThread().contextClassLoader)
if (runConfigurations.size() == 1) {
runSingle()
} else {
runMultiple()
}
if (failureLevel != null && maxSeverity != null && maxSeverity >= failureLevel) {
throw new GradleException("Failure level reached or exceeded: $maxSeverity >= $failureLevel")
}
}

/** Forwards the message to Log4J.
Expand All @@ -65,6 +74,9 @@ class AsciidoctorJExecuter extends ExecutorBase implements Runnable {
*/
@Override
protected void logMessage(ExecutorLogLevel level, String msg) {
if (maxSeverity == null || level.level > maxSeverity.level) {
maxSeverity = LoggerSeverity.of(level.level)
}
switch (level) {
case ExecutorLogLevel.DEBUG:
log.debug(msg)
Expand Down Expand Up @@ -180,6 +192,11 @@ class AsciidoctorJExecuter extends ExecutorBase implements Runnable {
this.class.classLoader
}

private LoggerSeverity findHighestFailureLevel(Iterable<LoggerSeverity> levels) {
int lvl = levels*.level.min() as int
LoggerSeverity.of(lvl)
}

private ExecutorLogLevel findHighestLogLevel(Iterable<ExecutorLogLevel> levels) {
int lvl = levels*.level.min() as int
ExecutorLogLevel.values().find {
Expand Down

0 comments on commit 64223f1

Please sign in to comment.