[systemd-devel] Python journal reader

Steven Hiscocks steven-systemd at hiscocks.me.uk
Fri Apr 12 10:13:09 PDT 2013


Hi,

In the python journal Reader,  the splitting out of monotonic and 
realtime stamps, has affected `get_next` function as timestamp values 
are no longer present in the dictionary returned. Also the new 
`get_monotonic` and `get_realtime` functions are not run through the 
converters. Equally, the 'get' method added is not run through the 
converters. I also noted the additional `next` method doesn't work on 
python2, as it clashes with the iter `next` method (python3 not affected 
as it changes iter method to `__next__`)

My suggestion with the python Reader `get_next` method is that the 
realtime and monotonic timestamps remain part of it, as these are key 
parts of a log entry, and two more lines in everyone's code to get them 
seems cumbersome. Equally also the cursor value. This also makes the 
output fields the same as the journalctl json format. (I agree it makes 
sense the _Reader object element to remain separate so close to actual C 
API).

I'm not sure what the best approach to the `next` method issue is…

Proposed changes below. I've add `get_cursor` to go through converters 
for consistency, even if not required currently.

```
diff --git a/src/python-systemd/journal.py b/src/python-systemd/journal.py
index 48f57ac..c163ff7 100644
--- a/src/python-systemd/journal.py
+++ b/src/python-systemd/journal.py
@@ -189,6 +189,21 @@ class Reader(_Reader):
          for arg in args:
              super(Reader, self).add_match(arg)

+    def get(self, key):
+        return self._convert_field(key, super(Reader, self).get(key))
+
+    def get_realtime(self):
+        return self._convert_field(
+            '__REALTIME_TIMESTAMP', super(Reader, self).get_realtime())
+
+    def get_monotonic(self):
+        return self._convert_field(
+            '__MONOTONIC_TIMESTAMP', super(Reader, self).get_monotonic())
+
+    def get_cursor(self):
+        return self._convert_field(
+            '__CURSOR', super(Reader, self).get_cursor())
+
      def get_next(self, skip=1):
          """Return the next log entry as a dictionary of fields.

@@ -197,8 +212,13 @@ class Reader(_Reader):
          Entries will be processed with converters specified during
          Reader creation.
          """
-        return self._convert_entry(
+        entry = self._convert_entry(
              super(Reader, self).get_next(skip))
+        entry['__REALTIME_TIMESTAMP'] =  self.get_realtime()
+        entry['__MONOTONIC_TIMESTAMP']  = self.get_monotonic()
+        entry['__CURSOR']  = self.get_cursor()
+
+        return entry

      def query_unique(self, field):
          """Return unique values appearing in the journal for given 
`field`.
```

Thanks
-- 
Steven Hiscocks


More information about the systemd-devel mailing list