Implement dynamic struct visitor
[libside.git] / src / tracer.c
index 0c8351c33ba4329227c7d1b67977be1e1d160591..e2beaf5fca7d2a4e665c14f28cda2e85851cb92c 100644 (file)
@@ -64,6 +64,9 @@ void tracer_print_type(const struct side_type_description *type_desc, const stru
                break;
        }
        switch (item->type) {
+       case SIDE_TYPE_BOOL:
+               printf("%s", item->u.side_bool ? "true" : "false");
+               break;
        case SIDE_TYPE_U8:
                printf("%" PRIu8, item->u.side_u8);
                break;
@@ -135,9 +138,8 @@ void tracer_print_type(const struct side_type_description *type_desc, const stru
 static
 void tracer_print_field(const struct side_event_field *item_desc, const struct side_arg_vec *item)
 {
-       printf("(%s: ", item_desc->field_name);
+       printf("%s: ", item_desc->field_name);
        tracer_print_type(&item_desc->side_type, item);
-       printf(")");
 }
 
 static
@@ -242,70 +244,43 @@ void tracer_print_array_fixint(const struct side_type_description *type_desc, co
        enum side_type side_type;
        int i;
 
-       if (elem_type->type != SIDE_TYPE_DYNAMIC) {
-               switch (item->type) {
-               case SIDE_TYPE_ARRAY_U8:
-                       if (elem_type->type != SIDE_TYPE_U8)
-                               goto type_error;
-                       break;
-               case SIDE_TYPE_ARRAY_U16:
-                       if (elem_type->type != SIDE_TYPE_U16)
-                               goto type_error;
-                       break;
-               case SIDE_TYPE_ARRAY_U32:
-                       if (elem_type->type != SIDE_TYPE_U32)
-                               goto type_error;
-                       break;
-               case SIDE_TYPE_ARRAY_U64:
-                       if (elem_type->type != SIDE_TYPE_U64)
-                               goto type_error;
-                       break;
-               case SIDE_TYPE_ARRAY_S8:
-                       if (elem_type->type != SIDE_TYPE_S8)
-                               goto type_error;
-                       break;
-               case SIDE_TYPE_ARRAY_S16:
-                       if (elem_type->type != SIDE_TYPE_S16)
-                               goto type_error;
-                       break;
-               case SIDE_TYPE_ARRAY_S32:
-                       if (elem_type->type != SIDE_TYPE_S32)
-                               goto type_error;
-                       break;
-               case SIDE_TYPE_ARRAY_S64:
-                       if (elem_type->type != SIDE_TYPE_S64)
-                               goto type_error;
-                       break;
-               }
-               side_type = elem_type->type;
-       } else {
-               switch (item->type) {
-               case SIDE_TYPE_ARRAY_U8:
-                       side_type = SIDE_TYPE_U8;
-                       break;
-               case SIDE_TYPE_ARRAY_U16:
-                       side_type = SIDE_TYPE_U16;
-                       break;
-               case SIDE_TYPE_ARRAY_U32:
-                       side_type = SIDE_TYPE_U32;
-                       break;
-               case SIDE_TYPE_ARRAY_U64:
-                       side_type = SIDE_TYPE_U64;
-                       break;
-               case SIDE_TYPE_ARRAY_S8:
-                       side_type = SIDE_TYPE_S8;
-                       break;
-               case SIDE_TYPE_ARRAY_S16:
-                       side_type = SIDE_TYPE_S16;
-                       break;
-               case SIDE_TYPE_ARRAY_S32:
-                       side_type = SIDE_TYPE_S32;
-                       break;
-               case SIDE_TYPE_ARRAY_S64:
-                       side_type = SIDE_TYPE_S64;
-                       break;
-               }
+       switch (item->type) {
+       case SIDE_TYPE_ARRAY_U8:
+               if (elem_type->type != SIDE_TYPE_U8)
+                       goto type_error;
+               break;
+       case SIDE_TYPE_ARRAY_U16:
+               if (elem_type->type != SIDE_TYPE_U16)
+                       goto type_error;
+               break;
+       case SIDE_TYPE_ARRAY_U32:
+               if (elem_type->type != SIDE_TYPE_U32)
+                       goto type_error;
+               break;
+       case SIDE_TYPE_ARRAY_U64:
+               if (elem_type->type != SIDE_TYPE_U64)
+                       goto type_error;
+               break;
+       case SIDE_TYPE_ARRAY_S8:
+               if (elem_type->type != SIDE_TYPE_S8)
+                       goto type_error;
+               break;
+       case SIDE_TYPE_ARRAY_S16:
+               if (elem_type->type != SIDE_TYPE_S16)
+                       goto type_error;
+               break;
+       case SIDE_TYPE_ARRAY_S32:
+               if (elem_type->type != SIDE_TYPE_S32)
+                       goto type_error;
+               break;
+       case SIDE_TYPE_ARRAY_S64:
+               if (elem_type->type != SIDE_TYPE_S64)
+                       goto type_error;
+               break;
+       default:
+               goto type_error;
        }
+       side_type = elem_type->type;
 
        printf("[ ");
        for (i = 0; i < side_sav_len; i++) {
@@ -450,10 +425,10 @@ type_error:
 }
 
 static
-void tracer_print_dynamic_kvpairs(const struct side_arg_dynamic_event_kvpairs *kvpairs)
+void tracer_print_dynamic_struct(const struct side_arg_dynamic_event_struct *dynamic_struct)
 {
-       const struct side_arg_dynamic_event_field *fields = kvpairs->fields;
-       uint32_t len = kvpairs->len;
+       const struct side_arg_dynamic_event_field *fields = dynamic_struct->fields;
+       uint32_t len = dynamic_struct->len;
        int i;
 
        printf("[ ");
@@ -465,10 +440,46 @@ void tracer_print_dynamic_kvpairs(const struct side_arg_dynamic_event_kvpairs *k
        printf(" ]");
 }
 
+struct tracer_dynamic_struct_visitor_priv {
+       int i;
+};
+
+static
+enum side_visitor_status tracer_dynamic_struct_write_elem_cb(
+                       const struct side_tracer_dynamic_struct_visitor_ctx *tracer_ctx,
+                       const struct side_arg_dynamic_event_field *dynamic_field)
+{
+       struct tracer_dynamic_struct_visitor_priv *tracer_priv = tracer_ctx->priv;
+
+       printf("%s", tracer_priv->i++ ? ", " : "");
+       printf("%s:: ", dynamic_field->field_name);
+       tracer_print_dynamic(&dynamic_field->elem);
+       return SIDE_VISITOR_STATUS_OK;
+}
+
 static
-void tracer_print_dynamic_kvpairs_visitor(const struct side_arg_dynamic_vec *item)
+void tracer_print_dynamic_struct_visitor(const struct side_arg_dynamic_vec *item)
 {
-       //TODO
+       enum side_visitor_status status;
+       struct tracer_dynamic_struct_visitor_priv tracer_priv = {
+               .i = 0,
+       };
+       const struct side_tracer_dynamic_struct_visitor_ctx tracer_ctx = {
+               .write_field = tracer_dynamic_struct_write_elem_cb,
+               .priv = &tracer_priv,
+       };
+       void *app_ctx = item->u.side_dynamic_struct_visitor.app_ctx;
+
+       printf("[ ");
+       status = item->u.side_dynamic_struct_visitor.visitor(&tracer_ctx, app_ctx);
+       switch (status) {
+       case SIDE_VISITOR_STATUS_OK:
+               break;
+       case SIDE_VISITOR_STATUS_ERROR:
+               printf("ERROR: Visitor error\n");
+               abort();
+       }
+       printf(" ]");
 }
 
 static
@@ -486,19 +497,57 @@ void tracer_print_dynamic_vla(const struct side_arg_dynamic_vec_vla *vla)
        printf(" ]");
 }
 
+struct tracer_dynamic_vla_visitor_priv {
+       int i;
+};
+
+static
+enum side_visitor_status tracer_dynamic_vla_write_elem_cb(
+                       const struct side_tracer_dynamic_vla_visitor_ctx *tracer_ctx,
+                       const struct side_arg_dynamic_vec *elem)
+{
+       struct tracer_dynamic_vla_visitor_priv *tracer_priv = tracer_ctx->priv;
+
+       printf("%s", tracer_priv->i++ ? ", " : "");
+       tracer_print_dynamic(elem);
+       return SIDE_VISITOR_STATUS_OK;
+}
+
 static
 void tracer_print_dynamic_vla_visitor(const struct side_arg_dynamic_vec *item)
 {
-       //TODO
+       enum side_visitor_status status;
+       struct tracer_dynamic_vla_visitor_priv tracer_priv = {
+               .i = 0,
+       };
+       const struct side_tracer_dynamic_vla_visitor_ctx tracer_ctx = {
+               .write_elem = tracer_dynamic_vla_write_elem_cb,
+               .priv = &tracer_priv,
+       };
+       void *app_ctx = item->u.side_dynamic_vla_visitor.app_ctx;
+
+       printf("[ ");
+       status = item->u.side_dynamic_vla_visitor.visitor(&tracer_ctx, app_ctx);
+       switch (status) {
+       case SIDE_VISITOR_STATUS_OK:
+               break;
+       case SIDE_VISITOR_STATUS_ERROR:
+               printf("ERROR: Visitor error\n");
+               abort();
+       }
+       printf(" ]");
 }
 
 static
 void tracer_print_dynamic(const struct side_arg_dynamic_vec *item)
 {
-       switch (item->type) {
+       switch (item->dynamic_type) {
        case SIDE_DYNAMIC_TYPE_NULL:
                printf("<NULL TYPE>");
                break;
+       case SIDE_DYNAMIC_TYPE_BOOL:
+               printf("%s", item->u.side_bool ? "true" : "false");
+               break;
        case SIDE_DYNAMIC_TYPE_U8:
                printf("%" PRIu8, item->u.side_u8);
                break;
@@ -526,11 +575,11 @@ void tracer_print_dynamic(const struct side_arg_dynamic_vec *item)
        case SIDE_DYNAMIC_TYPE_STRING:
                printf("\"%s\"", item->u.string);
                break;
-       case SIDE_DYNAMIC_TYPE_KVPAIRS:
-               tracer_print_dynamic_kvpairs(item->u.side_dynamic_kvpairs);
+       case SIDE_DYNAMIC_TYPE_STRUCT:
+               tracer_print_dynamic_struct(item->u.side_dynamic_struct);
                break;
-       case SIDE_DYNAMIC_TYPE_KVPAIRS_VISITOR:
-               tracer_print_dynamic_kvpairs_visitor(item);
+       case SIDE_DYNAMIC_TYPE_STRUCT_VISITOR:
+               tracer_print_dynamic_struct_visitor(item);
                break;
        case SIDE_DYNAMIC_TYPE_VLA:
                tracer_print_dynamic_vla(item->u.side_dynamic_vla);
@@ -544,7 +593,10 @@ void tracer_print_dynamic(const struct side_arg_dynamic_vec *item)
        }
 }
 
-void tracer_call(const struct side_event_description *desc, const struct side_arg_vec_description *sav_desc)
+static
+void tracer_print_static_fields(const struct side_event_description *desc,
+               const struct side_arg_vec_description *sav_desc,
+               int *nr_items)
 {
        const struct side_arg_vec *sav = sav_desc->sav;
        uint32_t side_sav_len = sav_desc->len;
@@ -559,5 +611,37 @@ void tracer_call(const struct side_event_description *desc, const struct side_ar
                printf("%s", i ? ", " : "");
                tracer_print_field(&desc->fields[i], &sav[i]);
        }
+       if (nr_items)
+               *nr_items = i;
+}
+
+void tracer_call(const struct side_event_description *desc, const struct side_arg_vec_description *sav_desc)
+{
+       if (side_unlikely(desc->flags & SIDE_EVENT_FLAG_VARIADIC)) {
+               printf("ERROR: unexpected variadic event description\n");
+               abort();
+       }
+       tracer_print_static_fields(desc, sav_desc, NULL);
+       printf("\n");
+}
+
+void tracer_call_variadic(const struct side_event_description *desc,
+       const struct side_arg_vec_description *sav_desc,
+       const struct side_arg_dynamic_event_struct *var_struct)
+{
+       uint32_t var_struct_len = var_struct->len;
+       int nr_fields = 0, i;
+
+       tracer_print_static_fields(desc, sav_desc, &nr_fields);
+
+       if (side_unlikely(!(desc->flags & SIDE_EVENT_FLAG_VARIADIC))) {
+               printf("ERROR: unexpected non-variadic event description\n");
+               abort();
+       }
+       for (i = 0; i < var_struct_len; i++, nr_fields++) {
+               printf("%s", nr_fields ? ", " : "");
+               printf("%s:: ", var_struct->fields[i].field_name);
+               tracer_print_dynamic(&var_struct->fields[i].elem);
+       }
        printf("\n");
 }
This page took 0.027495 seconds and 4 git commands to generate.