chiark / gitweb /
systemd-python: Moved _reader datetime usage to python
[elogind.git] / src / python-systemd / _reader.c
index f047ab9e6d95b81a362f2a7610d5618950d23724..f1c3175bbf294fb94b79003109dfacb17f9f9a53 100644 (file)
@@ -54,28 +54,26 @@ Journal_init(Journal *self, PyObject *args, PyObject *keywds)
     int flags=SD_JOURNAL_LOCAL_ONLY;
     char *path=NULL;
 
-    static char *kwlist[] = {"flags", NULL};
+    static char *kwlist[] = {"flags", "path", NULL};
     if (! PyArg_ParseTupleAndKeywords(args, keywds, "|is", kwlist,
                                       &flags, &path))
         return 1;
 
     int r;
+    Py_BEGIN_ALLOW_THREADS
     if (path) {
         r = sd_journal_open_directory(&self->j, path, 0);
     }else{
-        Py_BEGIN_ALLOW_THREADS
         r = sd_journal_open(&self->j, flags);
-        Py_END_ALLOW_THREADS
     }
-    if (r == -EINVAL) {
-        PyErr_SetString(PyExc_ValueError, "Invalid flags or path");
+    Py_END_ALLOW_THREADS
+    if (r < 0) {
+        errno = -r;
+        PyObject *errtype = r == -EINVAL ? PyExc_ValueError :
+                            r == -ENOMEM ? PyExc_MemoryError :
+                            PyExc_OSError;
+        PyErr_SetFromErrnoWithFilename(errtype, path);
         return -1;
-    }else if (r == -ENOMEM) {
-        PyErr_SetString(PyExc_MemoryError, "Not enough memory");
-        return 1;
-    }else if (r < 0) {
-        PyErr_SetString(PyExc_RuntimeError, "Error opening journal");
-        return 1;
     }
 
     return 0;
@@ -92,30 +90,27 @@ Journal_get_next(Journal *self, PyObject *args)
     if (! PyArg_ParseTuple(args, "|L", &skip))
         return NULL;
 
+    if (skip == 0LL) {
+        PyErr_SetString(PyExc_ValueError, "Skip number must positive/negative integer");
+        return NULL;
+    }
+
     int r;
+    Py_BEGIN_ALLOW_THREADS
     if (skip == 1LL) {
-        Py_BEGIN_ALLOW_THREADS
         r = sd_journal_next(self->j);
-        Py_END_ALLOW_THREADS
     }else if (skip == -1LL) {
-        Py_BEGIN_ALLOW_THREADS
         r = sd_journal_previous(self->j);
-        Py_END_ALLOW_THREADS
     }else if (skip > 1LL) {
-        Py_BEGIN_ALLOW_THREADS
         r = sd_journal_next_skip(self->j, skip);
-        Py_END_ALLOW_THREADS
     }else if (skip < -1LL) {
-        Py_BEGIN_ALLOW_THREADS
         r = sd_journal_previous_skip(self->j, -skip);
-        Py_END_ALLOW_THREADS
-    }else{
-        PyErr_SetString(PyExc_ValueError, "Skip number must positive/negative integer");
-        return NULL;
     }
+    Py_END_ALLOW_THREADS
 
     if (r < 0) {
-        PyErr_SetString(PyExc_RuntimeError, "Error getting next message");
+        errno = -r;
+        PyErr_SetFromErrno(PyExc_OSError);
         return NULL;
     }else if ( r == 0) { //EOF
         return PyDict_New();
@@ -256,14 +251,12 @@ Journal_add_match(Journal *self, PyObject *args, PyObject *keywds)
         if (PyErr_Occurred())
             return NULL;
         r = sd_journal_add_match(self->j, arg_match, arg_match_len);
-        if (r == -EINVAL) {
-            PyErr_SetString(PyExc_ValueError, "Invalid match");
-            return NULL;
-        }else if (r == -ENOMEM) {
-            PyErr_SetString(PyExc_MemoryError, "Not enough memory");
-            return NULL;
-        }else if (r < 0) {
-            PyErr_SetString(PyExc_RuntimeError, "Error adding match");
+        if (r < 0) {
+            errno = -r;
+            PyObject *errtype = r == -EINVAL ? PyExc_ValueError :
+                                r == -ENOMEM ? PyExc_MemoryError :
+                                PyExc_OSError;
+            PyErr_SetFromErrno(errtype);
             return NULL;
         }
     }
@@ -345,11 +338,11 @@ Journal_add_disjunction(Journal *self, PyObject *args)
 {
     int r;
     r = sd_journal_add_disjunction(self->j);
-    if (r == -ENOMEM) {
-        PyErr_SetString(PyExc_MemoryError, "Not enough memory");
-        return NULL;
-    }else if (r < 0) {
-        PyErr_SetString(PyExc_RuntimeError, "Error adding disjunction");
+    if (r < 0) {
+        errno = -r;
+        PyObject *errtype = r == -ENOMEM ? PyExc_MemoryError :
+                            PyExc_OSError;
+        PyErr_SetFromErrno(errtype);
         return NULL;
     }
     Py_RETURN_NONE;
@@ -383,38 +376,45 @@ Journal_seek(Journal *self, PyObject *args, PyObject *keywds)
                                       &offset, &whence))
         return NULL;
 
-    PyObject *arg;
+    PyObject *result=NULL;
     if (whence == SEEK_SET){
         int r;
         Py_BEGIN_ALLOW_THREADS
         r = sd_journal_seek_head(self->j);
         Py_END_ALLOW_THREADS
         if (r < 0) {
-            PyErr_SetString(PyExc_RuntimeError, "Error seeking to head");
+            errno = -r;
+            PyErr_SetFromErrno(PyExc_OSError);
             return NULL;
         }
         if (offset > 0LL) {
-            Py_DECREF(PyObject_CallMethod((PyObject *)self, "get_next", "L", offset));
+            result = PyObject_CallMethod((PyObject *)self, "get_next", "L", offset);
         }
     }else if (whence == SEEK_CUR){
-        Py_DECREF(PyObject_CallMethod((PyObject *)self, "get_next", "L", offset));
+        result = PyObject_CallMethod((PyObject *)self, "get_next", "L", offset);
     }else if (whence == SEEK_END){
         int r;
         Py_BEGIN_ALLOW_THREADS
         r = sd_journal_seek_tail(self->j);
         Py_END_ALLOW_THREADS
         if (r < 0) {
-            PyErr_SetString(PyExc_RuntimeError, "Error seeking to tail");
+            errno = -r;
+            PyErr_SetFromErrno(PyExc_OSError);
             return NULL;
         }
-        Py_DECREF(PyObject_CallMethod((PyObject *)self, "get_next", "L", -1LL));
         if (offset < 0LL) {
-            Py_DECREF(PyObject_CallMethod((PyObject *)self, "get_next", "L", offset));
+            result = PyObject_CallMethod((PyObject *)self, "get_next", "L", offset);
+        }else{
+            result = PyObject_CallMethod((PyObject *)self, "get_next", "L", -1LL);
         }
     }else{
         PyErr_SetString(PyExc_ValueError, "Invalid value for whence");
-        return NULL;
     }
+
+    if (result)
+        Py_DECREF(result);
+    if (PyErr_Occurred())
+        return NULL;
     Py_RETURN_NONE;
 }
 
@@ -426,34 +426,12 @@ PyDoc_STRVAR(Journal_seek_realtime__doc__,
 static PyObject *
 Journal_seek_realtime(Journal *self, PyObject *args)
 {
-    PyObject *arg;
-    if (! PyArg_ParseTuple(args, "O", &arg))
+    uint64_t timestamp;
+    if (! PyArg_ParseTuple(args, "K", &timestamp))
         return NULL;
 
-    uint64_t timestamp=-1LL;
-    if (PyDateTime_Check(arg)) {
-        PyObject *temp;
-        char *timestamp_str;
-        temp = PyObject_CallMethod(arg, "strftime", "s", "%s%f");
-#if PY_MAJOR_VERSION >=3
-        PyObject *temp2;
-        temp2 = PyUnicode_AsUTF8String(temp);
-        timestamp_str = PyBytes_AsString(temp2);
-        Py_DECREF(temp2);
-#else
-        timestamp_str = PyString_AsString(temp);
-#endif
-        Py_DECREF(temp);
-        timestamp = strtoull(timestamp_str, NULL, 10);
-    }else if (PyLong_Check(arg)) {
-        timestamp = PyLong_AsUnsignedLongLong(arg);
-#if PY_MAJOR_VERSION <3
-    }else if (PyInt_Check(arg)) {
-        timestamp = PyInt_AsUnsignedLongLongMask(arg);
-#endif
-    }
     if ((int64_t) timestamp < 0LL) {
-        PyErr_SetString(PyExc_ValueError, "Time must be positive integer or datetime instance");
+        PyErr_SetString(PyExc_ValueError, "Time must be positive integer");
         return NULL;
     }
 
@@ -462,7 +440,8 @@ Journal_seek_realtime(Journal *self, PyObject *args)
     r = sd_journal_seek_realtime_usec(self->j, timestamp);
     Py_END_ALLOW_THREADS
     if (r < 0) {
-        PyErr_SetString(PyExc_RuntimeError, "Error seek to time");
+        errno = -r;
+        PyErr_SetFromErrno(PyExc_OSError);
         return NULL;
     }
     Py_RETURN_NONE;
@@ -478,30 +457,16 @@ PyDoc_STRVAR(Journal_seek_monotonic__doc__,
 static PyObject *
 Journal_seek_monotonic(Journal *self, PyObject *args)
 {
-    PyObject *arg;
+    double timedouble;
     char *bootid=NULL;
-    if (! PyArg_ParseTuple(args, "O|s", &arg, &bootid))
+    if (! PyArg_ParseTuple(args, "d|z", &timedouble, &bootid))
         return NULL;
 
-    uint64_t timestamp=-1LL;
-    if PyDelta_Check(arg) {
-        PyObject *temp;
-        temp = PyObject_CallMethod(arg, "total_seconds", NULL);
-        timestamp = (uint64_t) (PyFloat_AsDouble(temp) * 1E6);
-        Py_DECREF(temp);
-    }else if (PyFloat_Check(arg)) {
-        timestamp = (uint64_t) (PyFloat_AsDouble(arg) * 1E6);
-    }else if (PyLong_Check(arg)) {
-        timestamp = PyLong_AsUnsignedLongLong(arg) * (uint64_t) 1E6;
-#if PY_MAJOR_VERSION <3
-    }else if (PyInt_Check(arg)) {
-        timestamp = PyInt_AsUnsignedLongLongMask(arg) * (uint64_t) 1E6;
-#endif
-
-    }
+    uint64_t timestamp;
+    timestamp = (uint64_t) (timedouble * 1.0E6);
 
     if ((int64_t) timestamp < 0LL) {
-        PyErr_SetString(PyExc_ValueError, "Time must be positive number or timedelta instance");
+        PyErr_SetString(PyExc_ValueError, "Time must be positive number");
         return NULL;
     }
 
@@ -512,8 +477,9 @@ Journal_seek_monotonic(Journal *self, PyObject *args)
         if (r == -EINVAL) {
             PyErr_SetString(PyExc_ValueError, "Invalid bootid");
             return NULL;
-        } else if (r < 0) {
-            PyErr_SetString(PyExc_RuntimeError, "Error processing bootid");
+        }else if (r < 0) {
+            errno = -r;
+            PyErr_SetFromErrno(PyExc_OSError);
             return NULL;
         }
     }else{
@@ -521,8 +487,9 @@ Journal_seek_monotonic(Journal *self, PyObject *args)
         if (r == -EIO) {
             PyErr_SetString(PyExc_IOError, "Error getting current boot ID");
             return NULL;
-        } else if (r < 0) {
-            PyErr_SetString(PyExc_RuntimeError, "Error getting current boot ID");
+        }else if (r < 0) {
+            errno = -r;
+            PyErr_SetFromErrno(PyExc_OSError);
             return NULL;
         }
     }
@@ -531,7 +498,8 @@ Journal_seek_monotonic(Journal *self, PyObject *args)
     r = sd_journal_seek_monotonic_usec(self->j, sd_id, timestamp);
     Py_END_ALLOW_THREADS
     if (r < 0) {
-        PyErr_SetString(PyExc_RuntimeError, "Error seek to time");
+        errno = -r;
+        PyErr_SetFromErrno(PyExc_OSError);
         return NULL;
     }
     Py_RETURN_NONE;
@@ -554,14 +522,19 @@ Journal_wait(Journal *self, PyObject *args, PyObject *keywds)
         return NULL;
 
     int r;
+    Py_BEGIN_ALLOW_THREADS
     if ( timeout == 0LL) {
-        Py_BEGIN_ALLOW_THREADS
         r = sd_journal_wait(self->j, (uint64_t) -1);
-        Py_END_ALLOW_THREADS
     }else{
-        Py_BEGIN_ALLOW_THREADS
         r = sd_journal_wait(self->j, timeout * 1E6);
-        Py_END_ALLOW_THREADS
+    }
+    Py_END_ALLOW_THREADS
+    if (r < 0) {
+        errno = -r;
+        PyObject *errtype = r == -ENOMEM ? PyExc_MemoryError :
+                            PyExc_OSError;
+        PyErr_SetFromErrno(errtype);
+        return NULL;
     }
 #if PY_MAJOR_VERSION >=3
     return PyLong_FromLong(r);
@@ -584,14 +557,12 @@ Journal_seek_cursor(Journal *self, PyObject *args)
     Py_BEGIN_ALLOW_THREADS
     r = sd_journal_seek_cursor(self->j, cursor);
     Py_END_ALLOW_THREADS
-    if (r == -EINVAL) {
-        PyErr_SetString(PyExc_ValueError, "Invalid cursor");
-        return NULL;
-    }else if (r == -ENOMEM) {
-        PyErr_SetString(PyExc_MemoryError, "Not enough memory");
-        return NULL;
-    }else if (r < 0) {
-        PyErr_SetString(PyExc_RuntimeError, "Error seeking to cursor");
+    if (r < 0) {
+        errno = -r;
+        PyObject *errtype = r == -EINVAL ? PyExc_ValueError :
+                            r == -ENOMEM ? PyExc_MemoryError :
+                            PyExc_OSError;
+        PyErr_SetFromErrno(errtype);
         return NULL;
     }
     Py_RETURN_NONE;
@@ -607,7 +578,7 @@ Journal_iter(PyObject *self)
 static PyObject *
 Journal_iternext(PyObject *self)
 {
-    PyObject *dict, *arg;
+    PyObject *dict;
     Py_ssize_t dict_size;
 
     dict = PyObject_CallMethod(self, "get_next", "");
@@ -637,14 +608,12 @@ Journal_query_unique(Journal *self, PyObject *args)
     Py_BEGIN_ALLOW_THREADS
     r = sd_journal_query_unique(self->j, query);
     Py_END_ALLOW_THREADS
-    if (r == -EINVAL) {
-        PyErr_SetString(PyExc_ValueError, "Invalid field name");
-        return NULL;
-    } else if (r == -ENOMEM) {
-        PyErr_SetString(PyExc_MemoryError, "Not enough memory");
-        return NULL;
-    } else if (r < 0) {
-        PyErr_SetString(PyExc_RuntimeError, "Error querying journal");
+    if (r < 0) {
+        errno = -r;
+        PyObject *errtype = r == -EINVAL ? PyExc_ValueError :
+                            r == -ENOMEM ? PyExc_MemoryError :
+                            PyExc_OSError;
+        PyErr_SetFromErrno(errtype);
         return NULL;
     }
 
@@ -671,100 +640,6 @@ Journal_query_unique(Journal *self, PyObject *args)
 }
 #endif //def SD_JOURNAL_FOREACH_UNIQUE
 
-PyDoc_STRVAR(Journal_log_level__doc__,
-"log_level(level) -> None\n\n"
-"Sets maximum log level by setting matches for PRIORITY.");
-static PyObject *
-Journal_log_level(Journal *self, PyObject *args)
-{
-    int level;
-    if (! PyArg_ParseTuple(args, "i", &level))
-        return NULL;
-
-    if (level < 0 || level > 7) {
-        PyErr_SetString(PyExc_ValueError, "Log level should be 0 <= level <= 7");
-        return NULL;
-    }
-    int i;
-    char level_str[2];
-    PyObject *arg, *keywds;
-    for(i = 0; i <= level; i++) {
-        sprintf(level_str, "%i", i);
-        arg = PyTuple_New(0);
-        keywds = Py_BuildValue("{s:s}", "PRIORITY", level_str);
-        Journal_add_match(self, arg, keywds);
-        Py_DECREF(arg);
-        Py_DECREF(keywds);
-        if (PyErr_Occurred())
-            return NULL;
-    }
-    Py_RETURN_NONE;
-}
-
-PyDoc_STRVAR(Journal_this_boot__doc__,
-"this_boot() -> None\n\n"
-"Sets match filter for the current _BOOT_ID.");
-static PyObject *
-Journal_this_boot(Journal *self, PyObject *args)
-{
-    sd_id128_t sd_id;
-    int r;
-    r = sd_id128_get_boot(&sd_id);
-    if (r == -EIO) {
-        PyErr_SetString(PyExc_IOError, "Error getting current boot ID");
-        return NULL;
-    } else if (r < 0) {
-        PyErr_SetString(PyExc_RuntimeError, "Error getting current boot ID");
-        return NULL;
-    }
-
-    char bootid[33];
-    sd_id128_to_string(sd_id, bootid);
-
-    PyObject *arg, *keywds;
-    arg = PyTuple_New(0);
-    keywds = Py_BuildValue("{s:s}", "_BOOT_ID", bootid);
-    Journal_add_match(self, arg, keywds);
-    Py_DECREF(arg);
-    Py_DECREF(keywds);
-    if (PyErr_Occurred())
-        return NULL;
-
-    Py_RETURN_NONE;
-}
-
-PyDoc_STRVAR(Journal_this_machine__doc__,
-"this_machine() -> None\n\n"
-"Sets match filter for the current _MACHINE_ID.");
-static PyObject *
-Journal_this_machine(Journal *self, PyObject *args)
-{
-    sd_id128_t sd_id;
-    int r;
-    r = sd_id128_get_machine(&sd_id);
-    if (r == -EIO) {
-        PyErr_SetString(PyExc_IOError, "Error getting current boot ID");
-        return NULL;
-    } else if (r < 0) {
-        PyErr_SetString(PyExc_RuntimeError, "Error getting current boot ID");
-        return NULL;
-    }
-
-    char machineid[33];
-    sd_id128_to_string(sd_id, machineid);
-
-    PyObject *arg, *keywds;
-    arg = PyTuple_New(0);
-    keywds = Py_BuildValue("{s:s}", "_MACHINE_ID", machineid);
-    Journal_add_match(self, arg, keywds);
-    Py_DECREF(arg);
-    Py_DECREF(keywds);
-    if (PyErr_Occurred())
-        return NULL;
-
-    Py_RETURN_NONE;
-}
-
 static PyObject *
 Journal_get_data_threshold(Journal *self, void *closure)
 {
@@ -773,8 +648,9 @@ Journal_get_data_threshold(Journal *self, void *closure)
     int r;
 
     r = sd_journal_get_data_threshold(self->j, &cvalue);
-    if (r < 0){
-        PyErr_SetString(PyExc_RuntimeError, "Error getting data threshold");
+    if (r < 0) {
+        errno = -r;
+        PyErr_SetFromErrno(PyExc_OSError);
         return NULL;
     }
 
@@ -807,9 +683,10 @@ Journal_set_data_threshold(Journal *self, PyObject *value, void *closure)
 #else
     r = sd_journal_set_data_threshold(self->j, (size_t) PyInt_AsLong(value));
 #endif
-    if (r < 0){
-        PyErr_SetString(PyExc_RuntimeError, "Error setting data threshold");
-        return -1;
+    if (r < 0) {
+        errno = -r;
+        PyErr_SetFromErrno(PyExc_OSError);
+        return NULL;
     }
     return 0;
 }
@@ -848,12 +725,6 @@ static PyMethodDef Journal_methods[] = {
     {"query_unique", (PyCFunction)Journal_query_unique, METH_VARARGS,
     Journal_query_unique__doc__},
 #endif
-    {"log_level", (PyCFunction)Journal_log_level, METH_VARARGS,
-    Journal_log_level__doc__},
-    {"this_boot", (PyCFunction)Journal_this_boot, METH_NOARGS,
-    Journal_this_boot__doc__},
-    {"this_machine", (PyCFunction)Journal_this_machine, METH_NOARGS,
-    Journal_this_machine__doc__},
     {NULL}  /* Sentinel */
 };