优化日志输出

This commit is contained in:
DevWiki 2023-10-30 19:44:12 +08:00
parent 0b683b9a8e
commit fb8463a51d
3 changed files with 41 additions and 19 deletions

View File

@ -1,6 +1,9 @@
#pragma once #pragma once
#include "spdlog/spdlog.h" #include "spdlog/spdlog.h"
#define LOG_FUNC_START() spdlog::info("=====> {0} start <=====", __FUNCTION__)
#define LOG_FUNC_END() spdlog::info("=====> {0} end <=====", __FUNCTION__)
enum LogLevel : int enum LogLevel : int
{ {
Debug = spdlog::level::debug, Debug = spdlog::level::debug,

View File

@ -4,6 +4,7 @@
#include "spdlog/sinks/basic_file_sink.h" #include "spdlog/sinks/basic_file_sink.h"
#include "spdlog/sinks/stdout_color_sinks.h" #include "spdlog/sinks/stdout_color_sinks.h"
#include "Utils/StringUtil.h" #include "Utils/StringUtil.h"
#include "Utils/Log.h"
ScreenManager::ScreenManager() ScreenManager::ScreenManager()
{ {
@ -42,31 +43,43 @@ void ScreenManager::_UpdateDisplayDeviceList()
void ScreenManager::_UpdateDisplayAdapterList() void ScreenManager::_UpdateDisplayAdapterList()
{ {
spdlog::info("=====_UpdateDisplayAdapterList start====="); LOG_FUNC_START();
HRESULT hr = S_OK; HRESULT hr = S_OK;
hr = CoInitialize(nullptr); IDXGIFactory* pFactory = nullptr;
hr = CreateDXGIFactory(__uuidof(IDXGIFactory), (void**)(&pFactory));
if (FAILED(hr)) { if (FAILED(hr)) {
spdlog::error("_UpdateDisplayAdapterList CoInitialize failed"); spdlog::error("CreateDXGIFactory failed");
return; return;
} }
IDXGIFactory7* dxgiFactory = nullptr; IDXGIAdapter* pAdapter = nullptr;
hr = CreateDXGIFactory1(IID_PPV_ARGS(&dxgiFactory)); for (UINT adapterIndex = 0; pFactory->EnumAdapters(adapterIndex, &pAdapter) != DXGI_ERROR_NOT_FOUND; ++adapterIndex)
if (FAILED(hr)) { {
spdlog::error("CreateDXGIFactory1 failed"); DXGI_ADAPTER_DESC adapterDesc;
CoUninitialize(); pAdapter->GetDesc(&adapterDesc);
return; spdlog::info("Adapter Index:{0}, Description:{1}, DeviceId:{2}, VendorId:{3}, SubSysId:{4}, Revision:{5}, AdapterLuid(H-L):{6}-{7} ",
} adapterIndex, Wchar2String(adapterDesc.Description), adapterDesc.DeviceId, adapterDesc.VendorId, adapterDesc.SubSysId,
adapterDesc.Revision, adapterDesc.AdapterLuid.HighPart, adapterDesc.AdapterLuid.LowPart);
UINT adapterIndex = 0; // print adapter output info
IDXGIAdapter* adapter = nullptr; IDXGIOutput* pOutput;
int outputCount = 0;
for (UINT j = 0; pAdapter->EnumOutputs(j, &pOutput) != DXGI_ERROR_NOT_FOUND; ++j) {
DXGI_OUTPUT_DESC outputDesc;
pOutput->GetDesc(&outputDesc);
while (dxgiFactory->EnumAdapters(adapterIndex, &adapter) != DXGI_ERROR_NOT_FOUND) { MONITORINFOEX monitorInfo;
_displayAdapterList.push_back(adapter); // 将 IDXGIAdapter 指针存储在容器中 monitorInfo.cbSize = sizeof(MONITORINFOEX);
adapterIndex++; if (GetMonitorInfo(outputDesc.Monitor, &monitorInfo))
{
// 输出友好名称
spdlog::info("Adapter Output Index:{0}, DeviceName:{1}, szDevice:{2}, right:{3}, bottom:{4}",
j, Wchar2String(outputDesc.DeviceName), monitorInfo.szDevice, monitorInfo.rcMonitor.right, monitorInfo.rcMonitor.bottom);
} }
dxgiFactory->Release(); }
CoUninitialize(); }
pFactory->Release();
LOG_FUNC_END();
} }
BOOL ScreenManager::_EnumMonitorProc(HMONITOR hMonitor) BOOL ScreenManager::_EnumMonitorProc(HMONITOR hMonitor)

View File

@ -8,9 +8,15 @@ using namespace std;
int main() int main()
{ {
HRESULT hr = S_OK;
hr = CoInitialize(nullptr);
if (FAILED(hr)) {
return 0;
}
cout << "Hello CMake." << endl; cout << "Hello CMake." << endl;
ScreenManager screenManager; ScreenManager screenManager;
screenManager.UpdateDisplayInfo(); screenManager.UpdateDisplayInfo();
CoUninitialize();
return 0; return 0;
} }