lib/ctf-ir/clock-class.c: serialize `true`/`false` instead of `TRUE`/`FALSE`
[babeltrace.git] / lib / ctf-ir / clock-class.c
index 7390c66395e46297218b9c5099d55a3c72adfead..a7ac560d5a270085a26da6c50ea13c41eb2d11aa 100644 (file)
@@ -29,6 +29,7 @@
 #define BT_LOG_TAG "CLOCK-CLASS"
 #include <babeltrace/lib-logging-internal.h>
 
+#include <babeltrace/compat/uuid-internal.h>
 #include <babeltrace/ctf-ir/clock-class-internal.h>
 #include <babeltrace/ctf-ir/utils.h>
 #include <babeltrace/ref.h>
@@ -91,13 +92,38 @@ end:
        return ret;
 }
 
-struct bt_ctf_clock_class *bt_ctf_clock_class_create(const char *name)
+static
+bool validate_freq(struct bt_ctf_clock_class *clock_class,
+               const char *name, uint64_t freq)
+{
+       bool is_valid = true;
+
+       if (freq == -1ULL || freq == 0) {
+               BT_LOGW("Invalid parameter: frequency is invalid: "
+                       "addr=%p, name=\"%s\", freq=%" PRIu64,
+                       clock_class, name, freq);
+               is_valid = false;
+               goto end;
+       }
+
+end:
+       return is_valid;
+}
+
+struct bt_ctf_clock_class *bt_ctf_clock_class_create(const char *name,
+               uint64_t freq)
 {
        int ret;
-       struct bt_ctf_clock_class *clock_class;
+       struct bt_ctf_clock_class *clock_class = NULL;
 
        BT_LOGD("Creating default clock class object: name=\"%s\"",
                name);
+
+       if (!validate_freq(NULL, name, freq)) {
+               /* validate_freq() logs errors */
+               goto error;
+       }
+
        clock_class = g_new0(struct bt_ctf_clock_class, 1);
        if (!clock_class) {
                BT_LOGE_STR("Failed to allocate one clock class.");
@@ -105,7 +131,7 @@ struct bt_ctf_clock_class *bt_ctf_clock_class_create(const char *name)
        }
 
        clock_class->precision = 1;
-       clock_class->frequency = 1000000000;
+       clock_class->frequency = freq;
        bt_object_init(clock_class, bt_ctf_clock_class_destroy);
 
        if (name) {
@@ -118,13 +144,6 @@ struct bt_ctf_clock_class *bt_ctf_clock_class_create(const char *name)
                }
        }
 
-       ret = bt_uuid_generate(clock_class->uuid);
-       if (ret) {
-               BT_LOGE_STR("Failed to generate a UUID.");
-               goto error;
-       }
-
-       clock_class->uuid_set = 1;
        BT_LOGD("Created clock class object: addr=%p, name=\"%s\"",
                clock_class, name);
        return clock_class;
@@ -217,15 +236,20 @@ int bt_ctf_clock_class_set_frequency(struct bt_ctf_clock_class *clock_class,
 {
        int ret = 0;
 
-       if (!clock_class || freq == -1ULL) {
+       if (!clock_class) {
                BT_LOGW("Invalid parameter: clock class is NULL or frequency is invalid: "
-                       "addr=%p, name=\"%s\", freq=%" PRIu64,
-                       clock_class, bt_ctf_clock_class_get_name(clock_class),
-                       freq);
+                       "addr=%p, name=\"%s\"",
+                       clock_class, bt_ctf_clock_class_get_name(clock_class));
                ret = -1;
                goto end;
        }
 
+       if (!validate_freq(clock_class, bt_ctf_clock_class_get_name(clock_class),
+                       freq)) {
+               /* validate_freq() logs errors */
+               goto end;
+       }
+
        if (clock_class->frozen) {
                BT_LOGW("Invalid parameter: clock class is frozen: addr=%p, name=\"%s\"",
                        clock_class, bt_ctf_clock_class_get_name(clock_class));
@@ -532,12 +556,16 @@ void bt_ctf_clock_class_serialize(struct bt_ctf_clock_class *clock_class,
        g_string_append(context->string, "clock {\n");
        g_string_append_printf(context->string, "\tname = %s;\n",
                clock_class->name->str);
-       g_string_append_printf(context->string,
-               "\tuuid = \"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\";\n",
-               uuid[0], uuid[1], uuid[2], uuid[3],
-               uuid[4], uuid[5], uuid[6], uuid[7],
-               uuid[8], uuid[9], uuid[10], uuid[11],
-               uuid[12], uuid[13], uuid[14], uuid[15]);
+
+       if (clock_class->uuid_set) {
+               g_string_append_printf(context->string,
+                       "\tuuid = \"%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\";\n",
+                       uuid[0], uuid[1], uuid[2], uuid[3],
+                       uuid[4], uuid[5], uuid[6], uuid[7],
+                       uuid[8], uuid[9], uuid[10], uuid[11],
+                       uuid[12], uuid[13], uuid[14], uuid[15]);
+       }
+
        if (clock_class->description) {
                g_string_append_printf(context->string, "\tdescription = \"%s\";\n",
                        clock_class->description->str);
@@ -552,7 +580,7 @@ void bt_ctf_clock_class_serialize(struct bt_ctf_clock_class *clock_class,
        g_string_append_printf(context->string, "\toffset = %" PRIu64 ";\n",
                clock_class->offset);
        g_string_append_printf(context->string, "\tabsolute = %s;\n",
-               clock_class->absolute ? "TRUE" : "FALSE");
+               clock_class->absolute ? "true" : "false");
        g_string_append(context->string, "};\n\n");
 }
 
This page took 0.024507 seconds and 4 git commands to generate.