diff --git a/src/libprojectM/Logging.cpp b/src/libprojectM/Logging.cpp index e283eaf524..f04a0551b7 100644 --- a/src/libprojectM/Logging.cpp +++ b/src/libprojectM/Logging.cpp @@ -53,15 +53,20 @@ auto Logging::HasCallback() -> bool } void Logging::Log(const std::string& message, LogLevel severity) +{ + Log(message.c_str(), severity); +} + +void Logging::Log(const char* message, LogLevel severity) { auto callback = GetLoggingCallback(); - if (callback.callbackFunction == nullptr) + if (callback.callbackFunction == nullptr || message == nullptr) { return; } - callback.callbackFunction(message.c_str(), static_cast(severity), callback.userData); + callback.callbackFunction(message, static_cast(severity), callback.userData); } auto Logging::GetLoggingCallback() -> UserCallback diff --git a/src/libprojectM/Logging.hpp b/src/libprojectM/Logging.hpp index 25f870fcc1..b83acedc38 100644 --- a/src/libprojectM/Logging.hpp +++ b/src/libprojectM/Logging.hpp @@ -91,6 +91,15 @@ class Logging */ PROJECTM_CXX_EXPORT static void Log(const std::string& message, LogLevel severity); + /** + * @brief Passes a log message with the given severity to the active thread or global callback. + * This overload avoids implicit string allocations. + * If no callbacks are registered, this function does nothing. + * @param message Null-terminated C string message + * @param severity LogLevel severity + */ + PROJECTM_CXX_EXPORT static void Log(const char* message, LogLevel severity); + /** * The default log level used if no log level is set (LogLevel::Information) */ diff --git a/src/libprojectM/ProjectMCWrapper.cpp b/src/libprojectM/ProjectMCWrapper.cpp index cd2bca2f2e..f822bdb47b 100644 --- a/src/libprojectM/ProjectMCWrapper.cpp +++ b/src/libprojectM/ProjectMCWrapper.cpp @@ -12,6 +12,8 @@ #include #include +#include + #include #include @@ -76,6 +78,8 @@ projectm_handle projectm_create() projectm_handle projectm_create_with_opengl_load_proc(void* (*load_proc)(const char*, void*), void* user_data) { + using libprojectM::Logging; + try { // Init resolver to discover gl function pointers (guarded internally, valid to call multiple times) @@ -95,8 +99,15 @@ projectm_handle projectm_create_with_opengl_load_proc(void* (*load_proc)(const c auto* projectMInstance = new libprojectM::projectMWrapper(); return reinterpret_cast(projectMInstance); } + catch (const std::exception& e) + { + LOG_ERROR("projectm_create_with_opengl_load_proc caught exception:"); + LOG_ERROR(e.what()); + return nullptr; + } catch (...) { + LOG_ERROR("projectm_create_with_opengl_load_proc caught unknown exception"); return nullptr; } }