diff options
| author | 2022-12-13 13:34:44 +0000 | |
|---|---|---|
| committer | 2022-12-13 14:17:50 +0000 | |
| commit | 33c08fc8aaf1ba5780a09d6ceaa2351abfd5e964 (patch) | |
| tree | e5afb388297e20123ca77baaabc24d5ae6f3c49d /src | |
| parent | Show the system error message when loading modules fails. (diff) | |
Deduplicate retrieving error messages on Windows.
Diffstat (limited to 'src')
| -rw-r--r-- | src/dynamic.cpp | 13 | ||||
| -rw-r--r-- | src/modulemanager.cpp | 7 | ||||
| -rw-r--r-- | src/socketengine.cpp | 15 |
3 files changed, 10 insertions, 25 deletions
diff --git a/src/dynamic.cpp b/src/dynamic.cpp index 5f69820c9..ba75a4a31 100644 --- a/src/dynamic.cpp +++ b/src/dynamic.cpp @@ -117,19 +117,14 @@ void* DLLManager::GetSymbol(const char* name) const void DLLManager::RetrieveLastError() { -#if defined _WIN32 - char errmsg[500]; - DWORD dwErrorCode = GetLastError(); - if (FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dwErrorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)errmsg, _countof(errmsg), NULL) == 0) - sprintf_s(errmsg, _countof(errmsg), "Error code: %u", dwErrorCode); +#ifdef _WIN32 + err = GetErrorMessage(GetLastError()); SetLastError(ERROR_SUCCESS); - err = errmsg; #else const char* errmsg = dlerror(); err = errmsg ? errmsg : "Unknown error"; #endif - std::string::size_type p; - while ((p = err.find_last_of("\r\n")) != std::string::npos) - err.erase(p, 1); + for (size_t pos = 0; ((pos = err.find_first_of("\r\n", pos)) != std::string::npos); ) + err[pos] = ' '; } diff --git a/src/modulemanager.cpp b/src/modulemanager.cpp index f44a7ca7b..aefccb0d5 100644 --- a/src/modulemanager.cpp +++ b/src/modulemanager.cpp @@ -134,12 +134,9 @@ void ModuleManager::LoadCoreModules(std::map<std::string, ServiceList>& servicem if (!FileSystem::GetFileList(ServerInstance->Config->Paths.Module, files, "core_*.so")) { #ifdef _WIN32 - DWORD errcode = GetLastError(); - char errmsg[500]; - if (FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errcode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)errmsg, _countof(errmsg), NULL) == 0) - sprintf_s(errmsg, _countof(errmsg), "Error code: %u", errcode); + const std::string errmsg = GetErrorMessage(GetLastError()); #else - char* errmsg = strerror(errno); + const char* errmsg = strerror(errno); #endif std::cout << "failed: " << errmsg << "!" << std::endl; ServerInstance->Exit(EXIT_STATUS_MODULE); diff --git a/src/socketengine.cpp b/src/socketengine.cpp index 85f2ddd04..06b948248 100644 --- a/src/socketengine.cpp +++ b/src/socketengine.cpp @@ -366,17 +366,10 @@ std::string SocketEngine::LastError() #ifndef _WIN32 return strerror(errno); #else - char szErrorString[500]; - DWORD dwErrorCode = WSAGetLastError(); - if (FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dwErrorCode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)szErrorString, _countof(szErrorString), NULL) == 0) - sprintf_s(szErrorString, _countof(szErrorString), "Error code: %u", dwErrorCode); - - std::string::size_type p; - std::string ret = szErrorString; - while ((p = ret.find_last_of("\r\n")) != std::string::npos) - ret.erase(p, 1); - - return ret; + std::string err = GetErrorMessage(WSAGetLastError()); + for (size_t pos = 0; ((pos = err.find_first_of("\r\n", pos)) != std::string::npos); ) + err[pos] = ' '; + return err; #endif } |
