Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bindings for lou_registerLogCallback and lou_setLogLevel
- Loading branch information
Bert Frees
committed
Jun 12, 2014
1 parent
5629f9c
commit a30a1ad
Showing
5 changed files
with
110 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package org.liblouis; | ||
|
||
import com.sun.jna.Callback; | ||
|
||
public interface Logger extends Callback { | ||
|
||
public static abstract class Level { | ||
public static final int ALL = -2147483648; | ||
public static final int DEBUG = 10000; | ||
public static final int INFO = 20000; | ||
public static final int WARN = 30000; | ||
public static final int ERROR = 40000; | ||
public static final int FATAL = 50000; | ||
public static final int OFF = 2147483647; | ||
} | ||
|
||
public void invoke(int level, String message); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package org.liblouis; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.File; | ||
import java.io.FilenameFilter; | ||
import java.io.PrintStream; | ||
import java.util.Collection; | ||
|
||
import static org.apache.commons.io.filefilter.FileFilterUtils.asFileFilter; | ||
import static org.apache.commons.io.filefilter.FileFilterUtils.trueFileFilter; | ||
import org.apache.commons.io.FileUtils; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import static org.liblouis.Logger.Level.DEBUG; | ||
import static org.liblouis.Logger.Level.INFO; | ||
import static org.liblouis.Logger.Level.WARN; | ||
import static org.liblouis.Logger.Level.ERROR; | ||
import static org.liblouis.Logger.Level.FATAL; | ||
|
||
public class LoggerTest { | ||
|
||
@Test | ||
public void testLogger() { | ||
logger.reset(); | ||
try { new Translator("unexisting_file"); } | ||
catch (CompilationException e) {} | ||
assertEquals("[ERROR] Cannot resolve table 'unexisting_file'" + "\n" + | ||
"[ERROR] 1 errors found." + "\n" + | ||
"[ERROR] unexisting_file could not be found" + "\n", | ||
logger.toString()); | ||
logger.reset(); | ||
Louis.getLibrary().lou_setLogLevel(FATAL); | ||
try { new Translator("unexisting_file"); } | ||
catch (CompilationException e) {} | ||
assertEquals("", logger.toString()); | ||
} | ||
|
||
private ByteArrayLogger logger; | ||
|
||
@Before | ||
@SuppressWarnings("unchecked") | ||
public void initialize() { | ||
final File testRootDir = new File(this.getClass().getResource("/").getPath()); | ||
Louis.setLibraryPath(((Collection<File>)FileUtils.listFiles( | ||
new File(testRootDir, "../dependency"), | ||
asFileFilter(new FilenameFilter() { | ||
public boolean accept(File dir, String fileName) { | ||
return dir.getName().equals("shared") && fileName.startsWith("liblouis"); }}), | ||
trueFileFilter())).iterator().next()); | ||
logger = new ByteArrayLogger() { | ||
public String format(int level, String message) { | ||
switch (level) { | ||
case DEBUG: return "[DEBUG] " + message; | ||
case INFO: return "[INFO] " + message; | ||
case WARN: return "[WARN] " + message; | ||
case ERROR: return "[ERROR] " + message; | ||
case FATAL: return "[FATAL] " + message; } | ||
return null; | ||
} | ||
}; | ||
Louis.getLibrary().lou_registerLogCallback(logger); | ||
} | ||
|
||
private abstract class ByteArrayLogger implements Logger { | ||
private ByteArrayOutputStream stream = new ByteArrayOutputStream(); | ||
private PrintStream printStream = new PrintStream(stream); | ||
public abstract String format(int level, String message); | ||
public void invoke(int level, String message) { | ||
String formattedMessage = format(level, message); | ||
if (formattedMessage != null) | ||
printStream.println(formattedMessage); | ||
} | ||
public void reset() { | ||
stream.reset(); | ||
} | ||
public String toString() { | ||
return stream.toString(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters