From c50784ba0bba2fafff8f65137bc7c84610408cbf Mon Sep 17 00:00:00 2001 From: Lauri Nurmi Date: Sun, 26 Apr 2020 23:43:58 +0300 Subject: [PATCH] Use the same function for logging dynlib errors on all platforms Reduces the amount of #ifdefs scattered all over the code, thereby simplifying the code. The function was renamed from Error() to ReportError() to emphasize what its purpose is. Error messages logged on *nix are now a bit more verbose, as they are prefixed with our own description text, which were earlier omitted on platforms using the dlxxx() API. --- include/wx/dynlib.h | 8 +++----- src/common/dynlib.cpp | 14 +++----------- src/msw/dlmsw.cpp | 25 +++++++++++++++++++++++++ src/unix/dlunix.cpp | 19 +++++++++++++------ 4 files changed, 44 insertions(+), 22 deletions(-) diff --git a/include/wx/dynlib.h b/include/wx/dynlib.h index 6c791cfaba..3a94ab9d9e 100644 --- a/include/wx/dynlib.h +++ b/include/wx/dynlib.h @@ -357,11 +357,9 @@ protected: // common part of GetSymbol() and HasSymbol() void* DoGetSymbol(const wxString& name, bool* success = NULL) const; -#ifdef HAVE_DLERROR - // log the error after a dlxxx() function failure - static void Error(); -#endif // HAVE_DLERROR - + // log the error after an OS dynamic library function failure + static void ReportError(const wxString& msg, + const wxString& name = wxString()); // the handle to DLL or NULL wxDllType m_handle; diff --git a/src/common/dynlib.cpp b/src/common/dynlib.cpp index 13b0f4544e..f36a6b7efa 100644 --- a/src/common/dynlib.cpp +++ b/src/common/dynlib.cpp @@ -86,11 +86,7 @@ bool wxDynamicLibrary::Load(const wxString& libnameOrig, int flags) if ( m_handle == 0 && !(flags & wxDL_QUIET) ) { -#ifdef HAVE_DLERROR - Error(); -#else - wxLogSysError(_("Failed to load shared library '%s'"), libname.c_str()); -#endif + ReportError(_("Failed to load shared library '%s'"), libname); } return IsLoaded(); @@ -114,12 +110,8 @@ void *wxDynamicLibrary::GetSymbol(const wxString& name, bool *success) const void *symbol = DoGetSymbol(name, success); if ( !symbol ) { -#ifdef HAVE_DLERROR - Error(); -#else - wxLogSysError(_("Couldn't find symbol '%s' in a dynamic library"), - name.c_str()); -#endif + ReportError(_("Couldn't find symbol '%s' in a dynamic library"), + name.c_str()); } return symbol; diff --git a/src/msw/dlmsw.cpp b/src/msw/dlmsw.cpp index d664c6ae4b..d5916d408f 100644 --- a/src/msw/dlmsw.cpp +++ b/src/msw/dlmsw.cpp @@ -145,6 +145,31 @@ wxDllType wxDynamicLibrary::GetProgramHandle() return (wxDllType)::GetModuleHandle(NULL); } +// ---------------------------------------------------------------------------- +// error handling +// ---------------------------------------------------------------------------- + +/* static */ +void wxDynamicLibrary::ReportError(const wxString& message, const wxString& name) +{ + wxString msg(message); + if ( name.IsEmpty() && msg.Find("%s") == wxNOT_FOUND ) + msg += "%s"; + // msg needs a %s for the name + wxASSERT(msg.Find("%s") != wxNOT_FOUND); + + const unsigned long code = wxSysErrorCode(); + wxString errMsg = wxSysErrorMsgStr(code); + + // The error message (specifically code==193) may contain a + // placeholder '%1' which stands for the filename. + errMsg.Replace("%1", name, false); + + // Mimic the output of wxLogSysError(), but use our pre-processed + // errMsg. + wxLogError(msg + " " + _("(error %d: %s)"), name, code, errMsg); +} + // ---------------------------------------------------------------------------- // loading/unloading DLLs // ---------------------------------------------------------------------------- diff --git a/src/unix/dlunix.cpp b/src/unix/dlunix.cpp index 325d029c02..66606df70d 100644 --- a/src/unix/dlunix.cpp +++ b/src/unix/dlunix.cpp @@ -120,8 +120,8 @@ void wxDynamicLibrary::Unload(wxDllType handle) #if defined(USE_POSIX_DL_FUNCS) && defined(HAVE_DLERROR) if ( rc != 0 ) - Error(); #endif + ReportError(_("Failed to unload shared library")); } /* static */ @@ -145,20 +145,27 @@ void *wxDynamicLibrary::RawGetSymbol(wxDllType handle, const wxString& name) // error handling // ---------------------------------------------------------------------------- -#ifdef HAVE_DLERROR - /* static */ -void wxDynamicLibrary::Error() +void wxDynamicLibrary::ReportError(const wxString& message, + const wxString& name) { + wxString msg(message); + if ( name.IsEmpty() && msg.Find("%s") == wxNOT_FOUND ) + msg += "%s"; + // msg needs a %s for the name + wxASSERT(msg.Find("%s") != wxNOT_FOUND); +#ifdef HAVE_DLERROR wxString err(dlerror()); if ( err.empty() ) err = _("Unknown dynamic library error"); - wxLogError(wxT("%s"), err); + wxLogError(msg + wxT(": %s"), name, err); +#else // !HAVE_DLERROR + wxLogSysError(msg, name); +#endif // HAVE_DLERROR } -#endif // HAVE_DLERROR // ---------------------------------------------------------------------------- // listing loaded modules