Accomodate component destructor API changes in text plugin
[babeltrace.git] / converter / babeltrace.c
CommitLineData
34ac0e6c
MD
1/*
2 * babeltrace.c
3 *
4 * Babeltrace Trace Converter
5 *
64fa3fec
MD
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
7 *
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
34ac0e6c
MD
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.
c462e188
MD
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.
34ac0e6c 27 */
4c8bfb7e 28
95d36295 29#include <babeltrace/babeltrace.h>
33bceaf8 30#include <babeltrace/plugin/component-factory.h>
7c7c0433
JG
31#include <babeltrace/plugin/plugin.h>
32#include <babeltrace/plugin/component-class.h>
2e339de1
JG
33#include <babeltrace/ref.h>
34#include <babeltrace/values.h>
34ac0e6c 35#include <stdlib.h>
a44bc4c9 36#include <babeltrace/ctf-ir/metadata.h> /* for clocks */
7f26a816
PP
37#include <popt.h>
38#include <string.h>
39#include <stdio.h>
a44bc4c9 40
34ac0e6c 41
33bceaf8 42static char *opt_plugin_path;
34ac0e6c 43
34ac0e6c
MD
44enum {
45 OPT_NONE = 0,
33bceaf8 46 OPT_PLUGIN_PATH,
34ac0e6c
MD
47 OPT_VERBOSE,
48 OPT_DEBUG,
7f26a816 49 OPT_HELP,
34ac0e6c
MD
50};
51
9fe26ec7 52/*
33bceaf8 53 * We are _not_ using POPT_ARG_STRING's ability to store directly into
9fe26ec7 54 * variables, because we want to cast the return to non-const, which is
41075e78
MD
55 * not possible without using poptGetOptArg explicitly. This helps us
56 * controlling memory allocation correctly without making assumptions
57 * about undocumented behaviors. poptGetOptArg is documented as
58 * requiring the returned const char * to be freed by the caller.
9fe26ec7 59 */
34ac0e6c
MD
60static struct poptOption long_options[] = {
61 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
33bceaf8 62 { "plugin-path", 0, POPT_ARG_STRING, NULL, OPT_PLUGIN_PATH, NULL, NULL },
34ac0e6c
MD
63 { "verbose", 'v', POPT_ARG_NONE, NULL, OPT_VERBOSE, NULL, NULL },
64 { "debug", 'd', POPT_ARG_NONE, NULL, OPT_DEBUG, NULL, NULL },
65 { NULL, 0, 0, NULL, 0, NULL, NULL },
66};
67
34ac0e6c
MD
68/*
69 * Return 0 if caller should continue, < 0 if caller should return
70 * error, > 0 if caller should exit without reporting error.
71 */
bbefb8dd 72static int parse_options(int argc, char **argv)
34ac0e6c
MD
73{
74 poptContext pc;
75 int opt, ret = 0;
76
0f980a35 77 if (argc == 1) {
0f980a35
MD
78 return 1; /* exit cleanly */
79 }
80
bbefb8dd 81 pc = poptGetContext(NULL, argc, (const char **) argv, long_options, 0);
34ac0e6c
MD
82 poptReadDefaultConfig(pc, 0);
83
cba1661c 84 /* set default */
e505794f 85 opt_context_field_names = 1;
cba1661c
MD
86 opt_payload_field_names = 1;
87
34ac0e6c
MD
88 while ((opt = poptGetNextOpt(pc)) != -1) {
89 switch (opt) {
90 case OPT_HELP:
34ac0e6c
MD
91 ret = 1; /* exit cleanly */
92 goto end;
33bceaf8
JG
93 case OPT_PLUGIN_PATH:
94 opt_plugin_path = (char *) poptGetOptArg(pc);
95 if (!opt_plugin_path) {
96 ret = -EINVAL;
97 goto end;
98 } ;
99 break;
34ac0e6c
MD
100 case OPT_VERBOSE:
101 babeltrace_verbose = 1;
102 break;
103 case OPT_DEBUG:
104 babeltrace_debug = 1;
105 break;
106 default:
107 ret = -EINVAL;
108 goto end;
109 }
110 }
111
34ac0e6c
MD
112end:
113 if (pc) {
114 poptFreeContext(pc);
115 }
116 return ret;
117}
118
7c7c0433
JG
119static
120const char *component_type_str(enum bt_component_type type)
121{
122 switch (type) {
123 case BT_COMPONENT_TYPE_SOURCE:
124 return "source";
125 case BT_COMPONENT_TYPE_SINK:
126 return "sink";
127 case BT_COMPONENT_TYPE_FILTER:
128 return "filter";
129 case BT_COMPONENT_TYPE_UNKNOWN:
130 default:
131 return "unknown";
132 }
133}
134
135static
136void print_detected_component_classes(struct bt_component_factory *factory)
137{
138 int count, i;
139
140 if (!babeltrace_verbose) {
141 return;
142 }
143
144 count = bt_component_factory_get_component_class_count(factory);
145 if (count <= 0) {
146 fprintf(stderr, "No component classes found. Please make sure your plug-in search path is set correctly.");
147 return;
148 }
149
150 printf_verbose("Found %d component classes.\n", count);
151 for (i = 0; i < count; i++) {
152 struct bt_component_class *component_class =
153 bt_component_factory_get_component_class_index(
154 factory, i);
155 struct bt_plugin *plugin = bt_component_class_get_plugin(
156 component_class);
157 const char *plugin_name = bt_plugin_get_name(plugin);
158 const char *component_name = bt_component_class_get_name(
159 component_class);
160 const char *path = bt_plugin_get_path(plugin);
161 const char *author = bt_plugin_get_author(plugin);
162 const char *license = bt_plugin_get_license(plugin);
163 const char *plugin_description = bt_plugin_get_description(
164 plugin);
165 const char *component_description =
166 bt_component_class_get_description(
167 component_class);
168 enum bt_component_type type = bt_component_class_get_type(
169 component_class);
170
171 printf_verbose("[%s - %s (%s)]\n", plugin_name, component_name,
172 component_type_str(type));
173 printf_verbose("\tpath: %s\n", path);
174 printf_verbose("\tauthor: %s\n", author);
175 printf_verbose("\tlicense: %s\n", license);
176 printf_verbose("\tplugin description: %s\n",
177 plugin_description ? plugin_description : "None");
178 printf_verbose("\tcomponent description: %s\n",
179 component_description ? component_description : "None");
0cc9e945
JG
180
181 bt_put(plugin);
182 bt_put(component_class);
7c7c0433
JG
183 }
184}
185
186static
187void test_sink_notifications(struct bt_component *sink)
7e3e3582 188{
7c7c0433 189 return;
7e3e3582
JG
190}
191
bbefb8dd 192int main(int argc, char **argv)
34ac0e6c 193{
7f26a816 194 int ret;
7c7c0433
JG
195 struct bt_component_factory *component_factory = NULL;
196 struct bt_component_class *source_class = NULL;
197 struct bt_component_class *sink_class = NULL;
198 struct bt_component *source = NULL, *sink = NULL;
199 struct bt_value *source_params = NULL, *sink_params = NULL;
34ac0e6c
MD
200
201 ret = parse_options(argc, argv);
202 if (ret < 0) {
3394d22e 203 fprintf(stderr, "Error parsing options.\n\n");
34ac0e6c
MD
204 exit(EXIT_FAILURE);
205 } else if (ret > 0) {
206 exit(EXIT_SUCCESS);
207 }
7f26a816 208
34ac0e6c
MD
209 printf_verbose("Verbose mode active.\n");
210 printf_debug("Debug mode active.\n");
211
33bceaf8
JG
212 if (!opt_plugin_path) {
213 fprintf(stderr, "No plugin path specified, aborting...\n");
214 ret = -1;
215 goto end;
216 }
7c7c0433 217 printf_verbose("Looking-up plugins at %s\n",
33bceaf8
JG
218 opt_plugin_path ? opt_plugin_path : "Invalid");
219 component_factory = bt_component_factory_create();
220 if (!component_factory) {
221 fprintf(stderr, "Failed to create component factory.\n");
222 ret = -1;
223 goto end;
224 }
225
01c34eb9 226 ret = bt_component_factory_load_recursive(component_factory, opt_plugin_path);
33bceaf8
JG
227 if (ret) {
228 fprintf(stderr, "Failed to load plugins.\n");
229 goto end;
230 }
231
7c7c0433
JG
232 print_detected_component_classes(component_factory);
233
234 sink_class = bt_component_factory_get_component_class(component_factory,
235 NULL, BT_COMPONENT_TYPE_SINK, "text");
236 if (!sink_class) {
237 fprintf(stderr, "Could not find text output component class. Aborting...\n");
238 ret = -1;
239 goto end;
240 }
241
242 sink = bt_component_create(sink_class, "bt_text_output", sink_params);
243 if (!sink) {
244 fprintf(stderr, "Failed to instanciate text output. Aborting...\n");
245 ret = -1;
2e339de1
JG
246 goto end;
247 }
248
7c7c0433 249 test_sink_notifications(sink);
7c7c0433 250
11e1d048
MD
251 /* teardown and exit */
252end:
2e339de1 253 BT_PUT(component_factory);
7c7c0433
JG
254 BT_PUT(sink_class);
255 BT_PUT(source_class);
256 BT_PUT(source);
257 BT_PUT(sink);
258 BT_PUT(source_params);
259 BT_PUT(sink_params);
7f26a816 260 return ret ? 1 : 0;
4c8bfb7e 261}
This page took 0.050348 seconds and 4 git commands to generate.