Giter Club home page Giter Club logo

rust-dynamic-loading-plugins's Introduction

Dynamic Loading: Plugins in Rust

Ensure both the application and the plugin are built:

cargo build -p app -p example-plugin

Then run the application from the repository root

cargo run

This should print output like the following:

[2023-11-26T16:30:06Z DEBUG app::plugin_manager] Loading plugin from "target/debug/deps/libexample_plugin.so"
[2023-11-26T16:30:06Z DEBUG app::plugin_manager] Initializing logger for loaded library
[2023-11-26T16:30:06Z DEBUG example_plugin] Logger initialized
[2023-11-26T16:30:06Z DEBUG app::plugin_manager] Loaded plugin: TestPlugin 0.1.0+integration-test
[2023-11-26T16:30:06Z INFO  example_plugin] Plugin loaded
[2023-11-26T16:30:06Z INFO  app] Data before applying plugins: 21
[2023-11-26T16:30:06Z DEBUG app::plugin_manager] Firing pre_operation hooks
[2023-11-26T16:30:06Z TRACE app::plugin_manager] Firing pre_operation for TestPlugin 0.1.0+integration-test
[2023-11-26T16:30:06Z INFO  example_plugin] Added 1 to input
[2023-11-26T16:30:06Z DEBUG app::plugin_manager] Firing operation hooks
[2023-11-26T16:30:06Z TRACE app::plugin_manager] Firing operation for TestPlugin 0.1.0+integration-test
[2023-11-26T16:30:06Z INFO  example_plugin] Multiplied input by 2
[2023-11-26T16:30:06Z DEBUG app::plugin_manager] Firing post_operation hooks
[2023-11-26T16:30:06Z TRACE app::plugin_manager] Firing post_operation for TestPlugin 0.1.0+integration-test
[2023-11-26T16:30:06Z INFO  example_plugin] Subtracted 2 from input
[2023-11-26T16:30:06Z INFO  app] Data after applying plugins: 42
[2023-11-26T16:30:06Z DEBUG app::plugin_manager] Unloading plugins
[2023-11-26T16:30:06Z TRACE app::plugin_manager] Firing on_plugin_unload for TestPlugin 0.1.0+integration-test
[2023-11-26T16:30:06Z INFO  example_plugin] Plugin unloaded
[2023-11-26T16:30:06Z TRACE app::plugin_manager] Unloading logger

This assumes the environment provided in the .env file:

RUST_LOG=app=trace,example_plugin=debug

Logging in the Plugins

In order to achieve logging in the dynamically loaded libraries, a declare_logger_plugin! macro is provided. It exports the _logger_create used to initialize the logger. If this symbol is found, an instance to the logger and the lowest log level is passed, which is then set up as the global logger within the shared library.

For this to work, a &'static dyn Log reference needs to be available, which is a bit of a hackery in main: A StaticLogger type takes ownership of the Box<env_logger::Logger> and pretends to be able to provide a &'static dyn Log instance. This assumes that this logger is always created first and destroyed last.

Further reading

This toy project is inspired by Michael F. Bryan's Dynamic Loading & Plugins post.

rust-dynamic-loading-plugins's People

Contributors

sunsided avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.