python: Add stream event context support
authorSimon Marchi <simon.marchi@polymtl.ca>
Sun, 19 Jun 2016 02:54:51 +0000 (22:54 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Mon, 25 Jul 2016 21:36:55 +0000 (17:36 -0400)
This patch adds the possibility to get/set the event context type of a
stream class and set the stream context value of an event from Python.

From my experience, the value setter (Event.stream_context.setter) is
not so useful, since it looks like the context is already instanciated
when you create the event.  So in order to set the values in it, you
only need to get the existing instance and set its fields.  There is no
need to to re-assign the stream context instance to the event.  I still
put it there for completeness.

I added an example of the usage to the existing CTF writer Python
example.

Signed-off-by: Simon Marchi <simon.marchi@polymtl.ca>
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
bindings/python/babeltrace/examples/ctf_writer.py
bindings/python/babeltrace/nativebt.i
bindings/python/babeltrace/writer.py

index d60eb3d880b1af0dc41841e502594901a461ff98..03e01ea912247bd65ff4e3d5e24eb730a6c061e6 100644 (file)
@@ -71,6 +71,11 @@ for field in packet_context_type.fields:
 packet_context_type.add_field(uint16_type, "a_custom_packet_context_field")
 stream_class.packet_context_type = packet_context_type
 
+# Set a stream event context
+stream_event_context_type = btw.StructureFieldDeclaration()
+stream_event_context_type.add_field(int32_type, "field_in_stream_event_context")
+stream_class.event_context_type = stream_event_context_type
+
 # Create a string type
 string_type = btw.StringFieldDeclaration()
 
@@ -135,6 +140,8 @@ for i in range(100):
     integer_field = enumeration_field.container
     enumeration_field.value = i % 10
 
+    event.stream_context.field("field_in_stream_event_context").value = i * 10
+
     stream.append_event(event)
 
 # Populate custom packet context field before flushing
index 15726ba481008dd38ca32f9b65be7ee0170615cb..ad20c13bf96b1da51d2935cc92aa56e4778ef8bd 100644 (file)
@@ -560,6 +560,8 @@ void bt_ctf_event_class_put(struct bt_ctf_event_class *event_class);
 %rename("_bt_ctf_event_get_payload_by_index") bt_ctf_event_get_payload_by_index(struct bt_ctf_event *event, int index);
 %rename("_bt_ctf_event_get") bt_ctf_event_get(struct bt_ctf_event *event);
 %rename("_bt_ctf_event_put") bt_ctf_event_put(struct bt_ctf_event *event);
+%rename("_bt_ctf_event_get_stream_event_context") bt_ctf_event_get_stream_event_context(struct bt_ctf_event *event);
+%rename("_bt_ctf_event_set_stream_event_context") bt_ctf_event_set_stream_event_context(struct bt_ctf_event *event, struct bt_ctf_field *stream_event_context);
 
 struct bt_ctf_event *bt_ctf_event_create(struct bt_ctf_event_class *event_class);
 struct bt_ctf_event_class *bt_ctf_event_get_class(struct bt_ctf_event *event);
@@ -569,6 +571,8 @@ int bt_ctf_event_set_payload(struct bt_ctf_event *event, const char *name, struc
 struct bt_ctf_field *bt_ctf_event_get_payload_by_index(struct bt_ctf_event *event, int index);
 void bt_ctf_event_get(struct bt_ctf_event *event);
 void bt_ctf_event_put(struct bt_ctf_event *event);
+struct bt_ctf_field *bt_ctf_event_get_stream_event_context(struct bt_ctf_event *event);
+int bt_ctf_event_set_stream_event_context(struct bt_ctf_event *event, struct bt_ctf_field *stream_event_context);
 
 
 /* stream-class.h */
@@ -584,6 +588,8 @@ void bt_ctf_event_put(struct bt_ctf_event *event);
 %rename("_bt_ctf_stream_class_get_event_class_by_name") bt_ctf_stream_class_get_event_class_by_name(struct bt_ctf_stream_class *stream_class, const char *name);
 %rename("_bt_ctf_stream_class_get_packet_context_type") bt_ctf_stream_class_get_packet_context_type(struct bt_ctf_stream_class *stream_class);
 %rename("_bt_ctf_stream_class_set_packet_context_type") bt_ctf_stream_class_set_packet_context_type(struct bt_ctf_stream_class *stream_class, struct bt_ctf_field_type *packet_context_type);
+%rename("_bt_ctf_stream_class_get_event_context_type") bt_ctf_stream_class_get_event_context_type(struct bt_ctf_stream_class *stream_class);
+%rename("_bt_ctf_stream_class_set_event_context_type") bt_ctf_stream_class_set_event_context_type(struct bt_ctf_stream_class *stream_class, struct bt_ctf_field_type *event_context_type);
 %rename("_bt_ctf_stream_class_get") bt_ctf_stream_class_get(struct bt_ctf_stream_class *stream_class);
 %rename("_bt_ctf_stream_class_put") bt_ctf_stream_class_put(struct bt_ctf_stream_class *stream_class);
 
@@ -599,6 +605,8 @@ struct bt_ctf_event_class *bt_ctf_stream_class_get_event_class(struct bt_ctf_str
 struct bt_ctf_event_class *bt_ctf_stream_class_get_event_class_by_name(struct bt_ctf_stream_class *stream_class, const char *name);
 struct bt_ctf_field_type *bt_ctf_stream_class_get_packet_context_type(struct bt_ctf_stream_class *stream_class);
 int bt_ctf_stream_class_set_packet_context_type(struct bt_ctf_stream_class *stream_class, struct bt_ctf_field_type *packet_context_type);
+struct bt_ctf_field_type *bt_ctf_stream_class_get_event_context_type(struct bt_ctf_stream_class *stream_class);
+int bt_ctf_stream_class_set_event_context_type(struct bt_ctf_stream_class *stream_class, struct bt_ctf_field_type *event_context_type);
 void bt_ctf_stream_class_get(struct bt_ctf_stream_class *stream_class);
 void bt_ctf_stream_class_put(struct bt_ctf_stream_class *stream_class);
 
index dc65b2438d7c60976fe54a24cf80fcb5ac12f182..cd609afd730cf1c1e1120ff3c6b6abeed6a2b469 100644 (file)
@@ -1747,6 +1747,34 @@ class Event:
         if ret < 0:
             raise ValueError("Could not set event field payload.")
 
+    @property
+    def stream_context(self):
+        """
+        Stream event context field (instance of
+        :class:`StructureField`).
+
+        Set this attribute to assign a stream event context field
+        to this stream.
+
+        :exc:`ValueError` is raised on error.
+        """
+
+        native_field = nbt._bt_ctf_event_get_stream_event_context(self._e)
+
+        if native_field is None:
+            raise ValueError("Invalid Stream.")
+
+        return Field._create_field_from_native_instance(native_field)
+
+    @stream_context.setter
+    def stream_context(self, field):
+        if not isinstance(field, StructureField):
+            raise TypeError("Argument field must be of type StructureField")
+
+        ret = nbt._bt_ctf_event_set_stream_event_context(self._e, field._f)
+
+        if ret < 0:
+            raise ValueError("Invalid stream context field.")
 
 class StreamClass:
     """
@@ -1923,6 +1951,39 @@ class StreamClass:
         if ret < 0:
             raise ValueError("Failed to set packet context type.")
 
+    @property
+    def event_context_type(self):
+        """
+        Stream event context declaration.
+
+        Set this attribute to change the stream event context
+        declaration (must be an instance of
+        :class:`StructureFieldDeclaration`).
+
+        :exc:`ValueError` is raised on error.
+
+        """
+
+        field_type_native = nbt._bt_ctf_stream_class_get_event_context_type(self._sc)
+
+        if field_type_native is None:
+            raise ValueError("Invalid StreamClass")
+
+        field_type = FieldDeclaration._create_field_declaration_from_native_instance(field_type_native)
+
+        return field_type
+
+    @event_context_type.setter
+    def event_context_type(self, field_type):
+        if not isinstance(field_type, StructureFieldDeclaration):
+            raise TypeError("field_type argument must be of type StructureFieldDeclaration.")
+
+        ret = nbt._bt_ctf_stream_class_set_event_context_type(self._sc,
+                                                              field_type._ft)
+
+        if ret < 0:
+            raise ValueError("Failed to set event context type.")
+
 
 class Stream:
     """
This page took 0.026476 seconds and 4 git commands to generate.