Sinks own their input iterators
[babeltrace.git] / converter / babeltrace.c
1 /*
2 * babeltrace.c
3 *
4 * Babeltrace Trace Converter
5 *
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
7 *
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
27 */
28
29 #include <babeltrace/babeltrace.h>
30 #include <babeltrace/plugin/component-factory.h>
31 #include <babeltrace/plugin/plugin.h>
32 #include <babeltrace/plugin/component-class.h>
33 #include <babeltrace/plugin/notification/iterator.h>
34 #include <babeltrace/ref.h>
35 #include <babeltrace/values.h>
36 #include <stdlib.h>
37 #include <babeltrace/ctf-ir/metadata.h> /* for clocks */
38 #include <popt.h>
39 #include <string.h>
40 #include <stdio.h>
41
42
43 static char *opt_plugin_path;
44 static char *opt_input_path;
45
46 enum {
47 OPT_NONE = 0,
48 OPT_PLUGIN_PATH,
49 OPT_INPUT_PATH,
50 OPT_VERBOSE,
51 OPT_DEBUG,
52 OPT_HELP,
53 };
54
55 /*
56 * We are _not_ using POPT_ARG_STRING's ability to store directly into
57 * variables, because we want to cast the return to non-const, which is
58 * not possible without using poptGetOptArg explicitly. This helps us
59 * controlling memory allocation correctly without making assumptions
60 * about undocumented behaviors. poptGetOptArg is documented as
61 * requiring the returned const char * to be freed by the caller.
62 */
63 static struct poptOption long_options[] = {
64 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
65 { "plugin-path", 0, POPT_ARG_STRING, NULL, OPT_PLUGIN_PATH, NULL, NULL },
66 { "input-path", 0, POPT_ARG_STRING, NULL, OPT_INPUT_PATH, NULL, NULL },
67 { "verbose", 'v', POPT_ARG_NONE, NULL, OPT_VERBOSE, NULL, NULL },
68 { "debug", 'd', POPT_ARG_NONE, NULL, OPT_DEBUG, NULL, NULL },
69 { NULL, 0, 0, NULL, 0, NULL, NULL },
70 };
71
72 /*
73 * Return 0 if caller should continue, < 0 if caller should return
74 * error, > 0 if caller should exit without reporting error.
75 */
76 static int parse_options(int argc, char **argv)
77 {
78 poptContext pc;
79 int opt, ret = 0;
80
81 if (argc == 1) {
82 return 1; /* exit cleanly */
83 }
84
85 pc = poptGetContext(NULL, argc, (const char **) argv, long_options, 0);
86 poptReadDefaultConfig(pc, 0);
87
88 /* set default */
89 opt_context_field_names = 1;
90 opt_payload_field_names = 1;
91
92 while ((opt = poptGetNextOpt(pc)) != -1) {
93 switch (opt) {
94 case OPT_HELP:
95 ret = 1; /* exit cleanly */
96 goto end;
97 case OPT_PLUGIN_PATH:
98 opt_plugin_path = (char *) poptGetOptArg(pc);
99 if (!opt_plugin_path) {
100 ret = -EINVAL;
101 goto end;
102 }
103 break;
104 case OPT_VERBOSE:
105 babeltrace_verbose = 1;
106 break;
107 case OPT_DEBUG:
108 babeltrace_debug = 1;
109 break;
110 case OPT_INPUT_PATH:
111 opt_input_path = (char *) poptGetOptArg(pc);
112 if (!opt_input_path) {
113 ret = -EINVAL;
114 goto end;
115 }
116 break;
117 default:
118 ret = -EINVAL;
119 goto end;
120 }
121 }
122
123 end:
124 if (pc) {
125 poptFreeContext(pc);
126 }
127 return ret;
128 }
129
130 static
131 const char *component_type_str(enum bt_component_type type)
132 {
133 switch (type) {
134 case BT_COMPONENT_TYPE_SOURCE:
135 return "source";
136 case BT_COMPONENT_TYPE_SINK:
137 return "sink";
138 case BT_COMPONENT_TYPE_FILTER:
139 return "filter";
140 case BT_COMPONENT_TYPE_UNKNOWN:
141 default:
142 return "unknown";
143 }
144 }
145
146 static
147 void print_found_component_classes(struct bt_component_factory *factory)
148 {
149 int count, i;
150
151 if (!babeltrace_verbose) {
152 return;
153 }
154
155 count = bt_component_factory_get_component_class_count(factory);
156 if (count <= 0) {
157 fprintf(stderr, "No component classes found. Please make sure your plug-in search path is set correctly.");
158 return;
159 }
160
161 printf_verbose("Found %d component classes.\n", count);
162 for (i = 0; i < count; i++) {
163 struct bt_component_class *component_class =
164 bt_component_factory_get_component_class_index(
165 factory, i);
166 struct bt_plugin *plugin = bt_component_class_get_plugin(
167 component_class);
168 const char *plugin_name = bt_plugin_get_name(plugin);
169 const char *component_name = bt_component_class_get_name(
170 component_class);
171 const char *path = bt_plugin_get_path(plugin);
172 const char *author = bt_plugin_get_author(plugin);
173 const char *license = bt_plugin_get_license(plugin);
174 const char *plugin_description = bt_plugin_get_description(
175 plugin);
176 const char *component_description =
177 bt_component_class_get_description(
178 component_class);
179 enum bt_component_type type = bt_component_class_get_type(
180 component_class);
181
182 printf_verbose("[%s - %s (%s)]\n", plugin_name, component_name,
183 component_type_str(type));
184 printf_verbose("\tpath: %s\n", path);
185 printf_verbose("\tauthor: %s\n", author);
186 printf_verbose("\tlicense: %s\n", license);
187 printf_verbose("\tplugin description: %s\n",
188 plugin_description ? plugin_description : "None");
189 printf_verbose("\tcomponent description: %s\n",
190 component_description ? component_description : "None");
191
192 bt_put(plugin);
193 bt_put(component_class);
194 }
195 }
196
197 int main(int argc, char **argv)
198 {
199 int ret;
200 enum bt_value_status value_status;
201 struct bt_component_factory *component_factory = NULL;
202 struct bt_component_class *source_class = NULL, *sink_class = NULL;
203 struct bt_component *source = NULL, *sink = NULL;
204 struct bt_value *source_params = NULL, *sink_params = NULL;
205 struct bt_notification_iterator *it = NULL;
206 enum bt_component_status sink_status;
207
208 ret = parse_options(argc, argv);
209 if (ret < 0) {
210 fprintf(stderr, "Error parsing options.\n\n");
211 exit(EXIT_FAILURE);
212 } else if (ret > 0) {
213 exit(EXIT_SUCCESS);
214 }
215
216 source_params = bt_value_map_create();
217 if (!source_params) {
218 fprintf(stderr, "Failed to create source parameters map, aborting...\n");
219 ret = -1;
220 goto end;
221 }
222
223 value_status = bt_value_map_insert_string(source_params, "path",
224 opt_input_path);
225 if (value_status != BT_VALUE_STATUS_OK) {
226 ret = -1;
227 goto end;
228 }
229
230 printf_verbose("Verbose mode active.\n");
231 printf_debug("Debug mode active.\n");
232
233 if (!opt_plugin_path) {
234 fprintf(stderr, "No plugin path specified, aborting...\n");
235 ret = -1;
236 goto end;
237 }
238 printf_verbose("Looking-up plugins at %s\n",
239 opt_plugin_path ? opt_plugin_path : "Invalid");
240 component_factory = bt_component_factory_create();
241 if (!component_factory) {
242 fprintf(stderr, "Failed to create component factory.\n");
243 ret = -1;
244 goto end;
245 }
246
247 ret = bt_component_factory_load_recursive(component_factory, opt_plugin_path);
248 if (ret) {
249 fprintf(stderr, "Failed to load plugins.\n");
250 goto end;
251 }
252
253 print_found_component_classes(component_factory);
254
255 source_class = bt_component_factory_get_component_class(
256 component_factory, "ctf", BT_COMPONENT_TYPE_SOURCE,
257 "fs");
258 if (!source_class) {
259 fprintf(stderr, "Could not find ctf-fs source component class. Aborting...\n");
260 ret = -1;
261 goto end;
262 }
263
264 sink_class = bt_component_factory_get_component_class(component_factory,
265 "text", BT_COMPONENT_TYPE_SINK, "text");
266 if (!sink_class) {
267 fprintf(stderr, "Could not find text sink component class. Aborting...\n");
268 ret = -1;
269 goto end;
270 }
271
272 source = bt_component_create(source_class, "ctf-fs", source_params);
273 if (!source) {
274 fprintf(stderr, "Failed to instantiate source component. Aborting...\n");
275 ret = -1;
276 goto end;
277 }
278
279 sink = bt_component_create(sink_class, "bt_text_output", sink_params);
280 if (!sink) {
281 fprintf(stderr, "Failed to instantiate output component. Aborting...\n");
282 ret = -1;
283 goto end;
284 }
285
286 it = bt_component_source_create_iterator(source);
287 if (!it) {
288 fprintf(stderr, "Failed to instantiate source iterator. Aborting...\n");
289 ret = -1;
290 goto end;
291 }
292
293 sink_status = bt_component_sink_add_iterator(sink, it);
294 if (sink_status != BT_COMPONENT_STATUS_OK) {
295 ret = -1;
296 goto end;
297 }
298
299 while (true) {
300 sink_status = bt_component_sink_consume(sink);
301
302 switch (sink_status) {
303 case BT_COMPONENT_STATUS_AGAIN:
304 /* Wait for an arbitraty 500 ms. */
305 usleep(500000);
306 break;
307 case BT_COMPONENT_STATUS_OK:
308 break;
309 case BT_COMPONENT_STATUS_END:
310 goto end;
311 default:
312 fprintf(stderr, "Sink component returned an error, aborting...\n");
313 ret = -1;
314 goto end;
315 }
316 }
317 /* teardown and exit */
318 end:
319 BT_PUT(component_factory);
320 BT_PUT(sink_class);
321 BT_PUT(source_class);
322 BT_PUT(source);
323 BT_PUT(sink);
324 BT_PUT(source_params);
325 BT_PUT(sink_params);
326 BT_PUT(it);
327 return ret ? 1 : 0;
328 }
This page took 0.034859 seconds and 4 git commands to generate.