Support utf 16/32 attribute keys
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Thu, 10 Nov 2022 18:40:06 +0000 (13:40 -0500)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Thu, 10 Nov 2022 18:40:06 +0000 (13:40 -0500)
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
include/side/trace.h
src/tracer.c

index bfbf880ed0b27a1c505d44359cfc16bf010e265a..5fcbacc6217677b363d05cfd82a4868608196215 100644 (file)
@@ -268,9 +268,15 @@ struct side_attr_value {
        } SIDE_PACKED u;
 };
 
+struct side_type_raw_string {
+       const void *p;                  /* pointer to string */
+       uint8_t unit_size;              /* 1, 2, or 4 bytes */
+       uint8_t byte_order;             /* enum side_type_label_byte_order */
+} SIDE_PACKED;
+
 /* User attributes. */
 struct side_attr {
-       const char *key;
+       const struct side_type_raw_string key;
        const struct side_attr_value value;
 } SIDE_PACKED;
 
@@ -641,7 +647,11 @@ struct side_event_description {
 
 #define side_attr(_key, _value)        \
        { \
-               .key = _key, \
+               .key = { \
+                       .p = (_key), \
+                       .unit_size = sizeof(uint8_t), \
+                       .byte_order = SIDE_TYPE_BYTE_ORDER_HOST, \
+               }, \
                .value = SIDE_PARAM(_value), \
        }
 
index f8482cdd6af9fed66703b3849578b74c35cf4453..b892872608cff0631690e1bc593c546f07da47ea 100644 (file)
@@ -61,8 +61,9 @@ static
 void tracer_print_type(const struct side_type *type_desc, const struct side_arg *item);
 
 static
-void tracer_print_string(const void *p, uint8_t unit_size, enum side_type_label_byte_order byte_order,
-               size_t *strlen_with_null)
+void tracer_convert_string_to_utf8(const void *p, uint8_t unit_size, enum side_type_label_byte_order byte_order,
+               size_t *strlen_with_null,
+               char **output_str)
 {
        size_t ret, inbytesleft = 0, outbytesleft, bufsize;
        const char *str = p, *fromcode;
@@ -71,9 +72,9 @@ void tracer_print_string(const void *p, uint8_t unit_size, enum side_type_label_
 
        switch (unit_size) {
        case 1:
-               printf("\"%s\"", str);
                if (strlen_with_null)
                        *strlen_with_null = strlen(str) + 1;
+               *output_str = (char *) str;
                return;
        case 2:
        {
@@ -157,14 +158,25 @@ void tracer_print_string(const void *p, uint8_t unit_size, enum side_type_label_
                abort();
        }
        (*outbuf++) = '\0';
-       if (strlen_with_null)
-               *strlen_with_null = outbuf - buf;
-       printf("\"%s\"", buf);
-       free(buf);
        if (iconv_close(cd) == -1) {
                perror("iconv_close");
                abort();
        }
+       if (strlen_with_null)
+               *strlen_with_null = outbuf - buf;
+       *output_str = buf;
+}
+
+static
+void tracer_print_string(const void *p, uint8_t unit_size, enum side_type_label_byte_order byte_order,
+               size_t *strlen_with_null)
+{
+       char *output_str = NULL;
+
+       tracer_convert_string_to_utf8(p, unit_size, byte_order, strlen_with_null, &output_str);
+       printf("\"%s\"", output_str);
+       if (output_str != p)
+               free(output_str);
 }
 
 static
@@ -212,8 +224,15 @@ enum tracer_display_base get_attr_display_base(const struct side_attr *_attr, ui
 
        for (i = 0; i < nr_attr; i++) {
                const struct side_attr *attr = &_attr[i];
-
-               if (!strcmp(attr->key, "std.integer.base")) {
+               char *utf8_str = NULL;
+               bool cmp;
+
+               tracer_convert_string_to_utf8(attr->key.p, attr->key.unit_size,
+                       attr->key.byte_order, NULL, &utf8_str);
+               cmp = strcmp(utf8_str, "std.integer.base");
+               if (utf8_str != attr->key.p)
+                       free(utf8_str);
+               if (!cmp) {
                        int64_t val = get_attr_integer_value(attr);
 
                        switch (val) {
@@ -237,7 +256,13 @@ enum tracer_display_base get_attr_display_base(const struct side_attr *_attr, ui
 static
 void tracer_print_attr_type(const char *separator, const struct side_attr *attr)
 {
-       printf("{ key%s \"%s\", value%s ", separator, attr->key, separator);
+       char *utf8_str = NULL;
+
+       tracer_convert_string_to_utf8(attr->key.p, attr->key.unit_size,
+               attr->key.byte_order, NULL, &utf8_str);
+       printf("{ key%s \"%s\", value%s ", separator, utf8_str, separator);
+       if (utf8_str != attr->key.p)
+               free(utf8_str);
        switch (attr->value.type) {
        case SIDE_ATTR_TYPE_BOOL:
                printf("%s", attr->value.u.bool_value ? "true" : "false");
This page took 0.025974 seconds and 4 git commands to generate.