X-Git-Url: http://drtracing.org/?a=blobdiff_plain;f=src%2Fplugins%2Fctf%2Flttng-live%2Fviewer-connection.cpp;h=f829d07dcc65b5496aa1a4780f31fdd260cfc2a5;hb=e5694f0e64b1491d4455287db9f14ea84d85ec2e;hp=2f07797833a11d9ec790ecc4d3a52846c1f52376;hpb=71436ae48cd2fcfcc59ae9a7da909e29a191145c;p=babeltrace.git diff --git a/src/plugins/ctf/lttng-live/viewer-connection.cpp b/src/plugins/ctf/lttng-live/viewer-connection.cpp index 2f077978..f829d07d 100644 --- a/src/plugins/ctf/lttng-live/viewer-connection.cpp +++ b/src/plugins/ctf/lttng-live/viewer-connection.cpp @@ -11,13 +11,9 @@ #include -#define BT_COMP_LOG_SELF_COMP (viewer_connection->self_comp) -#define BT_LOG_OUTPUT_LEVEL ((enum bt_log_level) viewer_connection->log_level) -#define BT_LOG_TAG "PLUGIN/SRC.CTF.LTTNG-LIVE/VIEWER" -#include "logging/comp-logging.h" - #include "common/common.h" #include "compat/endian.h" /* IWYU pragma: keep */ +#include "cpp-common/bt2s/make-unique.hpp" #include "data-stream.hpp" #include "lttng-live.hpp" @@ -25,119 +21,32 @@ #include "metadata.hpp" #include "viewer-connection.hpp" -#define viewer_handle_send_recv_status(_self_comp, _self_comp_class, _status, _action, _msg_str) \ +#define viewer_handle_send_recv_status(_status, _action, _msg_str) \ do { \ switch (_status) { \ case LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED: \ break; \ case LTTNG_LIVE_VIEWER_STATUS_ERROR: \ - BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(_self_comp, _self_comp_class, \ - "Error " _action " " _msg_str); \ + BT_CPPLOGE_APPEND_CAUSE_SPEC(viewer_connection->logger, \ + "Error " _action " " _msg_str); \ break; \ default: \ bt_common_abort(); \ } \ } while (0) -#define viewer_handle_send_status(_self_comp, _self_comp_class, _status, _msg_str) \ - viewer_handle_send_recv_status(_self_comp, _self_comp_class, _status, "sending", _msg_str) +#define viewer_handle_send_status(_status, _msg_str) \ + viewer_handle_send_recv_status(_status, "sending", _msg_str) -#define viewer_handle_recv_status(_self_comp, _self_comp_class, _status, _msg_str) \ - viewer_handle_send_recv_status(_self_comp, _self_comp_class, _status, "receiving", _msg_str) +#define viewer_handle_recv_status(_status, _msg_str) \ + viewer_handle_send_recv_status(_status, "receiving", _msg_str) -#define LTTNG_LIVE_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE_ERRNO(_self_comp, _self_comp_class, _msg, \ - _fmt, ...) \ +#define LTTNG_LIVE_CPPLOGE_APPEND_CAUSE_ERRNO(_msg, _fmt, ...) \ do { \ - BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(_self_comp, _self_comp_class, _msg ": %s" _fmt, \ - bt_socket_errormsg(), ##__VA_ARGS__); \ + BT_CPPLOGE_APPEND_CAUSE_SPEC(viewer_connection->logger, _msg ": {}" _fmt, \ + bt_socket_errormsg(), ##__VA_ARGS__); \ } while (0) -static const char *lttng_viewer_command_string(enum lttng_viewer_command cmd) -{ - switch (cmd) { - case LTTNG_VIEWER_CONNECT: - return "CONNECT"; - case LTTNG_VIEWER_LIST_SESSIONS: - return "LIST_SESSIONS"; - case LTTNG_VIEWER_ATTACH_SESSION: - return "ATTACH_SESSION"; - case LTTNG_VIEWER_GET_NEXT_INDEX: - return "GET_NEXT_INDEX"; - case LTTNG_VIEWER_GET_PACKET: - return "GET_PACKET"; - case LTTNG_VIEWER_GET_METADATA: - return "GET_METADATA"; - case LTTNG_VIEWER_GET_NEW_STREAMS: - return "GET_NEW_STREAMS"; - case LTTNG_VIEWER_CREATE_SESSION: - return "CREATE_SESSION"; - case LTTNG_VIEWER_DETACH_SESSION: - return "DETACH_SESSION"; - } - - bt_common_abort(); -} - -static const char * -lttng_viewer_next_index_return_code_string(enum lttng_viewer_next_index_return_code code) -{ - switch (code) { - case LTTNG_VIEWER_INDEX_OK: - return "INDEX_OK"; - case LTTNG_VIEWER_INDEX_RETRY: - return "INDEX_RETRY"; - case LTTNG_VIEWER_INDEX_HUP: - return "INDEX_HUP"; - case LTTNG_VIEWER_INDEX_ERR: - return "INDEX_ERR"; - case LTTNG_VIEWER_INDEX_INACTIVE: - return "INDEX_INACTIVE"; - case LTTNG_VIEWER_INDEX_EOF: - return "INDEX_EOF"; - } - - bt_common_abort(); -} - -static const char *lttng_viewer_next_index_return_code_string(uint32_t code) -{ - return lttng_viewer_next_index_return_code_string((lttng_viewer_next_index_return_code) code); -} - -static const char * -lttng_viewer_get_packet_return_code_string(enum lttng_viewer_get_packet_return_code code) -{ - switch (code) { - case LTTNG_VIEWER_GET_PACKET_OK: - return "GET_PACKET_OK"; - case LTTNG_VIEWER_GET_PACKET_RETRY: - return "GET_PACKET_RETRY"; - case LTTNG_VIEWER_GET_PACKET_ERR: - return "GET_PACKET_ERR"; - case LTTNG_VIEWER_GET_PACKET_EOF: - return "GET_PACKET_EOF"; - } - - bt_common_abort(); -}; - -static const char *lttng_viewer_get_packet_return_code_string(uint32_t code) -{ - return lttng_viewer_get_packet_return_code_string((lttng_viewer_get_packet_return_code) code); -} - -static const char *lttng_viewer_seek_string(enum lttng_viewer_seek seek) -{ - switch (seek) { - case LTTNG_VIEWER_SEEK_BEGINNING: - return "SEEK_BEGINNING"; - case LTTNG_VIEWER_SEEK_LAST: - return "SEEK_LAST"; - } - - bt_common_abort(); -} - static inline enum lttng_live_iterator_status viewer_status_to_live_iterator_status(enum lttng_live_viewer_status viewer_status) { @@ -170,12 +79,14 @@ viewer_status_to_ctf_msg_iter_medium_status(enum lttng_live_viewer_status viewer static inline void viewer_connection_close_socket(struct live_viewer_connection *viewer_connection) { - bt_self_component_class *self_comp_class = viewer_connection->self_comp_class; - bt_self_component *self_comp = viewer_connection->self_comp; + if (viewer_connection->control_sock == BT_INVALID_SOCKET) { + return; + } + int ret = bt_socket_close(viewer_connection->control_sock); if (ret == -1) { - BT_COMP_OR_COMP_CLASS_LOGW_ERRNO(self_comp, self_comp_class, - "Error closing viewer connection socket: ", "."); + BT_CPPLOGW_ERRNO_SPEC(viewer_connection->logger, + "Error closing viewer connection socket: ", "."); } viewer_connection->control_sock = BT_INVALID_SOCKET; @@ -191,8 +102,6 @@ static enum lttng_live_viewer_status lttng_live_recv(struct live_viewer_connection *viewer_connection, void *buf, size_t len) { ssize_t received; - bt_self_component_class *self_comp_class = viewer_connection->self_comp_class; - bt_self_component *self_comp = viewer_connection->self_comp; size_t total_received = 0, to_receive = len; struct lttng_live_msg_iter *lttng_live_msg_iter = viewer_connection->lttng_live_msg_iter; enum lttng_live_viewer_status status; @@ -226,8 +135,7 @@ lttng_live_recv(struct live_viewer_connection *viewer_connection, void *buf, siz * For any other types of socket error, close * the socket and return an error. */ - LTTNG_LIVE_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE_ERRNO( - self_comp, self_comp_class, "Error receiving from Relay", "."); + LTTNG_LIVE_CPPLOGE_APPEND_CAUSE_ERRNO("Error receiving from Relay", "."); viewer_connection_close_socket(viewer_connection); status = LTTNG_LIVE_VIEWER_STATUS_ERROR; @@ -241,8 +149,8 @@ lttng_live_recv(struct live_viewer_connection *viewer_connection, void *buf, siz * a message from it, it means something when wrong. * Close the socket and return an error. */ - BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp, self_comp_class, - "Remote side has closed connection"); + BT_CPPLOGE_APPEND_CAUSE_SPEC(viewer_connection->logger, + "Remote side has closed connection"); viewer_connection_close_socket(viewer_connection); status = LTTNG_LIVE_VIEWER_STATUS_ERROR; goto end; @@ -271,8 +179,6 @@ static enum lttng_live_viewer_status lttng_live_send(struct live_viewer_connection *viewer_connection, const void *buf, size_t len) { enum lttng_live_viewer_status status; - bt_self_component_class *self_comp_class = viewer_connection->self_comp_class; - bt_self_component *self_comp = viewer_connection->self_comp; struct lttng_live_msg_iter *lttng_live_msg_iter = viewer_connection->lttng_live_msg_iter; BT_SOCKET sock = viewer_connection->control_sock; size_t to_send = len; @@ -302,8 +208,7 @@ lttng_live_send(struct live_viewer_connection *viewer_connection, const void *bu * For any other types of socket error, close * the socket and return an error. */ - LTTNG_LIVE_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE_ERRNO( - self_comp, self_comp_class, "Error sending to Relay", "."); + LTTNG_LIVE_CPPLOGE_APPEND_CAUSE_ERRNO("Error sending to Relay", "."); viewer_connection_close_socket(viewer_connection); status = LTTNG_LIVE_VIEWER_STATUS_ERROR; @@ -327,26 +232,25 @@ end: static int parse_url(struct live_viewer_connection *viewer_connection) { char error_buf[256] = {0}; - bt_self_component *self_comp = viewer_connection->self_comp; - bt_self_component_class *self_comp_class = viewer_connection->self_comp_class; struct bt_common_lttng_live_url_parts lttng_live_url_parts = {}; + bt_common_lttng_live_url_parts_deleter partsDeleter {lttng_live_url_parts}; int ret = -1; - const char *path = viewer_connection->url->str; - if (!path) { + if (viewer_connection->url.empty()) { goto end; } - lttng_live_url_parts = bt_common_parse_lttng_live_url(path, error_buf, sizeof(error_buf)); + lttng_live_url_parts = bt_common_parse_lttng_live_url(viewer_connection->url.c_str(), error_buf, + sizeof(error_buf)); if (!lttng_live_url_parts.proto) { - BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp, self_comp_class, - "Invalid LTTng live URL format: %s", error_buf); + BT_CPPLOGE_APPEND_CAUSE_SPEC(viewer_connection->logger, "Invalid LTTng live URL format: {}", + error_buf); goto end; } - viewer_connection->proto = lttng_live_url_parts.proto; + viewer_connection->proto.reset(lttng_live_url_parts.proto); lttng_live_url_parts.proto = NULL; - viewer_connection->relay_hostname = lttng_live_url_parts.hostname; + viewer_connection->relay_hostname.reset(lttng_live_url_parts.hostname); lttng_live_url_parts.hostname = NULL; if (lttng_live_url_parts.port >= 0) { @@ -355,18 +259,17 @@ static int parse_url(struct live_viewer_connection *viewer_connection) viewer_connection->port = LTTNG_DEFAULT_NETWORK_VIEWER_PORT; } - viewer_connection->target_hostname = lttng_live_url_parts.target_hostname; + viewer_connection->target_hostname.reset(lttng_live_url_parts.target_hostname); lttng_live_url_parts.target_hostname = NULL; if (lttng_live_url_parts.session_name) { - viewer_connection->session_name = lttng_live_url_parts.session_name; + viewer_connection->session_name.reset(lttng_live_url_parts.session_name); lttng_live_url_parts.session_name = NULL; } ret = 0; end: - bt_common_destroy_lttng_live_url_parts(<tng_live_url_parts); return ret; } @@ -376,15 +279,12 @@ lttng_live_handshake(struct live_viewer_connection *viewer_connection) struct lttng_viewer_cmd cmd; struct lttng_viewer_connect connect; enum lttng_live_viewer_status status; - bt_self_component_class *self_comp_class = viewer_connection->self_comp_class; - bt_self_component *self_comp = viewer_connection->self_comp; const size_t cmd_buf_len = sizeof(cmd) + sizeof(connect); char cmd_buf[cmd_buf_len]; - BT_COMP_OR_COMP_CLASS_LOGD( - self_comp, self_comp_class, - "Handshaking with the relay daemon: cmd=%s, major-version=%u, minor-version=%u", - lttng_viewer_command_string(LTTNG_VIEWER_CONNECT), LTTNG_LIVE_MAJOR, LTTNG_LIVE_MINOR); + BT_CPPLOGD_SPEC(viewer_connection->logger, + "Handshaking with the relay daemon: cmd={}, major-version={}, minor-version={}", + LTTNG_VIEWER_CONNECT, LTTNG_LIVE_MAJOR, LTTNG_LIVE_MINOR); cmd.cmd = htobe32(LTTNG_VIEWER_CONNECT); cmd.data_size = htobe64((uint64_t) sizeof(connect)); @@ -405,24 +305,24 @@ lttng_live_handshake(struct live_viewer_connection *viewer_connection) status = lttng_live_send(viewer_connection, &cmd_buf, cmd_buf_len); if (status != LTTNG_LIVE_VIEWER_STATUS_OK) { - viewer_handle_send_status(self_comp, self_comp_class, status, "viewer connect command"); + viewer_handle_send_status(status, "viewer connect command"); goto end; } status = lttng_live_recv(viewer_connection, &connect, sizeof(connect)); if (status != LTTNG_LIVE_VIEWER_STATUS_OK) { - viewer_handle_recv_status(self_comp, self_comp_class, status, "viewer connect reply"); + viewer_handle_recv_status(status, "viewer connect reply"); goto end; } - BT_COMP_OR_COMP_CLASS_LOGI(self_comp, self_comp_class, "Received viewer session ID : %" PRIu64, - (uint64_t) be64toh(connect.viewer_session_id)); - BT_COMP_OR_COMP_CLASS_LOGI(self_comp, self_comp_class, "Relayd version : %u.%u", - be32toh(connect.major), be32toh(connect.minor)); + BT_CPPLOGI_SPEC(viewer_connection->logger, "Received viewer session ID : {}", + (uint64_t) be64toh(connect.viewer_session_id)); + BT_CPPLOGI_SPEC(viewer_connection->logger, "Relayd version : {}.{}", be32toh(connect.major), + be32toh(connect.minor)); if (LTTNG_LIVE_MAJOR != be32toh(connect.major)) { - BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp, self_comp_class, - "Incompatible lttng-relayd protocol"); + BT_CPPLOGE_APPEND_CAUSE_SPEC(viewer_connection->logger, + "Incompatible lttng-relayd protocol"); status = LTTNG_LIVE_VIEWER_STATUS_ERROR; goto end; } @@ -448,19 +348,16 @@ lttng_live_connect_viewer(struct live_viewer_connection *viewer_connection) struct hostent *host; struct sockaddr_in server_addr; enum lttng_live_viewer_status status; - bt_self_component_class *self_comp_class = viewer_connection->self_comp_class; - bt_self_component *self_comp = viewer_connection->self_comp; if (parse_url(viewer_connection)) { - BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp, self_comp_class, "Failed to parse URL"); + BT_CPPLOGE_APPEND_CAUSE_SPEC(viewer_connection->logger, "Failed to parse URL"); status = LTTNG_LIVE_VIEWER_STATUS_ERROR; goto error; } - BT_COMP_OR_COMP_CLASS_LOGD( - self_comp, self_comp_class, - "Connecting to hostname : %s, port : %d, " - "target hostname : %s, session name : %s, proto : %s", + BT_CPPLOGD_SPEC( + viewer_connection->logger, + "Connecting to hostname : {}, port : {}, target hostname : {}, session name : {}, proto : {}", viewer_connection->relay_hostname->str, viewer_connection->port, !viewer_connection->target_hostname ? "" : viewer_connection->target_hostname->str, !viewer_connection->session_name ? "" : viewer_connection->session_name->str, @@ -468,16 +365,16 @@ lttng_live_connect_viewer(struct live_viewer_connection *viewer_connection) host = gethostbyname(viewer_connection->relay_hostname->str); if (!host) { - BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp, self_comp_class, - "Cannot lookup hostname: hostname=\"%s\"", - viewer_connection->relay_hostname->str); + BT_CPPLOGE_APPEND_CAUSE_SPEC(viewer_connection->logger, + "Cannot lookup hostname: hostname=\"{}\"", + viewer_connection->relay_hostname->str); status = LTTNG_LIVE_VIEWER_STATUS_ERROR; goto error; } if ((viewer_connection->control_sock = socket(AF_INET, SOCK_STREAM, 0)) == BT_INVALID_SOCKET) { - BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp, self_comp_class, - "Socket creation failed: %s", bt_socket_errormsg()); + BT_CPPLOGE_APPEND_CAUSE_SPEC(viewer_connection->logger, "Socket creation failed: {}", + bt_socket_errormsg()); status = LTTNG_LIVE_VIEWER_STATUS_ERROR; goto error; } @@ -489,8 +386,8 @@ lttng_live_connect_viewer(struct live_viewer_connection *viewer_connection) if (connect(viewer_connection->control_sock, (struct sockaddr *) &server_addr, sizeof(struct sockaddr)) == BT_SOCKET_ERROR) { - BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp, self_comp_class, "Connection failed: %s", - bt_socket_errormsg()); + BT_CPPLOGE_APPEND_CAUSE_SPEC(viewer_connection->logger, "Connection failed: {}", + bt_socket_errormsg()); status = LTTNG_LIVE_VIEWER_STATUS_ERROR; goto error; } @@ -502,8 +399,7 @@ lttng_live_connect_viewer(struct live_viewer_connection *viewer_connection) * interruption. */ if (status == LTTNG_LIVE_VIEWER_STATUS_ERROR) { - BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp, self_comp_class, - "Viewer handshake failed"); + BT_CPPLOGE_APPEND_CAUSE_SPEC(viewer_connection->logger, "Viewer handshake failed"); goto error; } else if (status == LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED) { goto end; @@ -512,120 +408,89 @@ lttng_live_connect_viewer(struct live_viewer_connection *viewer_connection) goto end; error: - if (viewer_connection->control_sock != BT_INVALID_SOCKET) { - if (bt_socket_close(viewer_connection->control_sock) == BT_SOCKET_ERROR) { - BT_COMP_OR_COMP_CLASS_LOGW(self_comp, self_comp_class, "Error closing socket: %s.", - bt_socket_errormsg()); - } - } - viewer_connection->control_sock = BT_INVALID_SOCKET; + viewer_connection_close_socket(viewer_connection); + end: return status; } -static void lttng_live_disconnect_viewer(struct live_viewer_connection *viewer_connection) -{ - bt_self_component_class *self_comp_class = viewer_connection->self_comp_class; - bt_self_component *self_comp = viewer_connection->self_comp; - - if (viewer_connection->control_sock == BT_INVALID_SOCKET) { - return; - } - if (bt_socket_close(viewer_connection->control_sock) == BT_SOCKET_ERROR) { - BT_COMP_OR_COMP_CLASS_LOGW(self_comp, self_comp_class, "Error closing socket: %s", - bt_socket_errormsg()); - viewer_connection->control_sock = BT_INVALID_SOCKET; - } -} - -static int list_update_session(bt_value *results, const struct lttng_viewer_session *session, - bool *_found, struct live_viewer_connection *viewer_connection) +static int list_update_session(const bt2::ArrayValue results, + const struct lttng_viewer_session *session, bool *_found, + struct live_viewer_connection *viewer_connection) { - bt_self_component_class *self_comp_class = viewer_connection->self_comp_class; - bt_self_component *self_comp = viewer_connection->self_comp; - int ret = 0; - uint64_t i, len; - bt_value *map = NULL; - bt_value *hostname = NULL; - bt_value *session_name = NULL; - bt_value *btval = NULL; bool found = false; - len = bt_value_array_get_length(results); - for (i = 0; i < len; i++) { - const char *hostname_str = NULL; - const char *session_name_str = NULL; - - map = bt_value_array_borrow_element_by_index(results, i); - hostname = bt_value_map_borrow_entry_value(map, "target-hostname"); - if (!hostname) { - BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp, self_comp_class, - "Error borrowing \"target-hostname\" entry."); - ret = -1; - goto end; + for (const auto value : results) { + const auto map = value.asMap(); + const auto hostnameVal = map["target-hostname"]; + + if (!hostnameVal) { + BT_CPPLOGE_APPEND_CAUSE_SPEC(viewer_connection->logger, + "Error borrowing \"target-hostname\" entry."); + return -1; } - session_name = bt_value_map_borrow_entry_value(map, "session-name"); - if (!session_name) { - BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp, self_comp_class, - "Error borrowing \"session-name\" entry."); - ret = -1; - goto end; + + const auto sessionNameVal = map["session-name"]; + + if (!sessionNameVal) { + BT_CPPLOGE_APPEND_CAUSE_SPEC(viewer_connection->logger, + "Error borrowing \"session-name\" entry."); + return -1; } - hostname_str = bt_value_string_get(hostname); - session_name_str = bt_value_string_get(session_name); + + const auto hostname_str = hostnameVal->asString().value(); + const auto session_name_str = sessionNameVal->asString().value(); if (strcmp(session->hostname, hostname_str) == 0 && strcmp(session->session_name, session_name_str) == 0) { - int64_t val; uint32_t streams = be32toh(session->streams); uint32_t clients = be32toh(session->clients); found = true; - btval = bt_value_map_borrow_entry_value(map, "stream-count"); - if (!btval) { - BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp, self_comp_class, - "Error borrowing \"stream-count\" entry."); - ret = -1; - goto end; + const auto streamCountVal = map["stream-count"]; + + if (!streamCountVal) { + BT_CPPLOGE_APPEND_CAUSE_SPEC(viewer_connection->logger, + "Error borrowing \"stream-count\" entry."); + return -1; } - val = bt_value_integer_unsigned_get(btval); + + auto val = streamCountVal->asUnsignedInteger().value(); + /* sum */ val += streams; - bt_value_integer_unsigned_set(btval, val); + streamCountVal->asUnsignedInteger().value(val); - btval = bt_value_map_borrow_entry_value(map, "client-count"); - if (!btval) { - BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp, self_comp_class, - "Error borrowing \"client-count\" entry."); - ret = -1; - goto end; + const auto clientCountVal = map["client-count"]; + + if (!clientCountVal) { + BT_CPPLOGE_APPEND_CAUSE_SPEC(viewer_connection->logger, + "Error borrowing \"client-count\" entry."); + return -1; } - val = bt_value_integer_unsigned_get(btval); + + val = clientCountVal->asUnsignedInteger().value(); + /* max */ - val = bt_max_t(int64_t, clients, val); - bt_value_integer_unsigned_set(btval, val); + val = std::max(clients, val); + clientCountVal->asUnsignedInteger().value(val); } if (found) { break; } } -end: + *_found = found; - return ret; + return 0; } -static int list_append_session(bt_value *results, GString *base_url, +static int list_append_session(const bt2::ArrayValue results, const std::string& base_url, const struct lttng_viewer_session *session, struct live_viewer_connection *viewer_connection) { int ret = 0; - bt_self_component_class *self_comp_class = viewer_connection->self_comp_class; - bt_value_map_insert_entry_status insert_status; - bt_value_array_append_element_status append_status; - bt_value *map = NULL; - GString *url = NULL; bool found = false; /* @@ -634,60 +499,34 @@ static int list_append_session(bt_value *results, GString *base_url, */ ret = list_update_session(results, session, &found, viewer_connection); if (ret || found) { - goto end; + return ret; } - map = bt_value_map_create(); - if (!map) { - BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, "Error creating map value."); - ret = -1; - goto end; - } + const auto map = bt2::MapValue::create(); - if (base_url->len < 1) { - BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, "Error: base_url length smaller than 1."); - ret = -1; - goto end; + if (base_url.empty()) { + BT_CPPLOGE_APPEND_CAUSE_SPEC(viewer_connection->logger, "Error: base_url empty."); + return -1; } + /* * key = "url", * value = , */ - url = g_string_new(base_url->str); - g_string_append(url, "/host/"); - g_string_append(url, session->hostname); - g_string_append_c(url, '/'); - g_string_append(url, session->session_name); - - insert_status = bt_value_map_insert_string_entry(map, "url", url->str); - if (insert_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) { - BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, "Error inserting \"url\" entry."); - ret = -1; - goto end; - } + map->insert("url", + fmt::format("{}/host/{}/{}", base_url, session->hostname, session->session_name)); /* * key = "target-hostname", * value = , */ - insert_status = bt_value_map_insert_string_entry(map, "target-hostname", session->hostname); - if (insert_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) { - BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, - "Error inserting \"target-hostname\" entry."); - ret = -1; - goto end; - } + map->insert("target-hostname", session->hostname); /* * key = "session-name", * value = , */ - insert_status = bt_value_map_insert_string_entry(map, "session-name", session->session_name); - if (insert_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) { - BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, "Error inserting \"session-name\" entry."); - ret = -1; - goto end; - } + map->insert("session-name", session->session_name); /* * key = "timer-us", @@ -696,12 +535,7 @@ static int list_append_session(bt_value *results, GString *base_url, { uint32_t live_timer = be32toh(session->live_timer); - insert_status = bt_value_map_insert_unsigned_integer_entry(map, "timer-us", live_timer); - if (insert_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) { - BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, "Error inserting \"timer-us\" entry."); - ret = -1; - goto end; - } + map->insert("timer-us", (uint64_t) live_timer); } /* @@ -711,13 +545,7 @@ static int list_append_session(bt_value *results, GString *base_url, { uint32_t streams = be32toh(session->streams); - insert_status = bt_value_map_insert_unsigned_integer_entry(map, "stream-count", streams); - if (insert_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) { - BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, - "Error inserting \"stream-count\" entry."); - ret = -1; - goto end; - } + map->insert("stream-count", (uint64_t) streams); } /* @@ -727,27 +555,11 @@ static int list_append_session(bt_value *results, GString *base_url, { uint32_t clients = be32toh(session->clients); - insert_status = bt_value_map_insert_unsigned_integer_entry(map, "client-count", clients); - if (insert_status != BT_VALUE_MAP_INSERT_ENTRY_STATUS_OK) { - BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, - "Error inserting \"client-count\" entry."); - ret = -1; - goto end; - } + map->insert("client-count", (uint64_t) clients); } - append_status = bt_value_array_append_element(results, map); - if (append_status != BT_VALUE_ARRAY_APPEND_ELEMENT_STATUS_OK) { - BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, "Error appending map to results."); - ret = -1; - } - -end: - if (url) { - g_string_free(url, true); - } - BT_VALUE_PUT_REF_AND_RESET(map); - return ret; + results.append(*map); + return 0; } /* @@ -786,27 +598,17 @@ end: * } */ -bt_component_class_query_method_status -live_viewer_connection_list_sessions(struct live_viewer_connection *viewer_connection, - const bt_value **user_result) +bt2::Value::Shared +live_viewer_connection_list_sessions(struct live_viewer_connection *viewer_connection) { - bt_self_component_class *self_comp_class = viewer_connection->self_comp_class; - bt_component_class_query_method_status status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_OK; - bt_value *result = NULL; enum lttng_live_viewer_status viewer_status; struct lttng_viewer_cmd cmd; struct lttng_viewer_list_sessions list; uint32_t i, sessions_count; + auto result = bt2::ArrayValue::create(); - result = bt_value_array_create(); - if (!result) { - BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, "Error creating array"); - status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_MEMORY_ERROR; - goto error; - } - - BT_LOGD("Requesting list of sessions: cmd=%s", - lttng_viewer_command_string(LTTNG_VIEWER_LIST_SESSIONS)); + BT_CPPLOGD_SPEC(viewer_connection->logger, "Requesting list of sessions: cmd={}", + LTTNG_VIEWER_LIST_SESSIONS); cmd.cmd = htobe32(LTTNG_VIEWER_LIST_SESSIONS); cmd.data_size = htobe64((uint64_t) 0); @@ -814,22 +616,18 @@ live_viewer_connection_list_sessions(struct live_viewer_connection *viewer_conne viewer_status = lttng_live_send(viewer_connection, &cmd, sizeof(cmd)); if (viewer_status == LTTNG_LIVE_VIEWER_STATUS_ERROR) { - BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, "Error sending list sessions command"); - status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR; - goto error; + BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC(viewer_connection->logger, bt2::Error, + "Error sending list sessions command"); } else if (viewer_status == LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED) { - status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_AGAIN; - goto error; + throw bt2c::TryAgain {}; } viewer_status = lttng_live_recv(viewer_connection, &list, sizeof(list)); if (viewer_status == LTTNG_LIVE_VIEWER_STATUS_ERROR) { - BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, "Error receiving session list"); - status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR; - goto error; + BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC(viewer_connection->logger, bt2::Error, + "Error receiving session list"); } else if (viewer_status == LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED) { - status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_AGAIN; - goto error; + throw bt2c::TryAgain {}; } sessions_count = be32toh(list.sessions_count); @@ -838,29 +636,21 @@ live_viewer_connection_list_sessions(struct live_viewer_connection *viewer_conne viewer_status = lttng_live_recv(viewer_connection, &lsession, sizeof(lsession)); if (viewer_status == LTTNG_LIVE_VIEWER_STATUS_ERROR) { - BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, "Error receiving session:"); - status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR; - goto error; + BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC(viewer_connection->logger, bt2::Error, + "Error receiving session:"); } else if (viewer_status == LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED) { - status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_AGAIN; - goto error; + throw bt2c::TryAgain {}; } lsession.hostname[LTTNG_VIEWER_HOST_NAME_MAX - 1] = '\0'; lsession.session_name[LTTNG_VIEWER_NAME_MAX - 1] = '\0'; - if (list_append_session(result, viewer_connection->url, &lsession, viewer_connection)) { - BT_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp_class, "Error appending session"); - status = BT_COMPONENT_CLASS_QUERY_METHOD_STATUS_ERROR; - goto error; + if (list_append_session(*result, viewer_connection->url, &lsession, viewer_connection)) { + BT_CPPLOGE_APPEND_CAUSE_AND_THROW_SPEC(viewer_connection->logger, bt2::Error, + "Error appending session"); } } - *user_result = result; - goto end; -error: - BT_VALUE_PUT_REF_AND_RESET(result); -end: - return status; + return result; } static enum lttng_live_viewer_status @@ -872,12 +662,11 @@ lttng_live_query_session_ids(struct lttng_live_msg_iter *lttng_live_msg_iter) uint32_t i, sessions_count; uint64_t session_id; enum lttng_live_viewer_status status; - struct live_viewer_connection *viewer_connection = lttng_live_msg_iter->viewer_connection; - bt_self_component *self_comp = viewer_connection->self_comp; - bt_self_component_class *self_comp_class = viewer_connection->self_comp_class; + live_viewer_connection *viewer_connection = lttng_live_msg_iter->viewer_connection.get(); - BT_COMP_LOGD("Asking the relay daemon for the list of sessions: cmd=%s", - lttng_viewer_command_string(LTTNG_VIEWER_LIST_SESSIONS)); + BT_CPPLOGD_SPEC(viewer_connection->logger, + "Asking the relay daemon for the list of sessions: cmd={}", + LTTNG_VIEWER_LIST_SESSIONS); cmd.cmd = htobe32(LTTNG_VIEWER_LIST_SESSIONS); cmd.data_size = htobe64((uint64_t) 0); @@ -885,13 +674,13 @@ lttng_live_query_session_ids(struct lttng_live_msg_iter *lttng_live_msg_iter) status = lttng_live_send(viewer_connection, &cmd, sizeof(cmd)); if (status != LTTNG_LIVE_VIEWER_STATUS_OK) { - viewer_handle_send_status(self_comp, self_comp_class, status, "list sessions command"); + viewer_handle_send_status(status, "list sessions command"); goto end; } status = lttng_live_recv(viewer_connection, &list, sizeof(list)); if (status != LTTNG_LIVE_VIEWER_STATUS_OK) { - viewer_handle_recv_status(self_comp, self_comp_class, status, "session list reply"); + viewer_handle_recv_status(status, "session list reply"); goto end; } @@ -899,16 +688,17 @@ lttng_live_query_session_ids(struct lttng_live_msg_iter *lttng_live_msg_iter) for (i = 0; i < sessions_count; i++) { status = lttng_live_recv(viewer_connection, &lsession, sizeof(lsession)); if (status != LTTNG_LIVE_VIEWER_STATUS_OK) { - viewer_handle_recv_status(self_comp, self_comp_class, status, "session reply"); + viewer_handle_recv_status(status, "session reply"); goto end; } lsession.hostname[LTTNG_VIEWER_HOST_NAME_MAX - 1] = '\0'; lsession.session_name[LTTNG_VIEWER_NAME_MAX - 1] = '\0'; session_id = be64toh(lsession.id); - BT_COMP_LOGI("Adding session to internal list: " - "session-id=%" PRIu64 ", hostname=\"%s\", session-name=\"%s\"", - session_id, lsession.hostname, lsession.session_name); + BT_CPPLOGI_SPEC(viewer_connection->logger, + "Adding session to internal list: " + "session-id={}, hostname=\"{}\", session-name=\"{}\"", + session_id, lsession.hostname, lsession.session_name); if ((strncmp(lsession.session_name, viewer_connection->session_name->str, LTTNG_VIEWER_NAME_MAX) == 0) && @@ -916,7 +706,8 @@ lttng_live_query_session_ids(struct lttng_live_msg_iter *lttng_live_msg_iter) LTTNG_VIEWER_HOST_NAME_MAX) == 0)) { if (lttng_live_add_session(lttng_live_msg_iter, session_id, lsession.hostname, lsession.session_name)) { - BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Failed to add live session"); + BT_CPPLOGE_APPEND_CAUSE_SPEC(viewer_connection->logger, + "Failed to add live session"); status = LTTNG_LIVE_VIEWER_STATUS_ERROR; goto end; } @@ -935,12 +726,10 @@ lttng_live_create_viewer_session(struct lttng_live_msg_iter *lttng_live_msg_iter struct lttng_viewer_cmd cmd; struct lttng_viewer_create_session_response resp; enum lttng_live_viewer_status status; - struct live_viewer_connection *viewer_connection = lttng_live_msg_iter->viewer_connection; - bt_self_component *self_comp = viewer_connection->self_comp; - bt_self_component_class *self_comp_class = viewer_connection->self_comp_class; + live_viewer_connection *viewer_connection = lttng_live_msg_iter->viewer_connection.get(); - BT_COMP_OR_COMP_CLASS_LOGD(self_comp, self_comp_class, "Creating a viewer session: cmd=%s", - lttng_viewer_command_string(LTTNG_VIEWER_CREATE_SESSION)); + BT_CPPLOGD_SPEC(viewer_connection->logger, "Creating a viewer session: cmd={}", + LTTNG_VIEWER_CREATE_SESSION); cmd.cmd = htobe32(LTTNG_VIEWER_CREATE_SESSION); cmd.data_size = htobe64((uint64_t) 0); @@ -948,25 +737,26 @@ lttng_live_create_viewer_session(struct lttng_live_msg_iter *lttng_live_msg_iter status = lttng_live_send(viewer_connection, &cmd, sizeof(cmd)); if (status != LTTNG_LIVE_VIEWER_STATUS_OK) { - viewer_handle_send_status(self_comp, self_comp_class, status, "create session command"); + viewer_handle_send_status(status, "create session command"); goto end; } status = lttng_live_recv(viewer_connection, &resp, sizeof(resp)); if (status != LTTNG_LIVE_VIEWER_STATUS_OK) { - viewer_handle_recv_status(self_comp, self_comp_class, status, "create session reply"); + viewer_handle_recv_status(status, "create session reply"); goto end; } if (be32toh(resp.status) != LTTNG_VIEWER_CREATE_SESSION_OK) { - BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Error creating viewer session"); + BT_CPPLOGE_APPEND_CAUSE_SPEC(viewer_connection->logger, "Error creating viewer session"); status = LTTNG_LIVE_VIEWER_STATUS_ERROR; goto end; } status = lttng_live_query_session_ids(lttng_live_msg_iter); if (status == LTTNG_LIVE_VIEWER_STATUS_ERROR) { - BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Failed to query live viewer session ids"); + BT_CPPLOGE_APPEND_CAUSE_SPEC(viewer_connection->logger, + "Failed to query live viewer session ids"); goto end; } else if (status == LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED) { goto end; @@ -983,10 +773,9 @@ static enum lttng_live_viewer_status receive_streams(struct lttng_live_session * uint32_t i; struct lttng_live_msg_iter *lttng_live_msg_iter = session->lttng_live_msg_iter; enum lttng_live_viewer_status status; - struct live_viewer_connection *viewer_connection = lttng_live_msg_iter->viewer_connection; - bt_self_component *self_comp = viewer_connection->self_comp; + live_viewer_connection *viewer_connection = lttng_live_msg_iter->viewer_connection.get(); - BT_COMP_LOGI("Getting %" PRIu32 " new streams", stream_count); + BT_CPPLOGI_SPEC(viewer_connection->logger, "Getting {} new streams", stream_count); for (i = 0; i < stream_count; i++) { struct lttng_viewer_stream stream; struct lttng_live_stream_iterator *live_stream; @@ -995,7 +784,7 @@ static enum lttng_live_viewer_status receive_streams(struct lttng_live_session * status = lttng_live_recv(viewer_connection, &stream, sizeof(stream)); if (status != LTTNG_LIVE_VIEWER_STATUS_OK) { - viewer_handle_recv_status(self_comp, NULL, status, "stream reply"); + viewer_handle_recv_status(status, "stream reply"); goto end; } stream.path_name[LTTNG_VIEWER_PATH_MAX - 1] = '\0'; @@ -1004,21 +793,22 @@ static enum lttng_live_viewer_status receive_streams(struct lttng_live_session * ctf_trace_id = be64toh(stream.ctf_trace_id); if (stream.metadata_flag) { - BT_COMP_LOGI(" metadata stream %" PRIu64 " : %s/%s", stream_id, stream.path_name, - stream.channel_name); + BT_CPPLOGI_SPEC(viewer_connection->logger, " metadata stream {} : {}/{}", stream_id, + stream.path_name, stream.channel_name); if (lttng_live_metadata_create_stream(session, ctf_trace_id, stream_id)) { - BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Error creating metadata stream"); + BT_CPPLOGE_APPEND_CAUSE_SPEC(viewer_connection->logger, + "Error creating metadata stream"); status = LTTNG_LIVE_VIEWER_STATUS_ERROR; goto end; } session->lazy_stream_msg_init = true; } else { - BT_COMP_LOGI(" stream %" PRIu64 " : %s/%s", stream_id, stream.path_name, - stream.channel_name); + BT_CPPLOGI_SPEC(viewer_connection->logger, " stream {} : {}/{}", stream_id, + stream.path_name, stream.channel_name); live_stream = lttng_live_stream_iterator_create(session, ctf_trace_id, stream_id, self_msg_iter); if (!live_stream) { - BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Error creating stream"); + BT_CPPLOGE_APPEND_CAUSE_SPEC(viewer_connection->logger, "Error creating stream"); status = LTTNG_LIVE_VIEWER_STATUS_ERROR; goto end; } @@ -1038,16 +828,15 @@ enum lttng_live_viewer_status lttng_live_session_attach(struct lttng_live_sessio struct lttng_viewer_attach_session_request rq; struct lttng_viewer_attach_session_response rp; struct lttng_live_msg_iter *lttng_live_msg_iter = session->lttng_live_msg_iter; - struct live_viewer_connection *viewer_connection = lttng_live_msg_iter->viewer_connection; - bt_self_component *self_comp = viewer_connection->self_comp; + live_viewer_connection *viewer_connection = lttng_live_msg_iter->viewer_connection.get(); uint64_t session_id = session->id; uint32_t streams_count; const size_t cmd_buf_len = sizeof(cmd) + sizeof(rq); char cmd_buf[cmd_buf_len]; - BT_COMP_LOGD("Attaching to session: cmd=%s, session-id=%" PRIu64 ", seek=%s", - lttng_viewer_command_string(LTTNG_VIEWER_ATTACH_SESSION), session_id, - lttng_viewer_seek_string(LTTNG_VIEWER_SEEK_LAST)); + BT_CPPLOGD_SPEC(viewer_connection->logger, + "Attaching to session: cmd={}, session-id={}, seek={}", + LTTNG_VIEWER_ATTACH_SESSION, session_id, LTTNG_VIEWER_SEEK_LAST); cmd.cmd = htobe32(LTTNG_VIEWER_ATTACH_SESSION); cmd.data_size = htobe64((uint64_t) sizeof(rq)); @@ -1068,13 +857,13 @@ enum lttng_live_viewer_status lttng_live_session_attach(struct lttng_live_sessio memcpy(cmd_buf + sizeof(cmd), &rq, sizeof(rq)); status = lttng_live_send(viewer_connection, &cmd_buf, cmd_buf_len); if (status != LTTNG_LIVE_VIEWER_STATUS_OK) { - viewer_handle_send_status(self_comp, NULL, status, "attach session command"); + viewer_handle_send_status(status, "attach session command"); goto end; } status = lttng_live_recv(viewer_connection, &rp, sizeof(rp)); if (status != LTTNG_LIVE_VIEWER_STATUS_OK) { - viewer_handle_recv_status(self_comp, NULL, status, "attach session reply"); + viewer_handle_recv_status(status, "attach session reply"); goto end; } @@ -1083,23 +872,26 @@ enum lttng_live_viewer_status lttng_live_session_attach(struct lttng_live_sessio case LTTNG_VIEWER_ATTACH_OK: break; case LTTNG_VIEWER_ATTACH_UNK: - BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Session id %" PRIu64 " is unknown", session_id); + BT_CPPLOGE_APPEND_CAUSE_SPEC(viewer_connection->logger, "Session id {} is unknown", + session_id); status = LTTNG_LIVE_VIEWER_STATUS_ERROR; goto end; case LTTNG_VIEWER_ATTACH_ALREADY: - BT_COMP_LOGE_APPEND_CAUSE(self_comp, "There is already a viewer attached to this session"); + BT_CPPLOGE_APPEND_CAUSE_SPEC(viewer_connection->logger, + "There is already a viewer attached to this session"); status = LTTNG_LIVE_VIEWER_STATUS_ERROR; goto end; case LTTNG_VIEWER_ATTACH_NOT_LIVE: - BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Not a live session"); + BT_CPPLOGE_APPEND_CAUSE_SPEC(viewer_connection->logger, "Not a live session"); status = LTTNG_LIVE_VIEWER_STATUS_ERROR; goto end; case LTTNG_VIEWER_ATTACH_SEEK_ERR: - BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Wrong seek parameter"); + BT_CPPLOGE_APPEND_CAUSE_SPEC(viewer_connection->logger, "Wrong seek parameter"); status = LTTNG_LIVE_VIEWER_STATUS_ERROR; goto end; default: - BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Unknown attach return code %u", be32toh(rp.status)); + BT_CPPLOGE_APPEND_CAUSE_SPEC(viewer_connection->logger, "Unknown attach return code {}", + be32toh(rp.status)); status = LTTNG_LIVE_VIEWER_STATUS_ERROR; goto end; } @@ -1112,7 +904,7 @@ enum lttng_live_viewer_status lttng_live_session_attach(struct lttng_live_sessio case LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED: goto end; case LTTNG_LIVE_VIEWER_STATUS_ERROR: - BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Error receiving streams"); + BT_CPPLOGE_APPEND_CAUSE_SPEC(viewer_connection->logger, "Error receiving streams"); goto end; default: bt_common_abort(); @@ -1132,8 +924,7 @@ enum lttng_live_viewer_status lttng_live_session_detach(struct lttng_live_sessio struct lttng_viewer_detach_session_request rq; struct lttng_viewer_detach_session_response rp; struct lttng_live_msg_iter *lttng_live_msg_iter = session->lttng_live_msg_iter; - bt_self_component *self_comp = session->self_comp; - struct live_viewer_connection *viewer_connection = lttng_live_msg_iter->viewer_connection; + live_viewer_connection *viewer_connection = lttng_live_msg_iter->viewer_connection.get(); uint64_t session_id = session->id; const size_t cmd_buf_len = sizeof(cmd) + sizeof(rq); char cmd_buf[cmd_buf_len]; @@ -1147,8 +938,8 @@ enum lttng_live_viewer_status lttng_live_session_detach(struct lttng_live_sessio return LTTNG_LIVE_VIEWER_STATUS_OK; } - BT_COMP_LOGD("Detaching from session: cmd=%s, session-id=%" PRIu64, - lttng_viewer_command_string(LTTNG_VIEWER_DETACH_SESSION), session_id); + BT_CPPLOGD_SPEC(viewer_connection->logger, "Detaching from session: cmd={}, session-id={}", + LTTNG_VIEWER_DETACH_SESSION, session_id); cmd.cmd = htobe32(LTTNG_VIEWER_DETACH_SESSION); cmd.data_size = htobe64((uint64_t) sizeof(rq)); @@ -1166,13 +957,13 @@ enum lttng_live_viewer_status lttng_live_session_detach(struct lttng_live_sessio memcpy(cmd_buf + sizeof(cmd), &rq, sizeof(rq)); status = lttng_live_send(viewer_connection, &cmd_buf, cmd_buf_len); if (status != LTTNG_LIVE_VIEWER_STATUS_OK) { - viewer_handle_send_status(self_comp, NULL, status, "detach session command"); + viewer_handle_send_status(status, "detach session command"); goto end; } status = lttng_live_recv(viewer_connection, &rp, sizeof(rp)); if (status != LTTNG_LIVE_VIEWER_STATUS_OK) { - viewer_handle_recv_status(self_comp, NULL, status, "detach session reply"); + viewer_handle_recv_status(status, "detach session reply"); goto end; } @@ -1180,15 +971,16 @@ enum lttng_live_viewer_status lttng_live_session_detach(struct lttng_live_sessio case LTTNG_VIEWER_DETACH_SESSION_OK: break; case LTTNG_VIEWER_DETACH_SESSION_UNK: - BT_COMP_LOGW("Session id %" PRIu64 " is unknown", session_id); + BT_CPPLOGW_SPEC(viewer_connection->logger, "Session id {} is unknown", session_id); status = LTTNG_LIVE_VIEWER_STATUS_ERROR; goto end; case LTTNG_VIEWER_DETACH_SESSION_ERR: - BT_COMP_LOGW("Error detaching session id %" PRIu64 "", session_id); + BT_CPPLOGW_SPEC(viewer_connection->logger, "Error detaching session id {}", session_id); status = LTTNG_LIVE_VIEWER_STATUS_ERROR; goto end; default: - BT_COMP_LOGE("Unknown detach return code %u", be32toh(rp.status)); + BT_CPPLOGE_SPEC(viewer_connection->logger, "Unknown detach return code {}", + be32toh(rp.status)); status = LTTNG_LIVE_VIEWER_STATUS_ERROR; goto end; } @@ -1202,7 +994,7 @@ end: } enum lttng_live_get_one_metadata_status -lttng_live_get_one_metadata_packet(struct lttng_live_trace *trace, FILE *fp, size_t *reply_len) +lttng_live_get_one_metadata_packet(struct lttng_live_trace *trace, std::vector& buf) { uint64_t len = 0; enum lttng_live_get_one_metadata_status status; @@ -1210,20 +1002,18 @@ lttng_live_get_one_metadata_packet(struct lttng_live_trace *trace, FILE *fp, siz struct lttng_viewer_cmd cmd; struct lttng_viewer_get_metadata rq; struct lttng_viewer_metadata_packet rp; - gchar *data = NULL; - ssize_t writelen; + std::vector data; struct lttng_live_session *session = trace->session; struct lttng_live_msg_iter *lttng_live_msg_iter = session->lttng_live_msg_iter; - struct lttng_live_metadata *metadata = trace->metadata; - struct live_viewer_connection *viewer_connection = lttng_live_msg_iter->viewer_connection; - bt_self_component *self_comp = viewer_connection->self_comp; + struct lttng_live_metadata *metadata = trace->metadata.get(); + live_viewer_connection *viewer_connection = lttng_live_msg_iter->viewer_connection.get(); const size_t cmd_buf_len = sizeof(cmd) + sizeof(rq); char cmd_buf[cmd_buf_len]; - BT_COMP_LOGD("Requesting new metadata for trace:" - "cmd=%s, trace-id=%" PRIu64 ", metadata-stream-id=%" PRIu64, - lttng_viewer_command_string(LTTNG_VIEWER_GET_METADATA), trace->id, - metadata->stream_id); + BT_CPPLOGD_SPEC(viewer_connection->logger, + "Requesting new metadata for trace:" + "cmd={}, trace-id={}, metadata-stream-id={}", + LTTNG_VIEWER_GET_METADATA, trace->id, metadata->stream_id); rq.stream_id = htobe64(metadata->stream_id); cmd.cmd = htobe32(LTTNG_VIEWER_GET_METADATA); @@ -1239,24 +1029,24 @@ lttng_live_get_one_metadata_packet(struct lttng_live_trace *trace, FILE *fp, siz memcpy(cmd_buf + sizeof(cmd), &rq, sizeof(rq)); viewer_status = lttng_live_send(viewer_connection, &cmd_buf, cmd_buf_len); if (viewer_status != LTTNG_LIVE_VIEWER_STATUS_OK) { - viewer_handle_send_status(self_comp, NULL, viewer_status, "get metadata command"); + viewer_handle_send_status(viewer_status, "get metadata command"); status = (enum lttng_live_get_one_metadata_status) viewer_status; goto end; } viewer_status = lttng_live_recv(viewer_connection, &rp, sizeof(rp)); if (viewer_status != LTTNG_LIVE_VIEWER_STATUS_OK) { - viewer_handle_recv_status(self_comp, NULL, viewer_status, "get metadata reply"); + viewer_handle_recv_status(viewer_status, "get metadata reply"); status = (enum lttng_live_get_one_metadata_status) viewer_status; goto end; } switch (be32toh(rp.status)) { case LTTNG_VIEWER_METADATA_OK: - BT_COMP_LOGD("Received get_metadata response: ok"); + BT_CPPLOGD_SPEC(viewer_connection->logger, "Received get_metadata response: ok"); break; case LTTNG_VIEWER_NO_NEW_METADATA: - BT_COMP_LOGD("Received get_metadata response: no new"); + BT_CPPLOGD_SPEC(viewer_connection->logger, "Received get_metadata response: no new"); status = LTTNG_LIVE_GET_ONE_METADATA_STATUS_END; goto end; case LTTNG_VIEWER_METADATA_ERR: @@ -1265,11 +1055,12 @@ lttng_live_get_one_metadata_packet(struct lttng_live_trace *trace, FILE *fp, siz * gone already. This can happen in short lived UST app * in a per-pid session. */ - BT_COMP_LOGD("Received get_metadata response: error"); + BT_CPPLOGD_SPEC(viewer_connection->logger, "Received get_metadata response: error"); status = LTTNG_LIVE_GET_ONE_METADATA_STATUS_CLOSED; goto end; default: - BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Received get_metadata response: unknown"); + BT_CPPLOGE_APPEND_CAUSE_SPEC(viewer_connection->logger, + "Received get_metadata response: unknown"); status = LTTNG_LIVE_GET_ONE_METADATA_STATUS_ERROR; goto end; } @@ -1281,28 +1072,24 @@ lttng_live_get_one_metadata_packet(struct lttng_live_trace *trace, FILE *fp, siz * length of 0. This means we must try again. This scenario * arises when a clear command is performed on an lttng session. */ - BT_COMP_LOGD( + BT_CPPLOGD_SPEC( + viewer_connection->logger, "Expecting a metadata packet of size 0. Retry to get a packet from the relay."); goto empty_metadata_packet_retry; } - BT_COMP_LOGD("Writing %" PRIu64 " bytes to metadata", len); + BT_CPPLOGD_SPEC(viewer_connection->logger, "Writing {} bytes to metadata", len); if (len <= 0) { - BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Erroneous response length"); + BT_CPPLOGE_APPEND_CAUSE_SPEC(viewer_connection->logger, "Erroneous response length"); status = LTTNG_LIVE_GET_ONE_METADATA_STATUS_ERROR; goto end; } - data = g_new0(gchar, len); - if (!data) { - BT_COMP_LOGE_APPEND_CAUSE_ERRNO(self_comp, "Failed to allocate data buffer", "."); - status = LTTNG_LIVE_GET_ONE_METADATA_STATUS_ERROR; - goto end; - } + data.resize(len); - viewer_status = lttng_live_recv(viewer_connection, data, len); + viewer_status = lttng_live_recv(viewer_connection, data.data(), len); if (viewer_status != LTTNG_LIVE_VIEWER_STATUS_OK) { - viewer_handle_recv_status(self_comp, NULL, viewer_status, "get metadata packet"); + viewer_handle_recv_status(viewer_status, "get metadata packet"); status = (enum lttng_live_get_one_metadata_status) viewer_status; goto end; } @@ -1310,19 +1097,12 @@ lttng_live_get_one_metadata_packet(struct lttng_live_trace *trace, FILE *fp, siz /* * Write the metadata to the file handle. */ - writelen = fwrite(data, sizeof(uint8_t), len, fp); - if (writelen != len) { - BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Writing in the metadata file stream"); - status = LTTNG_LIVE_GET_ONE_METADATA_STATUS_ERROR; - goto end; - } + buf.insert(buf.end(), data.begin(), data.end()); empty_metadata_packet_retry: - *reply_len = len; status = LTTNG_LIVE_GET_ONE_METADATA_STATUS_OK; end: - g_free(data); return status; } @@ -1345,15 +1125,11 @@ static void lttng_index_to_packet_index(struct lttng_viewer_index *lindex, static void lttng_live_need_new_streams(struct lttng_live_msg_iter *lttng_live_msg_iter) { - uint64_t session_idx; - struct live_viewer_connection *viewer_connection = lttng_live_msg_iter->viewer_connection; - - for (session_idx = 0; session_idx < lttng_live_msg_iter->sessions->len; session_idx++) { - struct lttng_live_session *session = - (lttng_live_session *) g_ptr_array_index(lttng_live_msg_iter->sessions, session_idx); - BT_COMP_LOGD("Marking session as needing new streams: " - "session-id=%" PRIu64, - session->id); + for (const auto& session : lttng_live_msg_iter->sessions) { + BT_CPPLOGD_SPEC(lttng_live_msg_iter->logger, + "Marking session as needing new streams: " + "session-id={}", + session->id); session->new_streams_needed = true; } } @@ -1367,17 +1143,16 @@ lttng_live_get_next_index(struct lttng_live_msg_iter *lttng_live_msg_iter, enum lttng_live_viewer_status viewer_status; struct lttng_viewer_index rp; enum lttng_live_iterator_status status; - struct live_viewer_connection *viewer_connection = lttng_live_msg_iter->viewer_connection; - bt_self_component *self_comp = viewer_connection->self_comp; + live_viewer_connection *viewer_connection = lttng_live_msg_iter->viewer_connection.get(); struct lttng_live_trace *trace = stream->trace; const size_t cmd_buf_len = sizeof(cmd) + sizeof(rq); char cmd_buf[cmd_buf_len]; uint32_t flags, rp_status; - BT_COMP_LOGD("Requesting next index for stream: cmd=%s, " - "viewer-stream-id=%" PRIu64, - lttng_viewer_command_string(LTTNG_VIEWER_GET_NEXT_INDEX), - stream->viewer_stream_id); + BT_CPPLOGD_SPEC(viewer_connection->logger, + "Requesting next index for stream: cmd={}, " + "viewer-stream-id={}", + LTTNG_VIEWER_GET_NEXT_INDEX, stream->viewer_stream_id); cmd.cmd = htobe32(LTTNG_VIEWER_GET_NEXT_INDEX); cmd.data_size = htobe64((uint64_t) sizeof(rq)); cmd.cmd_version = htobe32(0); @@ -1395,22 +1170,31 @@ lttng_live_get_next_index(struct lttng_live_msg_iter *lttng_live_msg_iter, viewer_status = lttng_live_send(viewer_connection, &cmd_buf, cmd_buf_len); if (viewer_status != LTTNG_LIVE_VIEWER_STATUS_OK) { - viewer_handle_send_status(self_comp, NULL, viewer_status, "get next index command"); + viewer_handle_send_status(viewer_status, "get next index command"); goto error; } viewer_status = lttng_live_recv(viewer_connection, &rp, sizeof(rp)); if (viewer_status != LTTNG_LIVE_VIEWER_STATUS_OK) { - viewer_handle_recv_status(self_comp, NULL, viewer_status, "get next index reply"); + viewer_handle_recv_status(viewer_status, "get next index reply"); goto error; } flags = be32toh(rp.flags); rp_status = be32toh(rp.status); - BT_COMP_LOGD("Received response from relay daemon: cmd=%s, response=%s", - lttng_viewer_command_string(LTTNG_VIEWER_GET_NEXT_INDEX), - lttng_viewer_next_index_return_code_string(rp_status)); + BT_CPPLOGD_SPEC( + viewer_connection->logger, "Received response from relay daemon: cmd=%s, response={}", + LTTNG_VIEWER_GET_NEXT_INDEX, static_cast(rp_status)); + + if (flags & LTTNG_VIEWER_FLAG_NEW_STREAM) { + BT_CPPLOGD_SPEC(viewer_connection->logger, + "Marking all sessions as possibly needing new streams: " + "response={}, response-flag=NEW_STREAM", + static_cast(rp_status)); + lttng_live_need_new_streams(lttng_live_msg_iter); + } + switch (rp_status) { case LTTNG_VIEWER_INDEX_INACTIVE: { @@ -1442,21 +1226,15 @@ lttng_live_get_next_index(struct lttng_live_msg_iter *lttng_live_msg_iter, stream->ctf_stream_class_id.value = ctf_stream_class_id; stream->ctf_stream_class_id.is_set = true; } - lttng_live_stream_iterator_set_state(stream, LTTNG_LIVE_STREAM_ACTIVE_DATA); if (flags & LTTNG_VIEWER_FLAG_NEW_METADATA) { - BT_COMP_LOGD("Marking trace as needing new metadata: " - "response=%s, response-flag=NEW_METADATA, trace-id=%" PRIu64, - lttng_viewer_next_index_return_code_string(rp_status), trace->id); + BT_CPPLOGD_SPEC(viewer_connection->logger, + "Marking trace as needing new metadata: " + "response={}, response-flag=NEW_METADATA, trace-id={}", + static_cast(rp_status), trace->id); trace->metadata_stream_state = LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED; } - if (flags & LTTNG_VIEWER_FLAG_NEW_STREAM) { - BT_COMP_LOGD("Marking all sessions as possibly needing new streams: " - "response=%s, response-flag=NEW_STREAM", - lttng_viewer_next_index_return_code_string(rp_status)); - lttng_live_need_new_streams(lttng_live_msg_iter); - } status = LTTNG_LIVE_ITERATOR_STATUS_OK; break; } @@ -1478,12 +1256,14 @@ lttng_live_get_next_index(struct lttng_live_msg_iter *lttng_live_msg_iter, status = LTTNG_LIVE_ITERATOR_STATUS_ERROR; goto end; default: - BT_COMP_LOGD("Received get_next_index response: unknown value"); + BT_CPPLOGD_SPEC(viewer_connection->logger, + "Received get_next_index response: unknown value"); memset(index, 0, sizeof(struct packet_index)); lttng_live_stream_iterator_set_state(stream, LTTNG_LIVE_STREAM_ACTIVE_NO_DATA); status = LTTNG_LIVE_ITERATOR_STATUS_ERROR; goto end; } + goto end; error: @@ -1502,16 +1282,16 @@ lttng_live_get_stream_bytes(struct lttng_live_msg_iter *lttng_live_msg_iter, struct lttng_viewer_trace_packet rp; struct lttng_viewer_cmd cmd; struct lttng_viewer_get_packet rq; - struct live_viewer_connection *viewer_connection = lttng_live_msg_iter->viewer_connection; - bt_self_component *self_comp = viewer_connection->self_comp; + live_viewer_connection *viewer_connection = lttng_live_msg_iter->viewer_connection.get(); struct lttng_live_trace *trace = stream->trace; const size_t cmd_buf_len = sizeof(cmd) + sizeof(rq); char cmd_buf[cmd_buf_len]; uint32_t flags, rp_status; - BT_COMP_LOGD("Requesting data from stream: cmd=%s, " - "offset=%" PRIu64 ", request-len=%" PRIu64, - lttng_viewer_command_string(LTTNG_VIEWER_GET_PACKET), offset, req_len); + BT_CPPLOGD_SPEC(viewer_connection->logger, + "Requesting data from stream: cmd={}, " + "offset={}, request-len={}", + LTTNG_VIEWER_GET_PACKET, offset, req_len); cmd.cmd = htobe32(LTTNG_VIEWER_GET_PACKET); cmd.data_size = htobe64((uint64_t) sizeof(rq)); @@ -1532,27 +1312,28 @@ lttng_live_get_stream_bytes(struct lttng_live_msg_iter *lttng_live_msg_iter, viewer_status = lttng_live_send(viewer_connection, &cmd_buf, cmd_buf_len); if (viewer_status != LTTNG_LIVE_VIEWER_STATUS_OK) { - viewer_handle_send_status(self_comp, NULL, viewer_status, "get data packet command"); + viewer_handle_send_status(viewer_status, "get data packet command"); goto error_convert_status; } viewer_status = lttng_live_recv(viewer_connection, &rp, sizeof(rp)); if (viewer_status != LTTNG_LIVE_VIEWER_STATUS_OK) { - viewer_handle_recv_status(self_comp, NULL, viewer_status, "get data packet reply"); + viewer_handle_recv_status(viewer_status, "get data packet reply"); goto error_convert_status; } flags = be32toh(rp.flags); rp_status = be32toh(rp.status); - BT_COMP_LOGD("Received response from relay daemon: cmd=%s, response=%s", - lttng_viewer_command_string(LTTNG_VIEWER_GET_PACKET), - lttng_viewer_get_packet_return_code_string(rp_status)); + BT_CPPLOGD_SPEC( + viewer_connection->logger, "Received response from relay daemon: cmd={}, response={}", + LTTNG_VIEWER_GET_PACKET, static_cast(rp_status)); switch (rp_status) { case LTTNG_VIEWER_GET_PACKET_OK: req_len = be32toh(rp.len); - BT_COMP_LOGD("Got packet from relay daemon: response=%s, packet-len=%" PRIu64 "", - lttng_viewer_get_packet_return_code_string(rp_status), req_len); + BT_CPPLOGD_SPEC(viewer_connection->logger, + "Got packet from relay daemon: response={}, packet-len={}", + static_cast(rp_status), req_len); break; case LTTNG_VIEWER_GET_PACKET_RETRY: /* Unimplemented by relay daemon */ @@ -1560,32 +1341,36 @@ lttng_live_get_stream_bytes(struct lttng_live_msg_iter *lttng_live_msg_iter, goto end; case LTTNG_VIEWER_GET_PACKET_ERR: if (flags & LTTNG_VIEWER_FLAG_NEW_METADATA) { - BT_COMP_LOGD("Marking trace as needing new metadata: " - "response=%s, response-flag=NEW_METADATA, trace-id=%" PRIu64, - lttng_viewer_next_index_return_code_string(rp_status), trace->id); + BT_CPPLOGD_SPEC(viewer_connection->logger, + "Marking trace as needing new metadata: " + "response={}, response-flag=NEW_METADATA, trace-id={}", + static_cast(rp_status), trace->id); trace->metadata_stream_state = LTTNG_LIVE_METADATA_STREAM_STATE_NEEDED; } if (flags & LTTNG_VIEWER_FLAG_NEW_STREAM) { - BT_COMP_LOGD("Marking all sessions as possibly needing new streams: " - "response=%s, response-flag=NEW_STREAM", - lttng_viewer_next_index_return_code_string(rp_status)); + BT_CPPLOGD_SPEC(viewer_connection->logger, + "Marking all sessions as possibly needing new streams: " + "response={}, response-flag=NEW_STREAM", + static_cast(rp_status)); lttng_live_need_new_streams(lttng_live_msg_iter); } if (flags & (LTTNG_VIEWER_FLAG_NEW_METADATA | LTTNG_VIEWER_FLAG_NEW_STREAM)) { status = CTF_MSG_ITER_MEDIUM_STATUS_AGAIN; - BT_COMP_LOGD("Reply with any one flags set means we should retry: response=%s", - lttng_viewer_get_packet_return_code_string(rp_status)); + BT_CPPLOGD_SPEC(viewer_connection->logger, + "Reply with any one flags set means we should retry: response={}", + static_cast(rp_status)); goto end; } - BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Received get_data_packet response: error"); + BT_CPPLOGE_APPEND_CAUSE_SPEC(viewer_connection->logger, + "Received get_data_packet response: error"); status = CTF_MSG_ITER_MEDIUM_STATUS_ERROR; goto end; case LTTNG_VIEWER_GET_PACKET_EOF: status = CTF_MSG_ITER_MEDIUM_STATUS_EOF; goto end; default: - BT_COMP_LOGE_APPEND_CAUSE(self_comp, "Received get_data_packet response: unknown (%d)", - rp_status); + BT_CPPLOGE_APPEND_CAUSE_SPEC(viewer_connection->logger, + "Received get_data_packet response: unknown ({})", rp_status); status = CTF_MSG_ITER_MEDIUM_STATUS_ERROR; goto end; } @@ -1597,7 +1382,7 @@ lttng_live_get_stream_bytes(struct lttng_live_msg_iter *lttng_live_msg_iter, viewer_status = lttng_live_recv(viewer_connection, buf, req_len); if (viewer_status != LTTNG_LIVE_VIEWER_STATUS_OK) { - viewer_handle_recv_status(self_comp, NULL, viewer_status, "get data packet"); + viewer_handle_recv_status(viewer_status, "get data packet"); goto error_convert_status; } *recv_len = req_len; @@ -1624,8 +1409,7 @@ lttng_live_session_get_new_streams(struct lttng_live_session *session, struct lttng_viewer_new_streams_response rp; struct lttng_live_msg_iter *lttng_live_msg_iter = session->lttng_live_msg_iter; enum lttng_live_viewer_status viewer_status; - struct live_viewer_connection *viewer_connection = lttng_live_msg_iter->viewer_connection; - bt_self_component *self_comp = viewer_connection->self_comp; + live_viewer_connection *viewer_connection = lttng_live_msg_iter->viewer_connection.get(); uint32_t streams_count; const size_t cmd_buf_len = sizeof(cmd) + sizeof(rq); char cmd_buf[cmd_buf_len]; @@ -1635,9 +1419,9 @@ lttng_live_session_get_new_streams(struct lttng_live_session *session, goto end; } - BT_COMP_LOGD("Requesting new streams for session: cmd=%s, " - "session-id=%" PRIu64, - lttng_viewer_command_string(LTTNG_VIEWER_GET_NEW_STREAMS), session->id); + BT_CPPLOGD_SPEC(viewer_connection->logger, + "Requesting new streams for session: cmd={}, session-id={}", + LTTNG_VIEWER_GET_NEW_STREAMS, session->id); cmd.cmd = htobe32(LTTNG_VIEWER_GET_NEW_STREAMS); cmd.data_size = htobe64((uint64_t) sizeof(rq)); @@ -1656,14 +1440,14 @@ lttng_live_session_get_new_streams(struct lttng_live_session *session, viewer_status = lttng_live_send(viewer_connection, &cmd_buf, cmd_buf_len); if (viewer_status != LTTNG_LIVE_VIEWER_STATUS_OK) { - viewer_handle_send_status(self_comp, NULL, viewer_status, "get new streams command"); + viewer_handle_send_status(viewer_status, "get new streams command"); status = viewer_status_to_live_iterator_status(viewer_status); goto end; } viewer_status = lttng_live_recv(viewer_connection, &rp, sizeof(rp)); if (viewer_status != LTTNG_LIVE_VIEWER_STATUS_OK) { - viewer_handle_recv_status(self_comp, NULL, viewer_status, "get new streams reply"); + viewer_handle_recv_status(viewer_status, "get new streams reply"); status = viewer_status_to_live_iterator_status(viewer_status); goto end; } @@ -1683,21 +1467,21 @@ lttng_live_session_get_new_streams(struct lttng_live_session *session, status = LTTNG_LIVE_ITERATOR_STATUS_END; goto end; case LTTNG_VIEWER_NEW_STREAMS_ERR: - BT_COMP_LOGD("Received get_new_streams response: error"); + BT_CPPLOGD_SPEC(viewer_connection->logger, "Received get_new_streams response: error"); status = LTTNG_LIVE_ITERATOR_STATUS_ERROR; goto end; default: - BT_COMP_LOGE_APPEND_CAUSE(self_comp, - "Received get_new_streams response: Unknown:" - "return code %u", - be32toh(rp.status)); + BT_CPPLOGE_APPEND_CAUSE_SPEC(viewer_connection->logger, + "Received get_new_streams response: Unknown:" + "return code {}", + be32toh(rp.status)); status = LTTNG_LIVE_ITERATOR_STATUS_ERROR; goto end; } viewer_status = receive_streams(session, streams_count, self_msg_iter); if (viewer_status != LTTNG_LIVE_VIEWER_STATUS_OK) { - viewer_handle_recv_status(self_comp, NULL, viewer_status, "new streams"); + viewer_handle_recv_status(viewer_status, "new streams"); status = viewer_status_to_live_iterator_status(viewer_status); goto end; } @@ -1707,111 +1491,51 @@ end: return status; } -enum lttng_live_viewer_status live_viewer_connection_create( - bt_self_component *self_comp, bt_self_component_class *self_comp_class, - bt_logging_level log_level, const char *url, bool in_query, - struct lttng_live_msg_iter *lttng_live_msg_iter, struct live_viewer_connection **viewer) +enum lttng_live_viewer_status +live_viewer_connection_create(const char *url, bool in_query, + struct lttng_live_msg_iter *lttng_live_msg_iter, + const bt2c::Logger& parentLogger, live_viewer_connection::UP& viewer) { - struct live_viewer_connection *viewer_connection; - enum lttng_live_viewer_status status; - - viewer_connection = g_new0(struct live_viewer_connection, 1); + auto viewer_connection = bt2s::make_unique(parentLogger); - if (bt_socket_init(log_level) != 0) { - BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp, self_comp_class, - "Failed to init socket"); - status = LTTNG_LIVE_VIEWER_STATUS_ERROR; - goto error; + if (bt_socket_init(viewer_connection->logger) != 0) { + BT_CPPLOGE_APPEND_CAUSE_SPEC(viewer_connection->logger, "Failed to init socket"); + return LTTNG_LIVE_VIEWER_STATUS_ERROR; } - viewer_connection->log_level = log_level; - - viewer_connection->self_comp = self_comp; - viewer_connection->self_comp_class = self_comp_class; - viewer_connection->control_sock = BT_INVALID_SOCKET; viewer_connection->port = -1; viewer_connection->in_query = in_query; viewer_connection->lttng_live_msg_iter = lttng_live_msg_iter; - viewer_connection->url = g_string_new(url); - if (!viewer_connection->url) { - BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp, self_comp_class, - "Failed to allocate URL buffer"); - status = LTTNG_LIVE_VIEWER_STATUS_ERROR; - goto error; - } + viewer_connection->url = url; + + BT_CPPLOGD_SPEC(viewer_connection->logger, "Establishing connection to url \"{}\"...", url); + const auto status = lttng_live_connect_viewer(viewer_connection.get()); - BT_COMP_OR_COMP_CLASS_LOGD(self_comp, self_comp_class, - "Establishing connection to url \"%s\"...", url); - status = lttng_live_connect_viewer(viewer_connection); /* * Only print error and append cause in case of error. not in case of * interruption. */ if (status == LTTNG_LIVE_VIEWER_STATUS_ERROR) { - BT_COMP_OR_COMP_CLASS_LOGE_APPEND_CAUSE(self_comp, self_comp_class, - "Failed to establish connection: " - "url=\"%s\"", - url); - goto error; + BT_CPPLOGE_APPEND_CAUSE_SPEC(viewer_connection->logger, + "Failed to establish connection: " + "url=\"{}\"", + url); + return status; } else if (status == LTTNG_LIVE_VIEWER_STATUS_INTERRUPTED) { - goto error; + return status; } - BT_COMP_OR_COMP_CLASS_LOGD(self_comp, self_comp_class, - "Connection to url \"%s\" is established", url); + BT_CPPLOGD_SPEC(viewer_connection->logger, "Connection to url \"{}\" is established", url); - *viewer = viewer_connection; - status = LTTNG_LIVE_VIEWER_STATUS_OK; - goto end; - -error: - if (viewer_connection) { - live_viewer_connection_destroy(viewer_connection); - } -end: - return status; + viewer = std::move(viewer_connection); + return LTTNG_LIVE_VIEWER_STATUS_OK; } -void live_viewer_connection_destroy(struct live_viewer_connection *viewer_connection) +live_viewer_connection::~live_viewer_connection() { - bt_self_component *self_comp = viewer_connection->self_comp; - bt_self_component_class *self_comp_class = viewer_connection->self_comp_class; - - if (!viewer_connection) { - goto end; - } + BT_CPPLOGD_SPEC(this->logger, "Closing connection to relay: relay-url=\"{}\"", this->url); - BT_COMP_OR_COMP_CLASS_LOGD(self_comp, self_comp_class, - "Closing connection to relay:" - "relay-url=\"%s\"", - viewer_connection->url->str); - - lttng_live_disconnect_viewer(viewer_connection); - - if (viewer_connection->url) { - g_string_free(viewer_connection->url, true); - } - - if (viewer_connection->relay_hostname) { - g_string_free(viewer_connection->relay_hostname, true); - } - - if (viewer_connection->target_hostname) { - g_string_free(viewer_connection->target_hostname, true); - } - - if (viewer_connection->session_name) { - g_string_free(viewer_connection->session_name, true); - } - - if (viewer_connection->proto) { - g_string_free(viewer_connection->proto, true); - } - - g_free(viewer_connection); + viewer_connection_close_socket(this); bt_socket_fini(); - -end: - return; }