starting work on systemd-journal-jna

This commit is contained in:
Jörg Prante 2020-11-15 22:28:56 +01:00
parent e6eec6d886
commit 7c7303ed96
5 changed files with 36 additions and 20 deletions

View file

@ -1,5 +1,5 @@
def junitVersion = project.hasProperty('junit.version')?project.property('junit.version'):'5.6.2' def junitVersion = project.hasProperty('junit.version')?project.property('junit.version'):'5.7.0'
def hamcrestVersion = project.hasProperty('hamcrest.version')?project.property('hamcrest.version'):'2.2' def hamcrestVersion = project.hasProperty('hamcrest.version')?project.property('hamcrest.version'):'2.2'
dependencies { dependencies {
@ -14,6 +14,7 @@ test {
systemProperty 'jna.debug_load', 'true' systemProperty 'jna.debug_load', 'true'
failFast = true failFast = true
testLogging { testLogging {
showStandardStreams = true
events 'STARTED', 'PASSED', 'FAILED', 'SKIPPED' events 'STARTED', 'PASSED', 'FAILED', 'SKIPPED'
} }
afterSuite { desc, result -> afterSuite { desc, result ->

View file

@ -3,4 +3,5 @@ package org.xbib.systemd.journal;
import com.sun.jna.Structure; import com.sun.jna.Structure;
public class SdJournal extends Structure { public class SdJournal extends Structure {
public int toplevel_fd;
} }

View file

@ -46,39 +46,47 @@ public class SystemdJournalConsumer implements Runnable {
private void loop() throws IOException { private void loop() throws IOException {
SystemdLibraryAPI api = SystemdLibraryAPI.getInstance(); SystemdLibraryAPI api = SystemdLibraryAPI.getInstance();
SdJournal sdJournal = new SdJournal() { SdJournal sdJournal = new SdJournal();
@Override logger.log(Level.INFO, "opening");
public int hashCode() {
return super.hashCode();
}
};
int rc = api.sd_journal_open(sdJournal, SD_JOURNAL_LOCAL_ONLY); int rc = api.sd_journal_open(sdJournal, SD_JOURNAL_LOCAL_ONLY);
if (rc < 0) { logger.log(Level.INFO, "open: " + rc);
if (rc != 0) {
logger.log(Level.WARNING, "error opening journal for read: " + rc); logger.log(Level.WARNING, "error opening journal for read: " + rc);
return;
} }
if (match != null) { if (match != null) {
rc = api.sd_journal_add_match(sdJournal, match, match.length()); rc = api.sd_journal_add_match(sdJournal, match, match.length());
if (rc < 0) { logger.log(Level.INFO, "add_match: " + rc);
if (rc != 0) {
logger.log(Level.WARNING, "error in add_match: " + rc); logger.log(Level.WARNING, "error in add_match: " + rc);
return;
} }
} }
rc = api.sd_journal_get_fd(sdJournal); rc = api.sd_journal_get_fd(sdJournal);
logger.log(Level.INFO, "get_fd: " + rc);
rc = api.sd_journal_seek_tail(sdJournal); rc = api.sd_journal_seek_tail(sdJournal);
logger.log(Level.INFO, "seek_tail: " + rc);
rc = api.sd_journal_previous(sdJournal); rc = api.sd_journal_previous(sdJournal);
logger.log(Level.INFO, "previous: " + rc);
rc = api.sd_journal_next(sdJournal); rc = api.sd_journal_next(sdJournal);
logger.log(Level.INFO, "next: " + rc);
String[] strings = new String[1]; String[] strings = new String[1];
StringArray cursor = new StringArray(strings); StringArray cursor = new StringArray(strings);
api.sd_journal_get_cursor(sdJournal, cursor); rc = api.sd_journal_get_cursor(sdJournal, cursor);
logger.log(Level.INFO, "get_cursor: " + rc);
while (true) { while (true) {
do { do {
rc = api.sd_journal_wait(sdJournal, -1); rc = api.sd_journal_wait(sdJournal, -1);
logger.log(Level.INFO, "wait: " + rc);
} while (rc == 0); // NOP } while (rc == 0); // NOP
while (api.sd_journal_next(sdJournal) > 0) { while ((rc = api.sd_journal_next(sdJournal)) > 0) {
logger.log(Level.INFO, "next: " + rc);
if (field != null) { if (field != null) {
Pointer dataPointer = new Memory(Native.POINTER_SIZE); Pointer dataPointer = new Memory(Native.POINTER_SIZE);
Pointer sizePointer = new Memory(Native.POINTER_SIZE); Pointer sizePointer = new Memory(Native.POINTER_SIZE);
rc = api.sd_journal_get_data(sdJournal, field, dataPointer, sizePointer); rc = api.sd_journal_get_data(sdJournal, field, dataPointer, sizePointer);
if (rc < 0) { logger.log(Level.INFO, "get_data: " + rc);
if (rc != 0) {
throw new IOException("error in get_data: " + rc); throw new IOException("error in get_data: " + rc);
} }
int size = sizePointer.getInt(0); int size = sizePointer.getInt(0);
@ -90,21 +98,22 @@ public class SystemdJournalConsumer implements Runnable {
} else { } else {
String[] strings2 = new String[1]; String[] strings2 = new String[1];
StringArray nextCursor = new StringArray(strings2); StringArray nextCursor = new StringArray(strings2);
api.sd_journal_get_cursor(sdJournal, nextCursor); rc = api.sd_journal_get_cursor(sdJournal, nextCursor);
logger.log(Level.INFO, "get_cursor: " + rc);
if (!cursor.getString(0).equals(nextCursor.getString(0))) { if (!cursor.getString(0).equals(nextCursor.getString(0))) {
cursor = nextCursor; cursor = nextCursor;
Pointer dataPointer = new Memory(Native.POINTER_SIZE); Pointer dataPointer = new Memory(Native.POINTER_SIZE);
Pointer sizePointer = new Memory(Native.POINTER_SIZE); Pointer sizePointer = new Memory(Native.POINTER_SIZE);
List<String> list = new ArrayList<>(); List<String> list = new ArrayList<>();
while (api.sd_journal_enumerate_data(sdJournal, dataPointer, sizePointer) > 0) { while ((rc = api.sd_journal_enumerate_data(sdJournal, dataPointer, sizePointer)) > 0) {
//Pointer<Byte> data = dataPointer.as(Byte.class); logger.log(Level.INFO, "enumerate_data: " + rc);
//String line = data.getPointer(Byte.class).getCString();
int size = sizePointer.getInt(0); int size = sizePointer.getInt(0);
byte[] b = dataPointer.getByteArray(0, size); byte[] b = dataPointer.getByteArray(0, size);
String s = new String(b, StandardCharsets.UTF_8); String s = new String(b, StandardCharsets.UTF_8);
list.add(s); list.add(s);
} }
rc = api.sd_journal_restart_data(sdJournal); rc = api.sd_journal_restart_data(sdJournal);
logger.log(Level.INFO, "restart_data: " + rc);
if (listener != null) { if (listener != null) {
listener.handleEntry(makeEntry(list)); listener.handleEntry(makeEntry(list));
} }

View file

@ -20,15 +20,12 @@ public class SystemdJournalReaderTest {
private static final Logger logger = Logger.getLogger(SystemdJournalReaderTest.class.getName()); private static final Logger logger = Logger.getLogger(SystemdJournalReaderTest.class.getName());
@Mock
private SystemdLibraryAPI api;
@Test @Test
void testConsumer() throws InterruptedException { void testConsumer() throws InterruptedException {
SystemdJournalConsumer consumer = new SystemdJournalConsumer(null, SystemdJournalConsumer consumer = new SystemdJournalConsumer(null,
entry -> logger.log(Level.INFO, entry.toString())); entry -> logger.log(Level.INFO, entry.toString()));
Executors.newSingleThreadExecutor().submit(consumer); Executors.newSingleThreadExecutor().submit(consumer);
// consuming for some seconds // consuming for some seconds
Thread.sleep(10000L); Thread.sleep(60000L);
} }
} }

View file

@ -0,0 +1,8 @@
handlers=java.util.logging.FileHandler, java.util.logging.ConsoleHandler
.level=ALL
java.util.logging.SimpleFormatter.format=%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS.%1$tL %4$-7s [%3$s] %5$s %6$s%n
java.util.logging.ConsoleHandler.level=ALL
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter
java.util.logging.FileHandler.level=ALL
java.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter
java.util.logging.FileHandler.pattern=build/test.log