Revision 7faa68e1 src/plugin_loader.cpp

View differences:

src/plugin_loader.cpp
1
#include <dlfcn.h>
2

  
3 1
#include "plugin_loader.h"
2

  
3
#include "plugin.h"
4 4
#include "logger.h"
5 5

  
6
using NF_ANALYZER::PluginLoader;
7
using NF_ANALYZER::SettingsGroup;
8

  
9
PluginLoader::PluginLoader(const std::string & fileName,
10
                           const SettingsGroup & s)
11
    : _loaded(false),
12
      _handle(NULL),
13
      _plugin(NULL),
14
      _strError(),
15
      _fileName(fileName),
16
      _settings(s)
6
#include <dlfcn.h>
7

  
8
using NFA::PluginLoader;
9
using NFA::SettingsGroup;
10

  
11
PluginLoader::PluginLoader(const std::string& fileName,
12
                           const SettingsGroup& s)
13
    : m_loaded(false),
14
      m_handle(NULL),
15
      m_plugin(NULL),
16
      m_fileName(fileName),
17
      m_settings(s)
17 18
{
18 19
}
19 20

  
......
24 25

  
25 26
bool PluginLoader::load()
26 27
{
27
    if (_loaded) {
28
    if (m_loaded) {
28 29
        logger << "PluginLoader::load() - plugin is already loaded" << std::endl;
29 30
        return false;
30 31
    }
31 32

  
32
    if (_fileName.empty()) {
33
        _strError = "Empty plugin filename";
34
        logger << "PluginLoader::load() - error loading plugin: '" << _strError << "'" << std::endl;
33
    if (m_fileName.empty()) {
34
        logger << "PluginLoader::load() - empty plugin filename." << std::endl;
35 35
        return false;
36 36
    }
37 37

  
38
    _handle = dlopen(_fileName.c_str(), RTLD_NOW);
38
    m_handle = dlopen(m_fileName.c_str(), RTLD_NOW);
39 39

  
40
    if (!_handle) {
41
        _strError = "dlopen error: '";
42
        _strError += dlerror();
43
        _strError += "'";
44
        logger << "PluginLoader::load() - failed to load plugin '" << _fileName << "'. " << _strError << std::endl;
40
    if (!m_handle) {
41
        logger << "PluginLoader::load() - failed to load plugin: " << dlerror() << std::endl;
45 42
        return false;
46 43
    }
47 44

  
48
    _loaded = true;
45
    m_loaded = true;
49 46

  
50
    Plugin * (* getPlugin)(const SettingsGroup &);
51
    getPlugin = (Plugin * (*)(const SettingsGroup &))dlsym(_handle, "getPlugin");
47
    auto getPlugin = (Plugin* (*)(const SettingsGroup &))dlsym(m_handle, "getPlugin");
52 48
    if (!getPlugin) {
53
        _strError = "getPlugin not found.";
54
        logger << "PluginLoader::load() - failed to get plugin accessor function for '" << _fileName << "'" << std::endl;
49
        logger << "PluginLoader::load() - failed to get plugin accessor function for '" << m_fileName << "'." << std::endl;
55 50
        return false;
56 51
    }
57 52

  
58
    _plugin = getPlugin(_settings);
53
    m_plugin = getPlugin(m_settings);
59 54

  
60
    if (!_plugin) {
61
        _strError = "NULL plugin";
62
        logger << "PluginLoader::load() - plugin accessor function return's NULL for '" << _fileName << "'" << std::endl;
55
    if (!m_plugin) {
56
        logger << "PluginLoader::load() - cannot create plugin instance for '" << m_fileName << "'." << std::endl;
63 57
        return false;
64 58
    }
65 59

  
66
    logger << "PluginLoader::load() - plugin '" << _plugin->name() << " " << _plugin->version() << "' loaded successfully" << std::endl;
60
    logger << "PluginLoader::load() - plugin '" << m_plugin->name() << " " << m_plugin->version() << "' loaded successfully." << std::endl;
67 61

  
68 62
    return true;
69 63
}
70 64

  
71 65
bool PluginLoader::unload()
72 66
{
73
    if (!_loaded) {
74
        logger << "PluginLoader::unload() - plugin is already unloaded" << std::endl;
67
    if (!m_loaded) {
68
        logger << "PluginLoader::unload() - plugin is already unloaded." << std::endl;
75 69
        return true;
76 70
    }
77 71

  
78
    if (dlclose(_handle)) {
79
        _strError = "dlclose error: '";
80
        _strError += dlerror();
81
        _strError += "'";
82
        logger << "PluginLoader::unload() - failed to unload plugin. " << _strError << std::endl;
72
    if (dlclose(m_handle)) {
73
        logger << "PluginLoader::unload() - failed to unload plugin: " << dlerror() << std::endl;
83 74
        return false;
84 75
    }
85 76

  
86
    _loaded = false;
77
    m_loaded = false;
87 78

  
88 79
    return true;
89 80
}

Also available in: Unified diff