Support Get log events

This commit is contained in:
2026-05-24 09:19:24 +10:00
parent 47b80102a8
commit ebba70b9d9
5 changed files with 316 additions and 2 deletions
@@ -419,6 +419,90 @@ int ScanDevicesTest()
return static_cast<int>(devices.size());
}
// ============================================================================
// Example: ANSCENTER::ANSUtilities::GetANSLoggerErrorEvents
//
// Reads every Error-level event written by the "ANSLogger" source in the
// Windows Application event log. Output is a UTF-8 string with one event
// per line in the form:
// "<DD/MM/YYYY h:mm:ss tt>: <event data>\n"
//
// Three usage patterns are demonstrated below:
// 1. Read everything as one blob — simplest, what LabVIEW will do.
// 2. Iterate line-by-line and split out timestamp + message — useful for
// programmatic filtering, alerting, or writing to your own sink.
// 3. Destructive clear (commented out) — wipes the entire Application
// channel; only use if you genuinely want every event from every
// source removed. Requires Administrator privileges.
//
// To verify against Event Viewer:
// eventvwr.msc → Windows Logs → Application → Filter Current Log:
// Event sources = ANSLogger, Event level = Error
// The number this function returns must match Event Viewer's "Number of
// events" line shown above the list.
// ============================================================================
int GetEventLogErrorsTest()
{
std::cout << "\n===== GetANSLoggerErrorEvents (Windows Event Log) =====" << std::endl;
// ── 1. Read every Error event from ANSLogger ────────────────────────────
const bool kClearAfterRead = false; // NEVER true unless you really mean it
std::string events = ANSCENTER::ANSUtilities::GetANSLoggerErrorEvents(kClearAfterRead);
// Count events (one per newline).
int lineCount = 0;
for (char c : events) if (c == '\n') ++lineCount;
std::cout << "Read " << lineCount << " ANSLogger error event(s), "
<< events.size() << " bytes total" << std::endl;
// ── 2. Iterate line-by-line: extract timestamp + message ────────────────
// Format is "<timestamp>: <message>" — split on the first ": ".
// Prints the most recent 5 events so output isn't overwhelming.
std::cout << "\n--- last 5 events (timestamp / message) ---" << std::endl;
std::vector<std::pair<std::string, std::string>> parsed;
parsed.reserve(static_cast<size_t>(lineCount));
size_t lineStart = 0;
for (size_t i = 0; i < events.size(); ++i) {
if (events[i] != '\n') continue;
std::string line = events.substr(lineStart, i - lineStart);
lineStart = i + 1;
auto sep = line.find(": ");
if (sep == std::string::npos) {
parsed.emplace_back(std::string(), std::move(line));
} else {
parsed.emplace_back(line.substr(0, sep), line.substr(sep + 2));
}
}
const size_t showFrom = parsed.size() > 5 ? parsed.size() - 5 : 0;
for (size_t i = showFrom; i < parsed.size(); ++i) {
// Truncate noisy messages so the console stays readable.
std::string msg = parsed[i].second;
if (msg.size() > 120) msg = msg.substr(0, 117) + "...";
std::cout << " [" << parsed[i].first << "] " << msg << std::endl;
}
// ── 3. C extern entry point (this is what LabVIEW calls) ────────────────
// Signature: int GetANSLoggerErrorEventsCpp(int clearAfterRead, std::string& out)
// Passing 0 for clearAfterRead = read only.
std::string fromExtern;
int rc = GetANSLoggerErrorEventsCpp(0, fromExtern);
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;
return lineCount;
}
int main()
{
//ANSLLMOllamaTest();
@@ -434,5 +518,6 @@ int main()
// EncrypyionExample();
ScanDevicesTest();
GetEventLogErrorsTest();
}