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

@@ -6,6 +6,7 @@
#include <windows.h>
#include <opencv2/opencv.hpp>
#include <opencv2/core/cuda.hpp>
#include <cstdio>
enum DetectionType {
CLASSIFICATION = 0,
DETECTION = 1,
@@ -263,6 +264,10 @@ namespace ANSCENTER {
public:
ANSLIB();
~ANSLIB() noexcept;
ANSLIB(const ANSLIB&) = delete;
ANSLIB& operator=(const ANSLIB&) = delete;
ANSLIB(ANSLIB&&) = delete;
ANSLIB& operator=(ANSLIB&&) = delete;
[[nodiscard]] static ANSLIB* Create();
static void Destroy(ANSLIB* instance);
[[nodiscard]] int Initialize(const char* licenseKey,
@@ -286,6 +291,20 @@ namespace ANSCENTER {
[[nodiscard]] int SetPrompt(const char* text);
[[nodiscard]] int SetTracker(int trackerType, int enableTracker);
[[nodiscard]] int SetTrackerParameters(const char* trackerParams);
// Logging — process-wide, thread-safe, no instance required.
// Routes to SPDLogger inside ANSLicensingSystem.dll via dynamically-resolved
// C exports. Silently no-ops if the DLL or symbols are unavailable.
static void LogInfo (const char* src, const char* msg, const char* file = "", int line = 0);
static void LogError(const char* src, const char* msg, const char* file = "", int line = 0);
static void LogFatal(const char* src, const char* msg, const char* file = "", int line = 0);
// DebugView gate. Wraps ANSCENTER_IsDebugViewEnabled() — non-zero when
// logging should be emitted (env var ANSCENTER_DBGVIEW or sentinel file
// C:\ProgramData\ANSCENTER\ansvisdebugview.txt). Cheap on the hot path.
// Used by the ANSLIB_DBG macro below; can also be called directly to
// gate expensive log-prep work.
static int IsDebugViewEnabled();
private:
HMODULE dllHandle = nullptr;
bool loaded = false;
@@ -436,4 +455,31 @@ public:
}
};
#endif
// ANSLIB_DBG: DebugView logging macro mirroring ANS_DBG, but the gate function
// is dynamic-loaded inside ANSLIB.dll — consumers don't need to link or even
// know about ANSLicensingSystem.dll. Output goes to BOTH OutputDebugStringA
// (DebugView) and stderr, identical format to ANS_DBG: "[tag] formatted\n".
//
// Usage: ANSLIB_DBG("MyModule", "value=%d ptr=%p", val, ptr);
//
// Hot-path cost when gate is OFF: one indirect call (atomic load + branch
// inside ANSLicensingSystem). When gate is ON: snprintf to a 1024-byte stack
// buffer + OutputDebugStringA + fputs. No heap, no per-call allocation.
#ifndef ANSCORE_DEBUGVIEW
#define ANSCORE_DEBUGVIEW 1 // 1 = runtime-gated (default), 0 = hard-off (stripped)
#endif
#if ANSCORE_DEBUGVIEW
#define ANSLIB_DBG(tag, fmt, ...) do { \
if (::ANSCENTER::ANSLIB::IsDebugViewEnabled()) { \
char _ansl_dbg_buf[1024]; \
snprintf(_ansl_dbg_buf, sizeof(_ansl_dbg_buf), "[" tag "] " fmt "\n", ##__VA_ARGS__); \
OutputDebugStringA(_ansl_dbg_buf); \
fputs(_ansl_dbg_buf, stderr); \
fflush(stderr); \
} \
} while(0)
#else
#define ANSLIB_DBG(tag, fmt, ...) ((void)0)
#endif
#endif