Skip to content

Commit 05bdd8c

Browse files
committed
Merge pull request #3 from keszybz/stream
sd_journal_stream_fd() wrapper
2 parents 6f36264 + a2df218 commit 05bdd8c

File tree

2 files changed

+63
-1
lines changed

2 files changed

+63
-1
lines changed

journald/__init__.py

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
from ._journald import sendv
21
import traceback as _traceback
2+
import os as _os
3+
from syslog import (LOG_EMERG, LOG_ALERT, LOG_CRIT, LOG_ERR,
4+
LOG_WARNING, LOG_NOTICE, LOG_INFO, LOG_DEBUG)
5+
from ._journald import sendv, stream_fd
36

47
def _make_line(field, value):
58
if isinstance(value, bytes):
@@ -52,3 +55,39 @@ def send(MESSAGE, MESSAGE_ID=None,
5255

5356
args.extend(_make_line(key, val) for key, val in kwargs.items())
5457
return sendv(*args)
58+
59+
def stream(identifier, priority=LOG_DEBUG, level_prefix=False):
60+
r"""Return a file object wrapping a stream to journald.
61+
62+
Log messages written to this file as simple newline sepearted
63+
text strings are written to the journal.
64+
65+
The file will be line buffered, so messages are actually sent
66+
after a newline character is written.
67+
68+
>>> stream = journald.stream('myapp')
69+
>>> stream
70+
<open file '<fdopen>', mode 'w' at 0x...>
71+
>>> stream.write('message...\n')
72+
73+
will produce the following message in the journal:
74+
75+
PRIORITY=7
76+
SYSLOG_IDENTIFIER=myapp
77+
MESSAGE=message...
78+
79+
Using the interface with print might be more convinient:
80+
81+
>>> from __future__ import print_function
82+
>>> print('message...', file=stream)
83+
84+
priority is the syslog priority, one of LOG_EMERG, LOG_ALERT,
85+
LOG_CRIT, LOG_ERR, LOG_WARNING, LOG_NOTICE, LOG_INFO, LOG_DEBUG.
86+
87+
level_prefix is a boolean. If true, kernel-style log priority
88+
level prefixes (such as '<1>') are interpreted. See sd-daemon(3)
89+
for more information.
90+
"""
91+
92+
fd = stream_fd(identifier, priority, level_prefix)
93+
return _os.fdopen(fd, 'w', 1)

journald/_journald.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,31 @@ journald_sendv(PyObject *self, PyObject *args) {
7979
return ret;
8080
}
8181

82+
PyDoc_STRVAR(journald_stream_fd__doc__,
83+
"stream_fd(identifier, priority, level_prefix) -> fd\n\n"
84+
"Open a stream to journald by calling sd_journal_stream_fd(3)."
85+
);
86+
87+
static PyObject*
88+
journald_stream_fd(PyObject *self, PyObject *args) {
89+
const char* identifier;
90+
int priority, level_prefix;
91+
int fd;
92+
if (!PyArg_ParseTuple(args, "sii:stream_fd",
93+
&identifier, &priority, &level_prefix))
94+
return NULL;
95+
96+
fd = sd_journal_stream_fd(identifier, priority, level_prefix);
97+
if (fd < 0)
98+
return PyErr_SetFromErrno(PyExc_IOError);
99+
100+
return PyLong_FromLong(fd);
101+
}
102+
82103
static PyMethodDef methods[] = {
83104
{"sendv", journald_sendv, METH_VARARGS, journald_sendv__doc__},
105+
{"stream_fd", journald_stream_fd, METH_VARARGS,
106+
journald_stream_fd__doc__},
84107
{NULL, NULL, 0, NULL} /* Sentinel */
85108
};
86109

0 commit comments

Comments
 (0)