Logging: standardize logging tags
[babeltrace.git] / src / lib / graph / component-class-sink-colander.c
CommitLineData
361ac4a6 1/*
f2b0325d 2 * Copyright 2017-2018 Philippe Proulx <pproulx@efficios.com>
361ac4a6
PP
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
b03487ab 23#define BT_LOG_TAG "LIB/COLANDER"
57952005 24#include "lib/lib-logging.h"
361ac4a6 25
57952005
MJ
26#include "common/assert.h"
27#include "lib/assert-pre.h"
28#include "lib/object.h"
71c5da58
MJ
29#include <babeltrace2/graph/component-class-sink.h>
30#include <babeltrace2/graph/self-component-sink.h>
31#include <babeltrace2/graph/self-component-port.h>
32#include <babeltrace2/graph/self-component-port-input-message-iterator.h>
33#include <babeltrace2/graph/self-component.h>
361ac4a6 34#include <glib.h>
361ac4a6 35
57952005
MJ
36#include "component-class-sink-colander.h"
37
361ac4a6 38static
7b53201c 39struct bt_component_class_sink *colander_comp_cls;
361ac4a6 40
361ac4a6 41static
834e9996
PP
42enum bt_self_component_status colander_init(
43 struct bt_self_component_sink *self_comp,
ce141536 44 const struct bt_value *params, void *init_method_data)
361ac4a6 45{
834e9996 46 enum bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK;
15a52f66 47 struct bt_component_class_sink_colander_priv_data *colander_data = NULL;
c3ac0193
PP
48 struct bt_component_class_sink_colander_data *user_provided_data =
49 init_method_data;
361ac4a6 50
a684a357 51 BT_ASSERT(init_method_data);
15a52f66
PP
52 colander_data = g_new0(
53 struct bt_component_class_sink_colander_priv_data, 1);
361ac4a6
PP
54 if (!colander_data) {
55 BT_LOGE_STR("Failed to allocate colander data.");
834e9996 56 status = BT_SELF_COMPONENT_STATUS_NOMEM;
361ac4a6
PP
57 goto end;
58 }
59
b09a5592 60 colander_data->msgs = user_provided_data->msgs;
3fd7b79d 61 colander_data->count_addr = user_provided_data->count_addr;
834e9996
PP
62 status = bt_self_component_sink_add_input_port(self_comp, "in",
63 NULL, NULL);
64 if (status != BT_SELF_COMPONENT_STATUS_OK) {
361ac4a6
PP
65 BT_LOGE_STR("Cannot add input port.");
66 goto end;
67 }
68
834e9996 69 bt_self_component_set_data(
bb61965b 70 bt_self_component_sink_as_self_component(self_comp),
834e9996 71 colander_data);
361ac4a6
PP
72
73end:
74 return status;
75}
76
77static
834e9996 78void colander_finalize(struct bt_self_component_sink *self_comp)
361ac4a6 79{
15a52f66 80 struct bt_component_class_sink_colander_priv_data *colander_data =
834e9996 81 bt_self_component_get_data(
bb61965b 82 bt_self_component_sink_as_self_component(self_comp));
361ac4a6
PP
83
84 if (!colander_data) {
85 return;
86 }
87
b09a5592 88 BT_OBJECT_PUT_REF_AND_RESET(colander_data->msg_iter);
361ac4a6
PP
89 g_free(colander_data);
90}
91
92static
ddcfe154
SM
93enum bt_self_component_status colander_graph_is_configured(
94 bt_self_component_sink *self_comp)
361ac4a6 95{
834e9996 96 enum bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK;
15a52f66 97 struct bt_component_class_sink_colander_priv_data *colander_data =
834e9996 98 bt_self_component_get_data(
bb61965b 99 bt_self_component_sink_as_self_component(self_comp));
361ac4a6 100
ddcfe154
SM
101 struct bt_self_component_port_input *self_port =
102 bt_self_component_sink_borrow_input_port_by_name(self_comp, "in");
103 BT_ASSERT(self_port);
104
8b45963b 105 BT_ASSERT(colander_data);
b09a5592
PP
106 BT_OBJECT_PUT_REF_AND_RESET(colander_data->msg_iter);
107 colander_data->msg_iter =
108 bt_self_component_port_input_message_iterator_create(
834e9996 109 self_port);
b09a5592
PP
110 if (!colander_data->msg_iter) {
111 BT_LIB_LOGE("Cannot create message iterator on "
834e9996
PP
112 "self component input port: %![port-]+p",
113 self_port);
114 status = BT_SELF_COMPONENT_STATUS_NOMEM;
361ac4a6
PP
115 goto end;
116 }
117
118end:
634f394c 119 return status;
361ac4a6
PP
120}
121
122static
834e9996
PP
123enum bt_self_component_status colander_consume(
124 struct bt_self_component_sink *self_comp)
361ac4a6 125{
834e9996 126 enum bt_self_component_status status = BT_SELF_COMPONENT_STATUS_OK;
b09a5592 127 enum bt_message_iterator_status msg_iter_status;
15a52f66 128 struct bt_component_class_sink_colander_priv_data *colander_data =
834e9996 129 bt_self_component_get_data(
bb61965b 130 bt_self_component_sink_as_self_component(self_comp));
b09a5592 131 bt_message_array_const msgs;
361ac4a6 132
8b45963b 133 BT_ASSERT(colander_data);
a684a357 134 BT_ASSERT(colander_data->msg_iter);
b09a5592
PP
135 msg_iter_status =
136 bt_self_component_port_input_message_iterator_next(
137 colander_data->msg_iter, &msgs,
834e9996 138 colander_data->count_addr);
b09a5592 139 switch (msg_iter_status) {
b09a5592 140 case BT_MESSAGE_ITERATOR_STATUS_AGAIN:
834e9996 141 status = BT_SELF_COMPONENT_STATUS_AGAIN;
361ac4a6 142 goto end;
b09a5592 143 case BT_MESSAGE_ITERATOR_STATUS_END:
834e9996 144 status = BT_SELF_COMPONENT_STATUS_END;
361ac4a6 145 goto end;
b09a5592
PP
146 case BT_MESSAGE_ITERATOR_STATUS_OK:
147 /* Move messages to user (count already set) */
148 memcpy(colander_data->msgs, msgs,
149 sizeof(*msgs) * *colander_data->count_addr);
361ac4a6
PP
150 break;
151 default:
834e9996 152 status = BT_SELF_COMPONENT_STATUS_ERROR;
361ac4a6
PP
153 goto end;
154 }
155
361ac4a6 156end:
361ac4a6
PP
157 return status;
158}
159
834e9996 160struct bt_component_class_sink *bt_component_class_sink_colander_get(void)
361ac4a6
PP
161{
162 if (colander_comp_cls) {
163 goto end;
164 }
165
7b53201c
PP
166 colander_comp_cls = bt_component_class_sink_create("colander",
167 colander_consume);
361ac4a6
PP
168 if (!colander_comp_cls) {
169 BT_LOGE_STR("Cannot create sink colander component class.");
170 goto end;
171 }
172
7b53201c 173 (void) bt_component_class_sink_set_init_method(
834e9996 174 colander_comp_cls, colander_init);
7b53201c 175 (void) bt_component_class_sink_set_finalize_method(
834e9996 176 colander_comp_cls, colander_finalize);
ddcfe154
SM
177 (void) bt_component_class_sink_set_graph_is_configured_method(
178 colander_comp_cls, colander_graph_is_configured);
361ac4a6
PP
179
180end:
4b70020d 181 bt_object_get_ref(colander_comp_cls);
88cbd22e 182 return colander_comp_cls;
361ac4a6
PP
183}
184
185__attribute__((destructor)) static
186void put_colander(void) {
8138bfe1 187 BT_OBJECT_PUT_REF_AND_RESET(colander_comp_cls);
361ac4a6 188}
This page took 0.041104 seconds and 4 git commands to generate.