diff --git a/apps/esmtool/esmtool.cpp b/apps/esmtool/esmtool.cpp index 065ea5ecae..092be66e97 100644 --- a/apps/esmtool/esmtool.cpp +++ b/apps/esmtool/esmtool.cpp @@ -439,7 +439,8 @@ namespace auto stream = Files::openBinaryInputFileStream(info.filename); if (!stream->is_open()) { - std::cout << "Failed to open file: " << std::strerror(errno) << '\n'; + std::cout << "Failed to open file " << info.filename << ": " << std::generic_category().message(errno) + << '\n'; return -1; } diff --git a/apps/wizard/unshield/unshieldworker.cpp b/apps/wizard/unshield/unshieldworker.cpp index 3dca7c1393..61162f5627 100644 --- a/apps/wizard/unshield/unshieldworker.cpp +++ b/apps/wizard/unshield/unshieldworker.cpp @@ -177,7 +177,7 @@ bool Wizard::UnshieldWorker::setupSettings() if (file.fail()) { emit error(tr("Failed to open Morrowind configuration file!"), - tr("Opening %1 failed: %2.").arg(getIniPath(), strerror(errno))); + tr("Opening %1 failed: %2.").arg(getIniPath(), std::generic_category().message(errno).c_str())); return false; } @@ -196,14 +196,14 @@ bool Wizard::UnshieldWorker::writeSettings() if (file.fail()) { emit error(tr("Failed to open Morrowind configuration file!"), - tr("Opening %1 failed: %2.").arg(getIniPath(), strerror(errno))); + tr("Opening %1 failed: %2.").arg(getIniPath(), std::generic_category().message(errno).c_str())); return false; } if (!mIniSettings.writeFile(getIniPath(), file, mIniEncoding)) { emit error(tr("Failed to write Morrowind configuration file!"), - tr("Writing to %1 failed: %2.").arg(getIniPath(), strerror(errno))); + tr("Writing to %1 failed: %2.").arg(getIniPath(), std::generic_category().message(errno).c_str())); return false; } diff --git a/components/crashcatcher/crashcatcher.cpp b/components/crashcatcher/crashcatcher.cpp index cd24b0e1f7..566b3a34fc 100644 --- a/components/crashcatcher/crashcatcher.cpp +++ b/components/crashcatcher/crashcatcher.cpp @@ -176,7 +176,8 @@ static void gdb_info(pid_t pid) /* Clean up */ if (remove(respfile) != 0) - Log(Debug::Warning) << "Warning: can not remove file '" << respfile << "': " << std::strerror(errno); + Log(Debug::Warning) << "Warning: can not remove file '" << respfile + << "': " << std::generic_category().message(errno); } else { @@ -184,9 +185,11 @@ static void gdb_info(pid_t pid) if (fd >= 0) { if (close(fd) != 0) - Log(Debug::Warning) << "Warning: can not close file '" << respfile << "': " << std::strerror(errno); + Log(Debug::Warning) << "Warning: can not close file '" << respfile + << "': " << std::generic_category().message(errno); else if (remove(respfile) != 0) - Log(Debug::Warning) << "Warning: can not remove file '" << respfile << "': " << std::strerror(errno); + Log(Debug::Warning) << "Warning: can not remove file '" << respfile + << "': " << std::generic_category().message(errno); } printf("!!! Could not create gdb command file\n"); } diff --git a/components/files/linuxpath.cpp b/components/files/linuxpath.cpp index 71affb6e06..4dbe119917 100644 --- a/components/files/linuxpath.cpp +++ b/components/files/linuxpath.cpp @@ -56,7 +56,8 @@ namespace Files const auto err = ec.value(); if (err != 0) { - Log(Debug::Warning) << "Error " << err << " " << std::strerror(err) << " when changing current directory"; + Log(Debug::Warning) << "Error " << err << " " << std::generic_category().message(errno) + << " when changing current directory"; } } diff --git a/components/files/openfile.cpp b/components/files/openfile.cpp index 58ab2869ff..550fa4e33c 100644 --- a/components/files/openfile.cpp +++ b/components/files/openfile.cpp @@ -10,8 +10,8 @@ namespace Files { auto stream = std::make_unique(path, std::ios::binary); if (!stream->is_open()) - throw std::runtime_error( - "Failed to open '" + Files::pathToUnicodeString(path) + "' for reading: " + std::strerror(errno)); + throw std::system_error(errno, std::generic_category(), + "Failed to open '" + Files::pathToUnicodeString(path) + "' for reading"); stream->exceptions(std::ios::badbit); return stream; } diff --git a/components/misc/strings/format.hpp b/components/misc/strings/format.hpp index ee52fceaaf..b542b7291f 100644 --- a/components/misc/strings/format.hpp +++ b/components/misc/strings/format.hpp @@ -7,6 +7,7 @@ #include #include #include +#include #include namespace Misc::StringUtils @@ -50,12 +51,11 @@ namespace Misc::StringUtils { const int size = std::snprintf(nullptr, 0, fmt, argument(args)...); if (size < 0) - throw std::runtime_error( - std::string("Failed to compute resulting string size: ") + std::strerror(errno)); + throw std::system_error(errno, std::generic_category(), "Failed to compute resulting string size"); // Note: sprintf also writes a trailing null character. We should remove it. std::string ret(static_cast(size) + 1, '\0'); if (std::sprintf(ret.data(), fmt, argument(args)...) < 0) - throw std::runtime_error(std::string("Failed to format string: ") + std::strerror(errno)); + throw std::system_error(errno, std::generic_category(), "Failed to format string"); ret.erase(static_cast(size)); return ret; } diff --git a/components/misc/thread.cpp b/components/misc/thread.cpp index 7a303a541b..7ac17d46d2 100644 --- a/components/misc/thread.cpp +++ b/components/misc/thread.cpp @@ -20,7 +20,7 @@ namespace Misc Log(Debug::Verbose) << "Using idle priority for thread=" << std::this_thread::get_id(); else Log(Debug::Warning) << "Failed to set idle priority for thread=" << std::this_thread::get_id() << ": " - << std::strerror(errno); + << std::generic_category().message(errno); } } @@ -56,7 +56,7 @@ namespace Misc Log(Debug::Verbose) << "Using idle priority for thread=" << std::this_thread::get_id(); else Log(Debug::Warning) << "Failed to set idle priority for thread=" << std::this_thread::get_id() << ": " - << std::strerror(errno); + << std::generic_category().message(errno); } } diff --git a/components/platform/file.posix.cpp b/components/platform/file.posix.cpp index b8f6adfcd7..591b4d651f 100644 --- a/components/platform/file.posix.cpp +++ b/components/platform/file.posix.cpp @@ -43,8 +43,8 @@ namespace Platform::File auto handle = ::open(filename.c_str(), openFlags, 0); if (handle == -1) { - throw std::runtime_error(std::string("Failed to open '") + Files::pathToUnicodeString(filename) - + "' for reading: " + strerror(errno)); + throw std::system_error(errno, std::generic_category(), + std::string("Failed to open '") + Files::pathToUnicodeString(filename) + "' for reading"); } return static_cast(handle); } @@ -63,7 +63,7 @@ namespace Platform::File if (::lseek(nativeHandle, position, nativeSeekType) == -1) { - throw std::runtime_error("An lseek() call failed: " + std::string(strerror(errno))); + throw std::system_error(errno, std::generic_category(), "An lseek() call failed"); } } @@ -85,7 +85,7 @@ namespace Platform::File size_t position = ::lseek(nativeHandle, 0, SEEK_CUR); if (position == size_t(-1)) { - throw std::runtime_error("An lseek() call failed: " + std::string(strerror(errno))); + throw std::system_error(errno, std::generic_category(), "An lseek() call failed"); } return position; } @@ -97,8 +97,8 @@ namespace Platform::File int amount = ::read(nativeHandle, data, size); if (amount == -1) { - throw std::runtime_error( - "An attempt to read " + std::to_string(size) + " bytes failed: " + strerror(errno)); + throw std::system_error( + errno, std::generic_category(), "An attempt to read " + std::to_string(size) + " bytes failed"); } return amount; } diff --git a/components/platform/file.stdio.cpp b/components/platform/file.stdio.cpp index c9553da712..1fa84fc44d 100644 --- a/components/platform/file.stdio.cpp +++ b/components/platform/file.stdio.cpp @@ -34,8 +34,8 @@ namespace Platform::File FILE* handle = fopen(filename.c_str(), "rb"); if (handle == nullptr) { - throw std::runtime_error(std::string("Failed to open '") + Files::pathToUnicodeString(filename) - + "' for reading: " + strerror(errno)); + throw std::system_error(errno, std::generic_category(), + std::string("Failed to open '") + Files::pathToUnicodeString(filename) + "' for reading"); } return static_cast(reinterpret_cast(handle)); } @@ -52,7 +52,7 @@ namespace Platform::File const auto nativeSeekType = getNativeSeekType(type); if (fseek(nativeHandle, position, nativeSeekType) != 0) { - throw std::runtime_error(std::string("An fseek() call failed: ") + strerror(errno)); + throw std::system_error(errno, std::generic_category(), std::string("An fseek() call failed")); } } @@ -75,7 +75,7 @@ namespace Platform::File long position = ftell(nativeHandle); if (position == -1) { - throw std::runtime_error(std::string("An ftell() call failed: ") + strerror(errno)); + throw std::system_error(errno, std::generic_category(), std::string("An ftell() call failed")); } return static_cast(position); } @@ -87,8 +87,8 @@ namespace Platform::File int amount = fread(data, 1, size, nativeHandle); if (amount == 0 && ferror(nativeHandle)) { - throw std::runtime_error( - std::string("An attempt to read ") + std::to_string(size) + " bytes failed: " + strerror(errno)); + throw std::system_error(errno, std::generic_category(), + std::string("An attempt to read ") + std::to_string(size) + " bytes failed"); } return static_cast(amount); }