chiark / gitweb /
systemd-python: add Journal method to add MESSAGE_ID match
[elogind.git] / src / python-systemd / _reader.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2013 Steven Hiscocks, Zbigniew JÄ™drzejewski-Szmek
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 #include <systemd/sd-journal.h>
22
23 #include <Python.h>
24 #include <structmember.h>
25 #include <datetime.h>
26
27 typedef struct {
28     PyObject_HEAD
29     sd_journal *j;
30 } Journal;
31 static PyTypeObject JournalType;
32
33 static void
34 Journal_dealloc(Journal* self)
35 {
36     sd_journal_close(self->j);
37     Py_TYPE(self)->tp_free((PyObject*)self);
38 }
39
40 PyDoc_STRVAR(Journal__doc__,
41 "Journal([flags][,path]) -> ...\n"
42 "Journal instance\n\n"
43 "Returns instance of Journal, which allows filtering and return\n"
44 "of journal entries.\n"
45 "Argument `flags` sets open flags of the journal, which can be one\n"
46 "of, or ORed combination of constants: LOCAL_ONLY (default) opens\n"
47 "journal on local machine only; RUNTIME_ONLY opens only\n"
48 "volatile journal files; and SYSTEM_ONLY opens only\n"
49 "journal files of system services and the kernel.\n"
50 "Argument `path` is the directory of journal files. Note that\n"
51 "currently flags are ignored when `path` is present as they are\n"
52 " not relevant.");
53 static int
54 Journal_init(Journal *self, PyObject *args, PyObject *keywds)
55 {
56     int flags=SD_JOURNAL_LOCAL_ONLY;
57     char *path=NULL;
58
59     static char *kwlist[] = {"flags", "path", NULL};
60     if (! PyArg_ParseTupleAndKeywords(args, keywds, "|iz", kwlist,
61                                       &flags, &path))
62         return 1;
63
64     int r;
65     Py_BEGIN_ALLOW_THREADS
66     if (path) {
67         r = sd_journal_open_directory(&self->j, path, 0);
68     }else{
69         r = sd_journal_open(&self->j, flags);
70     }
71     Py_END_ALLOW_THREADS
72     if (r < 0) {
73         errno = -r;
74         PyObject *errtype = r == -EINVAL ? PyExc_ValueError :
75                             r == -ENOMEM ? PyExc_MemoryError :
76                             PyExc_OSError;
77         PyErr_SetFromErrnoWithFilename(errtype, path);
78         return -1;
79     }
80
81     return 0;
82 }
83
84 PyDoc_STRVAR(Journal_get_next__doc__,
85 "get_next([skip]) -> dict\n\n"
86 "Return dictionary of the next log entry. Optional skip value will\n"
87 "return the `skip`th log entry.");
88 static PyObject *
89 Journal_get_next(Journal *self, PyObject *args)
90 {
91     int64_t skip=1LL;
92     if (! PyArg_ParseTuple(args, "|L", &skip))
93         return NULL;
94
95     if (skip == 0LL) {
96         PyErr_SetString(PyExc_ValueError, "Skip number must positive/negative integer");
97         return NULL;
98     }
99
100     int r = -EINVAL;
101     Py_BEGIN_ALLOW_THREADS
102     if (skip == 1LL) {
103         r = sd_journal_next(self->j);
104     }else if (skip == -1LL) {
105         r = sd_journal_previous(self->j);
106     }else if (skip > 1LL) {
107         r = sd_journal_next_skip(self->j, skip);
108     }else if (skip < -1LL) {
109         r = sd_journal_previous_skip(self->j, -skip);
110     }
111     Py_END_ALLOW_THREADS
112
113     if (r < 0) {
114         errno = -r;
115         PyErr_SetFromErrno(PyExc_OSError);
116         return NULL;
117     }else if ( r == 0) { //EOF
118         return PyDict_New();
119     }
120
121     PyObject *dict;
122     dict = PyDict_New();
123
124     const void *msg;
125     size_t msg_len;
126     const char *delim_ptr;
127     PyObject *key, *value, *cur_value, *tmp_list;
128
129     SD_JOURNAL_FOREACH_DATA(self->j, msg, msg_len) {
130         delim_ptr = memchr(msg, '=', msg_len);
131 #if PY_MAJOR_VERSION >=3
132         key = PyUnicode_FromStringAndSize(msg, delim_ptr - (const char*) msg);
133 #else
134         key = PyString_FromStringAndSize(msg, delim_ptr - (const char*) msg);
135 #endif
136         value = PyBytes_FromStringAndSize(delim_ptr + 1, (const char*) msg + msg_len - (delim_ptr + 1) );
137         if (PyDict_Contains(dict, key)) {
138             cur_value = PyDict_GetItem(dict, key);
139             if (PyList_CheckExact(cur_value)) {
140                 PyList_Append(cur_value, value);
141             }else{
142                 tmp_list = PyList_New(0);
143                 PyList_Append(tmp_list, cur_value);
144                 PyList_Append(tmp_list, value);
145                 PyDict_SetItem(dict, key, tmp_list);
146                 Py_DECREF(tmp_list);
147             }
148         }else{
149             PyDict_SetItem(dict, key, value);
150         }
151         Py_DECREF(key);
152         Py_DECREF(value);
153     }
154
155     uint64_t realtime;
156     if (sd_journal_get_realtime_usec(self->j, &realtime) == 0) {
157         char realtime_str[20];
158         sprintf(realtime_str, "%llu", (long long unsigned) realtime);
159
160 #if PY_MAJOR_VERSION >=3
161         key = PyUnicode_FromString("__REALTIME_TIMESTAMP");
162 #else
163         key = PyString_FromString("__REALTIME_TIMESTAMP");
164 #endif
165         value = PyBytes_FromString(realtime_str);
166         PyDict_SetItem(dict, key, value);
167         Py_DECREF(key);
168         Py_DECREF(value);
169     }
170
171     sd_id128_t sd_id;
172     uint64_t monotonic;
173     if (sd_journal_get_monotonic_usec(self->j, &monotonic, &sd_id) == 0) {
174         char monotonic_str[20];
175         sprintf(monotonic_str, "%llu", (long long unsigned) monotonic);
176 #if PY_MAJOR_VERSION >=3
177         key = PyUnicode_FromString("__MONOTONIC_TIMESTAMP");
178 #else
179         key = PyString_FromString("__MONOTONIC_TIMESTAMP");
180 #endif
181         value = PyBytes_FromString(monotonic_str);
182
183         PyDict_SetItem(dict, key, value);
184         Py_DECREF(key);
185         Py_DECREF(value);
186     }
187
188     char *cursor;
189     if (sd_journal_get_cursor(self->j, &cursor) > 0) { //Should return 0...
190 #if PY_MAJOR_VERSION >=3
191         key = PyUnicode_FromString("__CURSOR");
192 #else
193         key = PyString_FromString("__CURSOR");
194 #endif
195         value = PyBytes_FromString(cursor);
196         PyDict_SetItem(dict, key, value);
197         free(cursor);
198         Py_DECREF(key);
199         Py_DECREF(value);
200     }
201
202     return dict;
203 }
204
205 PyDoc_STRVAR(Journal_get_previous__doc__,
206 "get_previous([skip]) -> dict\n\n"
207 "Return dictionary of the previous log entry. Optional skip value\n"
208 "will return the -`skip`th log entry. Equivalent to get_next(-skip).");
209 static PyObject *
210 Journal_get_previous(Journal *self, PyObject *args)
211 {
212     int64_t skip=1LL;
213     if (! PyArg_ParseTuple(args, "|L", &skip))
214         return NULL;
215
216     return PyObject_CallMethod((PyObject *)self, "get_next", "L", -skip);
217 }
218
219 PyDoc_STRVAR(Journal_add_match__doc__,
220 "add_match(match) -> None\n\n"
221 "Add a match to filter journal log entries. All matches of different\n"
222 "fields are combined in logical AND, and matches of the same field\n"
223 "are automatically combined in logical OR.\n"
224 "Match is string of form \"field=value\".");
225 static PyObject *
226 Journal_add_match(Journal *self, PyObject *args, PyObject *keywds)
227 {
228     char *match;
229     int match_len;
230     if (! PyArg_ParseTuple(args, "s#", &match, &match_len))
231         return NULL;
232
233     int r;
234     r = sd_journal_add_match(self->j, match, match_len);
235     if (r < 0) {
236         errno = -r;
237         PyObject *errtype = r == -EINVAL ? PyExc_ValueError :
238                             r == -ENOMEM ? PyExc_MemoryError :
239                             PyExc_OSError;
240         PyErr_SetFromErrno(errtype);
241         return NULL;
242     }
243
244     Py_RETURN_NONE;
245 }
246
247 PyDoc_STRVAR(Journal_add_disjunction__doc__,
248 "add_disjunction() -> None\n\n"
249 "Once called, all matches before and after are combined in logical\n"
250 "OR.");
251 static PyObject *
252 Journal_add_disjunction(Journal *self, PyObject *args)
253 {
254     int r;
255     r = sd_journal_add_disjunction(self->j);
256     if (r < 0) {
257         errno = -r;
258         PyObject *errtype = r == -ENOMEM ? PyExc_MemoryError :
259                             PyExc_OSError;
260         PyErr_SetFromErrno(errtype);
261         return NULL;
262     }
263     Py_RETURN_NONE;
264 }
265
266 PyDoc_STRVAR(Journal_flush_matches__doc__,
267 "flush_matches() -> None\n\n"
268 "Clears all current match filters.");
269 static PyObject *
270 Journal_flush_matches(Journal *self, PyObject *args)
271 {
272     sd_journal_flush_matches(self->j);
273     Py_RETURN_NONE;
274 }
275
276 PyDoc_STRVAR(Journal_seek__doc__,
277 "seek(offset[, whence]) -> None\n\n"
278 "Seek through journal by `offset` number of entries. Argument\n"
279 "`whence` defines what the offset is relative to:\n"
280 "os.SEEK_SET (default) from first match in journal;\n"
281 "os.SEEK_CUR from current position in journal;\n"
282 "and os.SEEK_END is from last match in journal.");
283 static PyObject *
284 Journal_seek(Journal *self, PyObject *args, PyObject *keywds)
285 {
286     int64_t offset;
287     int whence=SEEK_SET;
288     static char *kwlist[] = {"offset", "whence", NULL};
289
290     if (! PyArg_ParseTupleAndKeywords(args, keywds, "L|i", kwlist,
291                                       &offset, &whence))
292         return NULL;
293
294     PyObject *result=NULL;
295     if (whence == SEEK_SET){
296         int r;
297         Py_BEGIN_ALLOW_THREADS
298         r = sd_journal_seek_head(self->j);
299         Py_END_ALLOW_THREADS
300         if (r < 0) {
301             errno = -r;
302             PyErr_SetFromErrno(PyExc_OSError);
303             return NULL;
304         }
305         if (offset > 0LL) {
306             result = PyObject_CallMethod((PyObject *)self, "get_next", "L", offset);
307         }
308     }else if (whence == SEEK_CUR){
309         result = PyObject_CallMethod((PyObject *)self, "get_next", "L", offset);
310     }else if (whence == SEEK_END){
311         int r;
312         Py_BEGIN_ALLOW_THREADS
313         r = sd_journal_seek_tail(self->j);
314         Py_END_ALLOW_THREADS
315         if (r < 0) {
316             errno = -r;
317             PyErr_SetFromErrno(PyExc_OSError);
318             return NULL;
319         }
320         if (offset < 0LL) {
321             result = PyObject_CallMethod((PyObject *)self, "get_next", "L", offset);
322         }else{
323             result = PyObject_CallMethod((PyObject *)self, "get_next", "L", -1LL);
324         }
325     }else{
326         PyErr_SetString(PyExc_ValueError, "Invalid value for whence");
327     }
328
329     Py_XDECREF(result);
330     if (PyErr_Occurred())
331         return NULL;
332     Py_RETURN_NONE;
333 }
334
335 PyDoc_STRVAR(Journal_seek_realtime__doc__,
336 "seek_realtime(realtime) -> None\n\n"
337 "Seek to nearest matching journal entry to `realtime`. Argument\n"
338 "`realtime` can must be an integer unix timestamp.");
339 static PyObject *
340 Journal_seek_realtime(Journal *self, PyObject *args)
341 {
342     double timedouble;
343     if (! PyArg_ParseTuple(args, "d", &timedouble))
344         return NULL;
345
346     uint64_t timestamp;
347     timestamp = (uint64_t) (timedouble * 1.0E6);
348
349     if ((int64_t) timestamp < 0LL) {
350         PyErr_SetString(PyExc_ValueError, "Time must be positive integer");
351         return NULL;
352     }
353
354     int r;
355     Py_BEGIN_ALLOW_THREADS
356     r = sd_journal_seek_realtime_usec(self->j, timestamp);
357     Py_END_ALLOW_THREADS
358     if (r < 0) {
359         errno = -r;
360         PyErr_SetFromErrno(PyExc_OSError);
361         return NULL;
362     }
363     Py_RETURN_NONE;
364 }
365
366 PyDoc_STRVAR(Journal_seek_monotonic__doc__,
367 "seek_monotonic(monotonic[, bootid]) -> None\n\n"
368 "Seek to nearest matching journal entry to `monotonic`. Argument\n"
369 "`monotonic` is an timestamp from boot in seconds.\n"
370 "Argument `bootid` is a string representing which boot the\n"
371 "monotonic time is reference to. Defaults to current bootid.");
372 static PyObject *
373 Journal_seek_monotonic(Journal *self, PyObject *args)
374 {
375     double timedouble;
376     char *bootid=NULL;
377     if (! PyArg_ParseTuple(args, "d|z", &timedouble, &bootid))
378         return NULL;
379
380     uint64_t timestamp;
381     timestamp = (uint64_t) (timedouble * 1.0E6);
382
383     if ((int64_t) timestamp < 0LL) {
384         PyErr_SetString(PyExc_ValueError, "Time must be positive number");
385         return NULL;
386     }
387
388     sd_id128_t sd_id;
389     int r;
390     if (bootid) {
391         r = sd_id128_from_string(bootid, &sd_id);
392         if (r == -EINVAL) {
393             PyErr_SetString(PyExc_ValueError, "Invalid bootid");
394             return NULL;
395         }else if (r < 0) {
396             errno = -r;
397             PyErr_SetFromErrno(PyExc_OSError);
398             return NULL;
399         }
400     }else{
401         r = sd_id128_get_boot(&sd_id);
402         if (r == -EIO) {
403             PyErr_SetString(PyExc_IOError, "Error getting current boot ID");
404             return NULL;
405         }else if (r < 0) {
406             errno = -r;
407             PyErr_SetFromErrno(PyExc_OSError);
408             return NULL;
409         }
410     }
411
412     Py_BEGIN_ALLOW_THREADS
413     r = sd_journal_seek_monotonic_usec(self->j, sd_id, timestamp);
414     Py_END_ALLOW_THREADS
415     if (r < 0) {
416         errno = -r;
417         PyErr_SetFromErrno(PyExc_OSError);
418         return NULL;
419     }
420     Py_RETURN_NONE;
421 }
422  
423 PyDoc_STRVAR(Journal_wait__doc__,
424 "wait([timeout]) -> Change state (integer)\n\n"
425 "Waits until there is a change in the journal. Argument `timeout`\n"
426 "is the maximum number of seconds to wait before returning\n"
427 "regardless if journal has changed. If `timeout` is not given or is\n"
428 "0, then it will block forever.\n"
429 "Will return constants: NOP if no change; APPEND if new\n"
430 "entries have been added to the end of the journal; and\n"
431 "INVALIDATE if journal files have been added or removed.");
432 static PyObject *
433 Journal_wait(Journal *self, PyObject *args, PyObject *keywds)
434 {
435     int64_t timeout=0LL;
436     if (! PyArg_ParseTuple(args, "|L", &timeout))
437         return NULL;
438
439     int r;
440     Py_BEGIN_ALLOW_THREADS
441     if ( timeout == 0LL) {
442         r = sd_journal_wait(self->j, (uint64_t) -1);
443     }else{
444         r = sd_journal_wait(self->j, timeout * 1E6);
445     }
446     Py_END_ALLOW_THREADS
447     if (r < 0) {
448         errno = -r;
449         PyObject *errtype = r == -ENOMEM ? PyExc_MemoryError :
450                             PyExc_OSError;
451         PyErr_SetFromErrno(errtype);
452         return NULL;
453     }
454 #if PY_MAJOR_VERSION >=3
455     return PyLong_FromLong(r);
456 #else
457     return PyInt_FromLong(r);
458 #endif
459 }
460
461 PyDoc_STRVAR(Journal_seek_cursor__doc__,
462 "seek_cursor(cursor) -> None\n\n"
463 "Seeks to journal entry by given unique reference `cursor`.");
464 static PyObject *
465 Journal_seek_cursor(Journal *self, PyObject *args)
466 {
467     const char *cursor;
468     if (! PyArg_ParseTuple(args, "s", &cursor))
469         return NULL;
470
471     int r;
472     Py_BEGIN_ALLOW_THREADS
473     r = sd_journal_seek_cursor(self->j, cursor);
474     Py_END_ALLOW_THREADS
475     if (r < 0) {
476         errno = -r;
477         PyObject *errtype = r == -EINVAL ? PyExc_ValueError :
478                             r == -ENOMEM ? PyExc_MemoryError :
479                             PyExc_OSError;
480         PyErr_SetFromErrno(errtype);
481         return NULL;
482     }
483     Py_RETURN_NONE;
484 }
485
486 static PyObject *
487 Journal_iter(PyObject *self)
488 {
489     Py_INCREF(self);
490     return self;
491 }
492
493 static PyObject *
494 Journal_iternext(PyObject *self)
495 {
496     PyObject *dict;
497     Py_ssize_t dict_size;
498
499     dict = PyObject_CallMethod(self, "get_next", "");
500     if (PyErr_Occurred())
501         return NULL;
502     dict_size = PyDict_Size(dict);
503     if ((int64_t) dict_size > 0LL) {
504         return dict;
505     }else{
506         Py_DECREF(dict);
507         PyErr_SetNone(PyExc_StopIteration);
508         return NULL;
509     }
510 }
511
512 PyDoc_STRVAR(Journal_query_unique__doc__,
513 "query_unique(field) -> a set of values\n\n"
514 "Returns a set of unique values in journal for given `field`.\n"
515 "Note this does not respect any journal matches.");
516 static PyObject *
517 Journal_query_unique(Journal *self, PyObject *args)
518 {
519     char *query;
520     if (! PyArg_ParseTuple(args, "s", &query))
521         return NULL;
522
523     int r;
524     Py_BEGIN_ALLOW_THREADS
525     r = sd_journal_query_unique(self->j, query);
526     Py_END_ALLOW_THREADS
527     if (r < 0) {
528         errno = -r;
529         PyObject *errtype = r == -EINVAL ? PyExc_ValueError :
530                             r == -ENOMEM ? PyExc_MemoryError :
531                             PyExc_OSError;
532         PyErr_SetFromErrno(errtype);
533         return NULL;
534     }
535
536     const void *uniq;
537     size_t uniq_len;
538     const char *delim_ptr;
539     PyObject *value_set, *key, *value;
540     value_set = PySet_New(0);
541
542 #if PY_MAJOR_VERSION >=3
543     key = PyUnicode_FromString(query);
544 #else
545     key = PyString_FromString(query);
546 #endif
547
548     SD_JOURNAL_FOREACH_UNIQUE(self->j, uniq, uniq_len) {
549         delim_ptr = memchr(uniq, '=', uniq_len);
550         value = PyBytes_FromStringAndSize(delim_ptr + 1, (const char*) uniq + uniq_len - (delim_ptr + 1));
551         PySet_Add(value_set, value);
552         Py_DECREF(value);
553     }
554     Py_DECREF(key);
555     return value_set;
556 }
557
558 static PyObject *
559 Journal_get_data_threshold(Journal *self, void *closure)
560 {
561     size_t cvalue;
562     PyObject *value;
563     int r;
564
565     r = sd_journal_get_data_threshold(self->j, &cvalue);
566     if (r < 0) {
567         errno = -r;
568         PyErr_SetFromErrno(PyExc_OSError);
569         return NULL;
570     }
571
572 #if PY_MAJOR_VERSION >=3
573     value = PyLong_FromSize_t(cvalue);
574 #else
575     value = PyInt_FromSize_t(cvalue);
576 #endif
577     return value;
578 }
579
580 static int
581 Journal_set_data_threshold(Journal *self, PyObject *value, void *closure)
582 {
583     if (value == NULL) {
584         PyErr_SetString(PyExc_TypeError, "Cannot delete data threshold");
585         return -1;
586     }
587 #if PY_MAJOR_VERSION >=3
588     if (! PyLong_Check(value)){
589 #else
590     if (! PyInt_Check(value)){
591 #endif
592         PyErr_SetString(PyExc_TypeError, "Data threshold must be int");
593         return -1;
594     }
595     int r;
596 #if PY_MAJOR_VERSION >=3
597     r = sd_journal_set_data_threshold(self->j, (size_t) PyLong_AsLong(value));
598 #else
599     r = sd_journal_set_data_threshold(self->j, (size_t) PyInt_AsLong(value));
600 #endif
601     if (r < 0) {
602         errno = -r;
603         PyErr_SetFromErrno(PyExc_OSError);
604         return -1;
605     }
606     return 0;
607 }
608
609 static PyGetSetDef Journal_getseters[] = {
610     {"data_threshold",
611     (getter)Journal_get_data_threshold,
612     (setter)Journal_set_data_threshold,
613     "data threshold",
614     NULL},
615     {NULL}
616 };
617
618 static PyMethodDef Journal_methods[] = {
619     {"get_next", (PyCFunction)Journal_get_next, METH_VARARGS,
620     Journal_get_next__doc__},
621     {"get_previous", (PyCFunction)Journal_get_previous, METH_VARARGS,
622     Journal_get_previous__doc__},
623     {"add_match", (PyCFunction)Journal_add_match, METH_VARARGS|METH_KEYWORDS,
624     Journal_add_match__doc__},
625     {"add_disjunction", (PyCFunction)Journal_add_disjunction, METH_NOARGS,
626     Journal_add_disjunction__doc__},
627     {"flush_matches", (PyCFunction)Journal_flush_matches, METH_NOARGS,
628     Journal_flush_matches__doc__},
629     {"seek", (PyCFunction)Journal_seek, METH_VARARGS | METH_KEYWORDS,
630     Journal_seek__doc__},
631     {"seek_realtime", (PyCFunction)Journal_seek_realtime, METH_VARARGS,
632     Journal_seek_realtime__doc__},
633     {"seek_monotonic", (PyCFunction)Journal_seek_monotonic, METH_VARARGS,
634     Journal_seek_monotonic__doc__},
635     {"wait", (PyCFunction)Journal_wait, METH_VARARGS,
636     Journal_wait__doc__},
637     {"seek_cursor", (PyCFunction)Journal_seek_cursor, METH_VARARGS,
638     Journal_seek_cursor__doc__},
639     {"query_unique", (PyCFunction)Journal_query_unique, METH_VARARGS,
640     Journal_query_unique__doc__},
641     {NULL}  /* Sentinel */
642 };
643
644 static PyTypeObject JournalType = {
645     PyVarObject_HEAD_INIT(NULL, 0)
646     "_reader._Journal",               /*tp_name*/
647     sizeof(Journal),                  /*tp_basicsize*/
648     0,                                /*tp_itemsize*/
649     (destructor)Journal_dealloc,      /*tp_dealloc*/
650     0,                                /*tp_print*/
651     0,                                /*tp_getattr*/
652     0,                                /*tp_setattr*/
653     0,                                /*tp_compare*/
654     0,                                /*tp_repr*/
655     0,                                /*tp_as_number*/
656     0,                                /*tp_as_sequence*/
657     0,                                /*tp_as_mapping*/
658     0,                                /*tp_hash */
659     0,                                /*tp_call*/
660     0,                                /*tp_str*/
661     0,                                /*tp_getattro*/
662     0,                                /*tp_setattro*/
663     0,                                /*tp_as_buffer*/
664     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,/*tp_flags*/
665     Journal__doc__,                   /* tp_doc */
666     0,                                /* tp_traverse */
667     0,                                /* tp_clear */
668     0,                                /* tp_richcompare */
669     0,                                /* tp_weaklistoffset */
670     Journal_iter,                     /* tp_iter */
671     Journal_iternext,                 /* tp_iternext */
672     Journal_methods,                  /* tp_methods */
673     0,                                /* tp_members */
674     Journal_getseters,                /* tp_getset */
675     0,                                /* tp_base */
676     0,                                /* tp_dict */
677     0,                                /* tp_descr_get */
678     0,                                /* tp_descr_set */
679     0,                                /* tp_dictoffset */
680     (initproc)Journal_init,           /* tp_init */
681     0,                                /* tp_alloc */
682     PyType_GenericNew,                /* tp_new */
683 };
684
685 #if PY_MAJOR_VERSION >= 3
686 static PyModuleDef _reader_module = {
687     PyModuleDef_HEAD_INIT,
688     "_reader",
689     "Module that reads systemd journal similar to journalctl.",
690     -1,
691     NULL, NULL, NULL, NULL, NULL
692 };
693 #endif
694
695 PyMODINIT_FUNC
696 #if PY_MAJOR_VERSION >= 3
697 PyInit__reader(void)
698 #else
699 init_reader(void) 
700 #endif
701 {
702     PyObject* m;
703
704     PyDateTime_IMPORT;
705
706     if (PyType_Ready(&JournalType) < 0)
707 #if PY_MAJOR_VERSION >= 3
708         return NULL;
709 #else
710         return;
711 #endif
712
713 #if PY_MAJOR_VERSION >= 3
714     m = PyModule_Create(&_reader_module);
715     if (m == NULL)
716         return NULL;
717 #else
718     m = Py_InitModule3("_reader", NULL,
719                    "Module that reads systemd journal similar to journalctl.");
720     if (m == NULL)
721         return;
722 #endif
723
724     Py_INCREF(&JournalType);
725     PyModule_AddObject(m, "_Journal", (PyObject *)&JournalType);
726     PyModule_AddIntConstant(m, "NOP", SD_JOURNAL_NOP);
727     PyModule_AddIntConstant(m, "APPEND", SD_JOURNAL_APPEND);
728     PyModule_AddIntConstant(m, "INVALIDATE", SD_JOURNAL_INVALIDATE);
729     PyModule_AddIntConstant(m, "LOCAL_ONLY", SD_JOURNAL_LOCAL_ONLY);
730     PyModule_AddIntConstant(m, "RUNTIME_ONLY", SD_JOURNAL_RUNTIME_ONLY);
731     PyModule_AddIntConstant(m, "SYSTEM_ONLY", SD_JOURNAL_SYSTEM_ONLY);
732
733 #if PY_MAJOR_VERSION >= 3
734     return m;
735 #endif
736 }