summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTing-Wei Lan <lantw44@gmail.com>2014-06-20 16:11:31 +0800
committerTing-Wei Lan <lantw44@gmail.com>2014-06-20 16:11:31 +0800
commit312b7fb8672c546f509674941af46ca56d8bf735 (patch)
treeac4696af24400b19388c5c77130f1d137cae208c
parentdc2681769da4e2ab4ec6a8beae0d3b29cb578f0e (diff)
downloadfastalg-nfqueue-312b7fb8672c546f509674941af46ca56d8bf735.tar.gz
fastalg-nfqueue-312b7fb8672c546f509674941af46ca56d8bf735.tar.zst
fastalg-nfqueue-312b7fb8672c546f509674941af46ca56d8bf735.zip
Support debug level setting
-rw-r--r--src/falgnfq-loop.c14
-rw-r--r--src/falgnfq-main.c19
-rw-r--r--src/falgnfq-private.h8
3 files changed, 27 insertions, 14 deletions
diff --git a/src/falgnfq-loop.c b/src/falgnfq-loop.c
index 8ea5de1..10e2cf0 100644
--- a/src/falgnfq-loop.c
+++ b/src/falgnfq-loop.c
@@ -169,7 +169,7 @@ static void queue_verdict (FalgnfqLoop *loop,
static int before_get_param (FalgprotoPacket *pkt,
FalgnfqLoop *loop, const char *caller_name) {
- if_debug {
+ if_debug (1) {
if (loop->proto.printer == NULL) {
warning (" %s: application layer data printer or debugger "
"is not available", caller_name);
@@ -362,7 +362,7 @@ static int queue_cb (const struct nlmsghdr *nlh, void *loop_generic) {
pkt_len = mnl_attr_get_payload_len (attr[NFQA_PAYLOAD]);
pkt_payload = mnl_attr_get_payload (attr[NFQA_PAYLOAD]);
- if_debug {
+ if_debug (2) {
char dump_file[50]= {0};
snprintf(dump_file, 50, "packet_%" PRIu32 "_dumpfile", pkt_id);
if(falgnfq_dump_payload(dump_file, pkt_payload, pkt_len) == pkt_len){
@@ -425,7 +425,7 @@ static int queue_cb (const struct nlmsghdr *nlh, void *loop_generic) {
iph->protocol == IPPROTO_ICMPV6 ? "layer 4 is ICMPv6" :
"unknown layer 4 protocol");
- if_debug {
+ if_debug (1) {
char print_buf[2048];
ip_snprintf (print_buf, 2048, iph);
debug (" packet id %" PRIu32 ", %s", pkt_id, print_buf);
@@ -444,7 +444,7 @@ static int queue_cb (const struct nlmsghdr *nlh, void *loop_generic) {
error (" packet id %" PRIu32 ", truncated IPv6 packet", pkt_id);
}
- if_debug {
+ if_debug (1) {
char print_buf[2048];
nfq_ip6_snprintf (print_buf, 2048, ip6h);
debug (" packet id %" PRIu32 ", %s", pkt_id, print_buf);
@@ -474,7 +474,7 @@ static int queue_cb (const struct nlmsghdr *nlh, void *loop_generic) {
goto free_pktb;
}
- if_debug {
+ if_debug (1) {
char print_buf[2048];
nfq_tcp_snprintf (print_buf, 2048, th);
debug (" packet id %" PRIu32 ", %s", pkt_id, print_buf);
@@ -502,7 +502,7 @@ static int queue_cb (const struct nlmsghdr *nlh, void *loop_generic) {
goto free_pktb;
}
- if_debug {
+ if_debug (1) {
char print_buf[2048];
nfq_udp_snprintf (print_buf, 2048, uh);
debug (" packet id %" PRIu32 ", %s", pkt_id, print_buf);
@@ -636,7 +636,7 @@ int falgnfq_loop_run (FalgnfqLoop *loop) {
debug ("FalgnfqLoop %p run: mnl_cb_run", loop);
if (mnl_cb_run (pkt, pkt_len, 0, loop->portid, queue_cb, loop) < 0) {
error ("mnl_cb_run: %s", ERRMSG);
- if_debug {
+ if_debug (1) {
error ("DEVELOPER_MODE: UNEXPECTED ERROR, EXIT NOW!");
return -1;
}
diff --git a/src/falgnfq-main.c b/src/falgnfq-main.c
index 39706d8..6c35276 100644
--- a/src/falgnfq-main.c
+++ b/src/falgnfq-main.c
@@ -14,7 +14,7 @@
#include <time.h>
-int falgnfq_ndebug;
+unsigned int falgnfq_debug;
volatile int falgnfq_exit;
static void falgnfq_exit_setter (int signo) {
@@ -42,10 +42,23 @@ int main (int argc, char *argv[]) {
}
#ifdef NDEBUG
- falgnfq_ndebug = 1;
+ falgnfq_debug = 0;
#else
if (getenv ("NDEBUG")) {
- falgnfq_ndebug = 1;
+ falgnfq_debug = 0;
+ } else {
+ char *debug_level_str = getenv ("FALGNFQ_DEBUG");
+ if (debug_level_str) {
+ int debug_level = atoi (debug_level_str);
+ falgnfq_debug = debug_level < 0 ? 1 : (unsigned int)debug_level;
+ } else {
+ falgnfq_debug = 1;
+ }
+
+ debug (" ::: DEVELOPER_MODE :::");
+ debug ("Set NDEBUG in your environment to suppress all debug meesage");
+ debug ("Use FALGNFQ_DEBUG to set the debug level");
+ debug ("Current debug level is %u", falgnfq_debug);
}
struct sigaction sa_int;
diff --git a/src/falgnfq-private.h b/src/falgnfq-private.h
index 5300d7f..7fcce22 100644
--- a/src/falgnfq-private.h
+++ b/src/falgnfq-private.h
@@ -13,13 +13,13 @@
#ifndef NDEBUG
# define debug(...) \
- if (!falgnfq_ndebug) { \
+ if (falgnfq_debug) { \
message_with_prefix ("DEBUG: ", __VA_ARGS__); \
}
-# define if_debug if (!falgnfq_ndebug)
+# define if_debug(level) if (falgnfq_debug >= (level))
#else
# define debug(...)
-# define if_debug while (0)
+# define if_debug(level) if (0)
#endif
#define message(...) printf (__VA_ARGS__); putchar ('\n')
@@ -29,7 +29,7 @@
// DO NOT USE this variable directly!
// Please use above debug and if_debug macro instead.
-extern int falgnfq_ndebug;
+extern unsigned int falgnfq_debug;
// Exit