Add internal BT_ASSERT() and BT_ASSERT_PRE() helpers
[babeltrace.git] / lib / graph / port.c
index 7a464708e9371a55a49899340685c402fe82acfd..43f3134ce938fb21fde27b5c67f523a9782b02be 100644 (file)
@@ -26,6 +26,9 @@
  * SOFTWARE.
  */
 
+#define BT_LOG_TAG "PORT"
+#include <babeltrace/lib-logging-internal.h>
+
 #include <babeltrace/graph/port.h>
 #include <babeltrace/graph/component-internal.h>
 #include <babeltrace/graph/port-internal.h>
@@ -38,16 +41,20 @@ void bt_port_destroy(struct bt_object *obj)
 {
        struct bt_port *port = container_of(obj, struct bt_port, base);
 
+       BT_LOGD("Destroying port: addr=%p, name=\"%s\", comp-addr=%p",
+               port, bt_port_get_name(port), obj->parent);
+
        if (port->name) {
                g_string_free(port->name, TRUE);
        }
+
        g_free(port);
 }
 
-struct bt_port *bt_port_from_private_port(
+struct bt_port *bt_port_from_private(
                struct bt_private_port *private_port)
 {
-       return bt_get(bt_port_from_private(private_port));
+       return bt_get(bt_port_borrow_from_private(private_port));
 }
 
 BT_HIDDEN
@@ -61,25 +68,39 @@ struct bt_port *bt_port_create(struct bt_component *parent_component,
        assert(type == BT_PORT_TYPE_INPUT || type == BT_PORT_TYPE_OUTPUT);
 
        if (strlen(name) == 0) {
+               BT_LOGW_STR("Invalid parameter: name is an empty string.");
                goto end;
        }
 
        port = g_new0(struct bt_port, 1);
        if (!port) {
+               BT_LOGE_STR("Failed to allocate one port.");
                goto end;
        }
 
+       BT_LOGD("Creating port for component: "
+               "comp-addr=%p, comp-name=\"%s\", port-type=%s, "
+               "port-name=\"%s\"",
+               parent_component, bt_component_get_name(parent_component),
+               bt_port_type_string(type), name);
+
        bt_object_init(port, bt_port_destroy);
        port->name = g_string_new(name);
        if (!port->name) {
+               BT_LOGE_STR("Failed to allocate one GString.");
                BT_PUT(port);
                goto end;
        }
 
        port->type = type;
        port->user_data = user_data;
-
        bt_object_set_parent(port, &parent_component->base);
+       BT_LOGD("Created port for component: "
+               "comp-addr=%p, comp-name=\"%s\", port-type=%s, "
+               "port-name=\"%s\", port-addr=%p",
+               parent_component, bt_component_get_name(parent_component),
+               bt_port_type_string(type), name, port);
+
 end:
        return port;
 }
@@ -98,11 +119,18 @@ struct bt_connection *bt_port_get_connection(struct bt_port *port)
 {
        struct bt_connection *connection = NULL;
 
-       if (!port || !port->connection) {
+       if (!port) {
+               BT_LOGW_STR("Invalid parameter: port is NULL.");
+               goto end;
+       }
+
+       if (!port->connection) {
+               /* Not an error: means disconnected */
                goto end;
        }
 
        connection = bt_get(port->connection);
+
 end:
        return connection;
 }
@@ -116,14 +144,14 @@ struct bt_private_connection *bt_private_port_get_private_connection(
                struct bt_private_port *private_port)
 {
        return bt_private_connection_from_connection(bt_port_get_connection(
-               bt_port_from_private(private_port)));
+               bt_port_borrow_from_private(private_port)));
 }
 
 struct bt_private_component *bt_private_port_get_private_component(
                struct bt_private_port *private_port)
 {
        return bt_private_component_from_component(bt_port_get_component(
-               bt_port_from_private(private_port)));
+               bt_port_borrow_from_private(private_port)));
 }
 
 BT_HIDDEN
@@ -136,43 +164,65 @@ void bt_port_set_connection(struct bt_port *port,
         * connection exists.
         */
        port->connection = connection;
+       BT_LOGV("Set port's connection: "
+               "port-addr=%p, port-name=\"%s\", conn-addr=%p",
+               port, bt_port_get_name(port), connection);
 }
 
-int bt_private_port_remove_from_component(
+enum bt_port_status bt_private_port_remove_from_component(
                struct bt_private_port *private_port)
 {
-       int ret = 0;
-       struct bt_port *port = bt_port_from_private(private_port);
+       enum bt_port_status status = BT_PORT_STATUS_OK;
+       struct bt_port *port = bt_port_borrow_from_private(private_port);
        struct bt_component *comp = NULL;
+       enum bt_component_status comp_status;
 
        if (!port) {
-               ret = -1;
+               BT_LOGW_STR("Invalid parameter: private port is NULL.");
+               status = BT_PORT_STATUS_INVALID;
                goto end;
        }
 
        comp = (void *) bt_object_get_parent(port);
-       ret = bt_component_remove_port(comp, port);
+       if (!comp) {
+               BT_LOGV("Port already removed from its component: "
+                       "port-addr=%p, port-name=\"%s\", ",
+                       port, bt_port_get_name(port));
+               goto end;
+       }
+
+       /* bt_component_remove_port() logs details */
+       comp_status = bt_component_remove_port(comp, port);
+       assert(comp_status != BT_COMPONENT_STATUS_INVALID);
+       if (comp_status < 0) {
+               status = BT_PORT_STATUS_ERROR;
+               goto end;
+       }
 
 end:
        bt_put(comp);
-       return ret;
+       return status;
 }
 
-int bt_port_disconnect(struct bt_port *port)
+enum bt_port_status bt_port_disconnect(struct bt_port *port)
 {
-       int ret = 0;
+       enum bt_port_status status = BT_PORT_STATUS_OK;
 
        if (!port) {
-               ret = -1;
+               BT_LOGW_STR("Invalid parameter: port is NULL.");
+               status = BT_PORT_STATUS_INVALID;
                goto end;
        }
 
        if (port->connection) {
-               bt_connection_disconnect_ports(port->connection);
+               bt_connection_end(port->connection, true);
+               BT_LOGV("Disconnected port: "
+                       "port-addr=%p, port-name=\"%s\"",
+                       port, bt_port_get_name(port));
        }
 
 end:
-       return ret;
+       return status;
 }
 
 bt_bool bt_port_is_connected(struct bt_port *port)
@@ -180,6 +230,7 @@ bt_bool bt_port_is_connected(struct bt_port *port)
        int ret;
 
        if (!port) {
+               BT_LOGW_STR("Invalid parameter: port is NULL.");
                ret = -1;
                goto end;
        }
@@ -194,5 +245,5 @@ void *bt_private_port_get_user_data(
                struct bt_private_port *private_port)
 {
        return private_port ?
-               bt_port_from_private(private_port)->user_data : NULL;
+               bt_port_borrow_from_private(private_port)->user_data : NULL;
 }
This page took 0.026181 seconds and 4 git commands to generate.