- Adapted indenation to new agreed upon system

- Added support for second generation scriptcontext based counter
This commit is contained in:
koennecke
2009-02-13 09:00:03 +00:00
parent a3dcad2bfa
commit 91d4af0541
405 changed files with 88101 additions and 88173 deletions

663
varlog.c
View File

@@ -66,175 +66,168 @@
*/
#define MAXRING 1024
/*------------------------------------------------------------------------*/
typedef struct __VarLog
{
time_t tFrequency;
time_t tNext;
int iCount;
double dSum;
double dDeviation;
FILE *fd;
pCircular pTail;
}VarLog;
typedef struct __VarLog {
time_t tFrequency;
time_t tNext;
int iCount;
double dSum;
double dDeviation;
FILE *fd;
pCircular pTail;
} VarLog;
/*------------------------------------------------------------------------*/
typedef struct {
time_t tTime;
float fVal;
}LogItem, *pLogItem;
typedef struct {
time_t tTime;
float fVal;
} LogItem, *pLogItem;
/*--------------------------------------------------------------------------*/
int VarlogInit(pVarLog *self)
{
int i;
pVarLog pNew = NULL;
pNew = (pVarLog)malloc(sizeof(VarLog));
if(!pNew)
{
return 0;
}
pNew->tFrequency = 300; /* 5 minutes */
pNew->tNext = 0;
pNew->iCount = 0;
pNew->dSum = 0.;
pNew->dDeviation = 0.;
pNew->fd = NULL;
pNew->pTail = createCircular(MAXRING,free);
if(!pNew->pTail)
{
VarlogDelete(pNew);
return 0;
}
int VarlogInit(pVarLog * self)
{
int i;
pVarLog pNew = NULL;
pNew = (pVarLog) malloc(sizeof(VarLog));
if (!pNew) {
return 0;
}
pNew->tFrequency = 300; /* 5 minutes */
pNew->tNext = 0;
pNew->iCount = 0;
pNew->dSum = 0.;
pNew->dDeviation = 0.;
pNew->fd = NULL;
pNew->pTail = createCircular(MAXRING, free);
if (!pNew->pTail) {
VarlogDelete(pNew);
return 0;
}
*self = pNew;
return 1;
}
*self = pNew;
return 1;
}
/*---------------------------------------------------------------------------*/
int VarlogDelete(pVarLog self)
{
if(self->fd != NULL)
{
fclose(self->fd);
}
if(self->pTail != NULL)
{
deleteCircular(self->pTail);
}
free(self); /* M.Z. */
return 1;
}
int VarlogDelete(pVarLog self)
{
if (self->fd != NULL) {
fclose(self->fd);
}
if (self->pTail != NULL) {
deleteCircular(self->pTail);
}
free(self); /* M.Z. */
return 1;
}
/*--------------------------------------------------------------------------*/
int VarlogClear(pVarLog self)
{
int iRet, iList;
self->iCount = 0;
self->dSum = 0.;
self->dDeviation = 0.;
self->tNext = 0;
return 1;
}
int VarlogClear(pVarLog self)
{
int iRet, iList;
self->iCount = 0;
self->dSum = 0.;
self->dDeviation = 0.;
self->tNext = 0;
return 1;
}
/*-------------------------------------------------------------------------*/
static void VLFormatTime(time_t tTime, char *pBuffer, int iBufLen)
{
struct tm *psTime;
static void VLFormatTime(time_t tTime, char *pBuffer, int iBufLen)
{
struct tm *psTime;
/* make time string */
psTime = localtime(&tTime);
memset(pBuffer, 0, iBufLen);
strftime(pBuffer, iBufLen, "%Y-%d-%m %H:%M:%S", psTime);
}
/* make time string */
psTime = localtime(&tTime);
memset(pBuffer,0,iBufLen);
strftime(pBuffer,iBufLen,"%Y-%d-%m %H:%M:%S",psTime);
}
/*--------------------------------------------------------------------------*/
int VarlogAdd(pVarLog self, float fVal)
{
LogItem sItem;
time_t tCurrent;
int iFile = 0;
char pBuffer[80];
double dMean, dTmp;
pLogItem newLog = NULL;
int VarlogAdd(pVarLog self, float fVal)
{
LogItem sItem;
time_t tCurrent;
int iFile = 0;
char pBuffer[80];
double dMean, dTmp;
pLogItem newLog = NULL;
assert(self);
tCurrent = time(NULL);
assert(self);
/* check if we are logging to file */
if(self->fd != NULL)
{
iFile = 1;
tCurrent = time(NULL);
/* check if we are logging to file */
if (self->fd != NULL) {
iFile = 1;
}
/* update the running mean */
self->dSum += fVal;
self->iCount++;
dMean = self->dSum / self->iCount;
dTmp = fVal - dMean;
self->dDeviation += dTmp * dTmp;
/* if not initialised, do it */
if (self->tNext < 1) {
if (iFile) {
VLFormatTime(tCurrent, pBuffer, 79);
fprintf(self->fd, " %s %f \n", pBuffer, fVal);
}
/* update the running mean */
self->dSum += fVal;
self->iCount++;
dMean = self->dSum/self->iCount;
dTmp = fVal - dMean;
self->dDeviation += dTmp*dTmp;
/* if not initialised, do it */
if(self->tNext < 1)
{
if(iFile)
{
VLFormatTime(tCurrent,pBuffer,79);
fprintf(self->fd," %s %f \n", pBuffer,fVal);
}
self->tNext = tCurrent + self->tFrequency;
return 1;
self->tNext = tCurrent + self->tFrequency;
return 1;
}
/* if, log time passed, write to file and into ring buffer */
if (tCurrent > self->tNext) {
if (iFile) {
VLFormatTime(tCurrent, pBuffer, 79);
fprintf(self->fd, " %s %f \n", pBuffer, fVal);
}
/* if, log time passed, write to file and into ring buffer */
if(tCurrent > self->tNext)
{
if(iFile)
{
VLFormatTime(tCurrent,pBuffer,79);
fprintf(self->fd," %s %f \n", pBuffer,fVal);
}
newLog = (pLogItem)malloc(sizeof(LogItem));
if(newLog != NULL)
{
newLog->tTime = tCurrent;
newLog->fVal = fVal;
setCircular(self->pTail,newLog);
nextCircular(self->pTail);
}
self->tNext = tCurrent + self->tFrequency;
return 1;
newLog = (pLogItem) malloc(sizeof(LogItem));
if (newLog != NULL) {
newLog->tTime = tCurrent;
newLog->fVal = fVal;
setCircular(self->pTail, newLog);
nextCircular(self->pTail);
}
return 1;
}
self->tNext = tCurrent + self->tFrequency;
return 1;
}
return 1;
}
/*--------------------------------------------------------------------------*/
int VarlogLength(pVarLog self, int *iLength)
{
*iLength = self->iCount;
return 1;
}
int VarlogLength(pVarLog self, int *iLength)
{
*iLength = self->iCount;
return 1;
}
/*---------------------------------------------------------------------------*/
int VarlogGetMean(pVarLog self, float *fMean, float *fStdDev)
{
double dMean, dStdDev;
int success;
int VarlogGetMean(pVarLog self, float *fMean, float *fStdDev)
{
double dMean, dStdDev;
int success;
if(self->iCount > 0)
{
dMean = self->dSum/(double)self->iCount;
dStdDev = sqrt(self->dDeviation/(double)self->iCount);
success = 1;
}
else
{
dMean = .0;
dStdDev = .0;
success = 0;
}
*fMean = (float)dMean;
*fStdDev = (float)dStdDev;
if (self->iCount > 0) {
dMean = self->dSum / (double) self->iCount;
dStdDev = sqrt(self->dDeviation / (double) self->iCount);
success = 1;
} else {
dMean = .0;
dStdDev = .0;
success = 0;
}
*fMean = (float) dMean;
*fStdDev = (float) dStdDev;
return success;
}
return success;
}
/*-----------------------------------------------------------------------*/
static int VarlogToSicsData(pVarLog self, pSICSData data)
{
@@ -242,44 +235,41 @@ static int VarlogToSicsData(pVarLog self, pSICSData data)
int *dataPtr = NULL;
pLogItem log = NULL;
dataPtr = getSICSDataPointer(data,0,2*MAXRING+1);
if(!dataPtr)
{
dataPtr = getSICSDataPointer(data, 0, 2 * MAXRING + 1);
if (!dataPtr) {
return 0;
}
dataPtr[0] = MAXRING;
/*
skip back MAXRING steps
*/
for(i = 0; i < MAXRING; i++)
{
*/
for (i = 0; i < MAXRING; i++) {
previousCircular(self->pTail);
}
/*
forward again and copy data
*/
for(i = 0; i < MAXRING; i++)
{
forward again and copy data
*/
for (i = 0; i < MAXRING; i++) {
log = getCircular(self->pTail);
if(log != NULL)
{
dataPtr[i+1] = (int)log->tTime;
memcpy(&dataPtr[i+1+MAXRING],&log->fVal,sizeof(float));
if (log != NULL) {
dataPtr[i + 1] = (int) log->tTime;
memcpy(&dataPtr[i + 1 + MAXRING], &log->fVal, sizeof(float));
}
nextCircular(self->pTail);
}
/*
assign data types
*/
assignSICSType(data,0,MAXRING+1,INTTYPE);
assignSICSType(data,MAXRING+1,2*MAXRING+1,FLOATTYPE);
assign data types
*/
assignSICSType(data, 0, MAXRING + 1, INTTYPE);
assignSICSType(data, MAXRING + 1, 2 * MAXRING + 1, FLOATTYPE);
return 1;
}
/*-----------------------------------------------------------------------*/
static void VarlogDump(pVarLog self, SConnection *pCon)
static void VarlogDump(pVarLog self, SConnection * pCon)
{
int i, length;
pLogItem log = NULL;
@@ -287,31 +277,28 @@ static void VarlogDump(pVarLog self, SConnection *pCon)
/*
skip back MAXRING steps
*/
for(i = 0; i < MAXRING; i++)
{
*/
for (i = 0; i < MAXRING; i++) {
previousCircular(self->pTail);
}
/*
forward again and print data
*/
for(i = 0; i < MAXRING; i++)
{
forward again and print data
*/
for (i = 0; i < MAXRING; i++) {
log = getCircular(self->pTail);
if(log != NULL)
{
if(log->tTime > 0)
{
VLFormatTime(log->tTime,timeBuffer,131);
snprintf(pBueffel,255,"%s %12.3f",timeBuffer,log->fVal);
SCWrite(pCon,pBueffel,eValue);
if (log != NULL) {
if (log->tTime > 0) {
VLFormatTime(log->tTime, timeBuffer, 131);
snprintf(pBueffel, 255, "%s %12.3f", timeBuffer, log->fVal);
SCWrite(pCon, pBueffel, eValue);
}
}
nextCircular(self->pTail);
}
}
/*-----------------------------------------------------------------------*/
static void VarLogDumpFile(pVarLog self, FILE *fd)
static void VarLogDumpFile(pVarLog self, FILE * fd)
{
int i, length;
pLogItem log = NULL;
@@ -319,198 +306,164 @@ static void VarLogDumpFile(pVarLog self, FILE *fd)
/*
skip back MAXRING steps
*/
for(i = 0; i < MAXRING; i++)
{
*/
for (i = 0; i < MAXRING; i++) {
previousCircular(self->pTail);
}
/*
forward again and print data
*/
for(i = 0; i < MAXRING; i++)
{
forward again and print data
*/
for (i = 0; i < MAXRING; i++) {
log = getCircular(self->pTail);
if(log != NULL)
{
if(log->tTime > 0)
{
VLFormatTime(log->tTime,timeBuffer,131);
fprintf(fd,"%s %12.3f",timeBuffer,log->fVal);
if (log != NULL) {
if (log->tTime > 0) {
VLFormatTime(log->tTime, timeBuffer, 131);
fprintf(fd, "%s %12.3f", timeBuffer, log->fVal);
}
}
nextCircular(self->pTail);
}
}
/*--------------------------------------------------------------------------*/
int VarlogWrapper(pVarLog self, SConnection *pCon,
char *subcommand, char *sub2, char *pVarName)
{
float fMean, fStdDev, *fData = NULL;
time_t *pTArray = NULL;
int iLength, iRet, i, iList;
char pBueffel[256];
char *pData = NULL;
long lNew;
pSICSData data = NULL;
FILE *fd = NULL;
strtolower(subcommand);
int VarlogWrapper(pVarLog self, SConnection * pCon,
char *subcommand, char *sub2, char *pVarName)
{
float fMean, fStdDev, *fData = NULL;
time_t *pTArray = NULL;
int iLength, iRet, i, iList;
char pBueffel[256];
char *pData = NULL;
long lNew;
pSICSData data = NULL;
FILE *fd = NULL;
strtolower(subcommand);
/*--------- file */
if(strcmp(subcommand,"file") == 0)
{
if(self->fd != NULL)
{
fclose(self->fd);
self->fd = NULL;
}
self->fd = fopen(sub2,"w");
if(!self->fd)
{
sprintf(pBueffel,"ERROR: failed to open temperature log file: %s",
sub2);
SCWrite(pCon,pBueffel,eError);
return 0;
}
if(self->tFrequency < 20)
{
self->tFrequency = 300;
}
SCSendOK(pCon);
return 1;
if (strcmp(subcommand, "file") == 0) {
if (self->fd != NULL) {
fclose(self->fd);
self->fd = NULL;
}
self->fd = fopen(sub2, "w");
if (!self->fd) {
sprintf(pBueffel, "ERROR: failed to open temperature log file: %s",
sub2);
SCWrite(pCon, pBueffel, eError);
return 0;
}
if (self->tFrequency < 20) {
self->tFrequency = 300;
}
SCSendOK(pCon);
return 1;
}
/*----- flush */
else if(strcmp(subcommand,"flush") == 0)
{
if(self->fd != NULL)
{
fflush(self->fd);
}
SCSendOK(pCon);
return 1;
else if (strcmp(subcommand, "flush") == 0) {
if (self->fd != NULL) {
fflush(self->fd);
}
SCSendOK(pCon);
return 1;
}
/*----- close */
else if(strcmp(subcommand,"close") == 0)
{
if(self->fd != NULL)
{
fclose(self->fd);
self->fd = NULL;
}
SCSendOK(pCon);
return 1;
else if (strcmp(subcommand, "close") == 0) {
if (self->fd != NULL) {
fclose(self->fd);
self->fd = NULL;
}
SCSendOK(pCon);
return 1;
}
/*----- status */
else if(strcmp(subcommand,"status") == 0)
{
if(self->fd != NULL)
{
SCWrite(pCon,"Logging to file on",eValue);
}
else
{
SCWrite(pCon,"No Logging to file",eValue);
}
return 1;
else if (strcmp(subcommand, "status") == 0) {
if (self->fd != NULL) {
SCWrite(pCon, "Logging to file on", eValue);
} else {
SCWrite(pCon, "No Logging to file", eValue);
}
return 1;
}
/*--------- getmean */
else if(strcmp(subcommand,"getmean") == 0)
{
iRet = VarlogGetMean(self,&fMean,&fStdDev);
if(!iRet)
{
SCWrite(pCon,"WARNING: No mean and stddev log values available ",
eWarning);
return 0;
}
sprintf(pBueffel,"%s.Mean = %8.2f %s.StdDev = %8.2f",
pVarName,fMean,pVarName,fStdDev);
SCWrite(pCon,pBueffel,eValue);
return 1;
}
else if (strcmp(subcommand, "getmean") == 0) {
iRet = VarlogGetMean(self, &fMean, &fStdDev);
if (!iRet) {
SCWrite(pCon, "WARNING: No mean and stddev log values available ",
eWarning);
return 0;
}
sprintf(pBueffel, "%s.Mean = %8.2f %s.StdDev = %8.2f",
pVarName, fMean, pVarName, fStdDev);
SCWrite(pCon, pBueffel, eValue);
return 1;
}
/*------------ clear */
else if(strcmp(subcommand,"clear") == 0)
{
return VarlogClear(self);
}
else if (strcmp(subcommand, "clear") == 0) {
return VarlogClear(self);
}
/*------------- frequency */
else if(strcmp(subcommand,"frequency") == 0)
{
if(sub2)
{
iRet = sscanf(sub2,"%ld",&lNew);
}
else
{
iRet = 0;
}
if(iRet == 1) /* new value */
{
self->tFrequency = lNew;
return 1;
}
else /* print */
{
sprintf(pBueffel,"%s.frequency = %d",pVarName,(int)self->tFrequency);
SCWrite(pCon,pBueffel,eValue);
return 1;
}
else if (strcmp(subcommand, "frequency") == 0) {
if (sub2) {
iRet = sscanf(sub2, "%ld", &lNew);
} else {
iRet = 0;
}
/*-------------- tosicsdata */
else if(strcmp(subcommand,"tosicsdata") == 0)
{
if(!sub2)
{
SCWrite(pCon,"ERROR: tosicsdata needs an argument",eError);
return 0;
}
data = FindCommandData(pServ->pSics,sub2,"SICSData");
if(!data)
{
snprintf(pBueffel,255,"ERROR: %s is no sicsdata object",sub2);
SCWrite(pCon,pBueffel,eError);
return 0;
}
iRet = VarlogToSicsData(self,data);
if(iRet == 0)
{
SCWrite(pCon,"ERROR: out of memory in VarlogToSicsData",eError);
return 0;
}
SCSendOK(pCon);
if (iRet == 1) { /* new value */
self->tFrequency = lNew;
return 1;
}
/* ---------------- dumpring */
else if(strcmp(subcommand,"dump") == 0)
{
VarlogDump(self,pCon);
} else { /* print */
sprintf(pBueffel, "%s.frequency = %d", pVarName,
(int) self->tFrequency);
SCWrite(pCon, pBueffel, eValue);
return 1;
}
/*---------------- dumptofile */
else if(strcmp(subcommand,"dumptofile") == 0)
{
if(sub2 != NULL)
{
fd = fopen(sub2,"w");
}
if(fd == NULL)
{
snprintf(pBueffel,255,"ERROR: failed to open %s",sub2);
SCWrite(pCon,pBueffel,eError);
return 0;
}
VarLogDumpFile(self,fd);
fclose(fd);
SCSendOK(pCon);
return 1;
}
/* command not recognized */
else
{
sprintf(pBueffel,"ERROR: %s no valid command to varlog",subcommand);
SCWrite(pCon,pBueffel,eError);
return 0;
}
}
/*-------------- tosicsdata */
else if (strcmp(subcommand, "tosicsdata") == 0) {
if (!sub2) {
SCWrite(pCon, "ERROR: tosicsdata needs an argument", eError);
return 0;
}
data = FindCommandData(pServ->pSics, sub2, "SICSData");
if (!data) {
snprintf(pBueffel, 255, "ERROR: %s is no sicsdata object", sub2);
SCWrite(pCon, pBueffel, eError);
return 0;
}
iRet = VarlogToSicsData(self, data);
if (iRet == 0) {
SCWrite(pCon, "ERROR: out of memory in VarlogToSicsData", eError);
return 0;
}
SCSendOK(pCon);
return 1;
}
/* ---------------- dumpring */
else if (strcmp(subcommand, "dump") == 0) {
VarlogDump(self, pCon);
return 1;
}
/*---------------- dumptofile */
else if (strcmp(subcommand, "dumptofile") == 0) {
if (sub2 != NULL) {
fd = fopen(sub2, "w");
}
if (fd == NULL) {
snprintf(pBueffel, 255, "ERROR: failed to open %s", sub2);
SCWrite(pCon, pBueffel, eError);
return 0;
}
VarLogDumpFile(self, fd);
fclose(fd);
SCSendOK(pCon);
return 1;
}
/* command not recognized */
else {
sprintf(pBueffel, "ERROR: %s no valid command to varlog", subcommand);
SCWrite(pCon, pBueffel, eError);
return 0;
}
}