cli/babeltrace.c: add logging when the command completes
[babeltrace.git] / cli / babeltrace.c
index ba8473af3a02c7ea3fd6aa614fb98b9db50b5782..4a0a25c56aa2e5a90308178e98acbdbcbe2a5571 100644 (file)
@@ -48,6 +48,8 @@
 #include <stdio.h>
 #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)
 {
@@ -218,7 +238,7 @@ struct print_map_value_data {
 };
 
 static
-bool print_map_value(const char *key, struct bt_value *object, void *data)
+bt_bool print_map_value(const char *key, struct bt_value *object, void *data)
 {
        struct print_map_value_data *print_map_value_data = data;
 
@@ -244,13 +264,13 @@ bool print_map_value(const char *key, struct bt_value *object, void *data)
 
        print_value_rec(print_map_value_data->fp, object,
                print_map_value_data->indent + 2);
-       return true;
+       return BT_TRUE;
 }
 
 static
 void print_value_rec(FILE *fp, struct bt_value *value, size_t indent)
 {
-       bool bool_val;
+       bt_bool bool_val;
        int64_t int_val;
        double dbl_val;
        const char *str_val;
@@ -1325,6 +1345,7 @@ void cmd_run_ctx_destroy(struct cmd_run_ctx *ctx)
        }
 
        BT_PUT(ctx->graph);
+       the_graph = NULL;
        ctx->cfg = NULL;
 }
 
@@ -1346,6 +1367,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) {
@@ -1580,6 +1602,10 @@ int cmd_run(struct bt_config *cfg)
                goto error;
        }
 
+       if (canceled) {
+               goto end;
+       }
+
        BT_LOGI_STR("Running the graph.");
 
        /* Run the graph */
@@ -1589,7 +1615,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,
@@ -1677,6 +1713,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;
@@ -1684,6 +1734,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);
 
@@ -1760,6 +1811,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.034291 seconds and 4 git commands to generate.