Refactor get last error helper

master
BlackMark 2020-07-05 19:08:10 +02:00
parent 978420e4a0
commit 425f9c3e13
3 changed files with 71 additions and 47 deletions

View File

@ -0,0 +1,58 @@
#include "get_last_error.hpp"
#ifdef _MSC_VER
#include <type_traits>
#include <QtDebug>
#include <QtGlobal>
#include <tchar.h>
#include <windows.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");
}
}
} // namespace detail
QString getLastErrorString()
{
DWORD lastErrorId = GetLastError();
TCHAR* msgBuf = nullptr;
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 log_tr("FormatMessage failed with error code '%1' while formatting error message for code: %2").arg(GetLastError()).arg(lastErrorId);
}
const auto lastErrorString = detail::fromTchar(msgBuf);
LocalFree(msgBuf);
return lastErrorString;
}
#endif

View File

@ -0,0 +1,9 @@
#pragma once
#ifdef _MSC_VER
#include <QString>
QString getLastErrorString();
#endif

View File

@ -2,71 +2,28 @@
#ifdef _MSC_VER
#include <type_traits>
#include <QString>
#include <QtDebug>
#include <QtGlobal>
#include <tchar.h>
#include "get_last_error.hpp"
#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();
TCHAR* msgBuf = nullptr;
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 log_tr("FormatMessage failed with error code '%1' while formatting error message for code: %2").arg(GetLastError()).arg(lastErrorId);
}
const auto lastErrorString = fromTchar(msgBuf);
LocalFree(msgBuf);
return lastErrorString;
}
static BOOL enumMonitorsCallback(HMONITOR hMonitor, [[maybe_unused]] HDC hDC, [[maybe_unused]] LPRECT lpRect, LPARAM lParam)
{
std::vector<Monitor>& monitors = *reinterpret_cast<std::vector<Monitor>*>(lParam);
DWORD numMonitors;
if(!GetNumberOfPhysicalMonitorsFromHMONITOR(hMonitor, &numMonitors)) {
qCritical(ltr("Unable to get number of physical monitors from handle, error: %1").arg(detail::getLastErrorString()));
qCritical(ltr("Unable to get number of physical monitors from handle, error: %1").arg(getLastErrorString()));
return true; // Continue enumerating
}
std::vector<PHYSICAL_MONITOR> physicalMonitors(numMonitors);
if(!GetPhysicalMonitorsFromHMONITOR(hMonitor, numMonitors, physicalMonitors.data())) {
qCritical(ltr("Unable to get physical monitors from handle, error: %1").arg(detail::getLastErrorString()));
qCritical(ltr("Unable to get physical monitors from handle, error: %1").arg(getLastErrorString()));
return true; // Continue enumerating
}
@ -90,7 +47,7 @@ std::vector<Monitor> enumerateMonitors()
std::vector<Monitor> monitors;
if(!EnumDisplayMonitors(nullptr, nullptr, detail::enumMonitorsCallback, reinterpret_cast<LPARAM>(&monitors))) {
qCritical(ltr("Unable to enumerate display monitors, error: %1").arg(detail::getLastErrorString()));
qCritical(ltr("Unable to enumerate display monitors, error: %1").arg(getLastErrorString()));
}
return monitors;