Add bt_graph_add_component(), make bt_component_create() internal
[babeltrace.git] / cli / babeltrace.c
index 9de233bc1672718ec3bc3c31cd029b469a3e0b60..4e35b8b6dd057024f8e53d96244c6479e0ccb05a 100644 (file)
@@ -26,6 +26,9 @@
  * SOFTWARE.
  */
 
+#define BT_LOG_TAG "CLI"
+#include "logging.h"
+
 #include <babeltrace/babeltrace.h>
 #include <babeltrace/plugin/plugin.h>
 #include <babeltrace/common-internal.h>
@@ -54,9 +57,6 @@
 #include "babeltrace-cfg-cli-args.h"
 #include "babeltrace-cfg-cli-args-default.h"
 
-#define BT_LOG_TAG "CLI"
-#include "logging.h"
-
 #define ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH "BABELTRACE_CLI_WARN_COMMAND_NAME_DIRECTORY_CLASH"
 
 /* Application's processing graph (weak) */
@@ -65,9 +65,6 @@ static bool canceled = false;
 
 GPtrArray *loaded_plugins;
 
-BT_HIDDEN
-int bt_cli_log_level = BT_LOG_NONE;
-
 static
 void sigint_handler(int signum)
 {
@@ -581,6 +578,19 @@ int load_dynamic_plugins(struct bt_value *plugin_paths)
                plugin_path_value = bt_value_array_get(plugin_paths, i);
                bt_value_string_get(plugin_path_value, &plugin_path);
                assert(plugin_path);
+
+               /*
+                * Skip this if the directory does not exist because
+                * bt_plugin_create_all_from_dir() expects an existing
+                * directory.
+                */
+               if (!g_file_test(plugin_path, G_FILE_TEST_IS_DIR)) {
+                       BT_LOGV("Skipping nonexistent directory path: "
+                               "path=\"%s\"", plugin_path);
+                       BT_PUT(plugin_path_value);
+                       continue;
+               }
+
                plugin_set = bt_plugin_create_all_from_dir(plugin_path, false);
                if (!plugin_set) {
                        BT_LOGD("Unable to load dynamic plugins: path=\"%s\"",
@@ -1168,7 +1178,7 @@ int cmd_run_ctx_connect_upstream_port_to_downstream_component(
        uint64_t i;
        int64_t (*port_count_fn)(struct bt_component *);
        struct bt_port *(*port_by_index_fn)(struct bt_component *, uint64_t);
-       void *conn = NULL;
+       enum bt_graph_status status = BT_GRAPH_STATUS_ERROR;
 
        BT_LOGI("Connecting upstream port to the next available downstream port: "
                "upstream-port-addr=%p, upstream-port-name=\"%s\", "
@@ -1180,7 +1190,7 @@ int cmd_run_ctx_connect_upstream_port_to_downstream_component(
                cfg_conn->downstream_comp_name->str);
        assert(downstreamp_comp_name_quark > 0);
        downstream_comp = g_hash_table_lookup(ctx->components,
-               (gpointer) (long) downstreamp_comp_name_quark);
+               GUINT_TO_POINTER(downstreamp_comp_name_quark));
        if (!downstream_comp) {
                BT_LOGE("Cannot find downstream component:  comp-name=\"%s\", "
                        "conn-arg=\"%s\"", cfg_conn->downstream_comp_name->str,
@@ -1233,10 +1243,35 @@ int cmd_run_ctx_connect_upstream_port_to_downstream_component(
                                cfg_conn->downstream_port_glob->str, -1ULL,
                                downstream_port_name, -1ULL)) {
                        /* We have a winner! */
-                       conn = bt_graph_connect_ports(ctx->graph,
-                               upstream_port, downstream_port);
+                       status = bt_graph_connect_ports(ctx->graph,
+                               upstream_port, downstream_port, NULL);
                        bt_put(downstream_port);
-                       if (!conn) {
+                       switch (status) {
+                       case BT_GRAPH_STATUS_OK:
+                               break;
+                       case BT_GRAPH_STATUS_CANCELED:
+                               BT_LOGI_STR("Graph was canceled by user.");
+                               status = BT_GRAPH_STATUS_OK;
+                               break;
+                       case BT_GRAPH_STATUS_COMPONENT_REFUSES_PORT_CONNECTION:
+                               BT_LOGE("A component refused a connection to one of its ports: "
+                                       "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
+                                       "upstream-port-addr=%p, upstream-port-name=\"%s\", "
+                                       "downstream-comp-addr=%p, downstream-comp-name=\"%s\", "
+                                       "downstream-port-addr=%p, downstream-port-name=\"%s\", "
+                                       "conn-arg=\"%s\"",
+                                       upstream_comp, bt_component_get_name(upstream_comp),
+                                       upstream_port, bt_port_get_name(upstream_port),
+                                       downstream_comp, cfg_conn->downstream_comp_name->str,
+                                       downstream_port, downstream_port_name,
+                                       cfg_conn->arg->str);
+                               fprintf(stderr,
+                                       "A component refused a connection to one of its ports (`%s` to `%s`): %s\n",
+                                       bt_port_get_name(upstream_port),
+                                       downstream_port_name,
+                                       cfg_conn->arg->str);
+                               break;
+                       default:
                                BT_LOGE("Cannot create connection: graph refuses to connect ports: "
                                        "upstream-comp-addr=%p, upstream-comp-name=\"%s\", "
                                        "upstream-port-addr=%p, upstream-port-name=\"%s\", "
@@ -1274,7 +1309,7 @@ int cmd_run_ctx_connect_upstream_port_to_downstream_component(
                bt_put(downstream_port);
        }
 
-       if (!conn) {
+       if (status != BT_GRAPH_STATUS_OK) {
                BT_LOGE("Cannot create connection: cannot find a matching downstream port for upstream port: "
                        "upstream-port-addr=%p, upstream-port-name=\"%s\", "
                        "downstream-comp-name=\"%s\", conn-arg=\"%s\"",
@@ -1293,7 +1328,6 @@ error:
        ret = -1;
 
 end:
-       bt_put(conn);
        return ret;
 }
 
@@ -1384,6 +1418,11 @@ void graph_port_added_listener(struct bt_port *port, void *data)
                "comp-name=\"%s\", port-addr=%p, port-name=\"%s\"",
                comp, comp ? bt_component_get_name(comp) : "",
                port, bt_port_get_name(port));
+
+       if (!ctx->connect_ports) {
+               goto end;
+       }
+
        if (!comp) {
                BT_LOGW_STR("Port has no component.");
                goto end;
@@ -1564,9 +1603,9 @@ int cmd_run_ctx_create_components_from_config_components(
                        goto error;
                }
 
-               comp = bt_component_create(comp_cls,
-                       cfg_comp->instance_name->str, cfg_comp->params);
-               if (!comp) {
+               ret = bt_graph_add_component(ctx->graph, comp_cls,
+                       cfg_comp->instance_name->str, cfg_comp->params, &comp);
+               if (ret) {
                        BT_LOGE("Cannot create component: plugin-name=\"%s\", "
                                "comp-cls-name=\"%s\", comp-cls-type=%d, "
                                "comp-name=\"%s\"",
@@ -1586,7 +1625,7 @@ int cmd_run_ctx_create_components_from_config_components(
                quark = g_quark_from_string(cfg_comp->instance_name->str);
                assert(quark > 0);
                g_hash_table_insert(ctx->components,
-                       (gpointer) (long) quark, comp);
+                       GUINT_TO_POINTER(quark), comp);
                comp = NULL;
                BT_PUT(comp_cls);
        }
@@ -1710,8 +1749,6 @@ const char *bt_graph_status_str(enum bt_graph_status status)
                return "BT_GRAPH_STATUS_END";
        case BT_GRAPH_STATUS_OK:
                return "BT_GRAPH_STATUS_OK";
-       case BT_GRAPH_STATUS_ALREADY_IN_A_GRAPH:
-               return "BT_GRAPH_STATUS_ALREADY_IN_A_GRAPH";
        case BT_GRAPH_STATUS_INVALID:
                return "BT_GRAPH_STATUS_INVALID";
        case BT_GRAPH_STATUS_NO_SINK:
This page took 0.026368 seconds and 4 git commands to generate.