1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2012 David Strauss
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
24 #define SD_JOURNAL_SUPPRESS_LOCATION
25 #include <systemd/sd-journal.h>
29 PyDoc_STRVAR(journal_sendv__doc__,
30 "sendv('FIELD=value', 'FIELD=value', ...) -> None\n\n"
31 "Send an entry to the journal."
35 journal_sendv(PyObject *self, PyObject *args) {
36 struct iovec *iov = NULL;
37 int argc = PyTuple_Size(args);
41 PyObject **encoded = calloc(argc, sizeof(PyObject*));
43 ret = PyErr_NoMemory();
47 // Allocate sufficient iovector space for the arguments.
48 iov = malloc(argc * sizeof(struct iovec));
50 ret = PyErr_NoMemory();
54 // Iterate through the Python arguments and fill the iovector.
55 for (i = 0; i < argc; ++i) {
56 PyObject *item = PyTuple_GetItem(args, i);
60 if (PyUnicode_Check(item)) {
61 encoded[i] = PyUnicode_AsEncodedString(item, "utf-8", "strict");
62 if (encoded[i] == NULL)
66 if (PyBytes_AsStringAndSize(item, &stritem, &length))
69 iov[i].iov_base = stritem;
70 iov[i].iov_len = length;
73 // Clear errno, because sd_journal_sendv will not set it by
74 // itself, unless an error occurs in one of the system calls.
77 // Send the iovector to the journal.
78 r = sd_journal_sendv(iov, argc);
82 PyErr_SetFromErrno(PyExc_IOError);
84 PyErr_SetString(PyExc_ValueError, "invalid message format");
93 for (i = 0; i < argc; ++i)
94 Py_XDECREF(encoded[i]);
99 // Free the iovector. The actual strings
100 // are already managed by Python.
106 PyDoc_STRVAR(journal_stream_fd__doc__,
107 "stream_fd(identifier, priority, level_prefix) -> fd\n\n"
108 "Open a stream to journal by calling sd_journal_stream_fd(3)."
112 journal_stream_fd(PyObject *self, PyObject *args) {
113 const char* identifier;
114 int priority, level_prefix;
116 if (!PyArg_ParseTuple(args, "sii:stream_fd",
117 &identifier, &priority, &level_prefix))
120 fd = sd_journal_stream_fd(identifier, priority, level_prefix);
122 return PyErr_SetFromErrno(PyExc_IOError);
124 return PyLong_FromLong(fd);
127 static PyMethodDef methods[] = {
128 {"sendv", journal_sendv, METH_VARARGS, journal_sendv__doc__},
129 {"stream_fd", journal_stream_fd, METH_VARARGS,
130 journal_stream_fd__doc__},
131 {NULL, NULL, 0, NULL} /* Sentinel */
134 #if PY_MAJOR_VERSION < 3
139 (void) Py_InitModule("_journal", methods);
144 static struct PyModuleDef module = {
145 PyModuleDef_HEAD_INIT,
146 "_journal", /* name of module */
147 NULL, /* module documentation, may be NULL */
148 0, /* size of per-interpreter state of the module */
153 PyInit__journal(void)
155 return PyModule_Create(&module);