Recursively search the plugin path
[babeltrace.git] / converter / babeltrace.c
index 63ade113c81f719a1cb4b1a5b0178a5567fb826b..035c4a250badf04c0fe683671dd16cb35afb3d07 100644 (file)
@@ -3,7 +3,9 @@
  *
  * Babeltrace Trace Converter
  *
- * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
+ * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
+ *
+ * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to deal
  *
  * The above copyright notice and this permission notice shall be included in
  * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
  */
 
 #include <babeltrace/babeltrace.h>
-#include <babeltrace/format.h>
-#include <popt.h>
-#include <errno.h>
+#include <babeltrace/plugin/component-factory.h>
+#include <babeltrace/plugin/plugin.h>
+#include <babeltrace/plugin/component-class.h>
+#include <babeltrace/ref.h>
+#include <babeltrace/values.h>
 #include <stdlib.h>
+#include <babeltrace/ctf-ir/metadata.h>        /* for clocks */
+#include <popt.h>
+#include <string.h>
+#include <stdio.h>
 
-static const char *opt_input_format;
-static const char *opt_output_format;
-
-static const char *opt_input_path;
-static const char *opt_output_path;
 
-int babeltrace_verbose, babeltrace_debug;
+static char *opt_plugin_path;
 
 enum {
        OPT_NONE = 0,
-       OPT_HELP,
-       OPT_LIST,
+       OPT_PLUGIN_PATH,
        OPT_VERBOSE,
        OPT_DEBUG,
+       OPT_HELP,
 };
 
+/*
+ * We are _not_ using POPT_ARG_STRING's ability to store directly into
+ * variables, because we want to cast the return to non-const, which is
+ * not possible without using poptGetOptArg explicitly. This helps us
+ * controlling memory allocation correctly without making assumptions
+ * about undocumented behaviors. poptGetOptArg is documented as
+ * requiring the returned const char * to be freed by the caller.
+ */
 static struct poptOption long_options[] = {
        /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
-       { "input-format", 'i', POPT_ARG_STRING, &opt_input_format, OPT_NONE, NULL, NULL },
-       { "output-format", 'o', POPT_ARG_STRING, &opt_output_format, OPT_NONE, NULL, NULL },
-       { "help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL },
-       { "list", 'l', POPT_ARG_NONE, NULL, OPT_LIST, NULL, NULL },
+       { "plugin-path", 0, POPT_ARG_STRING, NULL, OPT_PLUGIN_PATH, NULL, NULL },
        { "verbose", 'v', POPT_ARG_NONE, NULL, OPT_VERBOSE, NULL, NULL },
        { "debug", 'd', POPT_ARG_NONE, NULL, OPT_DEBUG, NULL, NULL },
        { NULL, 0, 0, NULL, 0, NULL, NULL },
 };
 
-static void list_formats(FILE *fp)
-{
-       fprintf(fp, "\n");
-       bt_fprintf_format_list(fp);
-}
-
-static void usage(FILE *fp)
-{
-       fprintf(fp, "Babeltrace %u.%u\n\n", BABELTRACE_VERSION_MAJOR,
-               BABELTRACE_VERSION_MINOR);
-       fprintf(fp, "usage : babeltrace [OPTIONS] INPUT OUTPUT\n");
-       fprintf(fp, "\n");
-       fprintf(fp, "  INPUT               Input trace path\n");
-       fprintf(fp, "  OUTPUT              Output trace path\n");
-       fprintf(fp, "\n");
-       fprintf(fp, "  -i, --input-format  Input trace path\n");
-       fprintf(fp, "  -o, --output-format Input trace path\n");
-       fprintf(fp, "\n");
-       fprintf(fp, "  -h, --help          This help message\n");
-       fprintf(fp, "  -l, --list          List available formats\n");
-       fprintf(fp, "  -v, --verbose       Verbose mode\n");
-       fprintf(fp, "  -d, --debug         Debug mode\n");
-       list_formats(fp);
-       fprintf(fp, "\n");
-}
-
 /*
  * Return 0 if caller should continue, < 0 if caller should return
  * error, > 0 if caller should exit without reporting error.
  */
-static int parse_options(int argc, const char **argv)
+static int parse_options(int argc, char **argv)
 {
        poptContext pc;
        int opt, ret = 0;
 
-       pc = poptGetContext(NULL, argc, argv, long_options, 0);
+       if (argc == 1) {
+               return 1;       /* exit cleanly */
+       }
+
+       pc = poptGetContext(NULL, argc, (const char **) argv, long_options, 0);
        poptReadDefaultConfig(pc, 0);
 
+       /* set default */
+       opt_context_field_names = 1;
+       opt_payload_field_names = 1;
+
        while ((opt = poptGetNextOpt(pc)) != -1) {
                switch (opt) {
                case OPT_HELP:
-                       usage(stdout);
                        ret = 1;        /* exit cleanly */
                        goto end;
-               case OPT_LIST:
-                       list_formats(stdout);
-                       ret = 1;
-                       goto end;
+               case OPT_PLUGIN_PATH:
+                       opt_plugin_path = (char *) poptGetOptArg(pc);
+                       if (!opt_plugin_path) {
+                               ret = -EINVAL;
+                               goto end;
+                       }                       ;
+                       break;
                case OPT_VERBOSE:
                        babeltrace_verbose = 1;
                        break;
@@ -109,16 +109,6 @@ static int parse_options(int argc, const char **argv)
                }
        }
 
-       opt_input_path = poptGetArg(pc);
-       if (!opt_input_path) {
-               ret = -EINVAL;
-               goto end;
-       }
-       opt_output_path = poptGetArg(pc);
-       if (!opt_output_path) {
-               ret = -EINVAL;
-               goto end;
-       }
 end:
        if (pc) {
                poptFreeContext(pc);
@@ -126,27 +116,146 @@ end:
        return ret;
 }
 
-int main(int argc, const char **argv)
+static
+const char *component_type_str(enum bt_component_type type)
+{
+       switch (type) {
+       case BT_COMPONENT_TYPE_SOURCE:
+               return "source";
+       case BT_COMPONENT_TYPE_SINK:
+               return "sink";
+       case BT_COMPONENT_TYPE_FILTER:
+               return "filter";
+       case BT_COMPONENT_TYPE_UNKNOWN:
+       default:
+               return "unknown";
+       }
+}
+
+static
+void print_detected_component_classes(struct bt_component_factory *factory)
+{
+       int count, i;
+
+       if (!babeltrace_verbose) {
+               return;
+       }
+
+       count = bt_component_factory_get_component_class_count(factory);
+       if (count <= 0) {
+               fprintf(stderr, "No component classes found. Please make sure your plug-in search path is set correctly.");
+               return;
+       }
+
+       printf_verbose("Found %d component classes.\n", count);
+       for (i = 0; i < count; i++) {
+               struct bt_component_class *component_class =
+                               bt_component_factory_get_component_class_index(
+                               factory, i);
+               struct bt_plugin *plugin = bt_component_class_get_plugin(
+                               component_class);
+               const char *plugin_name = bt_plugin_get_name(plugin);
+               const char *component_name = bt_component_class_get_name(
+                               component_class);
+               const char *path = bt_plugin_get_path(plugin);
+               const char *author = bt_plugin_get_author(plugin);
+               const char *license = bt_plugin_get_license(plugin);
+               const char *plugin_description = bt_plugin_get_description(
+                               plugin);
+               const char *component_description =
+                               bt_component_class_get_description(
+                                       component_class);
+               enum bt_component_type type = bt_component_class_get_type(
+                               component_class);
+
+               printf_verbose("[%s - %s (%s)]\n", plugin_name, component_name,
+                              component_type_str(type));
+               printf_verbose("\tpath: %s\n", path);
+               printf_verbose("\tauthor: %s\n", author);
+               printf_verbose("\tlicense: %s\n", license);
+               printf_verbose("\tplugin description: %s\n",
+                               plugin_description ? plugin_description : "None");
+               printf_verbose("\tcomponent description: %s\n",
+                               component_description ? component_description : "None");
+
+               bt_put(plugin);
+               bt_put(component_class);
+       }
+}
+
+static
+void test_sink_notifications(struct bt_component *sink)
+{
+       return;
+}
+
+int main(int argc, char **argv)
 {
        int ret;
+       struct bt_component_factory *component_factory = NULL;
+       struct bt_component_class *source_class = NULL;
+       struct bt_component_class *sink_class = NULL;
+       struct bt_component *source = NULL, *sink = NULL;
+       struct bt_value *source_params = NULL, *sink_params = NULL;
 
        ret = parse_options(argc, argv);
        if (ret < 0) {
-               fprintf(stdout, "Error parsing options.\n");
-               usage(stdout);
+               fprintf(stderr, "Error parsing options.\n\n");
                exit(EXIT_FAILURE);
        } else if (ret > 0) {
                exit(EXIT_SUCCESS);
        }
+
        printf_verbose("Verbose mode active.\n");
        printf_debug("Debug mode active.\n");
 
-       printf_verbose("Converting from file: %s\n", opt_input_path);
-       printf_verbose("Converting from format: %s\n",
-               opt_input_format ? : "<autodetect>");
-       printf_verbose("Converting to file: %s\n", opt_output_path);
-       printf_verbose("Converting to format: %s\n",
-               opt_output_format ? : "CTF");
+       if (!opt_plugin_path) {
+               fprintf(stderr, "No plugin path specified, aborting...\n");
+               ret = -1;
+               goto end;
+       }
+       printf_verbose("Looking-up plugins at %s\n",
+                       opt_plugin_path ? opt_plugin_path : "Invalid");
+       component_factory = bt_component_factory_create();
+       if (!component_factory) {
+               fprintf(stderr, "Failed to create component factory.\n");
+               ret = -1;
+               goto end;
+       }
+
+       ret = bt_component_factory_load_recursive(component_factory, opt_plugin_path);
+       if (ret) {
+               fprintf(stderr, "Failed to load plugins.\n");
+               goto end;
+       }
+
+       print_detected_component_classes(component_factory);
+
+       sink_class = bt_component_factory_get_component_class(component_factory,
+                       NULL, BT_COMPONENT_TYPE_SINK, "text");
+       if (!sink_class) {
+               fprintf(stderr, "Could not find text output component class. Aborting...\n");
+               ret = -1;
+               goto end;
+       }
 
-       return 0;
+       sink = bt_component_create(sink_class, "bt_text_output", sink_params);
+       if (!sink) {
+               fprintf(stderr, "Failed to instanciate text output. Aborting...\n");
+               ret = -1;
+               goto end;
+       }
+
+       test_sink_notifications(sink);
+
+       /* teardown and exit */
+end:
+       BT_PUT(component_factory);
+       BT_PUT(sink_class);
+       BT_PUT(source_class);
+       BT_PUT(source);
+       BT_PUT(sink);
+       BT_PUT(source_params);
+       BT_PUT(sink_params);
+       return ret ? 1 : 0;
 }
This page took 0.027564 seconds and 4 git commands to generate.