chiark / gitweb /
c305b77ce298aefb276ebbf9fecd6144bfdcf087
[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
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 #define SD_JOURNAL_SUPPRESS_LOCATION
25 #include <systemd/sd-journal.h>
26
27 PyDoc_STRVAR(journal_sendv__doc__,
28              "sendv('FIELD=value', 'FIELD=value', ...) -> None\n\n"
29              "Send an entry to the journal."
30 );
31
32 static PyObject *journal_sendv(PyObject *self, PyObject *args) {
33         struct iovec *iov = NULL;
34         int argc;
35         int i, r;
36         PyObject *ret = NULL;
37         PyObject **encoded;
38
39         argc = PyTuple_Size(args);
40
41         encoded = calloc(argc, sizeof(PyObject*));
42         if (!encoded) {
43                 ret = PyErr_NoMemory();
44                 goto out1;
45         }
46
47         /* Allocate sufficient iovector space for the arguments. */
48         iov = malloc(argc * sizeof(struct iovec));
49         if (!iov) {
50                 ret = PyErr_NoMemory();
51                 goto out;
52         }
53
54         /* Iterate through the Python arguments and fill the iovector. */
55         for (i = 0; i < argc; ++i) {
56                 PyObject *item = PyTuple_GetItem(args, i);
57                 char *stritem;
58                 Py_ssize_t length;
59
60                 if (PyUnicode_Check(item)) {
61                         encoded[i] = PyUnicode_AsEncodedString(item, "utf-8", "strict");
62                         if (encoded[i] == NULL)
63                                 goto out;
64                         item = encoded[i];
65                 }
66                 if (PyBytes_AsStringAndSize(item, &stritem, &length))
67                         goto out;
68
69                 iov[i].iov_base = stritem;
70                 iov[i].iov_len = length;
71         }
72
73         /* Clear errno, because sd_journal_sendv will not set it by
74            itself, unless an error occurs in one of the system calls. */
75         errno = 0;
76
77         /* Send the iovector to the journal. */
78         r = sd_journal_sendv(iov, argc);
79         if (r) {
80                 if (errno)
81                         PyErr_SetFromErrno(PyExc_IOError);
82                 else
83                         PyErr_SetString(PyExc_ValueError, "invalid message format");
84                 goto out;
85         }
86
87         /* End with success. */
88         Py_INCREF(Py_None);
89         ret = Py_None;
90
91 out:
92         for (i = 0; i < argc; ++i)
93                 Py_XDECREF(encoded[i]);
94
95         free(encoded);
96
97 out1:
98         /* Free the iovector. The actual strings
99            are already managed by Python. */
100         free(iov);
101
102         return ret;
103 }
104
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)."
108 );
109
110 static PyObject* journal_stream_fd(PyObject *self, PyObject *args) {
111         const char* identifier;
112         int priority, level_prefix;
113         int fd;
114
115         if (!PyArg_ParseTuple(args, "sii:stream_fd",
116                               &identifier, &priority, &level_prefix))
117                 return NULL;
118
119         fd = sd_journal_stream_fd(identifier, priority, level_prefix);
120         if (fd < 0)
121                 return PyErr_SetFromErrno(PyExc_IOError);
122
123         return PyLong_FromLong(fd);
124 }
125
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 */
130 };
131
132 #if PY_MAJOR_VERSION < 3
133
134 PyMODINIT_FUNC init_journal(void) {
135         (void) Py_InitModule("_journal", methods);
136 }
137
138 #else
139
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 */
145         methods
146 };
147
148 PyMODINIT_FUNC PyInit__journal(void) {
149         return PyModule_Create(&module);
150 }
151
152 #endif