diff --git a/log.c b/log.c index 40ff3aa0..edcf3548 100644 --- a/log.c +++ b/log.c @@ -21,12 +21,25 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include "strbuf.h" #include +FILE *logfile = NULL; int debug = 0; #ifdef ANDROID #include #endif +int start_logging() +{ + if (logfile == NULL) { + const char *logpath = confValueGet("logfile", NULL); + if (logpath) + logfile = fopen(logpath, "a"); + } + if (logfile == NULL) + logfile = stderr; + return 0; +} + void logMessage(int level, const char *file, unsigned int line, const char *function, const char *fmt, ...) { va_list ap; @@ -60,7 +73,9 @@ void vlogMessage(int level, const char *file, unsigned int line, const char *fun case LOG_LEVEL_WARN: levelstr = "WARN"; break; case LOG_LEVEL_DEBUG: levelstr = "DEBUG"; break; } - fprintf(stderr, "%s: %s\n", levelstr, strbuf_str(b)); + if (logfile == NULL) + start_logging(); + fprintf(logfile, "%s: %s\n", levelstr, strbuf_str(b)); } } @@ -80,17 +95,21 @@ const char *trimbuildpath(const char *path) int dump(char *name, unsigned char *addr, int len) { + char buf[100]; int i,j; - fprintf(stderr,"Dump of %s\n",name); - for(i=0;i=' '&&addr[i+j]<0x7f?addr[i+j]:'.'); - fprintf(stderr,"\n"); - } + DEBUGF("Dump of %s", name); + for(i = 0; i < len; i += 16) { + strbuf b = strbuf_local(buf, sizeof buf); + strbuf_sprintf(b, " %04x :", i); + for (j = 0; j < 16 && i + j < len; j++) + strbuf_sprintf(b, " %02x", addr[i + j]); + for (; j < 16; j++) + strbuf_puts(b, " "); + strbuf_puts(b, " "); + for (j = 0; j < 16 && i + j < len; j++) + strbuf_sprintf(b, "%c", addr[i+j] >= ' ' && addr[i+j] < 0x7f ? addr[i+j] : '.'); + DEBUG(strbuf_str(b)); + } return 0; } @@ -111,16 +130,18 @@ char *catv(const char *data, char *buf, size_t len) int dumpResponses(struct response_set *responses) { struct response *r; - if (!responses) {fprintf(stderr,"Response set is NULL\n"); return 0; } - fprintf(stderr,"Response set claims to contain %d entries.\n",responses->response_count); - r=responses->responses; - while(r) - { - fprintf(stderr," response code 0x%02x\n",r->code); - if (r->next) - if (r->next->prev!=r) fprintf(stderr," !! response chain is broken\n"); - r=r->next; - } + if (!responses) { + DEBUG("Response set is NULL"); + return 0; + } + DEBUGF("Response set claims to contain %d entries.", responses->response_count); + r = responses->responses; + while(r) { + DEBUGF(" response code 0x%02x", r->code); + if (r->next && r->next->prev != r) + DEBUG(" !! response chain is broken"); + r = r->next; + } return 0; } diff --git a/serval.h b/serval.h index 11809438..545f6682 100755 --- a/serval.h +++ b/serval.h @@ -766,6 +766,7 @@ extern overlay_txqueue overlay_tx[OQ_MAX]; #define LOG_LEVEL_ERROR (3) #define LOG_LEVEL_FATAL (4) +int start_logging(); void logMessage(int level, const char *file, unsigned int line, const char *function, const char *fmt, ...); void vlogMessage(int level, const char *file, unsigned int line, const char *function, const char *fmt, va_list); long long debugFlagMask(const char *flagname);