Improve debug code:

Debug bit mask is unsigned int everywhere.
Refactor DEBUG() macros a bit.
Add SERVALD_DEBUG_FILE env var to support test scripts.
Test defs put instance directories under $TFWVAR not $TFWTMP.
This commit is contained in:
Andrew Bettison 2012-06-28 15:24:43 +09:30
parent f7483c2c15
commit 9625190c6a
4 changed files with 44 additions and 35 deletions

View File

@ -591,8 +591,8 @@ void confSetDebugFlags()
if (FORM_SERVAL_INSTANCE_PATH(filename, "serval.conf")) {
FILE *f = fopen(filename, "r");
if (f) {
long long setmask = 0;
long long clearmask = 0;
unsigned int setmask = 0;
unsigned int clearmask = 0;
int setall = 0;
int clearall = 0;
char line[1024];
@ -612,13 +612,13 @@ void confSetDebugFlags()
++q;
*q = '\0';
if ((flag = confParseBoolean(p + 1, flagname)) != -1) {
long long mask = debugFlagMask(flagname);
if (mask == -1) {
unsigned int mask = debugFlagMask(flagname);
if (mask == DEBUG_ALL) {
if (flag) {
DEBUGF("Set all debug flags");
// DEBUGF("Set all debug flags");
setall = 1;
} else {
DEBUGF("Clear all debug flags");
// DEBUGF("Clear all debug flags");
clearall = 1;
}
} else {
@ -637,7 +637,10 @@ void confSetDebugFlags()
fgets(line, sizeof line, f);
}
fclose(f);
if (setall) debug = -1; else if (clearall) debug = 0;
if (setall)
debug = -1;
else if (clearall)
debug = 0;
debug &= ~clearmask;
debug |= setmask;
}

32
log.c
View File

@ -22,21 +22,28 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include <ctype.h>
FILE *logfile = NULL;
int debug = 0;
unsigned int debug = 0;
#ifdef ANDROID
#include <android/log.h>
#endif
int start_logging()
static int start_logging()
{
if (logfile == NULL) {
const char *logpath = confValueGet("logfile", NULL);
if (logpath)
logfile = fopen(logpath, "a");
}
if (logfile == NULL)
const char *logpath = getenv("SERVALD_LOG_FILE");
if (!logpath)
logpath = confValueGet("logfile", NULL);
if (!logpath) {
logfile = stderr;
INFO("No logfile configured -- logging to stderr");
}
else if ((logfile = fopen(logpath, "a")))
setlinebuf(logfile);
else {
logfile = stderr;
WARN_perror("fopen");
WARNF("Cannot append to %s -- falling back to stderr", logpath);
}
return 0;
}
@ -73,9 +80,8 @@ 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;
}
if (logfile == NULL)
start_logging();
fprintf(logfile, "%s: %s\n", levelstr, strbuf_str(b));
if (logfile || start_logging() == 0)
fprintf(logfile, "%s: [%d] %s\n", levelstr, getpid(), strbuf_str(b));
}
}
@ -145,8 +151,8 @@ int dumpResponses(struct response_set *responses)
return 0;
}
long long debugFlagMask(const char *flagname) {
if (!strcasecmp(flagname,"all")) return -1;
unsigned int debugFlagMask(const char *flagname) {
if (!strcasecmp(flagname,"all")) return DEBUG_ALL;
else if (!strcasecmp(flagname,"interfaces")) return DEBUG_OVERLAYINTERFACES;
else if (!strcasecmp(flagname,"rx")) return DEBUG_PACKETRX;
else if (!strcasecmp(flagname,"tx")) return DEBUG_PACKETTX;

View File

@ -150,7 +150,6 @@ void TIMING_PAUSE();
/* Limit packet payloads to minimise packet loss of big packets in mesh networks */
#define MAX_DATA_BYTES 256
extern int debug;
extern int dnatimeout;
extern int hlr_size;
extern unsigned char *hlr;
@ -764,10 +763,10 @@ extern overlay_txqueue overlay_tx[OQ_MAX];
#define LOG_LEVEL_ERROR (3)
#define LOG_LEVEL_FATAL (4)
int start_logging();
extern unsigned int debug;
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);
unsigned int debugFlagMask(const char *flagname);
char *catv(const char *data, char *buf, size_t len);
int dump(char *name,unsigned char *addr,int len);
@ -776,9 +775,9 @@ int dump(char *name,unsigned char *addr,int len);
const char *trimbuildpath(const char *s);
#define LOGF(L,F,...) logMessage(L, __FILE__, __LINE__, __FUNCTION__, F, ##__VA_ARGS__)
#define LOGF(L,F,...) (logMessage(L, __FILE__, __LINE__, __FUNCTION__, F, ##__VA_ARGS__))
#define FATALF(F,...) do { logMessage(LOG_LEVEL_FATAL, __FILE__, __LINE__, __FUNCTION__, F, ##__VA_ARGS__); exit(-1); } while (1)
#define FATALF(F,...) do { LOGF(LOG_LEVEL_FATAL, F, ##__VA_ARGS__); exit(-1); } while (1)
#define FATAL(X) FATALF("%s", (X))
#define FATAL_perror(X) FATALF("%s: %s [errno=%d]", (X), strerror(errno), errno)
@ -787,14 +786,14 @@ const char *trimbuildpath(const char *s);
#define WHYNULL(X) (LOGF(LOG_LEVEL_ERROR, "%s", X), NULL)
#define WHY_perror(X) WHYF("%s: %s [errno=%d]", (X), strerror(errno), errno)
#define WARNF(F,...) logMessage(LOG_LEVEL_WARN, __FILE__, __LINE__, __FUNCTION__, F, ##__VA_ARGS__)
#define WARNF(F,...) LOGF(LOG_LEVEL_WARN, F, ##__VA_ARGS__)
#define WARN(X) WARNF("%s", (X))
#define WARN_perror(X) WARNF("%s: %s [errno=%d]", (X), strerror(errno), errno)
#define INFOF(F,...) logMessage(LOG_LEVEL_INFO, __FILE__, __LINE__, __FUNCTION__, F, ##__VA_ARGS__)
#define INFOF(F,...) LOGF(LOG_LEVEL_INFO, F, ##__VA_ARGS__)
#define INFO(X) INFOF("%s", (X))
#define DEBUGF(F,...) logMessage(LOG_LEVEL_DEBUG, __FILE__, __LINE__, __FUNCTION__, F, ##__VA_ARGS__)
#define DEBUGF(F,...) LOGF(LOG_LEVEL_DEBUG, F, ##__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")
@ -1059,6 +1058,7 @@ int overlay_saw_mdp_containing_frame(int interface,overlay_frame *f,long long no
#include "nacl.h"
#define DEBUG_ALL (~0)
#define DEBUG_PACKETRX (1 << 0)
#define DEBUG_OVERLAYINTERFACES (1 << 1)
#define DEBUG_VERBOSE (1 << 2)

View File

@ -39,6 +39,7 @@ setup_servald() {
ln -f -s "$servald_build_executable" $servald
unset SERVALD_OUTPUT_DELIMITER
unset SERVALD_SERVER_START_DELAY
servald_instances_dir="$SERVALD_VAR/instance"
set_instance +Z
}
@ -101,11 +102,11 @@ set_instance() {
+[A-Z])
instance_name="${1#+}"
tfw_log "# set instance = $instance_name"
export instance_dir="$TFWTMP/instance/$instance_name"
export instance_dir="${servald_instances_dir?:}/$instance_name"
mkdir -p "$instance_dir"
export instance_servald_log="$instance_dir/servald.log"
export SERVALINSTANCE_PATH="$instance_dir/servald"
export instance_servald_pidfile="$SERVALINSTANCE_PATH/servald.pid"
instance_servald_log="$instance_dir/servald.log"
instance_servald_pidfile="$SERVALINSTANCE_PATH/servald.pid"
;;
*)
error "malformed instance name argument, must be in form +[A-Z]"
@ -129,13 +130,12 @@ setup_servald_so() {
start_servald_server() {
push_instance
set_instance_fromarg "$1" && shift
executeOk $servald config set logfile "$instance_servald_log"
# Start servald server
local -a before_pids
local -a after_pids
get_servald_pids before_pids
tfw_log "# before_pids=$before_pids"
executeOk $servald start "$@"
SERVALD_LOG_FILE="$instance_servald_log" executeOk $servald start "$@"
extract_stdout_keyvalue start_instance_path instancepath '.*'
extract_stdout_keyvalue start_pid pid '[0-9]\+'
assert [ "$start_instance_path" = "$SERVALINSTANCE_PATH" ]
@ -269,7 +269,7 @@ assert_servald_server_pidfile() {
# - stop all servald server process instances in an orderly fashion
stop_all_servald_servers() {
push_instance
if pushd "$TFWTMP/instance" >/dev/null; then
if pushd "${servald_instances_dir?:}" >/dev/null; then
for name in *; do
set_instance "+$name"
get_servald_server_pidfile && stop_servald_server
@ -368,7 +368,7 @@ assert_servald_server_no_errors() {
# - assert that all instances of servald server logs contain no errors
assert_all_servald_servers_no_errors() {
push_instance
if pushd "$TFWTMP/instance" >/dev/null; then
if pushd "${servald_instances_dir?:}" >/dev/null; then
for name in *; do
set_instance "+$name"
assertGrep --matches=0 --message="stderr of $servald_basename $instance_name contains no error messages" "$instance_servald_log" '^ERROR:'