Improve servald logging

Use 'logfile' config entry, fall back to stderr if absent or can't be opened
Replace fprintf(stderr,...) with DEBUGF() in dump() and dumpResponses()
This commit is contained in:
Andrew Bettison 2012-06-22 17:19:14 +09:30
parent de4a9a1318
commit a6ac6a4246
2 changed files with 43 additions and 21 deletions

63
log.c
View File

@ -21,12 +21,25 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include "strbuf.h" #include "strbuf.h"
#include <ctype.h> #include <ctype.h>
FILE *logfile = NULL;
int debug = 0; int debug = 0;
#ifdef ANDROID #ifdef ANDROID
#include <android/log.h> #include <android/log.h>
#endif #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, ...) void logMessage(int level, const char *file, unsigned int line, const char *function, const char *fmt, ...)
{ {
va_list ap; 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_WARN: levelstr = "WARN"; break;
case LOG_LEVEL_DEBUG: levelstr = "DEBUG"; 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) int dump(char *name, unsigned char *addr, int len)
{ {
char buf[100];
int i,j; int i,j;
fprintf(stderr,"Dump of %s\n",name); DEBUGF("Dump of %s", name);
for(i=0;i<len;i+=16) for(i = 0; i < len; i += 16) {
{ strbuf b = strbuf_local(buf, sizeof buf);
fprintf(stderr," %04x :",i); strbuf_sprintf(b, " %04x :", i);
for(j=0;j<16&&(i+j)<len;j++) fprintf(stderr," %02x",addr[i+j]); for (j = 0; j < 16 && i + j < len; j++)
for(;j<16;j++) fprintf(stderr," "); strbuf_sprintf(b, " %02x", addr[i + j]);
fprintf(stderr," "); for (; j < 16; j++)
for(j=0;j<16&&(i+j)<len;j++) fprintf(stderr,"%c",addr[i+j]>=' '&&addr[i+j]<0x7f?addr[i+j]:'.'); strbuf_puts(b, " ");
fprintf(stderr,"\n"); 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; return 0;
} }
@ -111,16 +130,18 @@ char *catv(const char *data, char *buf, size_t len)
int dumpResponses(struct response_set *responses) int dumpResponses(struct response_set *responses)
{ {
struct response *r; struct response *r;
if (!responses) {fprintf(stderr,"Response set is NULL\n"); return 0; } if (!responses) {
fprintf(stderr,"Response set claims to contain %d entries.\n",responses->response_count); DEBUG("Response set is NULL");
r=responses->responses; return 0;
while(r) }
{ DEBUGF("Response set claims to contain %d entries.", responses->response_count);
fprintf(stderr," response code 0x%02x\n",r->code); r = responses->responses;
if (r->next) while(r) {
if (r->next->prev!=r) fprintf(stderr," !! response chain is broken\n"); DEBUGF(" response code 0x%02x", r->code);
r=r->next; if (r->next && r->next->prev != r)
} DEBUG(" !! response chain is broken");
r = r->next;
}
return 0; return 0;
} }

View File

@ -766,6 +766,7 @@ extern overlay_txqueue overlay_tx[OQ_MAX];
#define LOG_LEVEL_ERROR (3) #define LOG_LEVEL_ERROR (3)
#define LOG_LEVEL_FATAL (4) #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 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); void vlogMessage(int level, const char *file, unsigned int line, const char *function, const char *fmt, va_list);
long long debugFlagMask(const char *flagname); long long debugFlagMask(const char *flagname);