⚠️ DEPRECATED FOR PLUGIN DEVELOPERS: This document describes the observer pattern for plugin lifecycle events using the old IPlugin C++ interface, which is no longer used. Modern plugins use C-ABI exports only (see PLUGIN_SYSTEM.md and SDK_GETTING_STARTED.md).
For plugin developers: Please use the SDK Getting Started Guide and Basic Plugin Example instead. This document is for internal LogViewer architecture only.
Overview
The PluginManager uses the Observer pattern to notify interested parties about plugin lifecycle events. This allows the UI and other components to react to plugin loading, enabling, disabling, and unloading.
Architecture
IPluginObserver Interface
Note: While the observer pattern is still used, observers now receive PluginHandle (opaque pointer) instead of IPlugin* since plugins use C-ABI.
class IPluginObserver
{
public:
virtual ~IPluginObserver() = default;
virtual void OnPluginEvent(PluginEvent event,
const std::string& pluginId,
void* pluginHandle) = 0;
};
Plugin Events
enum class PluginEvent
{
Loaded,
Unloaded,
Enabled,
Disabled,
Registered,
Unregistered
};
Event Flow
Registration & Loading
1. RegisterPlugin() → copies file to user directory
2. Fires: PluginEvent::Registered
3. Loads plugin into memory
4. Fires: PluginEvent::Loaded
5. Marks as enabled
6. Fires: PluginEvent::Enabled
Enable/Disable Cycle
Enable:
1. EnablePlugin() → checks if plugin in memory
2. If not loaded: Load → PluginEvent::Loaded
3. Set enabled=true
4. Fire: PluginEvent::Enabled → UI creates tab
Disable:
1. DisablePlugin() → checks if enabled
2. Set enabled=false
3. Fire: PluginEvent::Disabled → UI removes tab
4. Plugin stays in memory for quick re-enable
Unregistration
1. UnregisterPlugin() → disables if enabled
2. Fires: PluginEvent::Disabled (if was enabled)
3. Deletes file from user directory
4. Fires: PluginEvent::Unregistered
5. Unloads from memory
6. Fires: PluginEvent::Unloaded
Implementation Example
Register as Observer
{
public:
void setupPluginManager() {
pluginManager.RegisterObserver(this);
}
const std::string& pluginId,
void* pluginHandle) override {
switch (event) {
createPluginTab(pluginId, pluginHandle);
break;
removePluginTab(pluginId);
break;
break;
}
}
};
Definition IPluginObserver.hpp:13
virtual void OnPluginEvent(PluginEvent event, const std::string &pluginId, IPlugin *plugin)=0
static PluginManager & GetInstance()
Gets singleton instance.
Definition PluginManager.cpp:377
Detailed information about plugin state change.
Definition PluginEventBus.hpp:56
Multiple Observers
The observer pattern allows multiple components to react to plugin events:
mainWindow->RegisterObserver(pluginManager);
pluginLogger->RegisterObserver(pluginManager);
analyticsTracker->RegisterObserver(pluginManager);
configManager->RegisterObserver(pluginManager);
Benefits
- Decoupling: PluginManager doesn't need to know about UI specifics
- Extensibility: Easy to add new observers without modifying PluginManager
- Multiple Observers: Many components can react to the same events
- Clear Event Types: Explicit enum makes event handling more maintainable
- Single Notification Point: All observers notified in consistent order
Migration from Callbacks
Old Approach
pluginManager.SetPluginEnableCallback([](
id,
plugin) { });
pluginManager.SetPluginDisableCallback([](id) { });
pluginManager.SetPluginLoadCallback([](
id,
plugin) { });
pluginManager.SetPluginUnloadCallback([](id) { });
Definition IFieldConversionPlugin.hpp:8
New Approach
pluginManager.RegisterObserver(observer);
Event Timing
| Event | When | Plugin Pointer |
| Registered | After file copied | Valid |
| Loaded | After library loaded | Valid |
| Enabled | After marking enabled | Valid |
| Disabled | Before marking disabled | Valid |
| Unloaded | Before unloading | nullptr |
| Unregistered | After file deleted | nullptr |
Thread Safety
- All observer notifications happen on the same thread that calls PluginManager methods
- Observers should not perform long-running operations in OnPluginEvent()
- UI updates from observers should be queued to the main thread if needed
Best Practices
- Unregister on Destruction: Always unregister observers before they're destroyed
~MainWindow() {
}
void UnregisterObserver(IPluginObserver *observer)
Unregisters an observer.
Definition PluginManager.cpp:1529
- Handle All Events: Use switch statement with all cases or default
void OnPluginEvent(PluginEvent event, ...) {
switch (event) {
case PluginEvent::Enabled: break;
case PluginEvent::Disabled: break;
default:
break;
}
}
- Quick Processing: Keep event handlers fast
void OnPluginEvent(PluginEvent event, ...) {
QMetaObject::invokeMethod(this, [this, pluginId]() {
}, Qt::QueuedConnection);
}
- Null Checks: Always check plugin pointer for Unloaded/Unregistered events
void OnPluginEvent(PluginEvent event,
const std::string&
id, IPlugin*
plugin) {
if (event == PluginEvent::Unloaded &&
plugin ==
nullptr) {
}
}