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.
This commit is contained in:
Lauri Nurmi
2020-04-26 23:43:58 +03:00
parent fb5c13ed00
commit c50784ba0b
4 changed files with 44 additions and 22 deletions

View File

@@ -357,11 +357,9 @@ protected:
// common part of GetSymbol() and HasSymbol() // common part of GetSymbol() and HasSymbol()
void* DoGetSymbol(const wxString& name, bool* success = NULL) const; void* DoGetSymbol(const wxString& name, bool* success = NULL) const;
#ifdef HAVE_DLERROR // log the error after an OS dynamic library function failure
// log the error after a dlxxx() function failure static void ReportError(const wxString& msg,
static void Error(); const wxString& name = wxString());
#endif // HAVE_DLERROR
// the handle to DLL or NULL // the handle to DLL or NULL
wxDllType m_handle; wxDllType m_handle;

View File

@@ -86,11 +86,7 @@ bool wxDynamicLibrary::Load(const wxString& libnameOrig, int flags)
if ( m_handle == 0 && !(flags & wxDL_QUIET) ) if ( m_handle == 0 && !(flags & wxDL_QUIET) )
{ {
#ifdef HAVE_DLERROR ReportError(_("Failed to load shared library '%s'"), libname);
Error();
#else
wxLogSysError(_("Failed to load shared library '%s'"), libname.c_str());
#endif
} }
return IsLoaded(); return IsLoaded();
@@ -114,12 +110,8 @@ void *wxDynamicLibrary::GetSymbol(const wxString& name, bool *success) const
void *symbol = DoGetSymbol(name, success); void *symbol = DoGetSymbol(name, success);
if ( !symbol ) if ( !symbol )
{ {
#ifdef HAVE_DLERROR ReportError(_("Couldn't find symbol '%s' in a dynamic library"),
Error();
#else
wxLogSysError(_("Couldn't find symbol '%s' in a dynamic library"),
name.c_str()); name.c_str());
#endif
} }
return symbol; return symbol;

View File

@@ -145,6 +145,31 @@ wxDllType wxDynamicLibrary::GetProgramHandle()
return (wxDllType)::GetModuleHandle(NULL); 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 // loading/unloading DLLs
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------

View File

@@ -120,8 +120,8 @@ void wxDynamicLibrary::Unload(wxDllType handle)
#if defined(USE_POSIX_DL_FUNCS) && defined(HAVE_DLERROR) #if defined(USE_POSIX_DL_FUNCS) && defined(HAVE_DLERROR)
if ( rc != 0 ) if ( rc != 0 )
Error();
#endif #endif
ReportError(_("Failed to unload shared library"));
} }
/* static */ /* static */
@@ -145,20 +145,27 @@ void *wxDynamicLibrary::RawGetSymbol(wxDllType handle, const wxString& name)
// error handling // error handling
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
#ifdef HAVE_DLERROR
/* static */ /* 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()); wxString err(dlerror());
if ( err.empty() ) if ( err.empty() )
err = _("Unknown dynamic library error"); 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 // listing loaded modules