From: Zbigniew Jędrzejewski-Szmek Date: Thu, 7 Mar 2013 05:35:28 +0000 (-0500) Subject: systemd-python: export sd_j_get_fd, sd_j_reliable_fd, sd_j_close X-Git-Tag: v198~44 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=commitdiff_plain;h=f2e82cd5add31d704b39a91d570ab6649653fa49;ds=sidebyside 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. --- diff --git a/src/python-systemd/_reader.c b/src/python-systemd/_reader.c index e5733f0bc..c435dadec 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 faa270746..9dc495ffd 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