1 parent
4fca5523f6
commit
fa30098129
5 files changed
+59
No files matched your search
@@ -3,6 +3,8 @@
|
|||||||
#include <Void/Log/Log.hpp>
|
#include <Void/Log/Log.hpp>
|
||||||
#include <Void/Log/Logger.hpp>
|
#include <Void/Log/Logger.hpp>
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
// Bridge between Void::Log (the thin emission layer used by all modules) and
|
// Bridge between Void::Log (the thin emission layer used by all modules) and
|
||||||
// Void::Logging::Logger (the full sink implementation).
|
// Void::Logging::Logger (the full sink implementation).
|
||||||
//
|
//
|
||||||
@@ -69,4 +71,23 @@ namespace Void::Log
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Owning variant for process-global sinks. The logger remains alive until
|
||||||
|
/// this sink is replaced or the logging state is destroyed.
|
||||||
|
[[nodiscard]] inline Sink
|
||||||
|
make_sink(std::shared_ptr<Void::Logging::Logger> logger)
|
||||||
|
{
|
||||||
|
return [logger = std::move(logger)](
|
||||||
|
Level level, std::string_view module, std::string_view message,
|
||||||
|
const std::source_location& location) {
|
||||||
|
if (!logger) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
auto full = std::string{"["};
|
||||||
|
full += module;
|
||||||
|
full += "] ";
|
||||||
|
full += message;
|
||||||
|
logger->log(to_logging_level(level), full, location);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace Void::Log
|
} // namespace Void::Log
|
||||||
@@ -86,6 +86,9 @@ namespace Void::Plugin
|
|||||||
// name argument is the registry name (IPlugin::name()).
|
// name argument is the registry name (IPlugin::name()).
|
||||||
std::function<std::string(std::string_view plugin_name)>
|
std::function<std::string(std::string_view plugin_name)>
|
||||||
config_provider;
|
config_provider;
|
||||||
|
// Host-specific contract checked after plugin activation and before
|
||||||
|
// publication. A failed validation aborts load/reload transactionally.
|
||||||
|
std::function<bool(IPlugin&, const PluginInfo&)> instance_validator;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Identifies an on_reloaded observer for remove_on_reloaded.
|
// Identifies an on_reloaded observer for remove_on_reloaded.
|
||||||
|
|||||||
@@ -243,9 +243,13 @@ std::expected<void, LoadError> PluginLoader::reload()
|
|||||||
try {
|
try {
|
||||||
next.plugin_->on_load();
|
next.plugin_->on_load();
|
||||||
} catch (...) {
|
} catch (...) {
|
||||||
|
std::error_code ec;
|
||||||
|
std::filesystem::remove(tmp, ec);
|
||||||
return std::unexpected{LoadError::ActivationFailed};
|
return std::unexpected{LoadError::ActivationFailed};
|
||||||
}
|
}
|
||||||
if (next.ready_ && next.ready_(next.plugin_) != 0) {
|
if (next.ready_ && next.ready_(next.plugin_) != 0) {
|
||||||
|
std::error_code ec;
|
||||||
|
std::filesystem::remove(tmp, ec);
|
||||||
return std::unexpected{LoadError::ActivationFailed};
|
return std::unexpected{LoadError::ActivationFailed};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -255,6 +255,10 @@ namespace Void::Plugin
|
|||||||
(*inst)->ready((*inst)->plugin) != 0) {
|
(*inst)->ready((*inst)->plugin) != 0) {
|
||||||
return std::unexpected{LoadError::ActivationFailed};
|
return std::unexpected{LoadError::ActivationFailed};
|
||||||
}
|
}
|
||||||
|
if (m_options.instance_validator &&
|
||||||
|
!m_options.instance_validator(*(*inst)->plugin, (*inst)->info)) {
|
||||||
|
return std::unexpected{LoadError::ActivationFailed};
|
||||||
|
}
|
||||||
|
|
||||||
std::lock_guard lock(m_mutex);
|
std::lock_guard lock(m_mutex);
|
||||||
auto entry = std::make_unique<Entry>();
|
auto entry = std::make_unique<Entry>();
|
||||||
@@ -394,6 +398,12 @@ namespace Void::Plugin
|
|||||||
"reload '{}' aborted: activation failed", name);
|
"reload '{}' aborted: activation failed", name);
|
||||||
return std::unexpected{LoadError::ActivationFailed};
|
return std::unexpected{LoadError::ActivationFailed};
|
||||||
}
|
}
|
||||||
|
if (m_options.instance_validator &&
|
||||||
|
!m_options.instance_validator(*(*next)->plugin, (*next)->info)) {
|
||||||
|
Void::Log::warn("Plugin.Manager",
|
||||||
|
"reload '{}' aborted: host validation failed", name);
|
||||||
|
return std::unexpected{LoadError::ActivationFailed};
|
||||||
|
}
|
||||||
// Atomic swap. The old instance keeps living while any acquired
|
// Atomic swap. The old instance keeps living while any acquired
|
||||||
// handle still pins it; it is destroyed when the last one drops.
|
// handle still pins it; it is destroyed when the last one drops.
|
||||||
entry.current.store(std::move(*next), std::memory_order_release);
|
entry.current.store(std::move(*next), std::memory_order_release);
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
#include <Void/Plugin/Manager.hpp>
|
#include <Void/Plugin/Manager.hpp>
|
||||||
|
#include <Void/Plugin/Loader.hpp>
|
||||||
#include <Void/System/TempDir.hpp>
|
#include <Void/System/TempDir.hpp>
|
||||||
#include <Void/Testing/Assert.hpp>
|
#include <Void/Testing/Assert.hpp>
|
||||||
#include <Void/Testing/Test.hpp>
|
#include <Void/Testing/Test.hpp>
|
||||||
@@ -92,4 +93,24 @@ Void::Testing::Test activation_failure_keeps_current_instance()
|
|||||||
std::string_view{"accepted"});
|
std::string_view{"accepted"});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Void::Testing::Test host_validation_failure_keeps_current_instance()
|
||||||
|
{
|
||||||
|
Void::FileSystem::TempDir tmp{"void_cfg_validator"};
|
||||||
|
const auto so = tmp.path() / "plugin.so";
|
||||||
|
std::filesystem::copy_file(VOID_CONFIGURABLE_PATH, so);
|
||||||
|
|
||||||
|
bool accepted = true;
|
||||||
|
PluginManager mgr{{.instance_validator = [&accepted](IPlugin&,
|
||||||
|
const PluginInfo&) { return accepted; }}};
|
||||||
|
Void::Testing::expect(mgr.load(so).has_value());
|
||||||
|
auto before = mgr.acquire("ConfigurablePlugin");
|
||||||
|
|
||||||
|
accepted = false;
|
||||||
|
const auto result = mgr.reload("ConfigurablePlugin");
|
||||||
|
Void::Testing::expect(!result.has_value());
|
||||||
|
Void::Testing::expect(result.error() == LoadError::ActivationFailed);
|
||||||
|
auto after = mgr.acquire("ConfigurablePlugin");
|
||||||
|
Void::Testing::expect(after.get() == before.get());
|
||||||
|
}
|
||||||
|
|
||||||
#endif // configurable plugin available
|
#endif // configurable plugin available
|
||||||
Reference in new issue
Block a user