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