lib/plugin/plugin.c: add logging
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Fri, 26 May 2017 06:33:23 +0000 (02:33 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Sun, 28 May 2017 16:57:45 +0000 (12:57 -0400)
Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
include/babeltrace/plugin/plugin-internal.h
lib/plugin/plugin.c

index 539fe1413c89c432db32779d8be4af5f2879c34e..8249d1004db10c68cf7a268d7882c3f69baf344d 100644 (file)
@@ -299,4 +299,19 @@ void bt_plugin_set_add_plugin(struct bt_plugin_set *plugin_set,
        g_ptr_array_add(plugin_set->plugins, bt_get(plugin));
 }
 
+static inline
+const char *bt_plugin_status_string(enum bt_plugin_status status)
+{
+       switch (status) {
+       case BT_PLUGIN_STATUS_OK:
+               return "BT_PLUGIN_STATUS_OK";
+       case BT_PLUGIN_STATUS_ERROR:
+               return "BT_PLUGIN_STATUS_ERROR";
+       case BT_PLUGIN_STATUS_NOMEM:
+               return "BT_PLUGIN_STATUS_NOMEM";
+       default:
+               return "(unknown)";
+       }
+}
+
 #endif /* BABELTRACE_PLUGIN_PLUGIN_INTERNAL_H */
index d219fe893bfe5762afa2823a3aafa6b8395fb69e..31e78c3bacef87da9779e1184548949af095af43 100644 (file)
  * SOFTWARE.
  */
 
+#define BT_LOG_TAG "PLUGIN"
+#include <babeltrace/lib-logging-internal.h>
+
 #include <babeltrace/babeltrace-internal.h>
 #include <babeltrace/compiler-internal.h>
 #include <babeltrace/ref.h>
 #include <babeltrace/common-internal.h>
 #include <babeltrace/plugin/plugin-internal.h>
 #include <babeltrace/plugin/plugin-so-internal.h>
+#include <babeltrace/graph/component-class.h>
+#include <babeltrace/graph/component-class-internal.h>
 #include <babeltrace/types.h>
 #include <glib.h>
 #include <unistd.h>
 #include <stdlib.h>
 #include <stdint.h>
+#include <inttypes.h>
 #include <sys/stat.h>
 #include <dirent.h>
 
@@ -57,11 +63,12 @@ struct bt_plugin_set *(*bt_plugin_python_create_all_from_file_sym)(const char *p
 
 __attribute__((constructor)) static
 void init_python_plugin_provider(void) {
+       BT_LOGD_STR("Loading Python plugin provider module.");
        python_plugin_provider_module =
                g_module_open(PYTHON_PLUGIN_PROVIDER_FILENAME,
                        G_MODULE_BIND_LOCAL);
        if (!python_plugin_provider_module) {
-               printf_verbose("Cannot find `%s`: Python plugin support is disabled\n",
+               BT_LOGI("Cannot find `%s`: continuing without Python plugin support.",
                        PYTHON_PLUGIN_PROVIDER_FILENAME);
                return;
        }
@@ -69,14 +76,27 @@ void init_python_plugin_provider(void) {
        if (!g_module_symbol(python_plugin_provider_module,
                        PYTHON_PLUGIN_PROVIDER_SYM_NAME_STR,
                        (gpointer) &bt_plugin_python_create_all_from_file_sym)) {
-               printf_verbose("Cannot find the Python plugin provider loading symbole: Python plugin support is disabled\n");
+               BT_LOGI("Cannot find the Python plugin provider loading symbol: continuing without Python plugin support: "
+                       "file=\"%s\", symbol=\"%s\"",
+                       PYTHON_PLUGIN_PROVIDER_FILENAME,
+                       PYTHON_PLUGIN_PROVIDER_SYM_NAME_STR);
+               return;
        }
+
+       BT_LOGD("Loaded Python plugin provider module: addr=%p",
+               python_plugin_provider_module);
 }
 
 __attribute__((destructor)) static
 void fini_python_plugin_provider(void) {
        if (python_plugin_provider_module) {
-               (void) g_module_close(python_plugin_provider_module);
+               BT_LOGD("Unloading Python plugin provider module.");
+
+               if (!g_module_close(python_plugin_provider_module)) {
+                       BT_LOGE("Failed to close the Python plugin provider module: %s.",
+                               g_module_error());
+               }
+
                python_plugin_provider_module = NULL;
        }
 }
@@ -88,6 +108,7 @@ int64_t bt_plugin_set_get_plugin_count(struct bt_plugin_set *plugin_set)
        int64_t count = -1;
 
        if (!plugin_set) {
+               BT_LOGW_STR("Invalid parameter: plugin set is NULL.");
                goto end;
        }
 
@@ -103,7 +124,15 @@ struct bt_plugin *bt_plugin_set_get_plugin(struct bt_plugin_set *plugin_set,
 {
        struct bt_plugin *plugin = NULL;
 
-       if (!plugin_set || index >= plugin_set->plugins->len) {
+       if (!plugin_set) {
+               BT_LOGW_STR("Invalid parameter: plugin set is NULL.");
+               goto end;
+       }
+
+       if (index >= plugin_set->plugins->len) {
+               BT_LOGW("Invalid parameter: index is out of bounds: "
+                       "addr=%p, index=%" PRIu64 ", count=%u",
+                       plugin_set, index, plugin_set->plugins->len);
                goto end;
        }
 
@@ -115,6 +144,7 @@ end:
 
 struct bt_plugin_set *bt_plugin_create_all_from_static(void)
 {
+       /* bt_plugin_so_create_all_from_static() logs errors */
        return bt_plugin_so_create_all_from_static();
 }
 
@@ -123,10 +153,11 @@ struct bt_plugin_set *bt_plugin_create_all_from_file(const char *path)
        struct bt_plugin_set *plugin_set = NULL;
 
        if (!path) {
+               BT_LOGW_STR("Invalid parameter: path is NULL.");
                goto end;
        }
 
-       printf_verbose("Trying to load plugins from `%s`\n", path);
+       BT_LOGD("Creating plugins from file: path=\"%s\"", path);
 
        /* Try shared object plugins */
        plugin_set = bt_plugin_so_create_all_from_file(path);
@@ -143,6 +174,15 @@ struct bt_plugin_set *bt_plugin_create_all_from_file(const char *path)
        }
 
 end:
+       if (plugin_set) {
+               BT_LOGD("Created %u plugins from file: "
+                       "path=\"%s\", count=%u, plugin-set-addr=%p",
+                       plugin_set->plugins->len, path,
+                       plugin_set->plugins->len, plugin_set);
+       } else {
+               BT_LOGD("Found no plugins in file: path=\"%s\"", path);
+       }
+
        return plugin_set;
 }
 
@@ -163,11 +203,15 @@ struct bt_plugin *bt_plugin_find(const char *plugin_name)
        size_t i, j;
 
        if (!plugin_name) {
+               BT_LOGW_STR("Invalid parameter: plugin name is NULL.");
                goto end;
        }
 
+       BT_LOGD("Finding named plugin in standard directories and built-in plugins: "
+               "name=\"%s\"", plugin_name);
        dirs = g_ptr_array_new_with_free_func((GDestroyNotify) destroy_gstring);
        if (!dirs) {
+               BT_LOGE_STR("Failed to allocate a GPtrArray.");
                goto end;
        }
 
@@ -189,6 +233,7 @@ struct bt_plugin *bt_plugin_find(const char *plugin_name)
        if (envvar) {
                ret = bt_common_append_plugin_path_dirs(envvar, dirs);
                if (ret) {
+                       BT_LOGE_STR("Failed to append plugin path to array of directories.");
                        goto end;
                }
        }
@@ -199,6 +244,7 @@ struct bt_plugin *bt_plugin_find(const char *plugin_name)
                        g_string_new(home_plugin_dir);
 
                if (!home_plugin_dir_str) {
+                       BT_LOGE_STR("Failed to allocate a GString.");
                        goto end;
                }
 
@@ -211,6 +257,7 @@ struct bt_plugin *bt_plugin_find(const char *plugin_name)
                        g_string_new(system_plugin_dir);
 
                if (!system_plugin_dir_str) {
+                       BT_LOGE_STR("Failed to allocate a GString.");
                        goto end;
                }
 
@@ -220,11 +267,13 @@ struct bt_plugin *bt_plugin_find(const char *plugin_name)
        for (i = 0; i < dirs->len; i++) {
                GString *dir = g_ptr_array_index(dirs, i);
 
-               printf_verbose("Trying to load plugins from directory `%s`\n",
-                       dir->str);
                BT_PUT(plugin_set);
+
+               /* bt_plugin_create_all_from_dir() logs details/errors */
                plugin_set = bt_plugin_create_all_from_dir(dir->str, BT_FALSE);
                if (!plugin_set) {
+                       BT_LOGD("No plugins found in directory: path=\"%s\"",
+                               dir->str);
                        continue;
                }
 
@@ -234,23 +283,31 @@ struct bt_plugin *bt_plugin_find(const char *plugin_name)
 
                        if (strcmp(bt_plugin_get_name(candidate_plugin),
                                        plugin_name) == 0) {
+                               BT_LOGD("Plugin found in directory: name=\"%s\", path=\"%s\"",
+                                       plugin_name, dir->str);
                                plugin = bt_get(candidate_plugin);
                                goto end;
                        }
                }
+
+               BT_LOGD("Plugin not found in directory: name=\"%s\", path=\"%s\"",
+                       plugin_name, dir->str);
        }
 
        bt_put(plugin_set);
        plugin_set = bt_plugin_create_all_from_static();
+       if (plugin_set) {
+               for (j = 0; j < plugin_set->plugins->len; j++) {
+                       struct bt_plugin *candidate_plugin =
+                               g_ptr_array_index(plugin_set->plugins, j);
 
-       for (j = 0; j < plugin_set->plugins->len; j++) {
-               struct bt_plugin *candidate_plugin =
-                       g_ptr_array_index(plugin_set->plugins, j);
-
-               if (strcmp(bt_plugin_get_name(candidate_plugin),
-                               plugin_name) == 0) {
-                       plugin = bt_get(candidate_plugin);
-                       goto end;
+                       if (strcmp(bt_plugin_get_name(candidate_plugin),
+                                       plugin_name) == 0) {
+                               BT_LOGD("Plugin found in built-in plugins: "
+                                       "name=\"%s\"", plugin_name);
+                               plugin = bt_get(candidate_plugin);
+                               goto end;
+                       }
                }
        }
 
@@ -262,6 +319,15 @@ end:
                g_ptr_array_free(dirs, TRUE);
        }
 
+       if (plugin) {
+               BT_LOGD("Found plugin in standard directories and built-in plugins: "
+                       "addr=%p, name=\"%s\", path=\"%s\"",
+                       plugin, plugin_name, bt_plugin_get_path(plugin));
+       } else {
+               BT_LOGD("No plugin found in standard directories and built-in plugins: "
+                       "name=\"%s\"", plugin_name);
+       }
+
        return plugin;
 }
 
@@ -272,17 +338,32 @@ struct bt_component_class *bt_plugin_find_component_class(
        struct bt_plugin *plugin = NULL;
        struct bt_component_class *comp_cls = NULL;
 
-       if (!plugin_name || !comp_cls_name) {
+       if (!plugin_name) {
+               BT_LOGW_STR("Invalid parameter: plugin name is NULL.");
                goto end;
        }
 
+       if (!comp_cls_name) {
+               BT_LOGW_STR("Invalid parameter: component class name is NULL.");
+               goto end;
+       }
+
+       BT_LOGD("Finding named plugin and component class in standard directories and built-in plugins: "
+               "plugin-name=\"%s\", comp-class-name=\"%s\"",
+               plugin_name, comp_cls_name);
        plugin = bt_plugin_find(plugin_name);
        if (!plugin) {
+               BT_LOGD_STR("Plugin not found.");
                goto end;
        }
 
        comp_cls = bt_plugin_get_component_class_by_name_and_type(
                plugin, comp_cls_name, comp_cls_type);
+       if (!comp_cls) {
+               BT_LOGD("Component class not found in plugin: "
+                       "plugin-addr=%p, plugin-path=\"%s\"",
+                       plugin, bt_plugin_get_path(plugin));
+       }
 
 end:
        bt_put(plugin);
@@ -318,25 +399,29 @@ enum bt_plugin_status bt_plugin_create_append_all_from_dir(
        enum bt_plugin_status ret = BT_PLUGIN_STATUS_OK;
 
        if (!path) {
+               BT_LOGW_STR("Invalid parameter: path is NULL.");
                ret = BT_PLUGIN_STATUS_ERROR;
                goto end;
        }
 
        path_len = strlen(path);
-
        if (path_len >= PATH_MAX) {
+               BT_LOGW("Invalid parameter: path length is too large: "
+                       "path-length=%zu", path_len);
                ret = BT_PLUGIN_STATUS_ERROR;
                goto end;
        }
 
        entry = alloc_dirent(path);
        if (!entry) {
+               BT_LOGE_STR("Failed to allocate a directory entry.");
                ret = BT_PLUGIN_STATUS_ERROR;
                goto end;
        }
 
        file_path = zmalloc(PATH_MAX);
        if (!file_path) {
+               BT_LOGE("Failed to allocate %zu bytes.", (size_t) PATH_MAX);
                ret = BT_PLUGIN_STATUS_NOMEM;
                goto end;
        }
@@ -349,8 +434,16 @@ enum bt_plugin_status bt_plugin_create_append_all_from_dir(
 
        directory = opendir(file_path);
        if (!directory) {
-               printf_verbose("Failed to open plugin directory \"%s\"\n",
-                       file_path);
+               if (errno == EACCES) {
+                       BT_LOGD("Cannot open directory: %s: continuing: "
+                               "path=\"%s\", errno=%d",
+                               strerror(errno), file_path, errno);
+                       goto end;
+               }
+
+               BT_LOGE("Cannot open directory: %s: "
+                       "path=\"%s\", errno=%d",
+                       strerror(errno), file_path, errno);
                ret = BT_PLUGIN_STATUS_ERROR;
                goto end;
        }
@@ -361,14 +454,25 @@ enum bt_plugin_status bt_plugin_create_append_all_from_dir(
                int stat_ret;
                size_t file_name_len;
 
+               if (strcmp(result->d_name, ".") == 0 ||
+                               strcmp(result->d_name, "..") == 0) {
+                       continue;
+               }
+
                if (result->d_name[0] == '.') {
                        /* Skip hidden files, . and .. */
+                       BT_LOGV("Skipping hidden file: path=\"%s/%s\"",
+                               path, result->d_name);
                        continue;
                }
 
                file_name_len = strlen(result->d_name);
 
                if (path_len + file_name_len >= PATH_MAX) {
+                       BT_LOGD("Skipping file because its path length is too large: continuing: "
+                               "path=\"%s/%s\", length=%zu",
+                               path, result->d_name,
+                               (size_t) (path_len + file_name_len));
                        continue;
                }
 
@@ -377,7 +481,9 @@ enum bt_plugin_status bt_plugin_create_append_all_from_dir(
                stat_ret = stat(file_path, &st);
                if (stat_ret < 0) {
                        /* Continue to next file / directory. */
-                       printf_perror("Failed to stat() plugin file\n");
+                       BT_LOGD("Cannot get file information: %s: continuing: "
+                               "path=\"%s\", errno=%d",
+                               strerror(errno), file_path, errno);
                        continue;
                }
 
@@ -385,6 +491,8 @@ enum bt_plugin_status bt_plugin_create_append_all_from_dir(
                        ret = bt_plugin_create_append_all_from_dir(plugin_set,
                                file_path, BT_TRUE);
                        if (ret < 0) {
+                               BT_LOGW("Cannot recurse into directory to find plugins: "
+                                       "path=\"%s\", ret=%d", file_path, ret);
                                goto end;
                        }
                } else if (S_ISREG(st.st_mode)) {
@@ -398,6 +506,10 @@ enum bt_plugin_status bt_plugin_create_append_all_from_dir(
                                        struct bt_plugin *plugin =
                                                g_ptr_array_index(plugins_from_file->plugins, j);
 
+                                       BT_LOGD("Adding plugin to plugin set: "
+                                               "plugin-path=\"%s\", plugin-addr=%p, plugin-name=\"%s\"",
+                                               file_path, plugin,
+                                               bt_plugin_get_name(plugin));
                                        bt_plugin_set_add_plugin(plugin_set,
                                                plugin);
                                }
@@ -406,6 +518,7 @@ enum bt_plugin_status bt_plugin_create_append_all_from_dir(
                        }
                }
        }
+
 end:
        if (directory) {
                if (closedir(directory)) {
@@ -413,9 +526,12 @@ end:
                         * We don't want to override the error since there is
                         * nothing could do.
                         */
-                       perror("Failed to close plug-in directory");
+                       BT_LOGE("Cannot close directory entry: %s: "
+                               "path=\"%s\", errno=%d",
+                               strerror(errno), path, errno);
                }
        }
+
        free(entry);
        free(file_path);
        return ret;
@@ -427,8 +543,11 @@ struct bt_plugin_set *bt_plugin_create_all_from_dir(const char *path,
        struct bt_plugin_set *plugin_set;
        enum bt_plugin_status status;
 
+       BT_LOGD("Creating all plugins in directory: path=\"%s\", recurse=%d",
+               path, recurse);
        plugin_set = bt_plugin_set_create();
        if (!plugin_set) {
+               BT_LOGE_STR("Cannot create empty plugin set.");
                goto error;
        }
 
@@ -436,9 +555,13 @@ struct bt_plugin_set *bt_plugin_create_all_from_dir(const char *path,
        status = bt_plugin_create_append_all_from_dir(plugin_set, path,
                recurse);
        if (status < 0) {
+               BT_LOGW("Cannot append plugins found in directory: "
+                       "status=%s", bt_plugin_status_string(status));
                goto error;
        }
 
+       BT_LOGD("Created %u plugins from directory: count=%u, path=\"%s\"",
+               plugin_set->plugins->len, plugin_set->plugins->len, path);
        goto end;
 
 error:
@@ -450,27 +573,87 @@ end:
 
 const char *bt_plugin_get_name(struct bt_plugin *plugin)
 {
-       return plugin && plugin->info.name_set ? plugin->info.name->str : NULL;
+       const char *val = NULL;
+
+       if (!plugin) {
+               BT_LOGW_STR("Invalid parameter: plugin is NULL.");
+               goto end;
+       }
+
+       if (plugin->info.name_set) {
+               val = plugin->info.name->str;
+       }
+
+end:
+       return val;
 }
 
 const char *bt_plugin_get_author(struct bt_plugin *plugin)
 {
-       return plugin && plugin->info.author_set ? plugin->info.author->str : NULL;
+       const char *val = NULL;
+
+       if (!plugin) {
+               BT_LOGW_STR("Invalid parameter: plugin is NULL.");
+               goto end;
+       }
+
+       if (plugin->info.author_set) {
+               val = plugin->info.author->str;
+       }
+
+end:
+       return val;
 }
 
 const char *bt_plugin_get_license(struct bt_plugin *plugin)
 {
-       return plugin && plugin->info.license_set ? plugin->info.license->str : NULL;
+       const char *val = NULL;
+
+       if (!plugin) {
+               BT_LOGW_STR("Invalid parameter: plugin is NULL.");
+               goto end;
+       }
+
+       if (plugin->info.license_set) {
+               val = plugin->info.license->str;
+       }
+
+end:
+       return val;
 }
 
 const char *bt_plugin_get_path(struct bt_plugin *plugin)
 {
-       return plugin && plugin->info.path_set ? plugin->info.path->str : NULL;
+       const char *val = NULL;
+
+       if (!plugin) {
+               BT_LOGW_STR("Invalid parameter: plugin is NULL.");
+               goto end;
+       }
+
+       if (plugin->info.path_set) {
+               val = plugin->info.path->str;
+       }
+
+end:
+       return val;
 }
 
 const char *bt_plugin_get_description(struct bt_plugin *plugin)
 {
-       return plugin && plugin->info.description_set ? plugin->info.description->str : NULL;
+       const char *val = NULL;
+
+       if (!plugin) {
+               BT_LOGW_STR("Invalid parameter: plugin is NULL.");
+               goto end;
+       }
+
+       if (plugin->info.description_set) {
+               val = plugin->info.description->str;
+       }
+
+end:
+       return val;
 }
 
 enum bt_plugin_status bt_plugin_get_version(struct bt_plugin *plugin,
@@ -479,7 +662,14 @@ enum bt_plugin_status bt_plugin_get_version(struct bt_plugin *plugin,
 {
        enum bt_plugin_status status = BT_PLUGIN_STATUS_OK;
 
-       if (!plugin || !plugin->info.version_set) {
+       if (!plugin) {
+               BT_LOGW_STR("Invalid parameter: plugin is NULL.");
+               status = BT_PLUGIN_STATUS_ERROR;
+               goto end;
+       }
+
+       if (!plugin->info.version_set) {
+               BT_LOGV("Plugin's version is not set: addr=%p", plugin);
                status = BT_PLUGIN_STATUS_ERROR;
                goto end;
        }
@@ -514,7 +704,15 @@ struct bt_component_class *bt_plugin_get_component_class_by_index(
 {
        struct bt_component_class *comp_class = NULL;
 
-       if (!plugin || index >= plugin->comp_classes->len) {
+       if (!plugin) {
+               BT_LOGW_STR("Invalid parameter: plugin is NULL.");
+               goto error;
+       }
+
+       if (index >= plugin->comp_classes->len) {
+               BT_LOGW("Invalid parameter: index is out of bounds: "
+                       "addr=%p, index=%" PRIu64 ", count=%u",
+                       plugin, index, plugin->comp_classes->len);
                goto error;
        }
 
@@ -536,7 +734,13 @@ struct bt_component_class *bt_plugin_get_component_class_by_name_and_type(
        struct bt_component_class *comp_class = NULL;
        size_t i;
 
-       if (!plugin || !name) {
+       if (!plugin) {
+               BT_LOGW_STR("Invalid parameter: plugin is NULL.");
+               goto error;
+       }
+
+       if (!name) {
+               BT_LOGW_STR("Invalid parameter: name is NULL.");
                goto error;
        }
 
@@ -574,7 +778,20 @@ enum bt_plugin_status bt_plugin_add_component_class(
        struct bt_component_class *comp_class_dup = NULL;
        int comp_class_index = -1;
 
-       if (!plugin || !comp_class || plugin->frozen) {
+       if (!plugin) {
+               BT_LOGW_STR("Invalid parameter: plugin is NULL.");
+               goto error;
+       }
+
+       if (!comp_class) {
+               BT_LOGW_STR("Invalid parameter: component class is NULL.");
+               goto error;
+       }
+
+       if (plugin->frozen) {
+               BT_LOGW("Invalid parameter: plugin is frozen: "
+                       "addr=%p, name=\"%s\"", plugin,
+                       bt_plugin_get_name(plugin));
                goto error;
        }
 
@@ -583,10 +800,14 @@ enum bt_plugin_status bt_plugin_add_component_class(
                bt_component_class_get_name(comp_class),
                bt_component_class_get_type(comp_class));
        if (comp_class_dup) {
-               printf_verbose("Plugin `%s`: adding component class with existing name `%s` and type %d\n",
-                       bt_plugin_get_name(plugin),
+               BT_LOGW("Invalid parameter: a component class with this name and type already exists in the plugin: "
+                       "plugin-addr=%p, plugin-name=\"%s\", plugin-path=\"%s\", "
+                       "comp-class-name=\"%s\", comp-class-type=%s",
+                       plugin, bt_plugin_get_name(plugin),
+                       bt_plugin_get_path(plugin),
                        bt_component_class_get_name(comp_class),
-                       bt_component_class_get_type(comp_class));
+                       bt_component_class_type_string(
+                               bt_component_class_get_type(comp_class)));
                goto error;
        }
 
@@ -599,6 +820,15 @@ enum bt_plugin_status bt_plugin_add_component_class(
                bt_plugin_so_on_add_component_class(plugin, comp_class);
        }
 
+       BT_LOGD("Added component class to plugin: "
+               "plugin-addr=%p, plugin-name=\"%s\", plugin-path=\"%s\", "
+               "comp-class-addr=%p, comp-class-name=\"%s\", comp-class-type=%s",
+               plugin, bt_plugin_get_name(plugin),
+               bt_plugin_get_path(plugin),
+               comp_class,
+               bt_component_class_get_name(comp_class),
+               bt_component_class_type_string(
+                       bt_component_class_get_type(comp_class)));
        goto end;
 
 error:
This page took 0.031529 seconds and 4 git commands to generate.