Implemented windows error formatting

This commit is contained in:
BlackMark 2020-07-05 17:38:15 +02:00
parent 7afbc443c9
commit dc282b6ab5

View File

@ -2,19 +2,55 @@
#ifdef _MSC_VER
#include <type_traits>
#include <QString>
#include <QtDebug>
#include <QtGlobal>
#include <tchar.h>
#include "log_tr.hpp"
namespace detail {
template<typename...>
struct always_false : std::false_type {
};
template<typename... Ts>
inline constexpr auto always_false_v = always_false<Ts...>::value;
template<typename T>
static QString fromTchar(T* tString)
{
if constexpr(std::is_same_v<T, char>) {
return QString().fromLocal8Bit(tString).trimmed();
}
else if constexpr(std::is_same_v<T, wchar_t>) {
return QString().fromWCharArray(tString).trimmed();
}
else {
static_assert(always_false_v<T>, "Unknown TCHAR type");
}
}
static QString getLastErrorString()
{
DWORD lastErrorId = GetLastError();
return {};
TCHAR* msgBuf;
const auto formatFlags = FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS;
if(FormatMessage(formatFlags, nullptr, lastErrorId, 0, reinterpret_cast<LPTSTR>(&msgBuf), 0, nullptr) == 0) {
return "FormatMessage failed";
}
const auto lastErrorString = fromTchar(msgBuf);
LocalFree(msgBuf);
return lastErrorString;
}
static BOOL enumMonitorsCallback(HMONITOR hMonitor, [[maybe_unused]] HDC hDC, [[maybe_unused]] LPRECT lpRect, LPARAM lParam)