Make debug logging dependent on counter debug parameter

r1558 | dcl | 2007-02-26 17:40:49 +1100 (Mon, 26 Feb 2007) | 2 lines
This commit is contained in:
Douglas Clowes
2007-02-26 17:40:49 +11:00
parent 1bfd9eb459
commit e57006789b

View File

@@ -63,39 +63,41 @@ typedef struct {
float dummy_threshold;
BUFFER buffer;
uint64 counter_value;
int debug;
} BeamMon, *pBeamMon;
/** \brief file logging
*
* Logs text to a debug file. The file is opened and closed for each call.
*
* \param flag character used to distinguish calls
* \param self the beam monitor counter object
* \param format printf style format string
* \param ... printf style variable argument list
*/
static void flog(char flag, char* format, ...) {
va_list ap;
FILE* file;
int iRet;
char buffer[2048];
static void flog(pBeamMon self, char* format, ...) {
if (self->debug) {
va_list ap;
FILE* file;
int iRet;
char buffer[2048];
file = fopen("flog.txt", "a");
if (file) {
struct timeval tv;
(void) gettimeofday(&tv, NULL);
fprintf(file, "%02ld:%02ld:%02ld:%06ld::%c::",
tv.tv_sec % (24 * 3600) / 3600,
tv.tv_sec % (3600) / 60,
tv.tv_sec % 60,
tv.tv_usec,
flag);
va_start(ap, format);
iRet = vsnprintf(buffer, sizeof(buffer), format, ap);
va_end(ap);
fputs(buffer, file);
if (buffer[iRet - 1] != '\n')
(void) fputc('\n', file);
(void) fclose(file);
file = fopen("flog.txt", "a");
if (file) {
struct timeval tv;
(void) gettimeofday(&tv, NULL);
fprintf(file, "%02ld:%02ld:%02ld:%06ld::",
tv.tv_sec % (24 * 3600) / 3600,
tv.tv_sec % (3600) / 60,
tv.tv_sec % 60,
tv.tv_usec);
va_start(ap, format);
iRet = vsnprintf(buffer, sizeof(buffer), format, ap);
va_end(ap);
fputs(buffer, file);
if (buffer[iRet - 1] != '\n')
(void) fputc('\n', file);
(void) fclose(file);
}
}
}
@@ -133,12 +135,12 @@ static int checkConnection(pBeamMon self)
/* no data to read, otherwise OK */
return SUCCESS;
}
flog('#', "counter recv = %d, errno = %d:%s\n", status, errno, strerror(errno));
flog('#', "Channel Error!");
flog(self, "#::counter recv = %d, errno = %d:%s\n", status, errno, strerror(errno));
flog(self, "#::Channel Error!");
self->errorCode = BADREAD;
return FAILURE;
} else if (status == 0) {
flog('#', "Channel Disconnected!");
flog(self, "#::Channel Disconnected!");
self->errorCode = NOTCONNECTED;
return FAILURE;
}
@@ -146,14 +148,14 @@ static int checkConnection(pBeamMon self)
status = send(sock, NULL, 0, 0);
if (status < 0) {
if (errno == EAGAIN) {
flog('#', "Unexpected EAGAIN\n");
flog(self, "#::Unexpected EAGAIN\n");
return SUCCESS;
} else if (errno == EPIPE) {
flog('#', "Unexpected EPIPE\n");
flog(self, "#::Unexpected EPIPE\n");
self->errorCode = NOTCONNECTED;
return FAILURE;
} else {
flog('#', "Unexpected error %d: %s\n", errno, strerror(errno));
flog(self, "#::Unexpected error %d: %s\n", errno, strerror(errno));
self->errorCode = NOTCONNECTED;
return FAILURE;
}
@@ -176,10 +178,10 @@ static int fixConnection(pBeamMon self)
{
int status;
flog('_', "fixConnection");
flog(self, "_::fixConnection");
if (self && self->controller) {
if (self->errorCode != NOTCONNECTED) {
flog('!', "unbroken");
flog(self, "!::unbroken");
return FAILURE;
}
status = initRS232(self->controller);
@@ -189,7 +191,7 @@ static int fixConnection(pBeamMon self)
int iret;
int sock = self->controller->pSock->sockid;
if (sock < 0 || sock > FD_SETSIZE) {
flog('!', "bad socket");
flog(self, "!::bad socket");
return FAILURE;
}
FD_ZERO(&wmask);
@@ -198,10 +200,10 @@ static int fixConnection(pBeamMon self)
FD_SET(sock, &rmask);
iret = select(sock+1, &rmask, &wmask, NULL, &tmo);
if (iret < 0) {
flog('!', "%s", strerror(errno));
flog(self, "!::%s", strerror(errno));
return FAILURE; /* some sort of error */
} else if (iret == 0) {
flog('!', "timeout");
flog(self, "!::timeout");
return FAILURE; /* timed out */
} else {
if (FD_ISSET(sock, &wmask)) { /* should always be true */
@@ -209,28 +211,28 @@ static int fixConnection(pBeamMon self)
char buff[1];
iret = recv(sock, buff, 1, MSG_PEEK); /* just peek */
if (iret == 0 || (iret < 0 && errno != EAGAIN)) {
flog('!', "disconnected");
flog(self, "!::disconnected");
return FAILURE; /* disconnected */
}
}
iret = send(sock, NULL, 0, 0); /* zero length, check only return value */
if (iret < 0) {
flog('!', "%s", strerror(errno));
flog(self, "!::%s", strerror(errno));
return FAILURE; /* some sort of error */
}
}
flog('!', "success");
flog(self, "!::success");
return SUCCESS;
}
} else {
flog('!', "initRS232 failed");
flog(self, "!::initRS232 failed");
return FAILURE;
}
} else {
flog('!', "no initRS232 controller");
flog(self, "!::no initRS232 controller");
return FAILURE;
}
flog('!', "fallthrough");
flog(self, "!::fallthrough");
return FAILURE;
}
@@ -247,11 +249,11 @@ static int MonWrite(pBeamMon self, char* text) {
len = strlen(text);
if (len > 0)
flog('>', "%s", text);
flog(self, ">::%s", text);
status = writeRS232(self->controller, text, (int) len);
if (status != 1) {
flog('#', "MonWrite status = %d, errno = %d\n", status, errno);
flog(self, "#::MonWrite status = %d, errno = %d\n", status, errno);
self->errorCode = status;
if (status == BADSEND && errno == EPIPE)
self->errorCode = NOTCONNECTED;
@@ -277,7 +279,7 @@ static int MonRead(pBeamMon self, /*@out@*/ char* text, int *pLen) {
switch (status) {
case 1:
if (pLen > 0)
flog('<', "%s", text);
flog(self, "<::%s", text);
return SUCCESS;
case TIMEOUT:
/*
@@ -289,16 +291,16 @@ static int MonRead(pBeamMon self, /*@out@*/ char* text, int *pLen) {
return FAILURE;
}
flog('<', "*TIMEOUT* %d", self->controller->timeout);
flog(self, "<::*TIMEOUT* %d", self->controller->timeout);
self->errorCode = status;
continue;
default:
flog('<', "*ERROR*");
flog(self, "<::*ERROR*");
self->errorCode = status;
return FAILURE;
}
}
flog('<', "***TIMEOUT*** %d", self->controller->timeout);
flog(self, "<::***TIMEOUT*** %d", self->controller->timeout);
return FAILURE;
}
@@ -411,7 +413,7 @@ static void HandleReport(CounterDriver *cntrData, BUFFER* bp)
self->state = HWNoBeam;
self->counter_value = strtoull(cp, &ep, 10);
cntrData->fLastCurrent = (float) self->counter_value;
flog('.', "READ %s, %f, %f, %f\n",
flog(self, ".::READ %s, %f, %f, %f\n",
cntrData->eMode == eTimer ? "eTimer" :
cntrData->eMode == ePreset ? "ePreset" : "Unknown",
cntrData->fPreset,
@@ -435,7 +437,7 @@ static void MonHandleInput(CounterDriver *cntrData, BUFFER* bp) {
const char rpt[] = "READ";
self = (BeamMon *) cntrData->pData;
flog('=', "%s", bp->body);
flog(self, "=::%s", bp->body);
if (strncasecmp(bp->body, "EVENT", 5) == 0) {
char* cp = bp->body + 5;
while (*cp && isspace(*cp)) // skip space
@@ -563,13 +565,13 @@ static int MonStart(CounterDriver *cntrData) {
BeamMon *self = NULL;
int status;
char str[100];
flog('.', "START %s, %f, %f, %f\n",
self = (BeamMon *) cntrData->pData;
flog(self, ".::START %s, %f, %f, %f\n",
cntrData->eMode == eTimer ? "eTimer" :
cntrData->eMode == ePreset ? "ePreset" : "Unknown",
cntrData->fPreset,
cntrData->fLastCurrent,
cntrData->fTime);
self = (BeamMon *) cntrData->pData;
MonDrainInput(cntrData);
if (cntrData->eMode == eTimer) {
status = MonSendBuffer(cntrData, "SICS SET TE_CHECK=TIMER", &self->buffer);
@@ -675,8 +677,8 @@ static int MonReadValues(CounterDriver *cntrData) {
BeamMon *self = NULL;
int status;
flog('.', "MonReadValues");
self = (BeamMon *) cntrData->pData;
flog(self, ".::MonReadValues");
status = MonSendBuffer(cntrData, "SICS READ", &self->buffer);
if (status == SUCCESS) {
HandleReport(cntrData, &self->buffer);
@@ -704,7 +706,7 @@ static int MonGetError(CounterDriver *cntrData, int *iCode, char *error, int iEr
self->errorMsg = NULL;
}
*iCode = self->errorCode;
flog('.', "MonGetError: %d", self->errorCode);
flog(self, ".::MonGetError: %d", self->errorCode);
switch (*iCode) {
case 0:
if (checkConnection(self) == FAILURE) {
@@ -742,8 +744,8 @@ static int MonGetError(CounterDriver *cntrData, int *iCode, char *error, int iEr
*iCode);
break;
}
flog('#', "MonGetError: %d", *iCode);
flog('#', "%s", error);
flog(self, "#::MonGetError: %d", *iCode);
flog(self, "#::%s", error);
self->errorMsg = strdup(error);
self->errorCode = 0;
return SUCCESS;
@@ -760,8 +762,8 @@ static int MonGetError(CounterDriver *cntrData, int *iCode, char *error, int iEr
static int MonTryAndFixIt(CounterDriver *cntrData, int iCode) {
BeamMon *self = NULL;
flog('.', "MonTryAndFixIt");
self = (BeamMon *) cntrData->pData;
flog(self, ".::MonTryAndFixIt");
assert(self != NULL);
switch(iCode){
@@ -789,13 +791,17 @@ static int MonTryAndFixIt(CounterDriver *cntrData, int iCode) {
*/
static int MonSet(CounterDriver *cntrData, char *name, int iCter, float fVal) {
BeamMon *self = NULL;
flog('.', "MonSet(%s, %d, %f)", name, iCter, fVal);
self = (BeamMon *) cntrData->pData;
flog(self, ".::MonSet(%s, %d, %f)", name, iCter, fVal);
if(strcmp(name,"threshold") == 0){
//TODO set threshold
self->dummy_threshold = fVal;
}
else if(strcmp(name,"debug") == 0){
//TODO set threshold
self->debug = fVal;
}
/* TODO*/
return SUCCESS;
}
@@ -808,9 +814,13 @@ static int MonGet(CounterDriver *cntrData, char *name, int iCter, float *fVal) {
//TODO get threshold
*fVal = self->dummy_threshold;
}
else if(strcasecmp(name,"debug") == 0){
//TODO get threshold
*fVal = self->debug;
}
/* TODO*/
flog('.', "MonGet(%s, %d, %f)", name, iCter, *fVal);
flog(self, ".::MonGet(%s, %d, %f)", name, iCter, *fVal);
return SUCCESS;
}