Add debug and log functions to ANSLIB

This commit is contained in:
2026-04-26 06:59:42 +10:00
parent 947398483a
commit f81737ffc6
9 changed files with 585 additions and 23 deletions

View File

@@ -1,31 +1,61 @@
#include "ANSLIB.h"
#include "ANSLibsLoader.h"
#include <memory>
#include <mutex>
#define LOAD_FUNC(name) \
name##Func = (name##FuncT)GetProcAddress(dllHandle, #name); \
if (!name##Func) { success = false; }
namespace ANSCENTER
{
ANSLIB::ANSLIB() {
namespace {
// Function-pointer typedefs for the ANSLicensingSystem C-facade exports.
// Resolved exactly once during PreloadSharedDllsOnce(); after that the
// pointers are write-once globals — concurrent reads are race-free.
using ANSLogger_LogFnT = void(*)(const char*, const char*, const char*, int);
using ANSCENTER_IsDbgEnFnT = int (*)(void);
ANSLogger_LogFnT g_ANSLogger_LogInfo = nullptr;
ANSLogger_LogFnT g_ANSLogger_LogError = nullptr;
ANSLogger_LogFnT g_ANSLogger_LogFatal = nullptr;
ANSCENTER_IsDbgEnFnT g_ANSCENTER_IsDebugViewEnabled = nullptr;
// Pre-load all ANSCORE DLLs from the Shared folder using full paths.
// This ensures the correct versions are loaded regardless of PATH order
// (e.g., DLHUB_Runtime_Engine may contain older copies on some machines).
// Once loaded, Windows reuses them for all subsequent implicit dependencies.
const char* sharedDir = "C:\\ProgramData\\ANSCENTER\\Shared\\";
const char* preloadDlls[] = {
"ANSLicensingSystem.dll",
"anslicensing.dll",
"ANSMOT.dll",
"ANSODEngine.dll",
nullptr
};
for (int i = 0; preloadDlls[i] != nullptr; i++) {
std::string fullPath = std::string(sharedDir) + preloadDlls[i];
LoadLibraryA(fullPath.c_str());
}
// Run exactly once per process so LoadLibrary refcounts don't grow per
// ANSLIB instance. The DLLs are intentionally pinned for process lifetime.
void PreloadSharedDllsOnce() {
static std::once_flag s_preloadFlag;
std::call_once(s_preloadFlag, []() {
const char* sharedDir = "C:\\ProgramData\\ANSCENTER\\Shared\\";
const char* preloadDlls[] = {
"ANSLicensingSystem.dll",
"anslicensing.dll",
"ANSMOT.dll",
"ANSODEngine.dll",
nullptr
};
for (int i = 0; preloadDlls[i] != nullptr; i++) {
std::string fullPath = std::string(sharedDir) + preloadDlls[i];
LoadLibraryA(fullPath.c_str());
}
// Ensure all shared DLLs (OpenCV, OpenVINO, TRT, ORT) are pre-loaded
ANSCENTER::ANSLibsLoader::Initialize();
// Ensure all shared DLLs (OpenCV, OpenVINO, TRT, ORT) are pre-loaded
ANSCENTER::ANSLibsLoader::Initialize();
// Resolve logging C-facade symbols from the (now-pinned) licensing DLL.
// Use GetModuleHandleA — the DLL is already in this process's module
// list courtesy of the LoadLibraryA above, so refcount is unchanged.
if (HMODULE lic = GetModuleHandleA("ANSLicensingSystem.dll")) {
g_ANSLogger_LogInfo = (ANSLogger_LogFnT) GetProcAddress(lic, "ANSLogger_LogInfo");
g_ANSLogger_LogError = (ANSLogger_LogFnT) GetProcAddress(lic, "ANSLogger_LogError");
g_ANSLogger_LogFatal = (ANSLogger_LogFnT) GetProcAddress(lic, "ANSLogger_LogFatal");
g_ANSCENTER_IsDebugViewEnabled = (ANSCENTER_IsDbgEnFnT)GetProcAddress(lic, "ANSCENTER_IsDebugViewEnabled");
}
});
}
}
ANSLIB::ANSLIB() {
PreloadSharedDllsOnce();
const char* dllPath = "C:\\ProgramData\\ANSCENTER\\Shared\\ANSODEngine.dll";
dllHandle = LoadLibraryA(dllPath);
@@ -99,6 +129,10 @@ namespace ANSCENTER
return -1;
}
if (CreateANSODHandle_CSFunc) {
if (ANSHandle && ReleaseANSODHandleFunc) {
ReleaseANSODHandleFunc(&ANSHandle);
ANSHandle = nullptr;
}
const char* result = CreateANSODHandle_CSFunc(&ANSHandle, licenseKey, modelFilePath, modelFileZipPassword,
modelThreshold, modelConfThreshold, modelNMSThreshold,
1, modelType, detectionType, loadEngineOnCreation);
@@ -118,9 +152,8 @@ namespace ANSCENTER
return -1;
}
if (RunInference_CPPFunc) {
auto imagePtr = std::make_unique<cv::Mat>(cvImage);
try {
cv::Mat* rawPtr = imagePtr.get();
cv::Mat* rawPtr = &cvImage;
int result = RunInference_CPPFunc(&ANSHandle, &rawPtr, cameraId, detectionResult);
return result;
}
@@ -135,9 +168,8 @@ namespace ANSCENTER
return -1;
}
if (RunInferenceComplete_CPPFunc) {
auto imagePtr = std::make_unique<cv::Mat>(cvImage);
try {
cv::Mat* rawPtr = imagePtr.get();
cv::Mat* rawPtr = &cvImage;
int result = RunInferenceComplete_CPPFunc(&ANSHandle, &rawPtr, cameraId, activeROIMode,detectionResult);
return result;
}
@@ -193,6 +225,10 @@ namespace ANSCENTER
int autoDetectEngine, int modelType, int detectionType, int loadEngineOnCreation, const char* modelFolder,std::string& labelMap)
{
if (!loaded || !LoadModelFromFolderFunc) return -1;
if (ANSHandle && ReleaseANSODHandleFunc) {
ReleaseANSODHandleFunc(&ANSHandle);
ANSHandle = nullptr;
}
return LoadModelFromFolderFunc(&ANSHandle, licenseKey, modelName, className,
detectionScoreThreshold, modelConfThreshold, modelMNSThreshold,
autoDetectEngine, modelType, detectionType, loadEngineOnCreation,modelFolder, labelMap);
@@ -241,4 +277,27 @@ namespace ANSCENTER
if (!loaded || !SetTrackerParametersFunc) return -1;
return SetTrackerParametersFunc(&ANSHandle, trackerParams);
}
// ------------------------------------------------------------------------
// Static logging API. Each call ensures the DLL/symbols are resolved (cheap
// after the first call — std::call_once degenerates to an atomic load) then
// forwards to the C facade. Null-pointer guard makes missing symbols a
// silent no-op rather than a crash.
// ------------------------------------------------------------------------
int ANSLIB::IsDebugViewEnabled() {
PreloadSharedDllsOnce();
return g_ANSCENTER_IsDebugViewEnabled ? g_ANSCENTER_IsDebugViewEnabled() : 0;
}
void ANSLIB::LogInfo(const char* src, const char* msg, const char* file, int line) {
PreloadSharedDllsOnce();
if (g_ANSLogger_LogInfo) g_ANSLogger_LogInfo(src, msg, file, line);
}
void ANSLIB::LogError(const char* src, const char* msg, const char* file, int line) {
PreloadSharedDllsOnce();
if (g_ANSLogger_LogError) g_ANSLogger_LogError(src, msg, file, line);
}
void ANSLIB::LogFatal(const char* src, const char* msg, const char* file, int line) {
PreloadSharedDllsOnce();
if (g_ANSLogger_LogFatal) g_ANSLogger_LogFatal(src, msg, file, line);
}
}