diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/python-systemd/_reader.c | 44 | ||||
-rw-r--r-- | src/python-systemd/docs/journal.rst | 16 |
2 files changed, 60 insertions, 0 deletions
diff --git a/src/python-systemd/_reader.c b/src/python-systemd/_reader.c index e5733f0bca..c435dadecf 100644 --- a/src/python-systemd/_reader.c +++ b/src/python-systemd/_reader.c @@ -124,6 +124,47 @@ static int Reader_init(Reader *self, PyObject *args, PyObject *keywds) return set_error(r, path, "Invalid flags or path"); } +PyDoc_STRVAR(Reader_fileno__doc__, + "fileno() -> int\n\n" + "Get a file descriptor to poll for changes in the journal.\n" + "This method invokes sd_journal_get_fd().\n" + "See man:sd_journal_get_fd(3)."); +static PyObject* Reader_fileno(Reader *self, PyObject *args) +{ + int r; + r = sd_journal_get_fd(self->j); + set_error(r, NULL, NULL); + if (r < 0) + return NULL; + return long_FromLong(r); +} + +PyDoc_STRVAR(Reader_reliable_fd__doc__, + "reliable_fd() -> bool\n\n" + "Returns True iff the journal can be polled reliably.\n" + "This method invokes sd_journal_reliable_fd().\n" + "See man:sd_journal_reliable_fd(3)."); +static PyObject* Reader_reliable_fd(Reader *self, PyObject *args) +{ + int r; + r = sd_journal_reliable_fd(self->j); + set_error(r, NULL, NULL); + if (r < 0) + return NULL; + return PyBool_FromLong(r); +} + +PyDoc_STRVAR(Reader_close__doc__, + "reliable_fd() -> None\n\n" + "Free resources allocated by this Reader object.\n" + "This method invokes sd_journal_close().\n" + "See man:sd_journal_close(3)."); +static PyObject* Reader_close(Reader *self, PyObject *args) +{ + sd_journal_close(self->j); + Py_RETURN_NONE; +} + PyDoc_STRVAR(Reader_get_next__doc__, "get_next([skip]) -> dict\n\n" "Return dictionary of the next log entry. Optional skip value will\n" @@ -613,6 +654,9 @@ static PyGetSetDef Reader_getseters[] = { }; static PyMethodDef Reader_methods[] = { + {"fileno", (PyCFunction) Reader_fileno, METH_NOARGS, Reader_fileno__doc__}, + {"reliable_fd", (PyCFunction) Reader_reliable_fd, METH_NOARGS, Reader_reliable_fd__doc__}, + {"close", (PyCFunction) Reader_close, METH_NOARGS, Reader_close__doc__}, {"get_next", (PyCFunction) Reader_get_next, METH_VARARGS, Reader_get_next__doc__}, {"get_previous", (PyCFunction) Reader_get_previous, METH_VARARGS, Reader_get_previous__doc__}, {"add_match", (PyCFunction) Reader_add_match, METH_VARARGS|METH_KEYWORDS, Reader_add_match__doc__}, diff --git a/src/python-systemd/docs/journal.rst b/src/python-systemd/docs/journal.rst index faa270746d..9dc495ffdb 100644 --- a/src/python-systemd/docs/journal.rst +++ b/src/python-systemd/docs/journal.rst @@ -27,6 +27,22 @@ Accessing the Journal .. autoattribute:: systemd.journal.DEFAULT_CONVERTERS +Example: polling for journal events +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +This example shows that journal events can be waited for (using +e.g. `poll`). This makes it easy to integrate Reader in an external +event loop: + + >>> import select + >>> from systemd import journal + >>> j = journal.Reader() + >>> j.seek_tail() + >>> p = select.poll() + >>> p.register(j, select.POLLIN) + >>> p.poll() + [(3, 1)] + >>> j.get_next() Journal access types |