chiark / gitweb /
build-sys: add a makefile target to run all tests through valgrind
[elogind.git] / src / python-systemd / _journal.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2012 David Strauss <david@davidstrauss.net>
7
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.
12
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.
17
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/>.
20 ***/
21
22 #include <Python.h>
23
24 #include <alloca.h>
25
26 #define SD_JOURNAL_SUPPRESS_LOCATION
27 #include <systemd/sd-journal.h>
28
29 PyDoc_STRVAR(journal_sendv__doc__,
30              "sendv('FIELD=value', 'FIELD=value', ...) -> None\n\n"
31              "Send an entry to the journal."
32 );
33
34 static PyObject *journal_sendv(PyObject *self, PyObject *args) {
35         struct iovec *iov = NULL;
36         int argc;
37         int i, r;
38         PyObject *ret = NULL;
39         PyObject **encoded;
40
41         /* Allocate an array for the argument strings */
42         argc = PyTuple_Size(args);
43         encoded = alloca(argc * sizeof(PyObject*));
44         memset(encoded, 0, argc * sizeof(PyObject*));
45
46         /* Allocate sufficient iovector space for the arguments. */
47         iov = alloca(argc * sizeof(struct iovec));
48
49         /* Iterate through the Python arguments and fill the iovector. */
50         for (i = 0; i < argc; ++i) {
51                 PyObject *item = PyTuple_GetItem(args, i);
52                 char *stritem;
53                 Py_ssize_t length;
54
55                 if (PyUnicode_Check(item)) {
56                         encoded[i] = PyUnicode_AsEncodedString(item, "utf-8", "strict");
57                         if (encoded[i] == NULL)
58                                 goto out;
59                         item = encoded[i];
60                 }
61                 if (PyBytes_AsStringAndSize(item, &stritem, &length))
62                         goto out;
63
64                 iov[i].iov_base = stritem;
65                 iov[i].iov_len = length;
66         }
67
68         /* Send the iovector to the journal. */
69         r = sd_journal_sendv(iov, argc);
70         if (r < 0) {
71                 errno = -r;
72                 PyErr_SetFromErrno(PyExc_IOError);
73                 goto out;
74         }
75
76         /* End with success. */
77         Py_INCREF(Py_None);
78         ret = Py_None;
79
80 out:
81         for (i = 0; i < argc; ++i)
82                 Py_XDECREF(encoded[i]);
83
84         return ret;
85 }
86
87 PyDoc_STRVAR(journal_stream_fd__doc__,
88              "stream_fd(identifier, priority, level_prefix) -> fd\n\n"
89              "Open a stream to journal by calling sd_journal_stream_fd(3)."
90 );
91
92 static PyObject* journal_stream_fd(PyObject *self, PyObject *args) {
93         const char* identifier;
94         int priority, level_prefix;
95         int fd;
96
97         if (!PyArg_ParseTuple(args, "sii:stream_fd",
98                               &identifier, &priority, &level_prefix))
99                 return NULL;
100
101         fd = sd_journal_stream_fd(identifier, priority, level_prefix);
102         if (fd < 0) {
103                 errno = -fd;
104                 return PyErr_SetFromErrno(PyExc_IOError);
105         }
106
107         return PyLong_FromLong(fd);
108 }
109
110 static PyMethodDef methods[] = {
111         { "sendv",  journal_sendv, METH_VARARGS, journal_sendv__doc__ },
112         { "stream_fd", journal_stream_fd, METH_VARARGS, journal_stream_fd__doc__ },
113         { NULL, NULL, 0, NULL }        /* Sentinel */
114 };
115
116 #pragma GCC diagnostic push
117 #pragma GCC diagnostic ignored "-Wmissing-prototypes"
118
119 #if PY_MAJOR_VERSION < 3
120
121 PyMODINIT_FUNC init_journal(void) {
122         PyObject *m;
123
124         m = Py_InitModule("_journal", methods);
125         if (m == NULL)
126                 return;
127
128         PyModule_AddStringConstant(m, "__version__", PACKAGE_VERSION);
129 }
130
131 #else
132
133 static struct PyModuleDef module = {
134         PyModuleDef_HEAD_INIT,
135         "_journal", /* name of module */
136         NULL, /* module documentation, may be NULL */
137         -1, /* size of per-interpreter state of the module */
138         methods
139 };
140
141 PyMODINIT_FUNC PyInit__journal(void) {
142         PyObject *m;
143
144         m = PyModule_Create(&module);
145         if (m == NULL)
146                 return NULL;
147
148         if (PyModule_AddStringConstant(m, "__version__", PACKAGE_VERSION)) {
149                 Py_DECREF(m);
150                 return NULL;
151         }
152
153         return m;
154 }
155
156 #endif
157
158 #pragma GCC diagnostic pop