Prevent OOM

This commit is contained in:
2026-06-18 05:55:31 +10:00
parent ebba70b9d9
commit b3436a61ee
44 changed files with 1797 additions and 66 deletions
@@ -1,3 +1,7 @@
// windows.h must be included before any header that pulls in wincrypt.h
// (Chilkat, OpenCV transitively). HKEY/RegOpenKeyExW are used by the
// watermark readout in GetEventLogErrorsWatermarkTest.
#include <windows.h>
#include <iostream>
#include "ANSUtilities.h"
#include "ANSLLM.h"
@@ -489,20 +493,123 @@ int GetEventLogErrorsTest()
std::cout << "\nGetANSLoggerErrorEventsCpp rc=" << rc
<< " bytes=" << fromExtern.size() << std::endl;
// ── 4. (DANGER) clear the Application channel after reading ─────────────
// Uncomment ONLY if you actually want to wipe every event from every
// source in the Application log (not just ANSLogger). Run as Admin or
// the clear silently fails (the read still succeeds either way).
//
// std::string cleared;
// int rcClear = GetANSLoggerErrorEventsCpp(1 /* clearAfterRead */, cleared);
// std::cout << "After clear: rc=" << rcClear
// << " events returned=" << std::count(cleared.begin(), cleared.end(), '\n')
// << " (next call should report 0)" << std::endl;
// ── 4. DIAGNOSTIC: read → read+clear → read, prove whether the clear
// flag actually does anything (and if not, capture the OS error).
{
std::cout << "\n--- clear-flag diagnostic ---" << std::endl;
auto countLines = [](const std::string& s) {
int n = 0;
for (char c : s) if (c == '\n') ++n;
return n;
};
std::string before = ANSCENTER::ANSUtilities::GetANSLoggerErrorEvents(false);
std::cout << " before clear : " << countLines(before) << " events" << std::endl;
std::string cleared = ANSCENTER::ANSUtilities::GetANSLoggerErrorEvents(true);
std::cout << " with clear=true call returned: "
<< countLines(cleared) << " events" << std::endl;
std::string after = ANSCENTER::ANSUtilities::GetANSLoggerErrorEvents(false);
std::cout << " after clear : " << countLines(after)
<< " events (should be 0 if clear worked)" << std::endl;
const int beforeN = countLines(before);
const int afterN = countLines(after);
if (beforeN == 0) {
std::cout << " RESULT: inconclusive (no events to clear). "
"Check stderr above for the actual EvtClearLog status."
<< std::endl;
} else if (afterN == 0) {
std::cout << " RESULT: clear SUCCEEDED — process was elevated."
<< std::endl;
} else if (afterN >= beforeN) {
std::cout << " RESULT: clear FAILED. "
"Check stderr above for the GetLastError code "
"(5 = ACCESS_DENIED — re-run as Administrator)."
<< std::endl;
} else {
std::cout << " RESULT: partial clear (" << beforeN << " -> "
<< afterN << "). Likely a concurrent writer."
<< std::endl;
}
}
return lineCount;
}
// ============================================================================
// Example: ANSCENTER::ANSUtilities::GetANSLoggerErrorEventsSinceLastCall
//
// Watermark-based variant. Two consecutive calls demonstrate the skip:
// 1st call: returns every Error event from ANSLogger (same as the full read)
// 2nd call: returns 0 (or only events written between the two calls)
// The watermark is stored at
// HKCU\Software\ANSCENTER\ANSUtilities\EventLogWatermark\ANSLogger (REG_QWORD)
// To force a re-read of everything, delete that registry value.
// ============================================================================
int GetEventLogErrorsWatermarkTest()
{
std::cout << "\n===== GetANSLoggerErrorEventsSinceLastCall (watermark) ====="
<< std::endl;
auto countLines = [](const std::string& s) {
int n = 0;
for (char c : s) if (c == '\n') ++n;
return n;
};
// Read the current watermark so we can show it before and after.
auto readWatermark = []() -> unsigned long long {
HKEY hKey = nullptr;
if (RegOpenKeyExW(HKEY_CURRENT_USER,
L"Software\\ANSCENTER\\ANSUtilities\\EventLogWatermark",
0, KEY_READ, &hKey) != ERROR_SUCCESS) return 0ULL;
unsigned long long v = 0;
DWORD sz = sizeof(v), tp = 0;
LONG rc = RegQueryValueExW(hKey, L"ANSLogger", nullptr, &tp,
reinterpret_cast<LPBYTE>(&v), &sz);
RegCloseKey(hKey);
return (rc == ERROR_SUCCESS && tp == REG_QWORD) ? v : 0ULL;
};
std::cout << " watermark before: " << readWatermark() << std::endl;
std::string first =
ANSCENTER::ANSUtilities::GetANSLoggerErrorEventsSinceLastCall(false);
std::cout << " 1st call returned: " << countLines(first) << " event(s)"
<< std::endl;
std::cout << " watermark after 1st call: " << readWatermark() << std::endl;
std::string second =
ANSCENTER::ANSUtilities::GetANSLoggerErrorEventsSinceLastCall(false);
std::cout << " 2nd call returned: " << countLines(second)
<< " event(s) (should be 0 if no new errors were logged "
"between the two calls)" << std::endl;
if (countLines(second) == 0 && countLines(first) > 0) {
std::cout << " RESULT: watermark works — duplicates suppressed."
<< std::endl;
} else if (countLines(first) == 0) {
std::cout << " RESULT: nothing to test (no ANSLogger errors in log)."
<< std::endl;
} else {
std::cout << " RESULT: 2nd call returned " << countLines(second)
<< " event(s) — either new errors arrived between calls, "
"or the watermark didn't persist." << std::endl;
}
// Note: passing the LabVIEW extern below as well so its parameter wiring
// is exercised. Pass 0 = no clear; clear path is identical to the
// already-tested admin-required version in GetEventLogErrorsTest.
std::string fromExtern;
int rc = GetANSLoggerErrorEventsSinceLastCallCpp(0, fromExtern);
std::cout << " GetANSLoggerErrorEventsSinceLastCallCpp rc=" << rc
<< " bytes=" << fromExtern.size()
<< " (should be 0 — already watermarked above)" << std::endl;
return countLines(first);
}
int main()
{
//ANSLLMOllamaTest();
@@ -519,5 +626,6 @@ int main()
ScanDevicesTest();
GetEventLogErrorsTest();
GetEventLogErrorsWatermarkTest();
}