aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Ridgers <[email protected]>2021-09-15 12:32:52 +0200
committerStefan Boberg <[email protected]>2021-09-15 13:05:54 +0200
commitfac9012f486991b03f579d9105ce5d769f2c4d30 (patch)
tree494e03311eee3706247cc8f5518ba09d5a3f5c11
parentChanged logging implementation (diff)
downloadzen-fac9012f486991b03f579d9105ce5d769f2c4d30.tar.xz
zen-fac9012f486991b03f579d9105ce5d769f2c4d30.zip
Cross-platform zen::GetLastError()
-rw-r--r--zencore/except.cpp6
-rw-r--r--zencore/include/zencore/except.h18
2 files changed, 19 insertions, 5 deletions
diff --git a/zencore/except.cpp b/zencore/except.cpp
index 9bd447308..609a5387e 100644
--- a/zencore/except.cpp
+++ b/zencore/except.cpp
@@ -22,13 +22,13 @@ ThrowSystemException([[maybe_unused]] HRESULT hRes, [[maybe_unused]] std::string
void
ThrowLastError(std::string_view Message)
{
- throw std::system_error(std::error_code(::GetLastError(), std::system_category()), std::string(Message));
+ throw std::system_error(std::error_code(zen::GetLastError(), std::system_category()), std::string(Message));
}
std::string
GetLastErrorAsString()
{
- return GetWindowsErrorAsString(::GetLastError());
+ return GetWindowsErrorAsString(zen::GetLastError());
}
std::string
@@ -41,7 +41,7 @@ void
ThrowLastError(std::string_view Message, const std::source_location& Location)
{
using namespace fmt::literals;
- throw std::system_error(std::error_code(::GetLastError(), std::system_category()),
+ throw std::system_error(std::error_code(zen::GetLastError(), std::system_category()),
"{}({}): {}"_format(Location.file_name(), Location.line(), Message));
}
diff --git a/zencore/include/zencore/except.h b/zencore/include/zencore/except.h
index 8625f01d0..b1479128e 100644
--- a/zencore/include/zencore/except.h
+++ b/zencore/include/zencore/except.h
@@ -3,7 +3,11 @@
#pragma once
#include <zencore/string.h>
-#include <zencore/windows.h>
+#if ZEN_PLATFORM_WINDOWS
+# include <zencore/windows.h>
+#else
+# include <errno.h>
+#endif
#include <source_location>
#include <string>
@@ -57,6 +61,16 @@ ZENCORE_API void ThrowLastError(std::string_view Message, const std::source_loca
ZENCORE_API std::string GetLastErrorAsString();
ZENCORE_API std::string GetWindowsErrorAsString(uint32_t Win32ErrorCode);
+inline int32_t
+GetLastError()
+{
+#if ZEN_PLATFORM_WINDOWS
+ return ::GetLastError();
+#else
+ return errno;
+#endif
+}
+
inline std::error_code
MakeWin32ErrorCode(uint32_t Win32ErrorCode) noexcept
{
@@ -66,7 +80,7 @@ MakeWin32ErrorCode(uint32_t Win32ErrorCode) noexcept
inline std::error_code
MakeErrorCodeFromLastError() noexcept
{
- return std::error_code(::GetLastError(), std::system_category());
+ return std::error_code(zen::GetLastError(), std::system_category());
}
} // namespace zen