- simplified errormsg interface

This commit is contained in:
zolliker
2008-06-13 11:15:14 +00:00
parent 657afffe9c
commit 0305826724
4 changed files with 34 additions and 81 deletions

10
ascon.c
View File

@ -75,9 +75,9 @@ void AsconError(Ascon *a, char *msg, int errorno) {
state = stateText[a->state]; state = stateText[a->state];
} }
if (errorno != 0) { if (errorno != 0) {
a->errList = ErrPutMsg(a->errList, &a->curError, "ASCERR: %s %s (during %s)", msg, strerror(errorno), state); a->errList = ErrPutMsg(a->errList, "ASCERR: %s %s (during %s)", msg, strerror(errorno), state);
} else { } else {
a->errList = ErrPutMsg(a->errList, &a->curError, "ASCERR: %s (during %s)", msg, state); a->errList = ErrPutMsg(a->errList, "ASCERR: %s (during %s)", msg, state);
} }
a->state |= AsconFailed; a->state |= AsconFailed;
} }
@ -132,7 +132,6 @@ int AsconStdInit(Ascon *a, SConnection *con,
a->state = AsconConnectStart; a->state = AsconConnectStart;
a->reconnectInterval = 10; a->reconnectInterval = 10;
a->hostport = strdup(argv[1]); a->hostport = strdup(argv[1]);
a->hostport = strdup(argv[1]);
if(argc < 2){ if(argc < 2){
a->sendTerminator = strdup(argv[2]); a->sendTerminator = strdup(argv[2]);
} else { } else {
@ -273,7 +272,6 @@ int AsconStdHandler(Ascon *a) {
} }
lastCall = now; lastCall = now;
switch (a->state) { switch (a->state) {
case AsconKillMe: return 0;
case AsconConnectStart: case AsconConnectStart:
AsconConnect(a); AsconConnect(a);
break; break;
@ -425,8 +423,6 @@ Ascon *AsconMake(SConnection *con, int argc, char *argv[]) {
} }
void AsconKill(Ascon *a) { void AsconKill(Ascon *a) {
a->state = AsconKillMe;
a->handler(a);
if (a->fd > 0) { if (a->fd > 0) {
close(a->fd); close(a->fd);
} }
@ -522,5 +518,5 @@ char *AsconRead(Ascon *a) {
} }
ErrMsg *AsconGetErrList(Ascon *a) { ErrMsg *AsconGetErrList(Ascon *a) {
return a->curError; return a->errList;
} }

View File

@ -32,7 +32,6 @@ typedef enum { AsconOnTheWay=0, AsconStart=1, AsconFinished=2, AsconFailed=3 } A
*/ */
typedef enum { typedef enum {
AsconNotConnected=0+AsconFinished, AsconNotConnected=0+AsconFinished,
AsconKillMe=0+AsconStart,
AsconConnecting=4+AsconOnTheWay, AsconConnecting=4+AsconOnTheWay,
AsconConnectStart=AsconConnecting+AsconStart, AsconConnectStart=AsconConnecting+AsconStart,
AsconConnectDone=AsconConnecting+AsconFinished, AsconConnectDone=AsconConnecting+AsconFinished,
@ -67,7 +66,6 @@ struct Ascon {
char *sendTerminator; /**< terminator for sending messages */ char *sendTerminator; /**< terminator for sending messages */
char *hostport; /**< host:port to connect */ char *hostport; /**< host:port to connect */
ErrMsg *errList; /**< error message list */ ErrMsg *errList; /**< error message list */
ErrMsg *curError; /**< the currently active error */
double start; /**< unix time when read was started */ double start; /**< unix time when read was started */
void *private; /**< private data of protocol */ void *private; /**< private data of protocol */
void (*killPrivate)(void *); /** < kill function for private */ void (*killPrivate)(void *); /** < kill function for private */

View File

@ -18,80 +18,42 @@ int ErrEqual(char *str1, char *str2) {
if (str1 == NULL || str2 == NULL) { if (str1 == NULL || str2 == NULL) {
return str1 == str2; return str1 == str2;
} }
str1++;
str2++;
} else {
str1++;
str2++;
} }
str1++;
str2++;
} }
return 1; return 1;
} }
ErrMsg *ErrFind(ErrMsg *dump, char *message){ ErrMsg *ErrPutMsg(ErrMsg *dump, char *fmt, ...) {
ErrMsg *cur = dump; ErrMsg *m = NULL;
while(cur != NULL){ ErrMsg **last = NULL;
if(ErrEqual(message,cur->text)){ va_list ap;
break; char buf[256];
} char *text = NULL;
cur = cur->next; int l;
}
return cur;
}
ErrMsg *ErrPutMsg(ErrMsg *dump, ErrMsg **current, char *fmt, ...){ va_start(ap, fmt);
ErrMsg *m = NULL; l = vsnprintf(buf, sizeof buf, fmt, ap);
va_list ap; va_end(ap);
char buf[1024]; if (l < sizeof buf) {
text = buf;
/* format message */ } else {
va_start(ap, fmt); /* assuming we have a C99 conforming snprintf and need a larger buffer */
memset(buf,0,1024*sizeof(char)); text = calloc(l, 1);
vsnprintf(buf, sizeof buf, fmt, ap); va_start(ap, fmt);
va_end(ap); vsnprintf(text, l, fmt, ap);
va_end(ap);
m = ErrFind(dump, buf); }
if(m == NULL){ last = &dump;
m = calloc(1, sizeof(*m)); for (m = dump; m != NULL; m = m->next) {
m->text = strdup(buf);
m->cnt = 1;
m->next = dump;
*current = m;
return m;
} else {
*current = m;
return dump;
}
}
ErrMsg *ErrOldPutMsg(ErrMsg *dump, char *fmt, ...) {
ErrMsg *m = NULL;
ErrMsg **last = NULL;
va_list ap;
char buf[256];
char *text = NULL;
int l;
va_start(ap, fmt);
l = vsnprintf(buf, sizeof buf, fmt, ap);
va_end(ap);
if (l < sizeof buf) {
text = buf;
} else {
/* assuming we have a C99 conforming snprintf and need a larger buffer */
text = calloc(l, 1);
va_start(ap, fmt);
vsnprintf(text, l, fmt, ap);
va_end(ap);
}
for (last = &dump; *last != NULL; last = &m->next) {
m = *last;
if (ErrEqual(text, m->text)) { if (ErrEqual(text, m->text)) {
*last = m->next; *last = m->next; /* remove found item from list */
break; break;
} }
last = &m->next;
} }
if (m == NULL) { if (m == NULL) { /* make a new item */
if (text == buf) text = strdup(buf); if (text == buf) text = strdup(buf);
m = calloc(1, sizeof(*m)); m = calloc(1, sizeof(*m));
m->text = text; m->text = text;
@ -100,9 +62,7 @@ ErrMsg *ErrOldPutMsg(ErrMsg *dump, char *fmt, ...) {
if (text != buf) free(text); if (text != buf) free(text);
m->cnt++; m->cnt++;
} }
if(m != dump){ m->next = dump;
m->next = dump;
}
time(&m->last); time(&m->last);
return m; return m;
} }

View File

@ -19,15 +19,14 @@ typedef struct ErrMsg {
* *
* The error message list contains only one entry for all messages * The error message list contains only one entry for all messages
* with the same text, storing only the count and the last used time. * with the same text, storing only the count and the last used time.
* Characters within sqaure brackets are not taken into account * Characters within square brackets are not taken into account
* when comparing messages. * when comparing messages.
* The newset message is always at the head of the list. * The new message is always at the head of the list.
* *
* \param dump the error message list * \param dump the error message list
* \param current The currently active error message
* \param fmt the format for the message * \param fmt the format for the message
* \return the new error message list head * \return the new error message list head
*/ */
ErrMsg *ErrPutMsg(ErrMsg *dump, ErrMsg **current, char *fmt, ...); ErrMsg *ErrPutMsg(ErrMsg *dump, char *fmt, ...);
#endif #endif