632 lines
24 KiB
C++
632 lines
24 KiB
C++
// 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"
|
|
#include "CkCrypt2.h"
|
|
#include <fstream>
|
|
#include <sstream>
|
|
#include <string>
|
|
#include <opencv2/opencv.hpp>
|
|
|
|
#include <CkHttp.h>
|
|
#include <CkJsonObject.h>
|
|
#include <CkHttpResponse.h>
|
|
#include <CkGlobal.h>
|
|
|
|
void EncrypyionExample(void)
|
|
{
|
|
std::string encryptedString = "";
|
|
//std::string inputKey = "mikael.holmstrom@anscenter.comAA";
|
|
std::string inputKey = "sale@anscenter.com.au";
|
|
std::string inputString = "https://anspushnotification.s3.ap-southeast-2.amazonaws.com/Events/20250113_154146_221140160_643.jpg";
|
|
AESEncryptionCpp(inputString.c_str(), inputKey.c_str(), encryptedString);
|
|
std::cout << "Encrypted String:" << encryptedString << std::endl;
|
|
//std::string NewEncryptionString = "DRqCU8fsgYZcwyjMdMwnEDStSVk13htAIznm1iee8ri0js1RJqN4/q7dMtK5HyQcOWPnQThN8971lFZwRB2baMOxhvyB/xyvP3mzjvPoTH98kvO1UxBltbi0xrlTKIO8xg1i7Iw07zhmSJzxRNz18A==";
|
|
std::string decryptedString = "";
|
|
AESDecryptionCpp(encryptedString.c_str(), inputKey.c_str(), decryptedString);
|
|
std::cout << "Decrypted String:" << decryptedString << std::endl;
|
|
}
|
|
int Test() {
|
|
ANSCENTER::ANSUtilities* Handle;
|
|
|
|
int result = CreateANSUtilityHandle(&Handle, "");
|
|
|
|
std::cout << "Intialisation:" << result << std::endl;
|
|
std::string arnTopic = "";
|
|
//CreateAWSSNSTopicCpp(&Handle, "MyTopicFromC",arnTopic);
|
|
//std::cout << "Topic ARN:" << arnTopic << std::endl;
|
|
|
|
|
|
// delete the topic
|
|
//arnTopic = "arn:aws:sns:ap-southeast-2:654654292871:MyTopicFromC";//arn:aws:sns:ap-southeast-2:654654292871:MyTopicFromC
|
|
//bool deleteResult= DeleteAWSSNSTopic(&Handle, arnTopic.c_str());
|
|
//std::cout << "Delete Topic:" << deleteResult << std::endl;
|
|
//+34623225024
|
|
// Send message
|
|
std::string messageId = "";
|
|
bool SendMessageResult = SendMessageToPhoneNumberCpp(&Handle, "+34623225024", "Hi Mr Mikael Holmstrom, this is the test message from ANS AI Box", messageId);
|
|
|
|
std::cout << "Send Message:" << SendMessageResult << std::endl;
|
|
std::cout << "Message ID:" << messageId << std::endl;
|
|
}
|
|
int uploadFileToGoogleStorage() {
|
|
ANSCENTER::ANSUtilities* Handle;
|
|
int result = CreateANSUtilityHandle(&Handle, "");
|
|
std::cout << "Intialisation:" << result << std::endl;
|
|
std::string googleTokenJsonfile = "C:\\Projects\\ANSVIS\\Documentation\\ImageStorage\\gcs_uploader_key.json";
|
|
std::string bucketName = "ansaibox-image-storage";
|
|
std::string objectName = "public-images/myTest2.jpg";
|
|
cv::Mat image = cv::imread("C:\\Programs\\DemoAssets\\NV\\test.jpg");
|
|
|
|
// Read json file to text first
|
|
std::ifstream file(googleTokenJsonfile); // Open the JSON file
|
|
if (!file) {
|
|
std::cerr << "Error: Cannot open file!" << std::endl;
|
|
return 1;
|
|
}
|
|
|
|
std::stringstream buffer;
|
|
buffer << file.rdbuf(); // Read file content into buffer
|
|
std::string jsonString = buffer.str(); // Convert buffer to std::string
|
|
|
|
std::cout << "JSON Content:\n" << jsonString << std::endl;
|
|
bool authResult = AuthenticateGCS(&Handle, jsonString.c_str());
|
|
std::cout << "Authenticate:" << authResult << std::endl;
|
|
|
|
bool uploadResult = UploadImageToGCS(& Handle, bucketName.c_str(), objectName.c_str(), image);
|
|
std::cout << "Upload Image:" << uploadResult << std::endl;
|
|
ReleaseANSUtilityHandle(&Handle);
|
|
}
|
|
void SendEmailExample(void)
|
|
{
|
|
std::string smtpServer = "smtp.office365.com";
|
|
int smtpPort = 587;
|
|
std::string smtpUser = "noreply@anscenter.com";
|
|
std::string smtpPassword = "PW";//
|
|
std::string subjectContent = "This is the test";
|
|
std::string bodyHTMLContent = "<html> <body><b>Test Email</b> <img src = \"http://www.chilkatsoft.com/images/dude.gif\"></body></html>";
|
|
std::string bodyTextContent = "";
|
|
std::string fromEmailSender = "noreply@anscenter.com";
|
|
std::string toEmails = "nghia.nguyen@anscenter.com;kate.truong@anscenter.com";
|
|
std::string ccEmails = "support@anscenter.com;sale@anscenter.com";
|
|
std::string bccEmails = "admin@anscenter.com";
|
|
int resultSent = SendEmail(smtpServer.c_str(), smtpPort, smtpUser.c_str(), smtpPassword.c_str(),
|
|
subjectContent.c_str(), bodyHTMLContent.c_str(), bodyTextContent.c_str(), fromEmailSender.c_str(),
|
|
toEmails.c_str(), ccEmails.c_str(), bccEmails.c_str());
|
|
|
|
std::cout << "Send Email Result:" << resultSent << std::endl;
|
|
}
|
|
int ANSLLMTest() {
|
|
std::cout << "Running ANSLLM Test..." << std::endl;
|
|
ANSCENTER::ANSLLM ansllm;
|
|
std::string licenseKey = "";
|
|
std::string apiKey = "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";
|
|
// Initialize with license key
|
|
ansllm.Initialize(licenseKey);
|
|
ansllm.SetLLMProvider("openai");
|
|
ansllm.SetApiKey(apiKey);
|
|
//std::vector<std::string> modelList = ansllm.GetModelList();
|
|
//std::cout << "Available Models:" << std::endl;
|
|
//for (const auto& model : modelList) {
|
|
// std::cout << model << std::endl;
|
|
//}
|
|
|
|
// Set model
|
|
ansllm.SetModel("gpt-5.2");
|
|
ansllm.CreateConversation("You are a helpful assistant.", "Respond only with markdown.", "TestConversation");
|
|
ansllm.InputAddText("Say Hello.");
|
|
|
|
|
|
std::string response = ansllm.Ask("text");
|
|
std::cout << "Response from ANSLLM: " << response << std::endl;
|
|
|
|
// ask others
|
|
std::string prompt2 = "Please tell me a person whose name is Sophie";
|
|
ansllm.InputAddText(prompt2);
|
|
|
|
std::string response2 = ansllm.Ask("text");
|
|
std::cout << "Response from ANSLLM: " << response2 << std::endl;
|
|
|
|
ansllm.InputAddText("Describe what you see in the following image.");
|
|
ansllm.InputAddImageUrl("https://www.chilkatsoft.com/images/starfish.jpg", "");
|
|
|
|
std::string response3 = ansllm.Ask("text");
|
|
std::cout << "Response from ANSLLM: " << response3 << std::endl;
|
|
|
|
|
|
return 0;
|
|
}
|
|
|
|
int ANSAWSTest() {
|
|
std::cout << "Running ANSAWS Test..." << std::endl;
|
|
ANSCENTER::ANSAWSS3* awsHandle = nullptr;
|
|
// Initialize AWS S3 handle
|
|
if (CreateANSAWSHandle(&awsHandle,"") != 1) {
|
|
std::cerr << "Failed to create AWS S3 handle." << std::endl;
|
|
return 1;
|
|
}
|
|
std::string baseDomain = "amazonaws.com";
|
|
std::string bucketRegion = "ap-southeast-2";
|
|
std::string serviceName = "s3";
|
|
std::string bucketName = "anspushnotification";
|
|
|
|
int port = 443;
|
|
bool bTls = true;
|
|
bool autoReconnect = true;
|
|
// Connect to AWS S3
|
|
int awsPath = 0;
|
|
if (ConnectANSAWSHandle(&awsHandle, baseDomain.c_str(), bucketRegion.c_str(), serviceName.c_str(), port, bTls, autoReconnect, &awsPath) == 0) {
|
|
std::cerr << "Failed to connect to AWS S3." << std::endl;
|
|
ReleaseANSAWSHandle(&awsHandle);
|
|
return 1;
|
|
}
|
|
std::cout << "Connected. AWS path style: " << (awsPath ? "true" : "false") << std::endl;
|
|
|
|
// Set authentication (test commit)
|
|
std::string accessKey = "AKIAZQ3DPYODSHZCECS4";
|
|
std::string secretKey = "ccnISNp05UDRmTP9TLx6kEz7EfnPQqNQXEJOycey";
|
|
if (SetAuthenticationANSAWSHandle(&awsHandle, accessKey.c_str(), secretKey.c_str()) != 1) {
|
|
std::cerr << "Failed to set AWS S3 authentication." << std::endl;
|
|
ReleaseANSAWSHandle(&awsHandle);
|
|
return 1;
|
|
}
|
|
// Upload a jpeg image to bucket
|
|
std::string jpegFilePath = "C:\\ProgramData\\ANSCENTER\\Shared\\Test.jpg";
|
|
std::string objectName = "test/test.jpg";
|
|
|
|
std::string uploadedUrl;
|
|
while (1) {
|
|
if (UploadBinaryDataANSAWSHandle_CPP(&awsHandle, bucketName.c_str(), jpegFilePath.c_str(), uploadedUrl) != 1) {
|
|
std::cerr << "Failed to upload JPEG image to AWS S3." << std::endl;
|
|
std::this_thread::sleep_for(std::chrono::seconds(1)); // Wait before retrying
|
|
}
|
|
else {
|
|
std::cout << "Successfully uploaded JPEG image to AWS S3." << std::endl;
|
|
break;
|
|
|
|
}
|
|
}
|
|
std::cout << "Uploaded to: " << uploadedUrl << std::endl;
|
|
|
|
|
|
//// List buckets
|
|
//std::string bucketList = "";
|
|
//if (ListBucketANSAWSHandle_CPP(&awsHandle, bucketList) != 1) {
|
|
// std::cerr << "Failed to list AWS S3 buckets." << std::endl;
|
|
// ReleaseANSAWSHandle(&awsHandle);
|
|
// return 1;
|
|
//}
|
|
//std::cout << "Buckets in AWS S3:" << std::endl;
|
|
//std::cout << bucketList << std::endl;
|
|
|
|
//// list objects in a bucket
|
|
//std::string bucketName = "anspushnotification";
|
|
//std::string prefix = "Events/ee2ejdgefcjl7j6ruhvsjlaea/";
|
|
//std::string bucketObjectList = "";
|
|
|
|
//if (ListBucketObjectsWithPrefixANSAWSHandle_CPP(&awsHandle, bucketName.c_str(), prefix.c_str(), bucketObjectList) != 1) {
|
|
// std::cerr << "Failed to list AWS S3 bucket objects." << std::endl;
|
|
// ReleaseANSAWSHandle(&awsHandle);
|
|
// return 1;
|
|
//}
|
|
//std::cout << "Bucket objects in AWS S3:" << std::endl;
|
|
//std::cout << bucketObjectList << std::endl;
|
|
|
|
|
|
//// Create a new bucket
|
|
//std::string newBucketName = "my-test-bucket-a3f9d2c1w2";
|
|
//std::string bucketPrefix = "test-prefix/";
|
|
//if (CreateBucketANSAWSHandle(&awsHandle, newBucketName.c_str()) != 1) {
|
|
// std::cerr << "Failed to create AWS S3 bucket." << std::endl;
|
|
// ReleaseANSAWSHandle(&awsHandle);
|
|
// return 1;
|
|
//}
|
|
//if (CreateBucketPrefixANSAWSHandle(&awsHandle, newBucketName.c_str(),bucketPrefix.c_str()) != 1) {
|
|
// std::cerr << "Failed to create AWS S3 bucket prefix." << std::endl;
|
|
// ReleaseANSAWSHandle(&awsHandle);
|
|
// return 1;
|
|
//}
|
|
|
|
//// Delete the bucket prefix
|
|
//std::string deleteBucketName = "my-test-bucket-a3f9d2c1";
|
|
//if (DeleteBucketPrefixANSAWSHandle(&awsHandle, newBucketName.c_str(), bucketPrefix.c_str()) != 1) {
|
|
// std::cerr << "Failed to delete AWS S3 bucket." << std::endl;
|
|
// ReleaseANSAWSHandle(&awsHandle);
|
|
// return 1;
|
|
//}
|
|
|
|
// Delete the bucket
|
|
//if (DeleteBucketANSAWSHandle(&awsHandle, newBucketName.c_str()) != 1) {
|
|
// std::cerr << "Failed to delete AWS S3 bucket." << std::endl;
|
|
// ReleaseANSAWSHandle(&awsHandle);
|
|
// return 1;
|
|
//}
|
|
|
|
|
|
// Download an object from bucket
|
|
//std::string objectName = "testevent/test01.jpg";
|
|
//std::string downloadFilePath = "E:\\Programs\\DemoAssets\\Images";
|
|
//if (DownloadFileStreamANSAWSHandle(&awsHandle, bucketName.c_str(), objectName.c_str(), downloadFilePath.c_str()) != 1) {
|
|
// std::cerr << "Failed to download AWS S3 bucket object." << std::endl;
|
|
// ReleaseANSAWSHandle(&awsHandle);
|
|
// return 1;
|
|
//}
|
|
//
|
|
// Clean up
|
|
ReleaseANSAWSHandle(&awsHandle);
|
|
return 0;
|
|
}
|
|
|
|
int ANSFTechMinIOTest() {
|
|
std::cout << "Running ANSFTechMinIO Test..." << std::endl;
|
|
ANSCENTER::ANSAWSS3* awsHandle = nullptr;
|
|
// Initialize AWS S3 handle
|
|
if (CreateANSAWSHandle(&awsHandle, "") != 1) {
|
|
std::cerr << "Failed to create AWS S3 handle." << std::endl;
|
|
return 1;
|
|
}
|
|
std::string baseDomain = "https://minio.dev.ftech.ai";
|
|
std::string bucketRegion = "vn1";
|
|
std::string serviceName = "s3";
|
|
std::string bucketName = "funzy";
|
|
|
|
int port = 443;
|
|
bool bTls = true;
|
|
bool autoReconnect = true;
|
|
// Connect to AWS S3
|
|
int awsPath = 0;
|
|
if (ConnectANSAWSHandle(&awsHandle, baseDomain.c_str(), bucketRegion.c_str(), serviceName.c_str(), port, bTls, autoReconnect, &awsPath) == 0) {
|
|
std::cerr << "Failed to connect to AWS S3." << std::endl;
|
|
ReleaseANSAWSHandle(&awsHandle);
|
|
return 1;
|
|
}
|
|
|
|
// Set authentication
|
|
std::string accessKey = "tuanpm";
|
|
std::string secretKey = "BRFbQujGhkKrI3xqSUE9ywnpXHt27c";
|
|
if (SetAuthenticationANSAWSHandle(&awsHandle, accessKey.c_str(), secretKey.c_str()) != 1) {
|
|
std::cerr << "Failed to set AWS S3 authentication." << std::endl;
|
|
ReleaseANSAWSHandle(&awsHandle);
|
|
return 1;
|
|
}
|
|
|
|
|
|
// Upload a jpeg image to bucket
|
|
std::string jpegFilePath = "C:\\ProgramData\\ANSCENTER\\Shared\\Test.jpg";
|
|
std::string objectName = "test1.jpg";
|
|
|
|
|
|
std::string uploadedUrl;
|
|
if (UploadBinaryDataANSAWSHandle_CPP(&awsHandle, bucketName.c_str(), jpegFilePath.c_str(), uploadedUrl)!= 1) {
|
|
std::cerr << "Failed to upload JPEG image to AWS S3." << std::endl;
|
|
ReleaseANSAWSHandle(&awsHandle);
|
|
return 1;
|
|
}
|
|
|
|
|
|
// List buckets
|
|
//std::string bucketList = "";
|
|
//if (ListBucketANSAWSHandle_CPP(&awsHandle, bucketList) != 1) {
|
|
// std::cerr << "Failed to list AWS S3 buckets." << std::endl;
|
|
// ReleaseANSAWSHandle(&awsHandle);
|
|
// return 1;
|
|
//}
|
|
//std::cout << "Buckets in AWS S3:" << std::endl;
|
|
//std::cout << bucketList << std::endl;
|
|
|
|
// list objects in a bucket
|
|
//std::string bucketName = "anspushnotification";
|
|
//std::string prefix = "Events/ee2ejdgefcjl7j6ruhvsjlaea/";
|
|
//std::string bucketObjectList = "";
|
|
|
|
//if (ListBucketObjectsWithPrefixANSAWSHandle_CPP(&awsHandle, bucketName.c_str(), prefix.c_str(), bucketObjectList) != 1) {
|
|
// std::cerr << "Failed to list AWS S3 bucket objects." << std::endl;
|
|
// ReleaseANSAWSHandle(&awsHandle);
|
|
// return 1;
|
|
//}
|
|
//std::cout << "Bucket objects in AWS S3:" << std::endl;
|
|
//std::cout << bucketObjectList << std::endl;
|
|
|
|
|
|
// Create a new bucket
|
|
//std::string newBucketName = "my-test-bucket-a3f9d2c1w2";
|
|
//std::string bucketPrefix = "test-prefix/";
|
|
//if (CreateBucketANSAWSHandle(&awsHandle, newBucketName.c_str()) != 1) {
|
|
// std::cerr << "Failed to create AWS S3 bucket." << std::endl;
|
|
// ReleaseANSAWSHandle(&awsHandle);
|
|
// return 1;
|
|
//}
|
|
//if (CreateBucketPrefixANSAWSHandle(&awsHandle, newBucketName.c_str(),bucketPrefix.c_str()) != 1) {
|
|
// std::cerr << "Failed to create AWS S3 bucket prefix." << std::endl;
|
|
// ReleaseANSAWSHandle(&awsHandle);
|
|
// return 1;
|
|
//}
|
|
|
|
//// Delete the bucket prefix
|
|
//std::string deleteBucketName = "my-test-bucket-a3f9d2c1";
|
|
//if (DeleteBucketPrefixANSAWSHandle(&awsHandle, newBucketName.c_str(), bucketPrefix.c_str()) != 1) {
|
|
// std::cerr << "Failed to delete AWS S3 bucket." << std::endl;
|
|
// ReleaseANSAWSHandle(&awsHandle);
|
|
// return 1;
|
|
//}
|
|
|
|
// Delete the bucket
|
|
//if (DeleteBucketANSAWSHandle(&awsHandle, newBucketName.c_str()) != 1) {
|
|
// std::cerr << "Failed to delete AWS S3 bucket." << std::endl;
|
|
// ReleaseANSAWSHandle(&awsHandle);
|
|
// return 1;
|
|
//}
|
|
|
|
|
|
// Clean up
|
|
ReleaseANSAWSHandle(&awsHandle);
|
|
return 0;
|
|
}
|
|
|
|
int ANSLLMOllamaTest() {
|
|
std::cout << "Running ANSLLM Ollama Test..." << std::endl;
|
|
ANSCENTER::ANSLLM ansllm;
|
|
std::string licenseKey = "";
|
|
|
|
// Initialize with local Ollama enabled
|
|
ansllm.Initialize(licenseKey, true);
|
|
|
|
// Set the Ollama model (must be already pulled via 'ollama pull llama3.2')
|
|
ansllm.SetModel("llama3.2");
|
|
ansllm.CreateConversation("You are a helpful assistant.", "Respond only with markdown.", "TestConversation");
|
|
ansllm.InputAddText("Why is the sky blue?");
|
|
|
|
std::string response = ansllm.Ask("text");
|
|
std::cout << "Response from ANSLLM (Ollama): " << response << std::endl;
|
|
|
|
// Ask another question
|
|
ansllm.InputAddText("Say Hello.");
|
|
std::string response2 = ansllm.Ask("text");
|
|
std::cout << "Response from ANSLLM (Ollama): " << response2 << std::endl;
|
|
|
|
return 0;
|
|
}
|
|
|
|
// Exercise ANSCENTER::ANSUtilities::ScanDevices — discovers AIBOX boxes and
|
|
// ANSVIS servers on the local LAN via UDP multicast + HTTP /ServerApi sweep.
|
|
// Run on a machine connected to the same subnet as at least one device.
|
|
// DebugView (filter "[ANSDISC]") shows per-device hits and per-interface joins.
|
|
int ScanDevicesTest()
|
|
{
|
|
std::cout << "\n===== ScanDevices (AIBOX / ANSVIS discovery) =====" << std::endl;
|
|
|
|
// 1) Direct static C++ call.
|
|
const int timeoutMs = 5000;
|
|
std::cout << "Scanning for " << timeoutMs << " ms ..." << std::endl;
|
|
auto devices = ANSCENTER::ANSUtilities::ScanDevices(timeoutMs);
|
|
|
|
std::cout << "Found " << devices.size() << " device(s):" << std::endl;
|
|
for (const auto& d : devices) {
|
|
const char* src = (d.source == 3) ? "both"
|
|
: (d.source == 2) ? "sweep"
|
|
: (d.source == 1) ? "multicast"
|
|
: "unknown";
|
|
std::cout << " " << d.ip << ":" << d.port
|
|
<< " name=\"" << d.name << "\""
|
|
<< " firmware=\"" << d.firmware << "\""
|
|
<< " source=" << src << std::endl;
|
|
}
|
|
|
|
// 2) Exercise the C extern entrypoint that LabVIEW uses.
|
|
std::string json;
|
|
int rc = ScanANSDevicesCpp(timeoutMs, json);
|
|
std::cout << "\nScanANSDevicesCpp rc=" << rc << " json=" << json << std::endl;
|
|
|
|
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. 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();
|
|
|
|
//std::cout << "===== MinIO (path-style) =====" << std::endl;
|
|
//ANSFTechMinIOTest();
|
|
|
|
//std::cout << "\n===== AWS S3 (virtual-host) =====" << std::endl;
|
|
//ANSAWSTest();
|
|
//ANSLLMTest();
|
|
//SendEmailExample();
|
|
//uploadFileToGoogleStorage();
|
|
// EncrypyionExample();
|
|
|
|
ScanDevicesTest();
|
|
GetEventLogErrorsTest();
|
|
GetEventLogErrorsWatermarkTest();
|
|
}
|
|
|