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>
27 PyDoc_STRVAR(journal_sendv__doc__,
28 "sendv('FIELD=value', 'FIELD=value', ...) -> None\n\n"
29 "Send an entry to the journal."
32 static PyObject *journal_sendv(PyObject *self, PyObject *args) {
33 struct iovec *iov = NULL;
39 argc = PyTuple_Size(args);
41 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);
81 PyErr_SetFromErrno(PyExc_IOError);
83 PyErr_SetString(PyExc_ValueError, "invalid message format");
87 /* End with success. */
92 for (i = 0; i < argc; ++i)
93 Py_XDECREF(encoded[i]);
98 /* Free the iovector. The actual strings
99 are already managed by Python. */
105 PyDoc_STRVAR(journal_stream_fd__doc__,
106 "stream_fd(identifier, priority, level_prefix) -> fd\n\n"
107 "Open a stream to journal by calling sd_journal_stream_fd(3)."
110 static PyObject* journal_stream_fd(PyObject *self, PyObject *args) {
111 const char* identifier;
112 int priority, level_prefix;
115 if (!PyArg_ParseTuple(args, "sii:stream_fd",
116 &identifier, &priority, &level_prefix))
119 fd = sd_journal_stream_fd(identifier, priority, level_prefix);
121 return PyErr_SetFromErrno(PyExc_IOError);
123 return PyLong_FromLong(fd);
126 static PyMethodDef methods[] = {
127 { "sendv", journal_sendv, METH_VARARGS, journal_sendv__doc__ },
128 { "stream_fd", journal_stream_fd, METH_VARARGS, journal_stream_fd__doc__ },
129 { NULL, NULL, 0, NULL } /* Sentinel */
132 #if PY_MAJOR_VERSION < 3
134 PyMODINIT_FUNC init_journal(void) {
135 (void) Py_InitModule("_journal", methods);
140 static struct PyModuleDef module = {
141 PyModuleDef_HEAD_INIT,
142 "_journal", /* name of module */
143 NULL, /* module documentation, may be NULL */
144 0, /* size of per-interpreter state of the module */
148 PyMODINIT_FUNC PyInit__journal(void) {
149 return PyModule_Create(&module);