From 5c1b27dac519509ffe9c238101ab53415bdba214 Mon Sep 17 00:00:00 2001 From: lordofthejars Date: Fri, 12 Dec 2014 17:19:34 +0100 Subject: [PATCH] resolves #210 by redirecting asciidoctor console output to java logger system. --- asciidoctorj-core/build.gradle | 1 - .../main/java/org/asciidoctor/SafeMode.java | 9 ++ .../internal/AsciidoctorUtils.java | 3 +- .../internal/JRubyAsciidoctor.java | 94 ++++++++++++++++--- .../WhenAsciidoctorLogsToConsole.java | 34 +++++++ .../documentwithnotexistingfile.adoc | 3 + asciidoctorj-distribution/build.gradle | 1 - build.gradle | 2 - 8 files changed, 127 insertions(+), 20 deletions(-) create mode 100644 asciidoctorj-core/src/test/java/org/asciidoctor/WhenAsciidoctorLogsToConsole.java create mode 100644 asciidoctorj-core/src/test/resources/documentwithnotexistingfile.adoc diff --git a/asciidoctorj-core/build.gradle b/asciidoctorj-core/build.gradle index 0196dd15c..191440844 100644 --- a/asciidoctorj-core/build.gradle +++ b/asciidoctorj-core/build.gradle @@ -1,6 +1,5 @@ dependencies { compile "org.jruby:jruby-complete:$jrubyVersion" - compile "org.slf4j:slf4j-api:$slf4jVersion" compile "com.beust:jcommander:$jcommanderVersion" gems "rubygems:asciidoctor:$asciidoctorGemVersion" gems "rubygems:coderay:$coderayGemVersion" diff --git a/asciidoctorj-core/src/main/java/org/asciidoctor/SafeMode.java b/asciidoctorj-core/src/main/java/org/asciidoctor/SafeMode.java index 55a764539..b81ae37f9 100644 --- a/asciidoctorj-core/src/main/java/org/asciidoctor/SafeMode.java +++ b/asciidoctorj-core/src/main/java/org/asciidoctor/SafeMode.java @@ -46,4 +46,13 @@ public int getLevel() { return level; } + public static final SafeMode safeMode(int level) { + switch(level) { + case 0: return UNSAFE; + case 1: return SAFE; + case 10: return SERVER; + default: return SECURE; + } + } + } diff --git a/asciidoctorj-core/src/main/java/org/asciidoctor/internal/AsciidoctorUtils.java b/asciidoctorj-core/src/main/java/org/asciidoctor/internal/AsciidoctorUtils.java index 54c1e048b..988aa6399 100644 --- a/asciidoctorj-core/src/main/java/org/asciidoctor/internal/AsciidoctorUtils.java +++ b/asciidoctorj-core/src/main/java/org/asciidoctor/internal/AsciidoctorUtils.java @@ -92,7 +92,8 @@ private static String getOptions(Map options) { } if (options.containsKey(Options.SAFE)) { - SafeMode getSafeMode = SafeMode.values()[(Integer) options.get(Options.SAFE)]; + Integer level = (Integer) options.get(Options.SAFE); + SafeMode getSafeMode = SafeMode.safeMode(level); optionsAndAttributes.append(AsciidoctorCliOptions.SAFE) .append(" ").append(getSafeMode) .append(" "); diff --git a/asciidoctorj-core/src/main/java/org/asciidoctor/internal/JRubyAsciidoctor.java b/asciidoctorj-core/src/main/java/org/asciidoctor/internal/JRubyAsciidoctor.java index 5319d14cf..cc862552f 100644 --- a/asciidoctorj-core/src/main/java/org/asciidoctor/internal/JRubyAsciidoctor.java +++ b/asciidoctorj-core/src/main/java/org/asciidoctor/internal/JRubyAsciidoctor.java @@ -22,23 +22,41 @@ import org.jruby.RubyInstanceConfig.CompileMode; import org.jruby.embed.ScriptingContainer; import org.jruby.javasupport.JavaEmbedUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import java.io.File; -import java.io.IOException; -import java.io.Reader; -import java.io.Writer; +import java.io.*; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.logging.Level; +import java.util.logging.Logger; public class JRubyAsciidoctor implements Asciidoctor { - private static final Logger log = LoggerFactory.getLogger(JRubyAsciidoctor.class.getName()); + static { + + final Logger logger = Logger.getLogger("Asciidoctor"); + final LoggerOutputStream out = new LoggerOutputStream(logger); + System.setOut(new PrintStream(out)); + System.setErr(new PrintStream(out)); + + Runtime.getRuntime().addShutdownHook(new Thread() { + @Override + public void run() { + try { // can log or not something depending logger is already closed or not, best is to call it manually at the end of main if possible + out.flush(); + } catch (final IOException e) { + // no-op + } + } + }); + + } + + + private static final Logger logger = Logger.getLogger(JRubyAsciidoctor.class.getName()); private static final String GEM_PATH = "GEM_PATH"; @@ -279,12 +297,10 @@ public String render(String content, Map options) { this.rubyGemsPreloader.preloadRequiredLibraries(options); - if (log.isDebugEnabled()) { - log.debug(AsciidoctorUtils.toAsciidoctorCommand(options, "-")); + logger.fine(AsciidoctorUtils.toAsciidoctorCommand(options, "-")); - if (AsciidoctorUtils.isOptionWithAttribute(options, Attributes.SOURCE_HIGHLIGHTER, "pygments")) { - log.debug("In order to use Pygments with Asciidoctor, you need to install Pygments (and Python, if you don’t have it yet). Read http://asciidoctor.org/news/#syntax-highlighting-with-pygments."); - } + if (AsciidoctorUtils.isOptionWithAttribute(options, Attributes.SOURCE_HIGHLIGHTER, "pygments")) { + logger.fine("In order to use Pygments with Asciidoctor, you need to install Pygments (and Python, if you don’t have it yet). Read http://asciidoctor.org/news/#syntax-highlighting-with-pygments."); } String currentDirectory = rubyRuntime.getCurrentDirectory(); @@ -311,9 +327,7 @@ public String renderFile(File filename, Map options) { this.rubyGemsPreloader.preloadRequiredLibraries(options); - if (log.isDebugEnabled()) { - log.debug(AsciidoctorUtils.toAsciidoctorCommand(options, filename.getAbsolutePath())); - } + logger.fine(AsciidoctorUtils.toAsciidoctorCommand(options, filename.getAbsolutePath())); String currentDirectory = rubyRuntime.getCurrentDirectory(); @@ -566,4 +580,54 @@ public Document loadFile(File file, Map options) { } + static class LoggerOutputStream extends OutputStream { + private final StringBuilder builder = new StringBuilder(); + private Logger logger; + + public LoggerOutputStream(Logger logger) { + this.logger = logger; + } + + private boolean doLog() { + synchronized(this) { + if (builder.length() > 0) { + String msg = builder.toString(); + if(msg.contains("WARNING")) { + logger.logp(Level.WARNING, "", "", msg); + } else { + if(msg.contains("FAILED")) { + logger.logp(Level.SEVERE, "", "", msg); + } else { + logger.logp(Level.FINE, "", "", msg); + } + } + builder.setLength(0); + return true; + } + return false; + } + } + + @Override + public void write(int b) throws IOException { + if (b == '\n') { + if (!doLog()) { + logger.info(""); + } + } else { + builder.append((char) b); + } + } + + @Override + public void flush() throws IOException { + doLog(); + } + + @Override + public void close() throws IOException { + doLog(); + } + }; + } diff --git a/asciidoctorj-core/src/test/java/org/asciidoctor/WhenAsciidoctorLogsToConsole.java b/asciidoctorj-core/src/test/java/org/asciidoctor/WhenAsciidoctorLogsToConsole.java new file mode 100644 index 000000000..45f23bd5c --- /dev/null +++ b/asciidoctorj-core/src/test/java/org/asciidoctor/WhenAsciidoctorLogsToConsole.java @@ -0,0 +1,34 @@ +package org.asciidoctor; + +import org.asciidoctor.internal.JRubyAsciidoctor; +import org.asciidoctor.util.ClasspathResources; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +import java.io.File; + +import static org.asciidoctor.OptionsBuilder.options; + +public class WhenAsciidoctorLogsToConsole { + + @Rule + public ClasspathResources classpath = new ClasspathResources(); + + @Rule + public TemporaryFolder testFolder = new TemporaryFolder(); + + private Asciidoctor asciidoctor = JRubyAsciidoctor.create(); + + @Test + public void shouldBeRedirectToAsciidoctorJLoggerSystem() { + + File inputFile = classpath.getResource("documentwithnotexistingfile.adoc"); + String renderContent = asciidoctor.renderFile(inputFile, options() + .inPlace(true).safe(SafeMode.SERVER).asMap()); + + File expectedFile = new File(inputFile.getParent(), "documentwithnotexistingfile.html"); + expectedFile.delete(); + } + +} diff --git a/asciidoctorj-core/src/test/resources/documentwithnotexistingfile.adoc b/asciidoctorj-core/src/test/resources/documentwithnotexistingfile.adoc new file mode 100644 index 000000000..075c94b60 --- /dev/null +++ b/asciidoctorj-core/src/test/resources/documentwithnotexistingfile.adoc @@ -0,0 +1,3 @@ += My Document + +include::unexistingdoc.adoc[] \ No newline at end of file diff --git a/asciidoctorj-distribution/build.gradle b/asciidoctorj-distribution/build.gradle index 68631bbca..ca76c3b32 100644 --- a/asciidoctorj-distribution/build.gradle +++ b/asciidoctorj-distribution/build.gradle @@ -2,7 +2,6 @@ dependencies { compile project(':asciidoctorj') compile project(':asciidoctorj-epub3') compile project(':asciidoctorj-pdf') - runtime "org.slf4j:slf4j-simple:$slf4jVersion" } apply plugin: 'application' diff --git a/build.gradle b/build.gradle index 743d1bd1c..5e2ba4019 100644 --- a/build.gradle +++ b/build.gradle @@ -29,7 +29,6 @@ subprojects { jsoupVersion = '1.8.1' junitVersion = '4.12' saxonVersion = '9.5.1-6' - slf4jVersion = '1.7.7' xmlMatchersVersion = '1.0-RC1' // gem versions @@ -77,7 +76,6 @@ subprojects { dependencies { testCompile "junit:junit:$junitVersion" testCompile "org.hamcrest:hamcrest-library:$hamcrestVersion" - testRuntime "org.slf4j:slf4j-simple:$slf4jVersion" } test {