]> git.proxmox.com Git - systemd.git/blob - src/python-systemd/_journal.c
Imported Upstream version 222
[systemd.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 #include "util.h"
26
27 #define SD_JOURNAL_SUPPRESS_LOCATION
28 #include "systemd/sd-journal.h"
29
30 PyDoc_STRVAR(journal_sendv__doc__,
31 "sendv('FIELD=value', 'FIELD=value', ...) -> None\n\n"
32 "Send an entry to the journal."
33 );
34
35 static PyObject *journal_sendv(PyObject *self, PyObject *args) {
36 struct iovec *iov = NULL;
37 int argc;
38 int i, r;
39 PyObject *ret = NULL;
40 PyObject **encoded;
41
42 /* Allocate an array for the argument strings */
43 argc = PyTuple_Size(args);
44 encoded = alloca0(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 #if PY_MAJOR_VERSION < 3
117
118 DISABLE_WARNING_MISSING_PROTOTYPES;
119 PyMODINIT_FUNC init_journal(void) {
120 PyObject *m;
121
122 m = Py_InitModule("_journal", methods);
123 if (m == NULL)
124 return;
125
126 PyModule_AddStringConstant(m, "__version__", PACKAGE_VERSION);
127 }
128 REENABLE_WARNING;
129
130 #else
131
132 static struct PyModuleDef module = {
133 PyModuleDef_HEAD_INIT,
134 "_journal", /* name of module */
135 NULL, /* module documentation, may be NULL */
136 -1, /* size of per-interpreter state of the module */
137 methods
138 };
139
140 DISABLE_WARNING_MISSING_PROTOTYPES;
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 REENABLE_WARNING;
156
157 #endif