chiark / gitweb /
python: change license to LGPL 2.1
[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 #include "macro.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 *
35 journal_sendv(PyObject *self, PyObject *args) {
36     struct iovec *iov = NULL;
37     int argc = PyTuple_Size(args);
38     int i, r;
39     PyObject *ret = NULL;
40
41     PyObject **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
80     if (r) {
81         if (errno)
82             PyErr_SetFromErrno(PyExc_IOError);
83         else
84             PyErr_SetString(PyExc_ValueError, "invalid message format");
85         goto out;
86     }
87
88     // End with success.
89     Py_INCREF(Py_None);
90     ret = Py_None;
91
92 out:
93     for (i = 0; i < argc; ++i)
94         Py_XDECREF(encoded[i]);
95
96     free(encoded);
97
98 out1:
99     // Free the iovector. The actual strings
100     // are already managed by Python.
101     free(iov);
102
103     return ret;
104 }
105
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)."
109              );
110
111 static PyObject*
112 journal_stream_fd(PyObject *self, PyObject *args) {
113     const char* identifier;
114     int priority, level_prefix;
115     int fd;
116     if (!PyArg_ParseTuple(args, "sii:stream_fd",
117                           &identifier, &priority, &level_prefix))
118         return NULL;
119
120     fd = sd_journal_stream_fd(identifier, priority, level_prefix);
121     if (fd < 0)
122         return PyErr_SetFromErrno(PyExc_IOError);
123
124     return PyLong_FromLong(fd);
125 }
126
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 */
132 };
133
134 #if PY_MAJOR_VERSION < 3
135
136 PyMODINIT_FUNC
137 init_journal(void)
138 {
139     (void) Py_InitModule("_journal", methods);
140 }
141
142 #else
143
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 */
149     methods
150 };
151
152 PyMODINIT_FUNC
153 PyInit__journal(void)
154 {
155     return PyModule_Create(&module);
156 }
157
158 #endif