Clean-up: modernize pretty_xml.cpp
[lttng-tools.git] / src / common / uuid.cpp
index 23672a96fe131f755df81eb357cc7c865619a0da..4c64c08acedafe03de2e3d32b168146b97271134 100644 (file)
@@ -6,7 +6,13 @@
  *
  */
 
+#include "uuid.hpp"
+
 #include <common/compat/string.hpp>
+#include <common/error.hpp>
+#include <common/format.hpp>
+#include <common/random.hpp>
+
 #include <stddef.h>
 #include <stdint.h>
 #include <stdio.h>
 #include <string.h>
 #include <time.h>
 
-#include "uuid.hpp"
+namespace {
+const lttng_uuid nil_uuid = {};
+bool lttng_uuid_is_init;
+} /* namespace */
 
-static const lttng_uuid nil_uuid = { 0 };
-static bool lttng_uuid_is_init;
+void lttng_uuid_to_str(const lttng_uuid& uuid, char *uuid_str)
+{
+       snprintf(uuid_str, LTTNG_UUID_STR_LEN, LTTNG_UUID_FMT, LTTNG_UUID_FMT_VALUES(uuid));
+}
 
-void lttng_uuid_to_str(const lttng_uuid uuid, char *uuid_str)
+std::string lttng::utils::uuid_to_str(const lttng_uuid& uuid)
 {
-       sprintf(uuid_str, LTTNG_UUID_FMT, LTTNG_UUID_FMT_VALUES(uuid));
+       std::string uuid_str(LTTNG_UUID_STR_LEN, '\0');
+
+       ::lttng_uuid_to_str(uuid, &uuid_str[0]);
+
+       /* Don't include '\0' in the C++ string. */
+       uuid_str.resize(uuid_str.size() - 1);
+
+       return uuid_str;
 }
 
-int lttng_uuid_from_str(const char *str_in, lttng_uuid uuid_out)
+int lttng_uuid_from_str(const char *str_in, lttng_uuid& uuid_out)
 {
        int ret = 0;
-       lttng_uuid uuid_scan;
+       lttng_uuid uuid_scan = {};
 
-       if ((str_in == NULL) || (uuid_out == NULL)) {
+       if (str_in == nullptr) {
                ret = -1;
                goto end;
        }
@@ -40,53 +58,38 @@ int lttng_uuid_from_str(const char *str_in, lttng_uuid uuid_out)
        }
 
        /* Scan to a temporary location in case of a partial match. */
-       if (sscanf(str_in, LTTNG_UUID_FMT, LTTNG_UUID_SCAN_VALUES(uuid_scan)) !=
-                       LTTNG_UUID_LEN) {
+       if (sscanf(str_in, LTTNG_UUID_FMT, LTTNG_UUID_SCAN_VALUES(uuid_scan)) != LTTNG_UUID_LEN) {
                ret = -1;
+               goto end;
        }
 
-       lttng_uuid_copy(uuid_out, uuid_scan);
+       uuid_out = uuid_scan;
 end:
        return ret;
 }
 
-bool lttng_uuid_is_equal(const lttng_uuid a, const lttng_uuid b)
+bool lttng_uuid_is_nil(const lttng_uuid& uuid)
 {
-       return memcmp(a, b, LTTNG_UUID_LEN) == 0;
-}
-
-bool lttng_uuid_is_nil(const lttng_uuid uuid)
-{
-       return memcmp(nil_uuid, uuid, sizeof(lttng_uuid)) == 0;
-}
-
-void lttng_uuid_copy(lttng_uuid dst, const lttng_uuid src)
-{
-       memcpy(dst, src, LTTNG_UUID_LEN);
+       return uuid == nil_uuid;
 }
 
 /*
  * Generate a random UUID according to RFC4122, section 4.4.
  */
-int lttng_uuid_generate(lttng_uuid uuid_out)
+int lttng_uuid_generate(lttng_uuid& uuid_out)
 {
        int i, ret = 0;
 
-       LTTNG_ASSERT(uuid_out);
-
        if (!lttng_uuid_is_init) {
-               /*
-                * We don't need cryptographic quality randomness to
-                * generate UUIDs, seed rand with the epoch.
-                */
-               const time_t epoch = time(NULL);
-
-               if (epoch == (time_t) -1) {
+               try {
+                       srand(lttng::random::produce_best_effort_random_seed());
+               } catch (const std::exception& e) {
+                       ERR("Failed to initialize random seed during generation of UUID: %s",
+                           e.what());
                        ret = -1;
                        goto end;
                }
 
-               srand(epoch);
                lttng_uuid_is_init = true;
        }
 
This page took 0.023767 seconds and 4 git commands to generate.