Fix: lttng-destroy: string formating error when default session is unset
[lttng-tools.git] / src / bin / lttng / commands / start.cpp
index 87df0f72f13273cacfa731784d4826e1d838d376..f00ce973e39c59034dec8c3c270d7bd229ab496f 100644 (file)
@@ -7,6 +7,7 @@
 
 #define _LGPL_SOURCE
 #include "../command.hpp"
+#include "../exception.hpp"
 #include "../utils.hpp"
 
 #include <common/exception.hpp>
@@ -89,7 +90,7 @@ cmd_error_code start_tracing(const char *session_name)
 
        const int ret = lttng_start_tracing(session_name);
        if (ret < 0) {
-               LTTNG_THROW_CTL(fmt::format("Failed to start session `{}`", session_name),
+               LTTNG_THROW_CTL(lttng::format("Failed to start session `{}`", session_name),
                                static_cast<lttng_error_code>(-ret));
        }
 
@@ -103,13 +104,13 @@ cmd_error_code start_tracing(const char *session_name)
        return CMD_SUCCESS;
 }
 
-cmd_error_code start_tracing(const session_spec& spec) noexcept
+cmd_error_code start_tracing(const lttng::cli::session_spec& spec)
 {
        bool had_warning = false;
        bool had_error = false;
        bool listing_failed = false;
 
-       const auto sessions = [&listing_failed, &spec]() -> session_list {
+       const auto sessions = [&listing_failed, &spec]() -> lttng::cli::session_list {
                try {
                        return list_sessions(spec);
                } catch (const lttng::ctl::error& ctl_exception) {
@@ -117,10 +118,19 @@ cmd_error_code start_tracing(const session_spec& spec) noexcept
                                lttng_strerror(-ctl_exception.code()));
                        listing_failed = true;
                        return {};
+               } catch (const lttng::cli::no_default_session_error& cli_exception) {
+                       /*
+                        * The retrieval of the default session name already logs
+                        * an error when it fails. There is no value in printing
+                        * anything about this exception.
+                        */
+                       listing_failed = true;
+                       return {};
                }
        }();
 
-       if (!listing_failed && sessions.size() == 0 && spec.type == session_spec::type::NAME) {
+       if (!listing_failed && sessions.size() == 0 &&
+           spec.type_ == lttng::cli::session_spec::type::NAME) {
                ERR_FMT("Session `{}` not found", spec.value);
                return CMD_ERROR;
        }
@@ -141,7 +151,7 @@ cmd_error_code start_tracing(const session_spec& spec) noexcept
                                sub_ret = CMD_SUCCESS;
                                break;
                        case LTTNG_ERR_NO_SESSION:
-                               if (spec.type != session_spec::type::NAME) {
+                               if (spec.type_ != lttng::cli::session_spec::type::NAME) {
                                        /* Session destroyed during command, ignore and carry-on. */
                                        sub_ret = CMD_SUCCESS;
                                        break;
@@ -189,10 +199,7 @@ int cmd_start(int argc, const char **argv)
        bool success = true;
        static poptContext pc;
        const char *leftover = nullptr;
-       session_spec session_spec = {
-               .type = session_spec::NAME,
-               .value = nullptr,
-       };
+       lttng::cli::session_spec session_spec(lttng::cli::session_spec::type::NAME);
 
        pc = poptGetContext(nullptr, argc, argv, long_options, 0);
        poptReadDefaultConfig(pc, 0);
@@ -211,10 +218,10 @@ int cmd_start(int argc, const char **argv)
                        list_cmd_options(stdout, long_options);
                        goto end;
                case OPT_ENABLE_GLOB:
-                       session_spec.type = session_spec::GLOB_PATTERN;
+                       session_spec.type_ = lttng::cli::session_spec::type::GLOB_PATTERN;
                        break;
                case OPT_ALL:
-                       session_spec.type = session_spec::ALL;
+                       session_spec.type_ = lttng::cli::session_spec::type::ALL;
                        break;
                default:
                        command_ret = CMD_UNDEFINED;
This page took 0.024476 seconds and 4 git commands to generate.