libobs-winrt: Improve error logging code

Use code() instead of to_abi() on hresult_error structs as the latter
has additional side effects. Cast all values to int32_t to ensure the
hresult int32_t() operator is called so we pass the actual value and not
the full struct (detected by PVS Studio).
master
Richard Stanway 2021-08-19 02:07:00 +02:00
parent e8d4de6bec
commit 30257c9fe3
2 changed files with 34 additions and 31 deletions

View File

@ -19,12 +19,12 @@ try {
IsApiContractPresent(L"Windows.Foundation.UniversalApiContract", IsApiContractPresent(L"Windows.Foundation.UniversalApiContract",
8); 8);
} catch (const winrt::hresult_error &err) { } catch (const winrt::hresult_error &err) {
blog(LOG_ERROR, "winrt_capture_supported (0x%08X): %ls", err.to_abi(), blog(LOG_ERROR, "winrt_capture_supported (0x%08X): %ls",
err.message().c_str()); (int32_t)err.code(), err.message().c_str());
return false; return false;
} catch (...) { } catch (...) {
blog(LOG_ERROR, "winrt_capture_supported (0x%08X)", blog(LOG_ERROR, "winrt_capture_supported (0x%08X)",
winrt::to_hresult()); (int32_t)winrt::to_hresult());
return false; return false;
} }
@ -36,11 +36,11 @@ try {
L"IsCursorCaptureEnabled"); L"IsCursorCaptureEnabled");
} catch (const winrt::hresult_error &err) { } catch (const winrt::hresult_error &err) {
blog(LOG_ERROR, "winrt_capture_cursor_toggle_supported (0x%08X): %ls", blog(LOG_ERROR, "winrt_capture_cursor_toggle_supported (0x%08X): %ls",
err.to_abi(), err.message().c_str()); (int32_t)err.code(), err.message().c_str());
return false; return false;
} catch (...) { } catch (...) {
blog(LOG_ERROR, "winrt_capture_cursor_toggle_supported (0x%08X)", blog(LOG_ERROR, "winrt_capture_cursor_toggle_supported (0x%08X)",
winrt::to_hresult()); (int32_t)winrt::to_hresult());
return false; return false;
} }
@ -239,20 +239,20 @@ static void winrt_capture_device_loss_release(void *data)
} catch (winrt::hresult_error &err) { } catch (winrt::hresult_error &err) {
blog(LOG_ERROR, blog(LOG_ERROR,
"Direct3D11CaptureFramePool::Close (0x%08X): %ls", "Direct3D11CaptureFramePool::Close (0x%08X): %ls",
err.to_abi(), err.message().c_str()); (int32_t)err.code(), err.message().c_str());
} catch (...) { } catch (...) {
blog(LOG_ERROR, "Direct3D11CaptureFramePool::Close (0x%08X)", blog(LOG_ERROR, "Direct3D11CaptureFramePool::Close (0x%08X)",
winrt::to_hresult()); (int32_t)winrt::to_hresult());
} }
try { try {
capture->session.Close(); capture->session.Close();
} catch (winrt::hresult_error &err) { } catch (winrt::hresult_error &err) {
blog(LOG_ERROR, "GraphicsCaptureSession::Close (0x%08X): %ls", blog(LOG_ERROR, "GraphicsCaptureSession::Close (0x%08X): %ls",
err.to_abi(), err.message().c_str()); (int32_t)err.code(), err.message().c_str());
} catch (...) { } catch (...) {
blog(LOG_ERROR, "GraphicsCaptureSession::Close (0x%08X)", blog(LOG_ERROR, "GraphicsCaptureSession::Close (0x%08X)",
winrt::to_hresult()); (int32_t)winrt::to_hresult());
} }
capture->session = nullptr; capture->session = nullptr;
@ -271,11 +271,11 @@ try {
L"IsBorderRequired"); L"IsBorderRequired");
} catch (const winrt::hresult_error &err) { } catch (const winrt::hresult_error &err) {
blog(LOG_ERROR, "winrt_capture_border_toggle_supported (0x%08X): %ls", blog(LOG_ERROR, "winrt_capture_border_toggle_supported (0x%08X): %ls",
err.to_abi(), err.message().c_str()); (int32_t)err.code(), err.message().c_str());
return false; return false;
} catch (...) { } catch (...) {
blog(LOG_ERROR, "winrt_capture_border_toggle_supported (0x%08X)", blog(LOG_ERROR, "winrt_capture_border_toggle_supported (0x%08X)",
winrt::to_hresult()); (int32_t)winrt::to_hresult());
return false; return false;
} }
#endif #endif
@ -297,10 +297,10 @@ winrt_capture_create_item(IGraphicsCaptureItemInterop *const interop_factory,
blog(LOG_ERROR, "CreateForWindow (0x%08X)", hr); blog(LOG_ERROR, "CreateForWindow (0x%08X)", hr);
} catch (winrt::hresult_error &err) { } catch (winrt::hresult_error &err) {
blog(LOG_ERROR, "CreateForWindow (0x%08X): %ls", blog(LOG_ERROR, "CreateForWindow (0x%08X): %ls",
err.to_abi(), err.message().c_str()); (int32_t)err.code(), err.message().c_str());
} catch (...) { } catch (...) {
blog(LOG_ERROR, "CreateForWindow (0x%08X)", blog(LOG_ERROR, "CreateForWindow (0x%08X)",
winrt::to_hresult()); (int32_t)winrt::to_hresult());
} }
} else { } else {
assert(monitor); assert(monitor);
@ -317,10 +317,10 @@ winrt_capture_create_item(IGraphicsCaptureItemInterop *const interop_factory,
hr); hr);
} catch (winrt::hresult_error &err) { } catch (winrt::hresult_error &err) {
blog(LOG_ERROR, "CreateForMonitor (0x%08X): %ls", blog(LOG_ERROR, "CreateForMonitor (0x%08X): %ls",
err.to_abi(), err.message().c_str()); (int32_t)err.code(), err.message().c_str());
} catch (...) { } catch (...) {
blog(LOG_ERROR, "CreateForMonitor (0x%08X)", blog(LOG_ERROR, "CreateForMonitor (0x%08X)",
winrt::to_hresult()); (int32_t)winrt::to_hresult());
} }
} }
@ -390,10 +390,11 @@ static void winrt_capture_device_loss_rebuild(void *device_void, void *data)
session.StartCapture(); session.StartCapture();
capture->active = TRUE; capture->active = TRUE;
} catch (winrt::hresult_error &err) { } catch (winrt::hresult_error &err) {
blog(LOG_ERROR, "StartCapture (0x%08X): %ls", err.to_abi(), blog(LOG_ERROR, "StartCapture (0x%08X): %ls",
err.message().c_str()); (int32_t)err.code(), err.message().c_str());
} catch (...) { } catch (...) {
blog(LOG_ERROR, "StartCapture (0x%08X)", winrt::to_hresult()); blog(LOG_ERROR, "StartCapture (0x%08X)",
(int32_t)winrt::to_hresult());
} }
} }
@ -492,11 +493,12 @@ try {
return capture; return capture;
} catch (const winrt::hresult_error &err) { } catch (const winrt::hresult_error &err) {
blog(LOG_ERROR, "winrt_capture_init (0x%08X): %ls", err.to_abi(), blog(LOG_ERROR, "winrt_capture_init (0x%08X): %ls", (int32_t)err.code(),
err.message().c_str()); err.message().c_str());
return nullptr; return nullptr;
} catch (...) { } catch (...) {
blog(LOG_ERROR, "winrt_capture_init (0x%08X)", winrt::to_hresult()); blog(LOG_ERROR, "winrt_capture_init (0x%08X)",
(int32_t)winrt::to_hresult());
return nullptr; return nullptr;
} }
@ -541,11 +543,11 @@ extern "C" EXPORT void winrt_capture_free(struct winrt_capture *capture)
} catch (winrt::hresult_error &err) { } catch (winrt::hresult_error &err) {
blog(LOG_ERROR, blog(LOG_ERROR,
"Direct3D11CaptureFramePool::Close (0x%08X): %ls", "Direct3D11CaptureFramePool::Close (0x%08X): %ls",
err.to_abi(), err.message().c_str()); (int32_t)err.code(), err.message().c_str());
} catch (...) { } catch (...) {
blog(LOG_ERROR, blog(LOG_ERROR,
"Direct3D11CaptureFramePool::Close (0x%08X)", "Direct3D11CaptureFramePool::Close (0x%08X)",
winrt::to_hresult()); (int32_t)winrt::to_hresult());
} }
try { try {
@ -553,11 +555,11 @@ extern "C" EXPORT void winrt_capture_free(struct winrt_capture *capture)
} catch (winrt::hresult_error &err) { } catch (winrt::hresult_error &err) {
blog(LOG_ERROR, blog(LOG_ERROR,
"GraphicsCaptureSession::Close (0x%08X): %ls", "GraphicsCaptureSession::Close (0x%08X): %ls",
err.to_abi(), err.message().c_str()); (int32_t)err.code(), err.message().c_str());
} catch (...) { } catch (...) {
blog(LOG_ERROR, blog(LOG_ERROR,
"GraphicsCaptureSession::Close (0x%08X)", "GraphicsCaptureSession::Close (0x%08X)",
winrt::to_hresult()); (int32_t)winrt::to_hresult());
} }
delete capture; delete capture;
@ -614,11 +616,11 @@ extern "C" EXPORT BOOL winrt_capture_show_cursor(struct winrt_capture *capture,
} catch (winrt::hresult_error &err) { } catch (winrt::hresult_error &err) {
blog(LOG_ERROR, blog(LOG_ERROR,
"GraphicsCaptureSession::IsCursorCaptureEnabled (0x%08X): %ls", "GraphicsCaptureSession::IsCursorCaptureEnabled (0x%08X): %ls",
err.to_abi(), err.message().c_str()); (int32_t)err.code(), err.message().c_str());
} catch (...) { } catch (...) {
blog(LOG_ERROR, blog(LOG_ERROR,
"GraphicsCaptureSession::IsCursorCaptureEnabled (0x%08X)", "GraphicsCaptureSession::IsCursorCaptureEnabled (0x%08X)",
winrt::to_hresult()); (int32_t)winrt::to_hresult());
} }
return success; return success;

View File

@ -43,10 +43,10 @@ extern "C" EXPORT struct winrt_disaptcher *winrt_dispatcher_init()
} }
} catch (const winrt::hresult_error &err) { } catch (const winrt::hresult_error &err) {
blog(LOG_ERROR, "winrt_dispatcher_init (0x%08X): %ls", blog(LOG_ERROR, "winrt_dispatcher_init (0x%08X): %ls",
err.to_abi(), err.message().c_str()); (int32_t)err.code(), err.message().c_str());
} catch (...) { } catch (...) {
blog(LOG_ERROR, "winrt_dispatcher_init (0x%08X)", blog(LOG_ERROR, "winrt_dispatcher_init (0x%08X)",
winrt::to_hresult()); (int32_t)winrt::to_hresult());
} }
return dispatcher; return dispatcher;
@ -57,8 +57,9 @@ winrt_dispatcher_free(struct winrt_disaptcher *dispatcher)
try { try {
delete dispatcher; delete dispatcher;
} catch (const winrt::hresult_error &err) { } catch (const winrt::hresult_error &err) {
blog(LOG_ERROR, "winrt_dispatcher_free (0x%08X): %ls", err.to_abi(), blog(LOG_ERROR, "winrt_dispatcher_free (0x%08X): %ls",
err.message().c_str()); (int32_t)err.code(), err.message().c_str());
} catch (...) { } catch (...) {
blog(LOG_ERROR, "winrt_dispatcher_free (0x%08X)", winrt::to_hresult()); blog(LOG_ERROR, "winrt_dispatcher_free (0x%08X)",
(int32_t)winrt::to_hresult());
} }