CTF writer: Add function to add an integer environment field value
authorSimon Marchi <simon.marchi@polymtl.ca>
Sun, 19 Jun 2016 02:54:52 +0000 (22:54 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Mon, 25 Jul 2016 22:05:14 +0000 (18:05 -0400)
From the Python API, it's only possible to set an environment field
value as a string (whatever you pass, it gets stringified). This patch
adds a function to the CTF writer to allow setting an integer (int64_t)
environment field, and then exposes it in the Python interface.

The Python method Writer.add_environment_field now uses the type of the
passed value to determine which underlying C function to call (string or
integer). Any other type is rejected. This causes a behavior change,
since passing an integer value to add_environment_field used to produce
a string version of the value, whereas it will now produce an integer
version. However, I think it will now behave more closely to the
expectation of a lambda user.

Example:

  w.add_environment_field("foo", 2)

Result before:

  env {
    foo = "2";
  };

Result after:

  env {
    foo = 2;
  };

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

index ad20c13bf96b1da51d2935cc92aa56e4778ef8bd..345173ffd5292b93ad53841e1cad3af1bbfadce9 100644 (file)
@@ -635,6 +635,7 @@ void bt_ctf_stream_put(struct bt_ctf_stream *stream);
 %rename("_bt_ctf_writer_create") bt_ctf_writer_create(const char *path);
 %rename("_bt_ctf_writer_create_stream") bt_ctf_writer_create_stream(struct bt_ctf_writer *writer, struct bt_ctf_stream_class *stream_class);
 %rename("_bt_ctf_writer_add_environment_field") bt_ctf_writer_add_environment_field(struct bt_ctf_writer *writer, const char *name, const char *value);
+%rename("_bt_ctf_writer_add_environment_field_int64") bt_ctf_writer_add_environment_field_int64(struct bt_ctf_writer *writer, const char *name, int64_t value);
 %rename("_bt_ctf_writer_add_clock") bt_ctf_writer_add_clock(struct bt_ctf_writer *writer, struct bt_ctf_clock *clock);
 %newobject bt_ctf_writer_get_metadata_string;
 %rename("_bt_ctf_writer_get_metadata_string") bt_ctf_writer_get_metadata_string(struct bt_ctf_writer *writer);
@@ -646,6 +647,7 @@ void bt_ctf_stream_put(struct bt_ctf_stream *stream);
 struct bt_ctf_writer *bt_ctf_writer_create(const char *path);
 struct bt_ctf_stream *bt_ctf_writer_create_stream(struct bt_ctf_writer *writer, struct bt_ctf_stream_class *stream_class);
 int bt_ctf_writer_add_environment_field(struct bt_ctf_writer *writer, const char *name, const char *value);
+int bt_ctf_writer_add_environment_field_int64(struct bt_ctf_writer *writer, const char *name, int64_t value);
 int bt_ctf_writer_add_clock(struct bt_ctf_writer *writer, struct bt_ctf_clock *clock);
 char *bt_ctf_writer_get_metadata_string(struct bt_ctf_writer *writer);
 void bt_ctf_writer_flush_metadata(struct bt_ctf_writer *writer);
index cd609afd730cf1c1e1120ff3c6b6abeed6a2b469..8ed18c6c9bb9adb5d936c0055a94671df432e130 100644 (file)
@@ -2141,11 +2141,23 @@ class Writer:
         Sets the CTF environment variable named *name* to value *value*
         (converted to a string).
 
-        :exc:`ValueError` is raised on error.
+        :exc:`ValueError` or `TypeError` is raised on error.
         """
 
-        ret = nbt._bt_ctf_writer_add_environment_field(self._w, str(name),
-                                                       str(value))
+        if type(name) != str:
+            raise TypeError("Field name must be a string.")
+
+        t = type(value)
+
+        if t == str:
+            ret = nbt._bt_ctf_writer_add_environment_field(self._w, name,
+                                                           value)
+        elif t == int:
+            ret = nbt._bt_ctf_writer_add_environment_field_int64(self._w,
+                                                                 name,
+                                                                 value)
+        else:
+            raise TypeError("Value type is not supported.")
 
         if ret < 0:
             raise ValueError("Could not add environment field to trace.")
index 6c294931c49cb47ac1c55fc731a531342423d142..012b3ef04acfc88c2ab7f34f8cc95d363ae7da0f 100644 (file)
@@ -209,6 +209,22 @@ end:
        return ret;
 }
 
+int bt_ctf_writer_add_environment_field_int64(struct bt_ctf_writer *writer,
+               const char *name,
+               int64_t value)
+{
+       int ret = -1;
+
+       if (!writer || !name) {
+               goto end;
+       }
+
+       ret = bt_ctf_trace_set_environment_field_integer(writer->trace, name,
+               value);
+end:
+       return ret;
+}
+
 int bt_ctf_writer_add_clock(struct bt_ctf_writer *writer,
                struct bt_ctf_clock *clock)
 {
index 940736fe7eb552d22203a3b91cb20c68c85dfd4c..140bc5fdf6a39ea5ad66f2e597d877b5ff6ac8ff 100644 (file)
@@ -95,6 +95,23 @@ extern int bt_ctf_writer_add_environment_field(struct bt_ctf_writer *writer,
                const char *name,
                const char *value);
 
+/*
+ * bt_ctf_writer_add_environment_field_int64: add an environment field to the trace.
+ *
+ * Add an environment field to the trace. The name and value parameters are
+ * copied.
+ *
+ * @param writer Writer instance.
+ * @param name Name of the environment field (will be copied).
+ * @param value Value of the environment field.
+ *
+ * Returns 0 on success, a negative value on error.
+ */
+extern int bt_ctf_writer_add_environment_field_int64(
+               struct bt_ctf_writer *writer,
+               const char *name,
+               int64_t value);
+
 /*
  * bt_ctf_writer_add_clock: add a clock to the trace.
  *
This page took 0.027282 seconds and 4 git commands to generate.