| 1 | /* |
| 2 | * Copyright (c) 2024 EfficiOS, Inc. |
| 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | */ |
| 6 | |
| 7 | #ifndef BABELTRACE_CPP_COMMON_BT2_PLUGIN_LOAD_HPP |
| 8 | #define BABELTRACE_CPP_COMMON_BT2_PLUGIN_LOAD_HPP |
| 9 | |
| 10 | #include <babeltrace2/babeltrace.h> |
| 11 | |
| 12 | #include "common/common.h" |
| 13 | #include "cpp-common/bt2c/c-string-view.hpp" |
| 14 | |
| 15 | #include "exc.hpp" |
| 16 | #include "plugin-set.hpp" |
| 17 | #include "plugin.hpp" |
| 18 | |
| 19 | namespace bt2 { |
| 20 | |
| 21 | inline ConstPlugin::Shared |
| 22 | findPlugin(const bt2c::CStringView name, const bool findInStdEnvVar = true, |
| 23 | const bool findInUserDir = true, const bool findInSysDir = true, |
| 24 | const bool findInStatic = true, const bool failOnLoadError = false) |
| 25 | { |
| 26 | const bt_plugin *plugin; |
| 27 | const auto status = bt_plugin_find(name, findInStdEnvVar, findInUserDir, findInSysDir, |
| 28 | findInStatic, failOnLoadError, &plugin); |
| 29 | |
| 30 | if (status == BT_PLUGIN_FIND_STATUS_MEMORY_ERROR) { |
| 31 | throw MemoryError {}; |
| 32 | } else if (status == BT_PLUGIN_FIND_STATUS_ERROR) { |
| 33 | throw Error {}; |
| 34 | } else if (status == BT_PLUGIN_FIND_STATUS_NOT_FOUND) { |
| 35 | return ConstPlugin::Shared {}; |
| 36 | } |
| 37 | |
| 38 | return ConstPlugin::Shared::createWithoutRef(plugin); |
| 39 | } |
| 40 | |
| 41 | inline ConstPluginSet::Shared findAllPluginsFromFile(const bt2c::CStringView path, |
| 42 | const bool failOnLoadError) |
| 43 | { |
| 44 | const bt_plugin_set *pluginSet; |
| 45 | |
| 46 | switch (bt_plugin_find_all_from_file(path, failOnLoadError, &pluginSet)) { |
| 47 | case BT_PLUGIN_FIND_ALL_FROM_FILE_STATUS_OK: |
| 48 | return ConstPluginSet::Shared::createWithoutRef(pluginSet); |
| 49 | case BT_PLUGIN_FIND_ALL_FROM_FILE_STATUS_NOT_FOUND: |
| 50 | return ConstPluginSet::Shared {}; |
| 51 | case BT_PLUGIN_FIND_ALL_FROM_FILE_STATUS_MEMORY_ERROR: |
| 52 | throw MemoryError {}; |
| 53 | case BT_PLUGIN_FIND_ALL_FROM_FILE_STATUS_ERROR: |
| 54 | throw Error {}; |
| 55 | } |
| 56 | |
| 57 | bt_common_abort(); |
| 58 | } |
| 59 | |
| 60 | inline ConstPluginSet::Shared findAllPluginsFromDir(const bt2c::CStringView path, |
| 61 | const bool recurse, const bool failOnLoadError) |
| 62 | { |
| 63 | const bt_plugin_set *pluginSet; |
| 64 | |
| 65 | switch (bt_plugin_find_all_from_dir(path, recurse, failOnLoadError, &pluginSet)) { |
| 66 | case BT_PLUGIN_FIND_ALL_FROM_DIR_STATUS_OK: |
| 67 | return ConstPluginSet::Shared::createWithoutRef(pluginSet); |
| 68 | case BT_PLUGIN_FIND_ALL_FROM_DIR_STATUS_NOT_FOUND: |
| 69 | return ConstPluginSet::Shared {}; |
| 70 | case BT_PLUGIN_FIND_ALL_FROM_DIR_STATUS_MEMORY_ERROR: |
| 71 | throw MemoryError {}; |
| 72 | case BT_PLUGIN_FIND_ALL_FROM_DIR_STATUS_ERROR: |
| 73 | throw Error {}; |
| 74 | } |
| 75 | |
| 76 | bt_common_abort(); |
| 77 | } |
| 78 | |
| 79 | } /* namespace bt2 */ |
| 80 | |
| 81 | #endif /* BABELTRACE_CPP_COMMON_BT2_PLUGIN_LOAD_HPP */ |