strip sensitive information from logs

This commit is contained in:
2026-04-26 07:35:30 +10:00
parent f81737ffc6
commit da94e7f6da
6 changed files with 225 additions and 51 deletions

View File

@@ -13,10 +13,12 @@ namespace ANSCENTER
// 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;
using ANSCENTER_DebugLogFnT = void(*)(const char*);
ANSLogger_LogFnT g_ANSLogger_LogInfo = nullptr;
ANSLogger_LogFnT g_ANSLogger_LogError = nullptr;
ANSLogger_LogFnT g_ANSLogger_LogFatal = nullptr;
ANSCENTER_IsDbgEnFnT g_ANSCENTER_IsDebugViewEnabled = nullptr;
ANSCENTER_DebugLogFnT g_ANSCENTER_DebugLog = nullptr;
// Pre-load all ANSCORE DLLs from the Shared folder using full paths.
// This ensures the correct versions are loaded regardless of PATH order
@@ -45,10 +47,11 @@ namespace ANSCENTER
// 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");
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");
g_ANSCENTER_DebugLog = (ANSCENTER_DebugLogFnT)GetProcAddress(lic, "ANSCENTER_DebugLog");
}
});
}
@@ -288,6 +291,20 @@ namespace ANSCENTER
PreloadSharedDllsOnce();
return g_ANSCENTER_IsDebugViewEnabled ? g_ANSCENTER_IsDebugViewEnabled() : 0;
}
void ANSLIB::DebugLog(const char* preformattedLine) {
PreloadSharedDllsOnce();
if (!preformattedLine) return;
if (g_ANSCENTER_DebugLog) {
// Preferred path: licensing DLL applies the prefix scrubber and emits.
g_ANSCENTER_DebugLog(preformattedLine);
return;
}
// Fallback for environments where ANSLicensingSystem.dll is missing:
// emit raw to DebugView + stderr without filtering. Better than dropping.
OutputDebugStringA(preformattedLine);
std::fputs(preformattedLine, stderr);
std::fflush(stderr);
}
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);

View File

@@ -305,6 +305,14 @@ namespace ANSCENTER {
// Used by the ANSLIB_DBG macro below; can also be called directly to
// gate expensive log-prep work.
static int IsDebugViewEnabled();
// DebugView emit. Hands a pre-formatted "[Tag] body\n" line to
// ANSCENTER_DebugLog inside the licensing DLL, which strips noise path
// prefixes (e.g. C:\ProgramData\ANSCENTER\, C:\ProgramData\<profile>\)
// before writing to OutputDebugStringA + stderr. Used by ANSLIB_DBG.
// If the licensing DLL is unavailable, falls back to raw emit (no
// filtering) so messages aren't lost.
static void DebugLog(const char* preformattedLine);
private:
HMODULE dllHandle = nullptr;
bool loaded = false;
@@ -455,16 +463,22 @@ public:
}
};
// 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".
// ANSLIB_DBG: DebugView logging macro mirroring ANS_DBG. Both the gate and the
// emit path are 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".
//
// Noise path prefixes (e.g. C:\ProgramData\ANSCENTER\, C:\ProgramData\<profile>\)
// are stripped centrally inside ANSCENTER_DebugLog before emit, so log lines
// like "loaded from C:\ProgramData\Jh7O7nUe7vS\Models\Foo" appear as
// "loaded from Models\Foo".
//
// 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.
// Hot-path cost when gate is OFF: one indirect call. When gate is ON: snprintf
// to a 1024-byte stack buffer + cross-DLL call into ANSCENTER_DebugLog (which
// scrubs prefixes then emits). No heap, no per-call allocation in the macro.
#ifndef ANSCORE_DEBUGVIEW
#define ANSCORE_DEBUGVIEW 1 // 1 = runtime-gated (default), 0 = hard-off (stripped)
#endif
@@ -473,9 +487,7 @@ public:
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); \
::ANSCENTER::ANSLIB::DebugLog(_ansl_dbg_buf); \
} \
} while(0)
#else