From e88e0e5c582947a3179225ed55cdca6e70db7930 Mon Sep 17 00:00:00 2001 From: kinect-bot Date: Wed, 25 Nov 2026 15:30:25 -0500 Subject: [PATCH] feat(windows-nui): file logging + Global namespace shm fallback for cross-session diagnosis --- fallback/windows-nui/src/exports.cpp | 3 ++ fallback/windows-nui/src/sensor.cpp | 39 ++++++++++++++++++++- fallback/windows-nui/src/shim_log.h | 52 ++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 fallback/windows-nui/src/shim_log.h diff --git a/fallback/windows-nui/src/exports.cpp b/fallback/windows-nui/src/exports.cpp index f5e0b32..b7d4cc5 100644 --- a/fallback/windows-nui/src/exports.cpp +++ b/fallback/windows-nui/src/exports.cpp @@ -5,6 +5,7 @@ #include "nui_interfaces.h" #include "sensor.h" #include "sensor_com.h" +#include "shim_log.h" using namespace nui_shim; @@ -20,6 +21,7 @@ HRESULT WINAPI NuiGetSensorCount(int* pCount) { return E_POINTER; } *pCount = 1; + ShimLog("NuiGetSensorCount -> 1"); return S_OK; } @@ -31,6 +33,7 @@ HRESULT WINAPI NuiCreateSensorByIndex(int index, INuiSensor** ppNuiSensor) { return E_NUI_DEVICE_NOT_CONNECTED; } *ppNuiSensor = new NuiSensorCom(EnsureGlobalSensor()); + ShimLog("NuiCreateSensorByIndex(%d) -> sensor", index); return S_OK; } diff --git a/fallback/windows-nui/src/sensor.cpp b/fallback/windows-nui/src/sensor.cpp index 5014d0b..25153a7 100644 --- a/fallback/windows-nui/src/sensor.cpp +++ b/fallback/windows-nui/src/sensor.cpp @@ -1,5 +1,7 @@ #include "sensor.h" +#include "shim_log.h" + #include #include #include @@ -113,7 +115,19 @@ HRESULT VirtualKinectSensor::Initialize(DWORD dwFlags) { return E_NUI_ALREADY_INITIALIZED; } init_flags_ = dwFlags; - source_.open(MappingName().c_str()); + std::wstring name = MappingName(); + bool opened = source_.open(name.c_str()); + DWORD err = opened ? 0u : GetLastError(); + if (!opened) { + std::wstring global = L"Global\\" + name; + opened = source_.open(global.c_str()); + ShimLog("NuiInitialize flags=0x%08X shm='%ls' local-open=0 (err=%lu) global-open=%d (err=%lu)", + dwFlags, name.c_str(), err, opened ? 1 : 0, + opened ? 0u : GetLastError()); + } else { + ShimLog("NuiInitialize flags=0x%08X shm='%ls' local-open=1", dwFlags, + name.c_str()); + } stop_event_ = CreateEventW(nullptr, TRUE, FALSE, nullptr); skeleton_ready_event_ = CreateEventW(nullptr, TRUE, FALSE, nullptr); @@ -180,6 +194,8 @@ HRESULT VirtualKinectSensor::SkeletonTrackingEnable(HANDLE hNextFrameEvent, skeleton_flags_ = dwFlags; skeleton_app_event_ = hNextFrameEvent; LeaveCriticalSection(&lock_); + ShimLog("SkeletonTrackingEnable flags=0x%08X event=%p", dwFlags, + hNextFrameEvent); return S_OK; } @@ -302,6 +318,9 @@ HRESULT VirtualKinectSensor::ImageStreamOpen(NUI_IMAGE_TYPE eImageType, *phStreamHandle = reinterpret_cast(slot); LeaveCriticalSection(&lock_); + ShimLog("ImageStreamOpen type=%d res=%d -> %ux%u bpp=%u ring=%zu", + static_cast(eImageType), static_cast(eResolution), width, + height, bpp, slot->ring_size); return S_OK; } @@ -492,6 +511,24 @@ void VirtualKinectSensor::ProducerLoop() { SourceFrame src; const bool got = source_.is_open() && source_.read_latest(src); + if ((frame_number % 60u) == 0u) { + std::size_t depth_nonzero = 0; + if (got) { + for (uint16_t p : src.depth_pixels) { + if (p != 0) { + ++depth_nonzero; + } + } + } + ShimLog("producer tick=%u src_open=%d got_frame=%d has_player=%d depth_nonzero=%zu streams_open=%d skel_enabled=%d", + frame_number, source_.is_open() ? 1 : 0, got ? 1 : 0, + got && src.has_player ? 1 : 0, depth_nonzero, + std::any_of(streams_.begin(), streams_.end(), + [](const ImageStream& s) { return s.open; }) + ? 1 + : 0, + skeleton_enabled_ ? 1 : 0); + } if (!got) { src = SourceFrame{}; src.has_player = false; diff --git a/fallback/windows-nui/src/shim_log.h b/fallback/windows-nui/src/shim_log.h new file mode 100644 index 0000000..b3e92c0 --- /dev/null +++ b/fallback/windows-nui/src/shim_log.h @@ -0,0 +1,52 @@ +#ifndef FALLBACK_WINDOWS_NUI_SHIM_LOG_H +#define FALLBACK_WINDOWS_NUI_SHIM_LOG_H + +#include + +#include +#include + +namespace nui_shim { + +inline CRITICAL_SECTION& ShimLogLock() { + static CRITICAL_SECTION cs; + static BOOL once = [] { + InitializeCriticalSection(&cs); + return TRUE; + }(); + (void)once; + return cs; +} + +inline void ShimLog(const char* fmt, ...) { + wchar_t path[MAX_PATH]; + DWORD n = GetEnvironmentVariableW(L"KINECT_SHIM_LOG", path, MAX_PATH); + if (n == 0 || n >= MAX_PATH) { + wchar_t tmp[MAX_PATH]; + DWORD t = GetTempPathW(MAX_PATH, tmp); + if (t == 0 || t >= MAX_PATH) { + return; + } + _snwprintf(path, MAX_PATH, L"%skinect_shim.log", tmp); + } + + EnterCriticalSection(&ShimLogLock()); + FILE* f = _wfopen(path, L"a"); + if (f != nullptr) { + SYSTEMTIME st; + GetLocalTime(&st); + fprintf(f, "[%02d:%02d:%02d.%03d] ", st.wHour, st.wMinute, st.wSecond, + st.wMilliseconds); + va_list args; + va_start(args, fmt); + vfprintf(f, fmt, args); + va_end(args); + fprintf(f, "\n"); + fclose(f); + } + LeaveCriticalSection(&ShimLogLock()); +} + +} + +#endif