Enable log information

This commit is contained in:
2026-04-21 12:09:24 +10:00
parent 7e772f76bc
commit 00f6e2f852
5 changed files with 247 additions and 40 deletions

View File

@@ -14,6 +14,11 @@
#include "base64.h"
#include <spdlog/sinks/base_sink.h>
#include <winnt.h>
#include <atomic>
#include <chrono>
#include <filesystem>
#include <system_error>
#include <cstdlib>
#define MAX_CUSTOMER_NAME_LEN 100
#define FEATURE_1 0x1
@@ -2963,4 +2968,45 @@ namespace ANSCENTER
return logger.Release();
}
}
// ============================================================================
// Runtime DebugView toggle — gates ANS_DBG without requiring a rebuild.
//
// Precedence per poll: env var ANSCENTER_DBGVIEW wins over the sentinel file.
// Poll interval is 2 s, so toggling takes effect within that window. Hot-path
// cost on a cache hit is a single relaxed atomic load + branch.
// ============================================================================
extern "C" ANSLICENSE_API int ANSCENTER_IsDebugViewEnabled(void) {
using clock = std::chrono::steady_clock;
static std::atomic<int> s_enabled{0};
static std::atomic<long long> s_nextCheckTick{0};
const long long now = clock::now().time_since_epoch().count();
const long long next = s_nextCheckTick.load(std::memory_order_relaxed);
if (now < next) {
return s_enabled.load(std::memory_order_relaxed);
}
int enabled = 0;
const char* env = std::getenv("ANSCENTER_DBGVIEW");
if (env && env[0] == '1' && env[1] == '\0') {
enabled = 1;
} else if (env && env[0] == '0' && env[1] == '\0') {
enabled = 0;
} else {
#ifdef _WIN32
const wchar_t* kSentinel = L"C:\\ProgramData\\ANSCENTER\\ansvisdebugview.txt";
#else
const char* kSentinel = "/tmp/ansvisdebugview.txt";
#endif
std::error_code ec;
enabled = std::filesystem::exists(kSentinel, ec) ? 1 : 0;
}
s_enabled.store(enabled, std::memory_order_relaxed);
const long long deadline = now +
std::chrono::duration_cast<clock::duration>(std::chrono::seconds(2)).count();
s_nextCheckTick.store(deadline, std::memory_order_relaxed);
return enabled;
}

View File

@@ -1,20 +1,40 @@
#ifndef ANSLICENSE_H
#define ANSLICENSE_H
#ifdef ANSLICENSE_EXPORTS
#define ANSLICENSE_API __declspec(dllexport)
#else
#define ANSLICENSE_API __declspec(dllimport)
#endif
// ============================================================================
// Global debug toggle for DebugView (DbgView) logging.
// Define ANSCORE_DEBUGVIEW=1 to enable verbose OutputDebugStringA logging
// across all ANSCORE modules (ANSCV, ANSODEngine, TensorRT engine, etc.).
// Set to 0 for production builds to eliminate all debug output overhead.
// DebugView (DbgView) logging toggle.
//
// ANS_DBG is compiled into every build; visibility is chosen at runtime by
// ANSCENTER_IsDebugViewEnabled() — a cached check (~2 s) that reads:
// 1. env var ANSCENTER_DBGVIEW (="1" forces on, ="0" forces off), else
// 2. existence of C:\ProgramData\ANSCENTER\ansvisdebugview.txt (Windows) or
// /tmp/ansvisdebugview.txt (POSIX).
//
// Drop the sentinel file to turn logging on; delete it to turn it off. No
// rebuild, no restart — takes effect within ~2 s. ACLs on ProgramData are
// world-writable by default so any user can toggle.
//
// Hard kill-switch: compile with -DANSCORE_DEBUGVIEW=0 to strip ANS_DBG to
// ((void)0) — use only when even the atomic-load cost is unacceptable.
// ============================================================================
#ifndef ANSCORE_DEBUGVIEW
#define ANSCORE_DEBUGVIEW 1 // 1 = enabled (debug), 0 = disabled (production)
#define ANSCORE_DEBUGVIEW 1 // 1 = runtime-gated (default), 0 = hard-off (stripped)
#endif
// Runtime gate. Implemented in ANSLicense.cpp. Returns non-zero when logging
// should be emitted. Cheap on the hot path: one atomic load + branch; the
// filesystem check runs at most once per ~2 s.
extern "C" ANSLICENSE_API int ANSCENTER_IsDebugViewEnabled(void);
// ANS_DBG: Debug logging macro for DebugView (OutputDebugStringA on Windows).
// Usage: ANS_DBG("MyModule", "value=%d ptr=%p", val, ptr);
// Output: [MyModule] value=42 ptr=0x1234
// When ANSCORE_DEBUGVIEW=0, compiles to nothing (zero overhead).
// NOTE: We avoid #include <windows.h> here to prevent winsock.h/winsock2.h
// conflicts. Instead, forward-declare OutputDebugStringA directly.
#if ANSCORE_DEBUGVIEW && defined(_WIN32)
@@ -24,30 +44,28 @@ extern "C" __declspec(dllimport) void __stdcall OutputDebugStringA(const char* l
// without needing to attach DebugView. fputs + fflush keeps lines atomic even
// when multiple threads log concurrently.
#define ANS_DBG(tag, fmt, ...) do { \
char _ans_dbg_buf[1024]; \
snprintf(_ans_dbg_buf, sizeof(_ans_dbg_buf), "[" tag "] " fmt "\n", ##__VA_ARGS__); \
OutputDebugStringA(_ans_dbg_buf); \
fputs(_ans_dbg_buf, stderr); \
fflush(stderr); \
if (ANSCENTER_IsDebugViewEnabled()) { \
char _ans_dbg_buf[1024]; \
snprintf(_ans_dbg_buf, sizeof(_ans_dbg_buf), "[" tag "] " fmt "\n", ##__VA_ARGS__); \
OutputDebugStringA(_ans_dbg_buf); \
fputs(_ans_dbg_buf, stderr); \
fflush(stderr); \
} \
} while(0)
#elif ANSCORE_DEBUGVIEW
// Non-Windows: stderr only.
#define ANS_DBG(tag, fmt, ...) do { \
char _ans_dbg_buf[1024]; \
snprintf(_ans_dbg_buf, sizeof(_ans_dbg_buf), "[" tag "] " fmt "\n", ##__VA_ARGS__); \
fputs(_ans_dbg_buf, stderr); \
fflush(stderr); \
if (ANSCENTER_IsDebugViewEnabled()) { \
char _ans_dbg_buf[1024]; \
snprintf(_ans_dbg_buf, sizeof(_ans_dbg_buf), "[" tag "] " fmt "\n", ##__VA_ARGS__); \
fputs(_ans_dbg_buf, stderr); \
fflush(stderr); \
} \
} while(0)
#else
#define ANS_DBG(tag, fmt, ...) ((void)0)
#endif
#ifdef ANSLICENSE_EXPORTS
#define ANSLICENSE_API __declspec(dllexport)
#else
#define ANSLICENSE_API __declspec(dllimport)
#endif
#pragma once
#include <stdlib.h>