diff --git a/Src/Logging/Logging.h b/Src/Logging/Logging.h index 33372d4..4662143 100644 --- a/Src/Logging/Logging.h +++ b/Src/Logging/Logging.h @@ -137,15 +137,6 @@ class CLogger void setLogLevel(eLogLevel logLevel) { - if (m_mainLogger == nullptr) - { - spdlog::set_level(getSpdlogLevel(m_nLogLevel)); - } - else - { - m_mainLogger->set_level(getSpdlogLevel(m_nLogLevel)); - } - if (m_consoleSink != nullptr) { m_consoleSink->set_level(getSpdlogLevel(m_nLogLevel)); @@ -155,6 +146,15 @@ class CLogger { m_fileSink->set_level(getSpdlogLevel(m_nLogLevel)); } + + if (m_mainLogger == nullptr) + { + spdlog::set_level(getSpdlogLevel(m_nLogLevel)); + } + else + { + m_mainLogger->set_level(getSpdlogLevel(m_nLogLevel)); + } } std::string getLogFilePath(const std::string sDir) diff --git a/Src/PlugIn/CPluginFileInfoMgr.h b/Src/PlugIn/CPluginFileInfoMgr.h new file mode 100644 index 0000000..3283773 --- /dev/null +++ b/Src/PlugIn/CPluginFileInfoMgr.h @@ -0,0 +1,77 @@ +///**************************************************************************** +/// FILE: CPluginFileInfoMgr.h +/// +/// DEESC: Plugin (lib) interface manager class def +/// +/// AUTHOR: Russ Barker +/// + + +#ifndef PLUGIN_FILE_INFO_MANAGER_H +#define PLUGIN_FILE_INFO_MANAGER_H + + +#include +#include +#include +#include + +#include "../Error/CError.h" + +#include "PluginDefs.h" + + +// All plugins MUST provide a root class +// derived from this + +//class PLUGIN_LIB_API CPluginFileInfoMgrBase +class CPluginFileInfoMgrBase +{ + +protected: + + std::string m_sPluginModuleName; + + std::string m_sPluginDescription; + + int m_nPluginType; + +public: + + CPluginFileInfoMgrBase() + { + clear(); + } + + ~CPluginFileInfoMgrBase() + { + clear(); + } + + void clear() + { + m_sPluginModuleName.clear(); + m_sPluginDescription.clear(); + m_nPluginType = 0; + } + + std::string GetPluginModuleName() + { + return m_sPluginModuleName; + } + + std::string GetPluginDescription() + { + return m_sPluginDescription; + } + + int GetPluginType() + { + return m_nPluginType; + } + +}; + + + +#endif // PLUGIN_FILE_INFO_MANAGER_H \ No newline at end of file diff --git a/Src/PlugIn/CPluginFileMgr.h b/Src/PlugIn/CPluginFileMgr.h index a7352b3..b239eea 100644 --- a/Src/PlugIn/CPluginFileMgr.h +++ b/Src/PlugIn/CPluginFileMgr.h @@ -1,7 +1,7 @@ ///**************************************************************************** /// FILE: CPluginFileMgr.h /// -/// DEESC: Plugin (lib) file manager class def +/// DEESC: Plugin (lib) file inst manager class def /// /// AUTHOR: Russ Barker /// @@ -39,7 +39,7 @@ class CPluginFileMgr public: - CPluginFileMgr(const std::string &sDir = "", const std::string &sExt = "") : + CPluginFileMgr(const std::string &sDir = "", const std::string &sExt = "") : m_sDirPath(sDir), m_sPluginFileExt(sExt) { @@ -71,7 +71,7 @@ class CPluginFileMgr int findPluginFiles(const std::string &sExt = "") { - if (sExt.empty() != false) + if (sExt != "") m_sPluginFileExt = sExt; if (m_sDirPath.empty() || m_sPluginFileExt.empty()) @@ -87,7 +87,7 @@ class CPluginFileMgr std::string sCurrFileExt = entry.path().extension().string(); - if (sCurrFileExt == m_sPluginFileExt) + if (sCurrFileExt == ("." + m_sPluginFileExt)) { m_pluginFileList.push_back(sCurrFilePath); } @@ -113,4 +113,4 @@ class CPluginFileMgr }; -#endif / PLUGIN_FILE_MGR_H +#endif // PLUGIN_FILE_MGR_H diff --git a/Src/PlugIn/CPluginLibLoader.h b/Src/PlugIn/CPluginLibLoader.h new file mode 100644 index 0000000..970206f --- /dev/null +++ b/Src/PlugIn/CPluginLibLoader.h @@ -0,0 +1,260 @@ +///**************************************************************************** +/// FILE: CPluginLibLoader.h +/// +/// DEESC: Plugin (lib) loader class / function def +/// +/// AUTHOR: Russ Barker +/// + + +#ifndef _PLUGIN_LIB_LOADER_H +#define _PLUGIN_LIB_LOADER_H + + + + +#include +#include +#include +#include + +#ifdef WINDOWS +#include +#else +#include +#endif + +#include "../Error/CError.h" + +#include "CPluginFileInfoMgr.h" + + +#ifdef UNICODE +ERROR_MESSAGE("Unicode NOT supported. Multi byte compile type must be set.") +#endif + + +//class CPluginLoader; + + + +template +class CPluginLibLoader : public CErrorHandler +{ + typedef void * (*VoidFunctionPtr)(); + +protected: + + std::string m_sFilePath; + +#ifdef WINDOWS + HINSTANCE m_hLib; +#else + void *m_hLib; +#endif + + bool m_bLoaded; + + VoidFunctionPtr m_createFileInfoFunc; + + VoidFunctionPtr m_createClassInstFunc; + + std::shared_ptr m_pPluginFileInfoMgr; + +public: + + CPluginLibLoader(std::string sPath = "") + { + clear(); + + m_sFilePath = sPath; + + m_hLib = nullptr; + + + m_createFileInfoFunc= nullptr; + m_createClassInstFunc = nullptr; + + m_pPluginFileInfoMgr = nullptr; + } + + ~CPluginLibLoader() + { + clear(); + + if (m_hLib != nullptr) + { +#ifdef WINDOWS + FreeLibrary(m_hLib); +#else + free(m_hLib); +#endif + } + } + + void clear() + { + m_sFilePath.clear(); + + m_bLoaded = false; + + m_pPluginFileInfoMgr = nullptr; + } + + bool SetFilePath(std::string sPath) + { + if (sPath == "") + { + SetErrorText("Invalid lib path"); + return false; + } + + m_sFilePath = sPath; + + ClearError(); + return true; + } + + bool Load(std::string sPath) + { + if (sPath != "") + { + m_sFilePath = sPath; + } + +#ifdef WINDOWS + + if (m_hLib == nullptr) + { + m_hLib = LoadLibrary(m_sFilePath.c_str()); + } + + if (m_hLib == nullptr) + { + SetErrorText("Failed to load lib at specified path"); + return false; + } + + try + { + m_createFileInfoFunc = (VoidFunctionPtr) GetProcAddress(m_hLib, "CreatePluginFileInfoInstance"); + + m_createClassInstFunc = (VoidFunctionPtr) GetProcAddress(m_hLib, "CreatePluginClassInstance"); + } + catch (...) + { + + } + +#else + + if (m_hLib == nullptr) + { + m_hLib = dlopen(m_sFilePath.c_str(), RTLD_NOW | RTLD_GLOBAL); + } + + if (m_hLib == nullptr) + { + auto message = dlerror(); + + SetErrorText("Failed to load lib at specified path " + message); + return false; + } + + try + { + m_createFileInfoFunc = (VoidFunctionPtr) dlsym(m_hLib, "CreatePluginFileInfoInstance"); + + m_createClassInstFunc = (VoidFunctionPtr) dlsym(m_hLib, "CreatePluginClassInstance"); + } + catch(...) + { + } + +#endif + + if (m_createFileInfoFunc == nullptr) + { + SetErrorText("Failed to load CreatePluginFileInfoInstance"); + return false; + } + else + { + void *pFileInfoMgr = nullptr; + + try + { + pFileInfoMgr = m_createFileInfoFunc(); + } + catch (...) + { + + } + + std::shared_ptr sharePtr((CPluginFileInfoMgrBase *) pFileInfoMgr); + + m_pPluginFileInfoMgr = sharePtr; + } + + if (m_createClassInstFunc == nullptr) + { + SetErrorText("Failed to load CreatePluginFileInfoInstance"); + return false; + } + + if (m_pPluginFileInfoMgr != nullptr) + { + ClearError(); + return true; + } + + SetErrorText("Failed to create pligin functions / interfaces"); + + return false; + } + + std::shared_ptr GetPluginFileInfoMgr() + { + return m_pPluginFileInfoMgr; + } + + std::shared_ptr createPluginClassInst() + { + void *pClassInst = nullptr; + + try + { + pClassInst = m_createClassInstFunc(); + } + catch (...) + { + + } + + std::shared_ptr sharePtr((T*) pClassInst); + + return sharePtr; + } + +}; + + + + +/// +/// CreatePluginMgrInstance function +/// +/// All plugins based on this plugin base class +/// MUST impliment an instance of this function. +/// It creates the class interface pointer to a +/// class derrived from the CPluginClassInstMgrBase +/// that in turn creates the other classes/interfaces +/// that will be used for different kinds of plugins. +/// + + +// template +// T * CreatePluginMgrInstance(); + + +#endif // _PLUGIN_LOADER_H \ No newline at end of file diff --git a/Src/PlugIn/Plugin.h b/Src/PlugIn/Plugin.h index fe488cc..6614335 100644 --- a/Src/PlugIn/Plugin.h +++ b/Src/PlugIn/Plugin.h @@ -7,6 +7,13 @@ // +#ifndef PLUGIN_MODULE_H +#define PLUGIN_MODULE_H + + #include "CPluginFileMgr.h" -#include "CPluginClassInstMgr.h" -#include "CPluginLoader.h" +#include "CPluginFileInfoMgr.h" +#include "CPluginLibLoader.h" + + +#endif // PLUGIN_MODULE_H diff --git a/Src/PlugIn/PluginDefs.h b/Src/PlugIn/PluginDefs.h new file mode 100644 index 0000000..9d690d6 --- /dev/null +++ b/Src/PlugIn/PluginDefs.h @@ -0,0 +1,55 @@ +//**************************************************************************** +// FILE: Plugin.h +// +// DEESC: Plugin (lib) header base file +// +// AUTHOR: Russ Barker +// + + +#ifndef PLUGIN_MODULE_DEFS +#define PLUGIN_MODULE_DEFS + + +#ifdef EXPORT_LIB + +// if this is included from a "plugin lib" module, +// then export the functions and classes. + +#ifndef PLUGIN_LIB_API +#ifdef WINDOWS +#define PLUGIN_LIB_API __declspec(dllexport) +#else +#define PLUGIN_LIB_API __attribute__((visibility("default"))) +#endif +#endif + +#else + +// if this is included from an application, +// then import the functions and classes. + +#ifndef PLUGIN_LIB_API +#ifdef WINDOWS +#define PLUGIN_LIB_API __declspec(dllimport) +#else +#define PLUGIN_LIB_API __attribute__((visibility("default"))) +#endif +#endif + +#endif + + +// define thhe 2 primary functions that will be +// included / exported from within the plugin libs. + +extern "C" +{ + PLUGIN_LIB_API void* CreatePluginFileInfoInstance(); + + PLUGIN_LIB_API void* CreatePluginClassInstance(); + +}; + + +#endif // PLUGIN_MODULE_DEFS diff --git a/Src/Thread/ThreadBase.h b/Src/Thread/ThreadBase.h index 0f45dcd..8efe123 100644 --- a/Src/Thread/ThreadBase.h +++ b/Src/Thread/ThreadBase.h @@ -74,10 +74,10 @@ class CThreadBase threadProc(); } - //catch (std::exception &e) - //{ - // LogCritical("Unhandled exception: {}", e.what()); - //} + catch (std::exception &e) + { + LogCritical("Unhandled exception: {}", e.what()); + } catch (...) { LogCritical("Unhandled unknown exception");