Skip to content

Commit

Permalink
resolves #210 by redirecting asciidoctor console output to java logge…
Browse files Browse the repository at this point in the history
…r system.
  • Loading branch information
lordofthejars committed Dec 12, 2014
1 parent 8c4fe81 commit 5c1b27d
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 20 deletions.
1 change: 0 additions & 1 deletion asciidoctorj-core/build.gradle
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
9 changes: 9 additions & 0 deletions asciidoctorj-core/src/main/java/org/asciidoctor/SafeMode.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ private static String getOptions(Map<String, Object> 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(" ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -279,12 +297,10 @@ public String render(String content, Map<String, Object> 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();
Expand All @@ -311,9 +327,7 @@ public String renderFile(File filename, Map<String, Object> 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();

Expand Down Expand Up @@ -566,4 +580,54 @@ public Document loadFile(File file, Map<String, Object> 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();
}
};

}
Original file line number Diff line number Diff line change
@@ -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();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
= My Document

include::unexistingdoc.adoc[]
1 change: 0 additions & 1 deletion asciidoctorj-distribution/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
2 changes: 0 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -77,7 +76,6 @@ subprojects {
dependencies {
testCompile "junit:junit:$junitVersion"
testCompile "org.hamcrest:hamcrest-library:$hamcrestVersion"
testRuntime "org.slf4j:slf4j-simple:$slf4jVersion"
}

test {
Expand Down

1 comment on commit 5c1b27d

@mojavelinux
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Less dependencies. Yeah!

Please sign in to comment.