lib/ctf-ir/packet.c: add logging
[babeltrace.git] / cli / babeltrace.c
index 03d6589124c4ca4ea0bd0909743c97fdc23a256d..a5f8072c7d69001415a06a73108ca23a85d56ebb 100644 (file)
@@ -49,6 +49,7 @@
 #include <glib.h>
 #include <inttypes.h>
 #include <unistd.h>
+#include <signal.h>
 #include "babeltrace-cfg.h"
 #include "babeltrace-cfg-cli-args.h"
 #include "babeltrace-cfg-cli-args-default.h"
 
 #define ENV_BABELTRACE_WARN_COMMAND_NAME_DIRECTORY_CLASH "BABELTRACE_CLI_WARN_COMMAND_NAME_DIRECTORY_CLASH"
 
+/* Application's processing graph (weak) */
+static struct bt_graph *the_graph;
+static bool canceled = false;
+
 GPtrArray *loaded_plugins;
 
 BT_HIDDEN
 int bt_cli_log_level = BT_LOG_NONE;
 
+static
+void sigint_handler(int signum)
+{
+       if (signum != SIGINT) {
+               return;
+       }
+
+       if (the_graph) {
+               bt_graph_cancel(the_graph);
+       }
+
+       canceled = true;
+}
+
 static
 void init_static_data(void)
 {
@@ -669,15 +688,10 @@ void print_plugin_info(struct bt_plugin *plugin)
 static
 int cmd_query(struct bt_config *cfg)
 {
-       int ret;
+       int ret = 0;
        struct bt_component_class *comp_cls = NULL;
        struct bt_value *results = NULL;
 
-       ret = load_all_plugins(cfg->plugin_paths);
-       if (ret) {
-               goto end;
-       }
-
        comp_cls = find_component_class(cfg->cmd_data.query.cfg_component->plugin_name->str,
                cfg->cmd_data.query.cfg_component->comp_cls_name->str,
                cfg->cmd_data.query.cfg_component->type);
@@ -739,15 +753,10 @@ end:
 static
 int cmd_help(struct bt_config *cfg)
 {
-       int ret;
+       int ret = 0;
        struct bt_plugin *plugin = NULL;
        size_t i;
 
-       ret = load_all_plugins(cfg->plugin_paths);
-       if (ret) {
-               goto end;
-       }
-
        plugin = find_plugin(cfg->cmd_data.help.cfg_component->plugin_name->str);
        if (!plugin) {
                BT_LOGE("Cannot find plugin: plugin-name=\"%s\"",
@@ -851,11 +860,6 @@ int cmd_list_plugins(struct bt_config *cfg)
        int ret = 0;
        int plugins_count, component_classes_count = 0, i;
 
-       ret = load_all_plugins(cfg->plugin_paths);
-       if (ret) {
-               goto end;
-       }
-
        printf("From the following plugin paths:\n\n");
        print_value(stdout, cfg->plugin_paths, 2);
        printf("\n");
@@ -1326,6 +1330,7 @@ void cmd_run_ctx_destroy(struct cmd_run_ctx *ctx)
        }
 
        BT_PUT(ctx->graph);
+       the_graph = NULL;
        ctx->cfg = NULL;
 }
 
@@ -1347,6 +1352,7 @@ int cmd_run_ctx_init(struct cmd_run_ctx *ctx, struct bt_config *cfg)
                goto error;
        }
 
+       the_graph = ctx->graph;
        ret = bt_graph_add_port_added_listener(ctx->graph,
                graph_port_added_listener, ctx);
        if (ret) {
@@ -1555,11 +1561,6 @@ int cmd_run(struct bt_config *cfg)
        int ret = 0;
        struct cmd_run_ctx ctx = { 0 };
 
-       ret = load_all_plugins(cfg->plugin_paths);
-       if (ret) {
-               goto error;
-       }
-
        /* Initialize the command's context and the graph object */
        if (cmd_run_ctx_init(&ctx, cfg)) {
                BT_LOGE_STR("Cannot initialize the command's context.");
@@ -1581,6 +1582,10 @@ int cmd_run(struct bt_config *cfg)
                goto error;
        }
 
+       if (canceled) {
+               goto end;
+       }
+
        BT_LOGI_STR("Running the graph.");
 
        /* Run the graph */
@@ -1590,7 +1595,17 @@ int cmd_run(struct bt_config *cfg)
                switch (graph_status) {
                case BT_GRAPH_STATUS_OK:
                        break;
+               case BT_GRAPH_STATUS_CANCELED:
+                       BT_LOGI("Graph was canceled by user: status=%d",
+                               graph_status);
+                       goto error;
                case BT_GRAPH_STATUS_AGAIN:
+                       if (bt_graph_is_canceled(ctx.graph)) {
+                               BT_LOGI("Graph was canceled by user: status=%d",
+                                       graph_status);
+                               goto error;
+                       }
+
                        if (cfg->cmd_data.run.retry_duration_us > 0) {
                                BT_LOGV("Got BT_GRAPH_STATUS_AGAIN: sleeping: "
                                        "time-us=%" PRIu64,
@@ -1678,6 +1693,20 @@ set_level:
        bt_cli_log_level = log_level;
 }
 
+void set_sigint_handler(void)
+{
+       struct sigaction new_action, old_action;
+
+       new_action.sa_handler = sigint_handler;
+       sigemptyset(&new_action.sa_mask);
+       new_action.sa_flags = 0;
+       sigaction(SIGINT, NULL, &old_action);
+
+       if (old_action.sa_handler != SIG_IGN) {
+               sigaction(SIGINT, &new_action, NULL);
+       }
+}
+
 int main(int argc, const char **argv)
 {
        int ret;
@@ -1685,6 +1714,7 @@ int main(int argc, const char **argv)
        struct bt_config *cfg;
 
        init_log_level();
+       set_sigint_handler();
        init_static_data();
        cfg = bt_config_cli_args_create_with_default(argc, argv, &retcode);
 
@@ -1761,6 +1791,8 @@ int main(int argc, const char **argv)
                assert(false);
        }
 
+       BT_LOGI("Command completed: cmd=%d, command-name=\"%s\", ret=%d",
+               cfg->command, cfg->command_name, ret);
        warn_command_name_and_directory_clash(cfg);
        retcode = ret ? 1 : 0;
 
This page took 0.027494 seconds and 4 git commands to generate.