Hi, I am developing a utility file for ArEventLog in C++ for use in my PLC code. The code is largely built upon another member contribution from my earlier queries. I was wondering if there were any suggestions of possible improvements. Thanks.
- I am not sure if I need the CopyPLCString function. Is there a better way to convert/copy the strings?
- I am storing some messages for log later in case the logger is not at the correct step.
- I have hardcoded the message severities as when I used the enums to pass into the function to get the severity EventID, it returns 0.
I call LogInit in the Cyclic function. And use LogMsg to log a message.
Regards,
Kenneth
.h file:
#pragma once
#include
#include
#include <globalVAR.h>//custom file
#ifdef _DEFAULT_INCLUDES
#endif
#include <ArEventLog.h>
namespace ArEventLogUtils
{
enum
{
LOG_SEVERITY_SUCCESS=536870912,
LOG_SEVERITY_INFO=1610612736,
LOG_SEVERITY_WARNING=-1610612736,
LOG_SEVERITY_ERROR=-536870912
};
void CopyPLCString(STRING* strPLC, std::string& str);
void LogMsg(const char* strMsg, DINT severity=ArEventLogUtils::LOG_SEVERITY_INFO);
void ProcessMsgs();
void LogInit();
}
cpp file:
#include “ArEventLogUtils.hpp”
namespace ArEventLogUtils
{
std::vectorstd::string store;
std::vector severityValues;
void CopyPLCString(STRING* strPLC, const char* str)
{
for (int i = 0; ; i++)
{
if (str[i]=='\0'||i>=30)
{
break;
}
strPLC[i] = str[i];
}
}
void LogMsg(const char* strMsg, DINT severity)
{
if (Step==40)
{
ArEventLogWrite_0.Ident = Ident;
ArEventLogWrite_0.OriginRecordID = 0;
CopyPLCString(ArEventLogWrite_0.ObjectID, "PLC");
ArEventLogWrite_0.TimeStamp = 0;
ArEventLogWrite_0.EventID = severity;
ArEventLogWrite_0.AddData = (unsigned int)strMsg;
ArEventLogWrite_0.AddDataSize = strlen(strMsg)+1;
ArEventLogWrite_0.AddDataFormat = arEVENTLOG_ADDFORMAT_TEXT;
ArEventLogWrite_0.Execute = true;
ArEventLogWrite(&ArEventLogWrite_0); // synchron fub call
ArEventLogWrite_0.Execute = false;
ArEventLogWrite(&ArEventLogWrite_0); // synchron fub call
}
else
{
store.push_back(strMsg);
severityValues.push_back(severity);
}
}
void ProcessMsgs()
{
for (int i = 0; i < store.size(); i++)
{
LogMsg(store[i].c_str(), severityValues[i]);
}
store.clear();
severityValues.clear();
}
void LogInit()
{
switch (Step)
{
case 0:
// Add Start Condition if nessesary
Step = 5;
break;
case 5:
CopyPLCString(ArEventLogGetIdent_0.Name, "Sys");
ArEventLogGetIdent_0.Execute = false;
ArEventLogGetIdent(&ArEventLogGetIdent_0);
Step = 10;
break;
case 10:
ArEventLogGetIdent_0.Execute = true;
ArEventLogGetIdent(&ArEventLogGetIdent_0);
if (ArEventLogGetIdent_0.Done)
{
Ident = ArEventLogGetIdent_0.Ident;
ArEventLogGetIdent_0.Execute = false;
Step = 30; // Write
}
else if (ArEventLogGetIdent_0.Error)
{
if (ArEventLogGetIdent_0.StatusID == arEVENTLOG_ERR_LOGBOOK_NOT_FOUND)
{
Step = 20; // create
CopyPLCString(ArEventLogGetIdent_0.Name, "Sys");
ArEventLogCreate_0.Size = 65535;
ArEventLogCreate_0.Persistence = arEVENTLOG_PERSISTENCE_PERSIST;
ArEventLogCreate_0.Info = 0;
ArEventLogCreate_0.Execute = false;
ArEventLogCreate(&ArEventLogCreate_0);
}
else
{
Step = 255; // error
}
}
break;
case 20:
ArEventLogCreate_0.Execute = true;
ArEventLogCreate(&ArEventLogCreate_0);
if (ArEventLogCreate_0.Done)
{
Ident = ArEventLogCreate_0.Ident;
ArEventLogCreate_0.Execute = false;
Step = 30; // Write
}
else if (ArEventLogCreate_0.Error)
{
Step = 255; // error
}
break;
case 30:
Step = 40;
break;
case 40:
ProcessMsgs();
break;
}
}
}