- 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];
}
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 {
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;
}
@ -132,7 +132,6 @@ int AsconStdInit(Ascon *a, SConnection *con,
a->state = AsconConnectStart;
a->reconnectInterval = 10;
a->hostport = strdup(argv[1]);
a->hostport = strdup(argv[1]);
if(argc < 2){
a->sendTerminator = strdup(argv[2]);
} else {
@ -273,7 +272,6 @@ int AsconStdHandler(Ascon *a) {
}
lastCall = now;
switch (a->state) {
case AsconKillMe: return 0;
case AsconConnectStart:
AsconConnect(a);
break;
@ -425,8 +423,6 @@ Ascon *AsconMake(SConnection *con, int argc, char *argv[]) {
}
void AsconKill(Ascon *a) {
a->state = AsconKillMe;
a->handler(a);
if (a->fd > 0) {
close(a->fd);
}
@ -522,5 +518,5 @@ char *AsconRead(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 {
AsconNotConnected=0+AsconFinished,
AsconKillMe=0+AsconStart,
AsconConnecting=4+AsconOnTheWay,
AsconConnectStart=AsconConnecting+AsconStart,
AsconConnectDone=AsconConnecting+AsconFinished,
@ -67,7 +66,6 @@ struct Ascon {
char *sendTerminator; /**< terminator for sending messages */
char *hostport; /**< host:port to connect */
ErrMsg *errList; /**< error message list */
ErrMsg *curError; /**< the currently active error */
double start; /**< unix time when read was started */
void *private; /**< private data of protocol */
void (*killPrivate)(void *); /** < kill function for private */

View File

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

View File

@ -19,15 +19,14 @@ typedef struct ErrMsg {
*
* The error message list contains only one entry for all messages
* 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.
* 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 current The currently active error message
* \param fmt the format for the message
* \return the new error message list head
*/
ErrMsg *ErrPutMsg(ErrMsg *dump, ErrMsg **current, char *fmt, ...);
ErrMsg *ErrPutMsg(ErrMsg *dump, char *fmt, ...);
#endif