Refactor get last error helper
This commit is contained in:
parent
978420e4a0
commit
425f9c3e13
58
AdaptiveBrightness/get_last_error.cpp
Normal file
58
AdaptiveBrightness/get_last_error.cpp
Normal 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
|
9
AdaptiveBrightness/get_last_error.hpp
Normal file
9
AdaptiveBrightness/get_last_error.hpp
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#ifdef _MSC_VER
|
||||||
|
|
||||||
|
#include <QString>
|
||||||
|
|
||||||
|
QString getLastErrorString();
|
||||||
|
|
||||||
|
#endif
|
@ -2,71 +2,28 @@
|
|||||||
|
|
||||||
#ifdef _MSC_VER
|
#ifdef _MSC_VER
|
||||||
|
|
||||||
#include <type_traits>
|
|
||||||
|
|
||||||
#include <QString>
|
|
||||||
#include <QtDebug>
|
#include <QtDebug>
|
||||||
#include <QtGlobal>
|
#include <QtGlobal>
|
||||||
|
|
||||||
#include <tchar.h>
|
#include "get_last_error.hpp"
|
||||||
|
|
||||||
#include "log_tr.hpp"
|
#include "log_tr.hpp"
|
||||||
|
|
||||||
namespace detail {
|
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)
|
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);
|
std::vector<Monitor>& monitors = *reinterpret_cast<std::vector<Monitor>*>(lParam);
|
||||||
DWORD numMonitors;
|
DWORD numMonitors;
|
||||||
|
|
||||||
if(!GetNumberOfPhysicalMonitorsFromHMONITOR(hMonitor, &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
|
return true; // Continue enumerating
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<PHYSICAL_MONITOR> physicalMonitors(numMonitors);
|
std::vector<PHYSICAL_MONITOR> physicalMonitors(numMonitors);
|
||||||
|
|
||||||
if(!GetPhysicalMonitorsFromHMONITOR(hMonitor, numMonitors, physicalMonitors.data())) {
|
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
|
return true; // Continue enumerating
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -90,7 +47,7 @@ std::vector<Monitor> enumerateMonitors()
|
|||||||
std::vector<Monitor> monitors;
|
std::vector<Monitor> monitors;
|
||||||
|
|
||||||
if(!EnumDisplayMonitors(nullptr, nullptr, detail::enumMonitorsCallback, reinterpret_cast<LPARAM>(&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;
|
return monitors;
|
||||||
|
Loading…
Reference in New Issue
Block a user