summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Strauss <david@davidstrauss.net>2012-09-03 15:46:58 -0700
committerDavid Strauss <david@davidstrauss.net>2012-09-03 15:46:58 -0700
commit05bdd8ce12357776e2f6794d59f4f49e6d85d6b8 (patch)
tree5daaee74c3ee79be3183d9318ff40c109f3132aa
parent6f3626418964aa2f6dcc91ecebebd369beed1606 (diff)
parenta2df218d26e5663564207cf3938b5e9714e6202d (diff)
downloadpython-systemd-05bdd8ce12357776e2f6794d59f4f49e6d85d6b8.tar.gz
Merge pull request #3 from keszybz/stream
sd_journal_stream_fd() wrapper
-rw-r--r--journald/__init__.py41
-rw-r--r--journald/_journald.c23
2 files changed, 63 insertions, 1 deletions
diff --git a/journald/__init__.py b/journald/__init__.py
index bcf682b..f918b23 100644
--- a/journald/__init__.py
+++ b/journald/__init__.py
@@ -1,5 +1,8 @@
-from ._journald import sendv
import traceback as _traceback
+import os as _os
+from syslog import (LOG_EMERG, LOG_ALERT, LOG_CRIT, LOG_ERR,
+ LOG_WARNING, LOG_NOTICE, LOG_INFO, LOG_DEBUG)
+from ._journald import sendv, stream_fd
def _make_line(field, value):
if isinstance(value, bytes):
@@ -52,3 +55,39 @@ def send(MESSAGE, MESSAGE_ID=None,
args.extend(_make_line(key, val) for key, val in kwargs.items())
return sendv(*args)
+
+def stream(identifier, priority=LOG_DEBUG, level_prefix=False):
+ r"""Return a file object wrapping a stream to journald.
+
+ Log messages written to this file as simple newline sepearted
+ text strings are written to the journal.
+
+ The file will be line buffered, so messages are actually sent
+ after a newline character is written.
+
+ >>> stream = journald.stream('myapp')
+ >>> stream
+ <open file '<fdopen>', mode 'w' at 0x...>
+ >>> stream.write('message...\n')
+
+ will produce the following message in the journal:
+
+ PRIORITY=7
+ SYSLOG_IDENTIFIER=myapp
+ MESSAGE=message...
+
+ Using the interface with print might be more convinient:
+
+ >>> from __future__ import print_function
+ >>> print('message...', file=stream)
+
+ priority is the syslog priority, one of LOG_EMERG, LOG_ALERT,
+ LOG_CRIT, LOG_ERR, LOG_WARNING, LOG_NOTICE, LOG_INFO, LOG_DEBUG.
+
+ level_prefix is a boolean. If true, kernel-style log priority
+ level prefixes (such as '<1>') are interpreted. See sd-daemon(3)
+ for more information.
+ """
+
+ fd = stream_fd(identifier, priority, level_prefix)
+ return _os.fdopen(fd, 'w', 1)
diff --git a/journald/_journald.c b/journald/_journald.c
index 82b3b61..b713c73 100644
--- a/journald/_journald.c
+++ b/journald/_journald.c
@@ -79,8 +79,31 @@ out1:
return ret;
}
+PyDoc_STRVAR(journald_stream_fd__doc__,
+ "stream_fd(identifier, priority, level_prefix) -> fd\n\n"
+ "Open a stream to journald by calling sd_journal_stream_fd(3)."
+ );
+
+static PyObject*
+journald_stream_fd(PyObject *self, PyObject *args) {
+ const char* identifier;
+ int priority, level_prefix;
+ int fd;
+ if (!PyArg_ParseTuple(args, "sii:stream_fd",
+ &identifier, &priority, &level_prefix))
+ return NULL;
+
+ fd = sd_journal_stream_fd(identifier, priority, level_prefix);
+ if (fd < 0)
+ return PyErr_SetFromErrno(PyExc_IOError);
+
+ return PyLong_FromLong(fd);
+}
+
static PyMethodDef methods[] = {
{"sendv", journald_sendv, METH_VARARGS, journald_sendv__doc__},
+ {"stream_fd", journald_stream_fd, METH_VARARGS,
+ journald_stream_fd__doc__},
{NULL, NULL, 0, NULL} /* Sentinel */
};