mirror of
https://github.com/servalproject/serval-dna.git
synced 2024-12-26 08:11:09 +00:00
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:
parent
de4a9a1318
commit
a6ac6a4246
55
log.c
55
log.c
@ -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,16 +95,20 @@ 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,14 +130,16 @@ 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");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
DEBUGF("Response set claims to contain %d entries.", responses->response_count);
|
||||||
r = responses->responses;
|
r = responses->responses;
|
||||||
while(r)
|
while(r) {
|
||||||
{
|
DEBUGF(" response code 0x%02x", r->code);
|
||||||
fprintf(stderr," response code 0x%02x\n",r->code);
|
if (r->next && r->next->prev != r)
|
||||||
if (r->next)
|
DEBUG(" !! response chain is broken");
|
||||||
if (r->next->prev!=r) fprintf(stderr," !! response chain is broken\n");
|
|
||||||
r = r->next;
|
r = r->next;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
|
1
serval.h
1
serval.h
@ -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);
|
||||||
|
Loading…
Reference in New Issue
Block a user