Improve debugging/logging macros

Replace 'WHYRETNULL();' statements with 'return WHYNULL();'
Introduce DEBUG(), DEBUGF(), DEBUG_perror() and D macros
Add logMessage() and vlogMessage() functions
Implement setReason() using vlogMessage()
This commit is contained in:
Andrew Bettison 2012-05-10 12:53:57 +09:30
parent 0cf69c9b30
commit b15e5cfee7
5 changed files with 69 additions and 31 deletions

58
dna.c
View File

@ -19,7 +19,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include "serval.h"
#include "rhizome.h"
#include <stdarg.h>
#include <signal.h>
#include <unistd.h>
#include <dirent.h>
@ -88,25 +87,50 @@ int dumpResponses(struct response_set *responses)
#include <android/log.h>
#endif
int setReason(char *fmt, ...)
void logMessage(int level, char *fmt, ...)
{
va_list ap,ap2;
char msg[8192];
va_start(ap,fmt);
va_copy(ap2,ap);
vsnprintf(msg,8192,fmt,ap2); msg[8191]=0;
va_end(ap);
#ifdef ANDROID
__android_log_print(ANDROID_LOG_DEBUG, "servald", "%s",msg);
#endif
fprintf(stderr,"Error: %s\n",msg);
return -1;
va_list ap;
va_start(ap, fmt);
vlogMessage(level, fmt, ap);
}
void vlogMessage(int level, char *fmt, va_list ap)
{
va_list ap2;
char buf[8192];
va_copy(ap2, ap);
vsnprintf(buf, sizeof buf, fmt, ap2);
va_end(ap);
buf[sizeof buf - 1] = '\0';
#ifdef ANDROID
int alevel = ANDROID_LOG_UNKNOWN;
switch (level) {
case LOG_LEVEL_FATAL: alevel = ANDROID_LOG_FATAL; break;
case LOG_LEVEL_ERROR: alevel = ANDROID_LOG_ERROR; break;
case LOG_LEVEL_INFO: alevel = ANDROID_LOG_INFO; break;
case LOG_LEVEL_WARN: alevel = ANDROID_LOG_WARN; break;
case LOG_LEVEL_DEBUG: alevel = ANDROID_LOG_DEBUG; break;
}
__android_log_print(alevel, "servald", "%s", buf);
#endif
const char *levelstr = "UNKNOWN";
switch (level) {
case LOG_LEVEL_FATAL: levelstr = "FATAL"; break;
case LOG_LEVEL_ERROR: levelstr = "ERROR"; break;
case LOG_LEVEL_INFO: levelstr = "INFO"; break;
case LOG_LEVEL_WARN: levelstr = "WARN"; break;
case LOG_LEVEL_DEBUG: levelstr = "DEBUG"; break;
}
fprintf(stderr, "%s: %s\n", levelstr, buf);
}
int setReason(char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vlogMessage(LOG_LEVEL_ERROR, fmt, ap);
return -1;
}
int hexvalue(unsigned char c)
{

View File

@ -1108,7 +1108,7 @@ unsigned char *keyring_find_sas_private(keyring_file *k,unsigned char *sid,
int cn=0,in=0,kp=0;
if (!keyring_find_sid(k,&cn,&in,&kp,sid))
WHYRETNULL("Could not find SID in keyring, so can't find SAS");
return WHYNULL("Could not find SID in keyring, so can't find SAS");
for(kp=0;kp<k->contexts[cn]->identities[in]->keypair_count;kp++)
if (k->contexts[cn]->identities[in]->keypairs[kp]->type==KEYTYPE_CRYPTOSIGN)
@ -1119,7 +1119,7 @@ unsigned char *keyring_find_sas_private(keyring_file *k,unsigned char *sid,
return k->contexts[cn]->identities[in]->keypairs[kp]->private_key;
}
WHYRETNULL("Identity lacks SAS");
return WHYNULL("Identity lacks SAS");
}
struct sid_sas_mapping {
@ -1303,7 +1303,7 @@ unsigned char *keyring_find_sas_public(keyring_file *k,unsigned char *sid)
==KEYTYPE_CRYPTOBOX)
bcopy(keyring->contexts[0]->identities[0]->keypairs[0]->public_key,
mdp.out.src.sid,SID_SIZE);
else WHYRETNULL("couldn't request SAS (I don't know who I am)");
else return WHYNULL("couldn't request SAS (I don't know who I am)");
mdp.out.payload_length=1;
mdp.out.payload[0]=KEYTYPE_CRYPTOSIGN;
overlay_mdp_dispatch(&mdp,0 /* system generated */,
@ -1424,9 +1424,9 @@ struct nm_record nm_cache[NM_CACHE_SLOTS];
unsigned char *keyring_get_nm_bytes(sockaddr_mdp *known,sockaddr_mdp *unknown)
{
if (!known) WHYRETNULL("known pub key is null");
if (!unknown) WHYRETNULL("unknown pub key is null");
if (!keyring) WHYRETNULL("keyring is null");
if (!known) return WHYNULL("known pub key is null");
if (!unknown) return WHYNULL("unknown pub key is null");
if (!keyring) return WHYNULL("keyring is null");
int i;
@ -1444,7 +1444,7 @@ unsigned char *keyring_get_nm_bytes(sockaddr_mdp *known,sockaddr_mdp *unknown)
in fact a known key */
int cn=0,in=0,kp=0;
if (!keyring_find_sid(keyring,&cn,&in,&kp,known->sid))
WHYRETNULL("known key is not in fact known.");
return WHYNULL("known key is not in fact known.");
/* work out where to store it */
if (nm_slots_used<NM_CACHE_SLOTS) {

View File

@ -186,8 +186,7 @@ int overlayServerMode()
if (r<0) {
/* select had a problem */
if (debug&DEBUG_IO) WHY_perror("poll()");
WHY("select() complained.");
WHY_perror("poll");
} else if (r>0) {
/* We have data, so try to receive it */
if (debug&DEBUG_IO) {

View File

@ -342,19 +342,19 @@ overlay_frame *op_dup(overlay_frame *in)
/* clone the frame */
overlay_frame *out=malloc(sizeof(overlay_frame));
if (!out) WHYRETNULL("malloc() failed");
if (!out) return WHYNULL("malloc() failed");
/* copy main data structure */
bcopy(in,out,sizeof(overlay_frame));
out->payload=ob_new(in->payload->length);
if (!out->payload) {
free(out);
WHYRETNULL("ob_new() failed");
return WHYNULL("ob_new() failed");
}
if (ob_append_bytes(out->payload,&in->payload->bytes[0],in->payload->length))
{
op_free(out);
WHYRETNULL("could not duplicate payload bytes");
return WHYNULL("could not duplicate payload bytes");
}
return out;
}

View File

@ -22,6 +22,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <stdarg.h>
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
@ -736,12 +737,26 @@ typedef struct overlay_txqueue {
#define OQ_MAX 5
extern overlay_txqueue overlay_tx[OQ_MAX];
#define LOG_LEVEL_DEBUG (0)
#define LOG_LEVEL_INFO (1)
#define LOG_LEVEL_WARN (2)
#define LOG_LEVEL_ERROR (3)
#define LOG_LEVEL_FATAL (4)
int setReason(char *fmt, ...);
void logMessage(int level, char *fmt, ...);
void vlogMessage(int level, char *fmt, va_list);
#define WHY(X) setReason("%s:%d:%s() %s",__FILE__,__LINE__,__FUNCTION__,X)
#define WHYRETNULL(X) { setReason("%s:%d:%s() %s",__FILE__,__LINE__,__FUNCTION__,X); return NULL; }
#define WHYF(F, ...) setReason("%s:%d:%s() " F, __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__)
#define WHYNULL(X) (setReason("%s:%d:%s() %s",__FILE__,__LINE__,__FUNCTION__,X), NULL)
#define WHYF(F,...) setReason("%s:%d:%s() " F, __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__)
#define WHY_perror(X) setReason("%s:%d:%s() %s: %s [errno=%d]", __FILE__, __LINE__, __FUNCTION__, X, strerror(errno), errno)
#define DEBUGF(F,...) logMessage(LOG_LEVEL_DEBUG, "%s:%d:%s() " F, __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__)
#define DEBUG(X) DEBUGF("%s", X)
#define DEBUG_perror(X) DEBUGF("%s: %s [errno=%d]", X, strerror(errno), errno)
#define D DEBUG("D")
overlay_buffer *ob_new(int size);
int ob_free(overlay_buffer *b);
int ob_checkpoint(overlay_buffer *b);