Implement dynamic struct visitor
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Mon, 17 Oct 2022 13:54:29 +0000 (09:54 -0400)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Mon, 17 Oct 2022 13:54:29 +0000 (09:54 -0400)
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
include/side/trace.h
src/test.c
src/tracer.c

index a36599ebcf4e746cd9fe2f8a62c1f870cad53653..eab37a86fb155601794f0a8e26f6a19f7841271b 100644 (file)
@@ -247,7 +247,7 @@ struct side_tracer_visitor_ctx {
 struct side_tracer_dynamic_struct_visitor_ctx {
        enum side_visitor_status (*write_field)(
                        const struct side_tracer_dynamic_struct_visitor_ctx *tracer_ctx,
-                       const struct side_arg_dynamic_event_struct *dynamic_struct);
+                       const struct side_arg_dynamic_event_field *dynamic_field);
        void *priv;             /* Private tracer context. */
 };
 
index 4ea2b5fa667af473d606516fef736f7b4a3d0051..58aadc45f7c063773965c12a0489d9f925c866f7 100644 (file)
@@ -595,6 +595,71 @@ void test_dynamic_vla_with_visitor(void)
        }
 }
 
+static side_define_event(my_provider_event_dynamic_struct_visitor,
+       "myprovider", "mydynamicstructvisitor", SIDE_LOGLEVEL_DEBUG,
+       side_field_list(
+               side_field("dynamic", SIDE_TYPE_DYNAMIC),
+       )
+);
+
+struct struct_visitor_pair {
+       const char *name;
+       uint32_t value;
+};
+
+struct app_dynamic_struct_visitor_ctx {
+       const struct struct_visitor_pair *ptr;
+       uint32_t length;
+};
+
+static
+enum side_visitor_status test_dynamic_struct_visitor(const struct side_tracer_dynamic_struct_visitor_ctx *tracer_ctx, void *_ctx)
+{
+       struct app_dynamic_struct_visitor_ctx *ctx = (struct app_dynamic_struct_visitor_ctx *) _ctx;
+       uint32_t length = ctx->length, i;
+
+       for (i = 0; i < length; i++) {
+               struct side_arg_dynamic_event_field dynamic_field = {
+                       .field_name = ctx->ptr[i].name,
+                       .elem = {
+                               .dynamic_type = SIDE_DYNAMIC_TYPE_U32,
+                               .u = {
+                                       .side_u32 = ctx->ptr[i].value,
+                               },
+                       },
+               };
+               if (tracer_ctx->write_field(tracer_ctx, &dynamic_field) != SIDE_VISITOR_STATUS_OK)
+                       return SIDE_VISITOR_STATUS_ERROR;
+       }
+       return SIDE_VISITOR_STATUS_OK;
+}
+
+static struct struct_visitor_pair testarray_dynamic_struct[] = {
+       { "a", 1, },
+       { "b", 2, },
+       { "c", 3, },
+       { "d", 4, },
+};
+
+static
+void test_dynamic_struct_with_visitor(void)
+{
+       my_provider_event_dynamic_struct_visitor.enabled = 1;
+       side_event_cond(&my_provider_event_dynamic_struct_visitor) {
+               struct app_dynamic_struct_visitor_ctx ctx = {
+                       .ptr = testarray_dynamic_struct,
+                       .length = SIDE_ARRAY_SIZE(testarray_dynamic_struct),
+               };
+               side_event_call(&my_provider_event_dynamic_struct_visitor,
+                       side_arg_list(
+                               side_arg_dynamic(
+                                       side_arg_dynamic_struct_visitor(test_dynamic_struct_visitor, &ctx)
+                               )
+                       )
+               );
+       }
+}
+
 int main()
 {
        test_fields();
@@ -618,5 +683,6 @@ int main()
        test_bool();
        test_dynamic_bool();
        test_dynamic_vla_with_visitor();
+       test_dynamic_struct_with_visitor();
        return 0;
 }
index 1e161c8d89444bc680e8ed6442c5fb684479f466..e2beaf5fca7d2a4e665c14f28cda2e85851cb92c 100644 (file)
@@ -440,10 +440,46 @@ void tracer_print_dynamic_struct(const struct side_arg_dynamic_event_struct *dyn
        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_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
@@ -500,7 +536,6 @@ void tracer_print_dynamic_vla_visitor(const struct side_arg_dynamic_vec *item)
                abort();
        }
        printf(" ]");
-
 }
 
 static
This page took 0.025639 seconds and 4 git commands to generate.