Implement --default-output option of create command
[lttng-tools.git] / src / bin / lttng / commands / create.c
index 2e45d004e62cdf66780a6ac5a9878bff5eccf5ea..75edd2bee22db679925db1e88ecd382f7f6c4977 100644 (file)
@@ -52,11 +52,15 @@ static int opt_no_consumer;
 static int opt_no_output;
 static int opt_snapshot;
 static unsigned int opt_live_timer;
+static bool opt_default_name;
+static bool opt_default_output;
 
 enum {
        OPT_HELP = 1,
        OPT_LIST_OPTIONS,
        OPT_LIVE_TIMER,
+       OPT_DEFAULT_NAME,
+       OPT_DEFAULT_OUTPUT,
 };
 
 enum output_type {
@@ -89,6 +93,8 @@ static struct poptOption long_options[] = {
        {"live",            0, POPT_ARG_INT | POPT_ARGFLAG_OPTIONAL, 0, OPT_LIVE_TIMER, 0, 0},
        {"shm-path",        0, POPT_ARG_STRING, &opt_shm_path, 0, 0, 0},
        {"template-path",        0, POPT_ARG_STRING, &opt_template_path, 0, 0, 0},
+       {"default-name",    0, POPT_ARG_NONE, NULL, OPT_DEFAULT_NAME, 0, 0},
+       {"default-output",  0, POPT_ARG_NONE, NULL, OPT_DEFAULT_OUTPUT, 0, 0},
        {0, 0, 0, 0, 0, 0, 0}
 };
 
@@ -256,6 +262,18 @@ static int validate_command_options(enum session_type type)
                }
        }
 
+       if (opt_session_name && opt_default_name) {
+               ERR("A session name must not be provided if the --default-name option is used");
+               ret = CMD_ERROR;
+               goto error;
+       }
+
+       if (opt_default_output && (opt_data_url || opt_ctrl_url || opt_url ||
+                       opt_output_path)) {
+               ERR("The --default-output option may not be used with the --output/-o, --set-url/-U, --ctrl-url/-C, or --data-url/-D options");
+               ret = CMD_ERROR;
+               goto error;
+       }
 error:
        return ret;
 }
@@ -459,7 +477,7 @@ static int parse_template (struct config_document *template,
                char **shm_path)
 {
        int ret = 0;
-       int printed_byte;
+       int printed_bytes;
        char *raw_value = NULL;
 
        assert(template);
@@ -496,8 +514,8 @@ static int parse_template (struct config_document *template,
 
                        if (strlen(raw_value) > 0) {
                                *output_type = OUTPUT_LOCAL;
-                               printed_byte = asprintf(url, "file://%s", raw_value);
-                               if (printed_byte < 0) {
+                               printed_bytes = asprintf(url, "file://%s", raw_value);
+                               if (printed_bytes < 0) {
                                        ret = -1;
                                        goto error;
                                }
@@ -533,8 +551,8 @@ static int parse_template (struct config_document *template,
 
                        if (strlen(raw_value) > 0) {
                                *output_type = OUTPUT_LOCAL;
-                               printed_byte = asprintf(url, "file://%s", raw_value);
-                               if (printed_byte < 0) {
+                               printed_bytes = asprintf(url, "file://%s", raw_value);
+                               if (printed_bytes < 0) {
                                        ret = -1;
                                        goto error;
                                }
@@ -583,7 +601,7 @@ static int create_session_from_template(struct config_document *template,
                const char *datetime)
 {
        int ret = CMD_SUCCESS;
-       int printed_byte;
+       int printed_bytes;
        struct config_element *temp_element = NULL;
        struct config_element *temp_element_child = NULL;
        char tmp_ctrl_uri[PATH_MAX];
@@ -624,8 +642,8 @@ static int create_session_from_template(struct config_document *template,
         */
        if (session_type == SESSION_LIVE) {
                if (config_document_element_exist(template, "/sessions/session/attributes/live_timer_interval")) {
-                       printed_byte = asprintf(&tmp_string, "%d", live_timer);
-                       if (printed_byte < 0) {
+                       printed_bytes = asprintf(&tmp_string, "%d", live_timer);
+                       if (printed_bytes < 0) {
                                ERR("Asprintf failed for live timer");
                                ret = CMD_ERROR;
                                goto error;
@@ -722,7 +740,9 @@ static int create_session_from_template(struct config_document *template,
                        ret = CMD_ERROR;
                        goto error;
                }
+
                config_element_free(temp_element);
+               temp_element = NULL;
 
                temp_element = config_element_create("data_uri", tmp_data_uri);
 
@@ -739,6 +759,7 @@ static int create_session_from_template(struct config_document *template,
                        goto error;
                }
                config_element_free(temp_element);
+               temp_element = NULL;
                break;
        default:
                ret = CMD_ERROR;
@@ -759,6 +780,63 @@ static int create_session_from_template(struct config_document *template,
                goto error;
        }
 
+       config_element_free(temp_element_child);
+       temp_element_child = NULL;
+
+       /* Construct the output/consumer_output/ node*/
+       temp_element_child = temp_element;
+       temp_element = config_element_create("consumer_output", NULL);
+       if (!temp_element) {
+               ERR("Could not create consumer_output node configuration");
+               ret = CMD_ERROR;
+               goto error;
+       }
+
+       ret = config_element_add_child(temp_element, temp_element_child);
+       if (ret) {
+               ERR("Could not append output data to the consumer_output node configuration");
+               ret = CMD_ERROR;
+               goto error;
+       }
+
+       config_element_free(temp_element_child);
+       temp_element_child = config_element_create("enabled", "true");
+       if (!temp_element_child) {
+               ERR("Could not create enbaled node configuration");
+               ret = CMD_ERROR;
+               goto error;
+       }
+
+       ret = config_element_add_child(temp_element, temp_element_child);
+       if (ret) {
+               ERR("Could not append node to the consumer_output node configuration");
+               ret = CMD_ERROR;
+               goto error;
+       }
+
+       config_element_free(temp_element_child);
+       temp_element_child = NULL;
+
+
+       temp_element_child = temp_element;
+       temp_element = config_element_create("output", NULL);
+       if (!temp_element) {
+               ERR("Could not create output node configuration");
+               ret = CMD_ERROR;
+               goto error;
+       }
+
+       ret = config_element_add_child(temp_element, temp_element_child);
+       if (ret) {
+               ERR("Could not append output data to the output node configuration");
+               ret = CMD_ERROR;
+               goto error;
+       }
+
+       config_element_free(temp_element_child);
+       temp_element_child = NULL;
+
+
        /*
         * validate and replace the destination node for each session type
         * TODO: export string as const and simply assign a base path for the
@@ -767,22 +845,44 @@ static int create_session_from_template(struct config_document *template,
        switch (session_type) {
        case SESSION_NORMAL:
        case SESSION_LIVE:
-               if (!config_document_element_exist(template, "/sessions/session/output/consumer_output/destination")) {
-                       ERR("Invalid template no destination node configuration present");
+               break;
+       case SESSION_SNAPSHOT:
+               /* construct the output/snapshots_outputs/ */
+               temp_element_child = temp_element;
+               temp_element = config_element_create("snapshot_outputs", NULL);
+               if (!temp_element) {
+                       ERR("Could not create snapshot_outputs node configuration");
                        ret = CMD_ERROR;
                        goto error;
                }
 
-               ret = config_document_replace_element(template, "/sessions/session/output/consumer_output/destination", temp_element);
-               break;
-       case SESSION_SNAPSHOT:
-               if (!config_document_element_exist(template, "/sessions/session/output/snapshot_outputs/output/consumer_output/destination")) {
-                       ERR("Invalid template no destination node configuration present");
+               ret = config_element_add_child(temp_element, temp_element_child);
+               if (ret) {
+                       ERR("Could not append output data to the snapshot_outputs node configuration");
                        ret = CMD_ERROR;
                        goto error;
                }
 
-               ret = config_document_replace_element(template, "/sessions/session/output/snapshot_outputs/output/consumer_output/destination", temp_element);
+               config_element_free(temp_element_child);
+               temp_element_child = NULL;
+
+               temp_element_child = temp_element;
+               temp_element = config_element_create("output", NULL);
+               if (!temp_element) {
+                       ERR("Could not create output node configuration");
+                       ret = CMD_ERROR;
+                       goto error;
+               }
+
+               ret = config_element_add_child(temp_element, temp_element_child);
+               if (ret) {
+                       ERR("Could not append output data to the output node configuration");
+                       ret = CMD_ERROR;
+                       goto error;
+               }
+
+               config_element_free(temp_element_child);
+               temp_element_child = NULL;
                break;
        default:
                ERR("Invalid session type");
@@ -790,6 +890,14 @@ static int create_session_from_template(struct config_document *template,
                goto error;
        }
 
+       if (!config_document_element_exist(template, "/sessions/session/output")) {
+               ret = config_document_insert_element(template, "/sessions/session", temp_element);
+       } else {
+               ret = config_document_replace_element(template, "/sessions/session/output", temp_element);
+       }
+
+       config_element_free(temp_element);
+       temp_element = NULL;
 
        if (ret) {
                ERR("%s", lttng_strerror(ret));
@@ -816,6 +924,9 @@ static int create_session_from_template(struct config_document *template,
                        ret = CMD_ERROR;
                        goto error;
                }
+
+               config_element_free(temp_element);
+               temp_element = NULL;
        }
 
        ret = config_load_configuration_sessions(template, session_name, 0);
@@ -839,7 +950,7 @@ error:
 static int create_session(void)
 {
        int ret;
-       int printed_byte;
+       int printed_bytes;
 
 
        /* Template */
@@ -953,24 +1064,24 @@ static int create_session(void)
                        goto error;
                }
 
-               printed_byte = asprintf(&session_name_date, "%s-%s", base_session_name, datetime);
-               if (printed_byte < 0) {
+               printed_bytes = asprintf(&session_name_date, "%s-%s", base_session_name, datetime);
+               if (printed_bytes < 0) {
                        PERROR("Asprintf session name");
                        ret = CMD_ERROR;
                        goto error;
                }
                DBG("Session name from command option set to %s", base_session_name);
-       } else if (base_session_name) {
-               printed_byte = asprintf(&session_name_date, "%s-%s", base_session_name, datetime);
-               if (printed_byte < 0) {
+       } else if (base_session_name && !opt_default_name) {
+               printed_bytes = asprintf(&session_name_date, "%s-%s", base_session_name, datetime);
+               if (printed_bytes < 0) {
                        PERROR("Asprintf session name");
                        ret = CMD_ERROR;
                        goto error;
                }
        } else {
                /* Generate a name */
-               printed_byte = asprintf(&base_session_name, DEFAULT_SESSION_NAME "-%s", datetime);
-               if (printed_byte < 0) {
+               printed_bytes = asprintf(&base_session_name, DEFAULT_SESSION_NAME "-%s", datetime);
+               if (printed_bytes < 0) {
                        PERROR("Asprintf session name");
                        ret = CMD_ERROR;
                        goto error;
@@ -1006,8 +1117,8 @@ static int create_session(void)
                }
 
                /* Create URL string from the local file system path */
-               printed_byte = asprintf(&temp_url, "file://%s", traces_path);
-               if (printed_byte < 0) {
+               printed_bytes = asprintf(&temp_url, "file://%s", traces_path);
+               if (printed_bytes < 0) {
                        PERROR("asprintf url path");
                        ret = CMD_FATAL;
                        goto error;
@@ -1023,8 +1134,9 @@ static int create_session(void)
                 */
                base_ctrl_url = strdup(opt_ctrl_url);
                base_data_url = strdup(opt_data_url);
-       } else if (!(opt_no_output || base_output_type == OUTPUT_NONE ||
-                               base_url || base_ctrl_url || base_data_url)) {
+       } else if (opt_default_output || !(opt_no_output ||
+                       base_output_type == OUTPUT_NONE || base_url ||
+                       base_ctrl_url || base_data_url)) {
                /* Generate default output depending on the session type */
                switch (base_session_type) {
                case SESSION_NORMAL:
@@ -1039,11 +1151,11 @@ static int create_session(void)
                                goto error;
                        }
 
-                       printed_byte = asprintf(&tmp_url,
+                       printed_bytes = asprintf(&tmp_url,
                                        "file://%s/" DEFAULT_TRACE_DIR_NAME "/%s",
                                        tmp_home_path, session_name_date);
 
-                       if (printed_byte < 0) {
+                       if (printed_bytes < 0) {
                                PERROR("asprintf trace dir name");
                                ret = CMD_FATAL;
                                goto error;
@@ -1053,8 +1165,8 @@ static int create_session(void)
                        break;
                case SESSION_LIVE:
                        /* Default to a net output */
-                       printed_byte = asprintf(&tmp_url, "net://127.0.0.1");
-                       if (printed_byte < 0) {
+                       printed_bytes = asprintf(&tmp_url, "net://127.0.0.1");
+                       if (printed_bytes < 0) {
                                PERROR("asprintf default live URL");
                                ret = CMD_FATAL;
                                goto error;
@@ -1074,8 +1186,8 @@ static int create_session(void)
        if (opt_shm_path) {
                /* Overwrite shm_path so clear any previously defined one */
                free(base_shm_path);
-               printed_byte = asprintf(&base_shm_path, "%s/%s", opt_shm_path, session_name_date);
-               if (printed_byte < 0) {
+               printed_bytes = asprintf(&base_shm_path, "%s/%s", opt_shm_path, session_name_date);
+               if (printed_bytes < 0) {
                        PERROR("asprintf shm_path");
                        ret = CMD_FATAL;
                        goto error;
@@ -1090,6 +1202,7 @@ static int create_session(void)
        }
 
        /* Get output type from urls */
+       /* TODO: Find a better way of inferring output type */
        if (base_url) {
                /* Get lttng uris from single url */
                uri_array_size = uri_parse_str_urls(base_url, NULL, &uris);
@@ -1135,7 +1248,7 @@ static int create_session(void)
                                base_shm_path,
                                datetime);
        } else {
-               ret = create_session_basic (base_session_name,
+               ret = create_session_basic(base_session_name,
                                base_session_type,
                                base_live_timer,
                                base_output_type,
@@ -1149,7 +1262,7 @@ static int create_session(void)
                goto error;
        }
 
-       ret = generate_output (base_session_name,
+       ret = generate_output(base_session_name,
                        base_session_type,
                        base_live_timer,
                        base_output_type,
@@ -1172,8 +1285,10 @@ static int create_session(void)
 
 error:
        /* Session temp stuff */
+       config_document_free(template);
        free(session_name_date);
        free(uris);
+       free(traces_path);
 
        if (ret < 0) {
                ERR("%s", lttng_strerror(ret));
@@ -1374,6 +1489,12 @@ int cmd_create(int argc, const char **argv)
                        DBG("Session live timer interval set to %d", opt_live_timer);
                        break;
                }
+               case OPT_DEFAULT_NAME:
+                       opt_default_name = true;
+                       break;
+               case OPT_DEFAULT_OUTPUT:
+                       opt_default_output = true;
+                       break;
                default:
                        ret = CMD_UNDEFINED;
                        goto end;
This page took 0.03191 seconds and 5 git commands to generate.