summaryrefslogtreecommitdiff
path: root/src/python-systemd
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2013-03-07 00:35:28 -0500
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2013-03-07 00:45:56 -0500
commitf2e82cd5add31d704b39a91d570ab6649653fa49 (patch)
tree27490afd9e15cf4cdca37ec7f26d448f201a4aa3 /src/python-systemd
parent5c1c14b3a021fbf91f31018256b0c241ea1fe3f0 (diff)
systemd-python: export sd_j_get_fd, sd_j_reliable_fd, sd_j_close
sd_journal_get_fd(j) is called j.fileno(), for compatiblity with Python conventions for file-like objects. More importantly, those new .seek_head() and .seek_tail() do not call .get_next(). This is better, if one wants to skip before retrieving an entry.
Diffstat (limited to 'src/python-systemd')
-rw-r--r--src/python-systemd/_reader.c44
-rw-r--r--src/python-systemd/docs/journal.rst16
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