Fixes Allow extensions to log themselves.
MODIFY
asciidoctorj-api/src/main/java/org/asciidoctor/log/LogRecord.java
asciidoctorj-api/src/main/java/org/asciidoctor/log/LogRecord.java
@@ -6,13 +6,24 @@ public class LogRecord {
private final Severity severity;
- private final Cursor cursor;
+ private Cursor cursor;
private final String message;
- private final String sourceFileName;
+ private String sourceFileName;
- private final String sourceMethodName;
+ private String sourceMethodName;
+
+ public LogRecord(Severity severity, String message) {
+ this.severity = severity;
+ this.message = message;
+ }
+
+ public LogRecord(Severity severity, Cursor cursor, String message) {
+ this.severity = severity;
+ this.cursor = cursor;
+ this.message = message;
+ }
public LogRecord(Severity severity, Cursor cursor, String message, String sourceFileName, String sourceMethodName) {
this.severity = severity;

MODIFY
asciidoctorj-core/src/main/java/org/asciidoctor/extension/JRubyProcessor.java
asciidoctorj-core/src/main/java/org/asciidoctor/extension/JRubyProcessor.java
@@ -26,6 +26,7 @@ import org.asciidoctor.internal.JRubyRuntimeContext;
import org.asciidoctor.internal.RubyHashUtil;
import org.asciidoctor.internal.RubyObjectWrapper;
import org.asciidoctor.internal.RubyUtils;
+import org.asciidoctor.log.LogRecord;
import org.jruby.Ruby;
import org.jruby.RubyArray;
import org.jruby.RubyFixnum;
@@ -469,4 +470,9 @@ public class JRubyProcessor implements Processor {
}
throw new IllegalArgumentException("Cannot unwrap to " + clazz);
}
+
+ @Override
+ public void log(LogRecord logRecord) {
+ unwrap(JRubyAsciidoctor.class).log(logRecord);
+ }
}

MODIFY
asciidoctorj-core/src/main/java/org/asciidoctor/log/internal/JavaLogger.java
asciidoctorj-core/src/main/java/org/asciidoctor/log/internal/JavaLogger.java
@@ -103,13 +103,21 @@ public class JavaLogger extends RubyObject {
private Severity mapRubyLogLevel(IRubyObject arg) {
final int rubyId = arg.convertToInteger().getIntValue();
- for (Severity severity: Severity.values()) {
- if (severity.getRubyId() == rubyId) {
- return severity;
- }
- }
+ switch (rubyId) {
+ case 0:
+ return Severity.DEBUG;
+ case 1:
+ return Severity.INFO;
+ case 2:
+ return Severity.WARN;
+ case 3:
+ return Severity.ERROR;
+ case 4:
+ return Severity.FATAL;
+ default:
return Severity.UNKNOWN;
}
+ }
private String formatMessage(final IRubyObject msg) {
if (getRuntime().getString().equals(msg.getType())) {

MODIFY
asciidoctorj-core/src/test/java/org/asciidoctor/WhenAsciidoctorLogsToConsole.java
asciidoctorj-core/src/test/java/org/asciidoctor/WhenAsciidoctorLogsToConsole.java
@@ -2,9 +2,14 @@ package org.asciidoctor;
import org.asciidoctor.arquillian.api.Unshared;
import org.asciidoctor.ast.Cursor;
+import org.asciidoctor.ast.StructuralNode;
+import org.asciidoctor.extension.BlockProcessor;
+import org.asciidoctor.extension.Name;
+import org.asciidoctor.extension.Reader;
import org.asciidoctor.internal.JRubyAsciidoctor;
import org.asciidoctor.log.LogHandler;
import org.asciidoctor.log.LogRecord;
+import org.asciidoctor.log.Severity;
import org.asciidoctor.log.TestLogHandlerService;
import org.asciidoctor.util.ClasspathResources;
import org.hamcrest.Matchers;
@@ -21,8 +26,10 @@ import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
+import java.util.Map;
import java.util.logging.LogManager;
import java.util.logging.Logger;
+import java.util.stream.Collectors;
import static org.asciidoctor.OptionsBuilder.options;
import static org.hamcrest.Matchers.both;
@@ -92,7 +99,6 @@ public class WhenAsciidoctorLogsToConsole {
final List<LogRecord> logRecords = new ArrayList<>();
-
final LogHandler logHandler = new LogHandler() {
@Override
public void log(LogRecord logRecord) {
@@ -285,5 +291,46 @@ public class WhenAsciidoctorLogsToConsole {
assertThat(logRecord.getCursor().getDir(), not(Matchers.nullValue()));
}
}
+
+ @Name("big")
+ public static class LoggingProcessor extends BlockProcessor {
+
+ @Override
+ public Object process(StructuralNode parent, Reader reader, Map<String, Object> attributes) {
+ log(new LogRecord(Severity.INFO, parent.getSourceLocation(), "Hello Log"));
+ final List<String> strings = reader.readLines().stream()
+ .map(String::toUpperCase)
+ .collect(Collectors.toList());
+
+ return createBlock(parent, "paragraph", strings);
+ }
+ }
+
+ @Test
+ public void a_extension_should_be_able_to_log() throws Exception {
+
+ final List<LogRecord> logRecords = new ArrayList<>();
+
+ final LogHandler logHandler = new LogHandler() {
+ @Override
+ public void log(LogRecord logRecord) {
+ logRecords.add(logRecord);
+ }
+ };
+ asciidoctor.registerLogHandler(logHandler);
+ asciidoctor.javaExtensionRegistry().block(LoggingProcessor.class);
+
+ String renderContent = asciidoctor.convert("= Test\n\n== Something different\n\n[big]\nHello World",
+ options().option("sourcemap", "true").asMap());
+
+ assertEquals(1, logRecords.size());
+ assertThat(logRecords.get(0).getMessage(), is("Hello Log"));
+ final Cursor cursor = logRecords.get(0).getCursor();
+ assertThat(cursor.getLineNumber(), is(3));
+
+ assertThat(renderContent, containsString("HELLO WORLD"));
+ }
+
+
}

Fixes #737. Allow extensions to log themselves.