1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2012 David Strauss <david@davidstrauss.net>
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/>.
26 #define SD_JOURNAL_SUPPRESS_LOCATION
27 #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."
34 static PyObject *journal_sendv(PyObject *self, PyObject *args) {
35 struct iovec *iov = NULL;
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*));
46 /* Allocate sufficient iovector space for the arguments. */
47 iov = alloca(argc * sizeof(struct iovec));
49 /* Iterate through the Python arguments and fill the iovector. */
50 for (i = 0; i < argc; ++i) {
51 PyObject *item = PyTuple_GetItem(args, i);
55 if (PyUnicode_Check(item)) {
56 encoded[i] = PyUnicode_AsEncodedString(item, "utf-8", "strict");
57 if (encoded[i] == NULL)
61 if (PyBytes_AsStringAndSize(item, &stritem, &length))
64 iov[i].iov_base = stritem;
65 iov[i].iov_len = length;
68 /* Send the iovector to the journal. */
69 r = sd_journal_sendv(iov, argc);
72 PyErr_SetFromErrno(PyExc_IOError);
76 /* End with success. */
81 for (i = 0; i < argc; ++i)
82 Py_XDECREF(encoded[i]);
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)."
92 static PyObject* journal_stream_fd(PyObject *self, PyObject *args) {
93 const char* identifier;
94 int priority, level_prefix;
97 if (!PyArg_ParseTuple(args, "sii:stream_fd",
98 &identifier, &priority, &level_prefix))
101 fd = sd_journal_stream_fd(identifier, priority, level_prefix);
104 return PyErr_SetFromErrno(PyExc_IOError);
107 return PyLong_FromLong(fd);
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 */
116 #pragma GCC diagnostic push
117 #pragma GCC diagnostic ignored "-Wmissing-prototypes"
119 #if PY_MAJOR_VERSION < 3
121 PyMODINIT_FUNC init_journal(void) {
124 m = Py_InitModule("_journal", methods);
128 PyModule_AddStringConstant(m, "__version__", PACKAGE_VERSION);
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 */
141 PyMODINIT_FUNC PyInit__journal(void) {
144 m = PyModule_Create(&module);
148 if (PyModule_AddStringConstant(m, "__version__", PACKAGE_VERSION)) {
158 #pragma GCC diagnostic pop