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()
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;

View File

@@ -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"),
ReportError(_("Couldn't find symbol '%s' in a dynamic library"),
name.c_str());
#endif
}
return symbol;

View File

@@ -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
// ----------------------------------------------------------------------------

View File

@@ -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