summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJiri Olsa <Jiri Olsa jolsa@redhat.com>2010-04-18 21:55:21 +0200
committerJiri Olsa <Jiri Olsa jolsa@redhat.com>2010-04-18 21:55:21 +0200
commit3112afc7a545b79917c17cfbbe3cd2211ff8ca62 (patch)
tree96bb409966a3bab9dd859811e0ed58597235740f
parent99f24c4e89b78a5926396f5909a1739f6fbce482 (diff)
downloadtsnif-3112afc7a545b79917c17cfbbe3cd2211ff8ca62.tar.gz
tsnif-3112afc7a545b79917c17cfbbe3cd2211ff8ca62.tar.xz
tsnif-3112afc7a545b79917c17cfbbe3cd2211ff8ca62.zip
finalizing the debug/verbose refactoring
-rw-r--r--src/debug.c46
-rw-r--r--src/debug.h80
-rw-r--r--src/misc.h9
-rw-r--r--src/tsnif-replay.c20
-rw-r--r--src/tsnif.c21
-rw-r--r--src/tsnifd.c32
6 files changed, 185 insertions, 23 deletions
diff --git a/src/debug.c b/src/debug.c
new file mode 100644
index 0000000..a0e4e93
--- /dev/null
+++ b/src/debug.c
@@ -0,0 +1,46 @@
+
+#include <getopt.h>
+#include <asm/errno.h>
+#include <string.h>
+
+#include "debug.h"
+
+uint32_t tsnif_debug;
+
+static int enable_flag(int debug, char *flag)
+{
+ int i;
+
+ for(i = 0; i < FLAGS_MAX; i++) {
+ char *f = debug_flag_name[i];
+
+ if (!strcmp(flag, f))
+ break;
+ }
+
+ if (FLAGS_MAX != i) {
+ if (!debug)
+ i += 16;
+
+ tsnif_debug |= BIT2NUM(i);
+ return 0;
+ }
+
+ if (!strcmp(flag, "all")) {
+ tsnif_debug |= debug ? 0xffff : 0xffff0000;
+ return 0;
+ }
+
+ return -EINVAL;
+}
+
+int debug_parse_flags(int debug, char *flags)
+{
+ char *flag;
+ int ret = 0, in = 0;
+
+ for(;(flag = strtok(flags, ",")) ;flags = NULL, in++)
+ ret |= enable_flag(debug, flag);
+
+ return in ? ret : -EINVAL;
+}
diff --git a/src/debug.h b/src/debug.h
new file mode 100644
index 0000000..6e29935
--- /dev/null
+++ b/src/debug.h
@@ -0,0 +1,80 @@
+#ifndef DEBUG_H
+#define DEBUG_H
+
+#include <unistd.h>
+#include <sys/syscall.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdarg.h>
+
+extern uint32_t tsnif_debug;
+
+#define BIT2NUM(bit) (1 << bit)
+
+enum {
+ DEBUG_STORAGE = BIT2NUM(0),
+ DEBUG_FSM = BIT2NUM(1),
+ DEBUG_INTF = BIT2NUM(2),
+ DEBUG_TRANS = BIT2NUM(3),
+ DEBUG_APP = BIT2NUM(4),
+ FLAGS_MAX = 5,
+ VERBOSE_STORAGE = BIT2NUM(16),
+ VERBOSE_FSM = BIT2NUM(17),
+ VERBOSE_INTF = BIT2NUM(18),
+ VERBOSE_TRANS = BIT2NUM(19),
+ VERBOSE_APP = BIT2NUM(20),
+};
+
+static char *debug_flag_name[FLAGS_MAX] = {
+ "storage",
+ "fsm",
+ "intf",
+ "trans",
+ "app",
+};
+
+static inline void debug_print(int flag, char *file, int line,
+ const char *func, char *fmt, ...)
+{
+#define MAXBUF 256
+ char buf[MAXBUF];
+ int name_idx;
+ va_list ap;
+
+ if (!(flag & tsnif_debug))
+ return;
+
+ name_idx = (ffs(flag) % 16) - 1;
+
+ if (flag < BIT2NUM(16)) {
+ snprintf(buf, MAXBUF, "%s [%3d:%s:%05d %s] %s",
+ debug_flag_name[name_idx],
+ (pid_t) syscall(SYS_gettid),
+ file,
+ line,
+ func,
+ fmt);
+ } else {
+ snprintf(buf, MAXBUF, "[%s] %s",
+ debug_flag_name[name_idx],
+ fmt);
+ }
+
+ va_start(ap, fmt);
+ vfprintf(stdout, buf, ap);
+ va_end(ap);
+}
+
+
+#define TSNIF_DEBUG(flag, fmt, ...) \
+ debug_print(DEBUG_##flag, __FILE__, __LINE__, __FUNCTION__, \
+ fmt, ##__VA_ARGS__)
+
+#define TSNIF_VERBOSE(flag, fmt, ...) \
+ debug_print(VERBOSE_##flag, __FILE__, __LINE__, __FUNCTION__, \
+ fmt, ##__VA_ARGS__)
+
+int debug_parse_flags(int debug, char *flags);
+
+#endif /* DEBUG_H */
diff --git a/src/misc.h b/src/misc.h
index a0b6012..2735e63 100644
--- a/src/misc.h
+++ b/src/misc.h
@@ -1,6 +1,15 @@
#ifndef MISC_H
#define MISC_H
+#include <unistd.h>
+#include "autoconf.h"
+
int set_term(int set_init);
+static inline void print_version(char *prog)
+{
+ printf("%s "CONFIG_TSNIF_VER"\n", prog);
+ _exit(0);
+}
+
#endif /* !MISC_H */
diff --git a/src/tsnif-replay.c b/src/tsnif-replay.c
index bac2b98..e6c5b0d 100644
--- a/src/tsnif-replay.c
+++ b/src/tsnif-replay.c
@@ -34,17 +34,21 @@ static void sig_handler(int sig)
static int get_args(int argc, char **argv)
{
+ int ret = 0;
+
while (1) {
int c;
int option_index = 0;
static struct option long_options[] = {
{"file", required_argument, 0, 'f'},
- {"debug", no_argument, 0, 'd'},
+ {"version", no_argument, 0, 'V'},
+ {"verbose", required_argument, 0, 'v'},
+ {"debug", required_argument, 0, 'd'},
{"help", no_argument, 0, 'h'},
{0, 0, 0, 0}
};
- c = getopt_long(argc, argv, "f:dh",
+ c = getopt_long(argc, argv, "f:d:v:hV",
long_options, &option_index);
if (c == -1)
@@ -55,8 +59,16 @@ static int get_args(int argc, char **argv)
filename = optarg;
break;
+ case 'V':
+ print_version(argv[0]);
+ break;
+
+ case 'v':
+ ret = debug_parse_flags(0, optarg);
+ break;
+
case 'd':
- tsnif_debug = 1;
+ ret = debug_parse_flags(1, optarg);
break;
case 'h':
@@ -68,7 +80,7 @@ static int get_args(int argc, char **argv)
} /* switch(c) */
} /* while(1) */
- return 0;
+ return ret;
}
static struct timeval get_timeout(struct timespec *ts_new,
diff --git a/src/tsnif.c b/src/tsnif.c
index 9cba8e0..ef83561 100644
--- a/src/tsnif.c
+++ b/src/tsnif.c
@@ -123,6 +123,8 @@ static int get_type(char *optarg)
static int get_args(int argc, char **argv)
{
+ int ret = 0;
+
while (1) {
int c;
int option_index = 0;
@@ -130,13 +132,14 @@ static int get_args(int argc, char **argv)
{"type", required_argument, 0, 't'},
{"idx", required_argument, 0, 'i'},
{"store", required_argument, 0, 's'},
- {"version", no_argument, 0, 'v'},
- {"debug", no_argument, 0, 'd'},
+ {"version", no_argument, 0, 'V'},
+ {"debug", required_argument, 0, 'd'},
+ {"verbose", required_argument, 0, 'v'},
{"help", no_argument, 0, 'h'},
{0, 0, 0, 0}
};
- c = getopt_long(argc, argv, "t:i:s:vdh",
+ c = getopt_long(argc, argv, "t:i:s:v:d:hV",
long_options, &option_index);
if (c == -1)
@@ -164,12 +167,16 @@ static int get_args(int argc, char **argv)
store = 1;
break;
+ case 'V':
+ print_version(argv[0]);
+ break;
+
case 'v':
- printf("tsnif "CONFIG_TSNIF_VER"\n");
+ ret = debug_parse_flags(0, optarg);
break;
case 'd':
- tsnif_debug = 1;
+ ret = debug_parse_flags(1, optarg);
break;
case 'h':
@@ -182,9 +189,9 @@ static int get_args(int argc, char **argv)
} /* while(1) */
if ((type == -1) || (idx == -1))
- return -1;
+ return -EINVAL;
- return 0;
+ return ret;
}
static int process_input(void)
diff --git a/src/tsnifd.c b/src/tsnifd.c
index 8a6c8eb..dc90ce6 100644
--- a/src/tsnifd.c
+++ b/src/tsnifd.c
@@ -282,39 +282,35 @@ static void sig_handler(int sig)
static void usage()
{
- printf("tsnifd [-dfvhs]\n");
+ printf("tsnifd [-fhsdv]\n");
_exit(-1);
}
static int get_args(int argc, char **argv)
{
+ int ret = 0;
+
while (1) {
int c;
int option_index = 0;
static struct option long_options[] = {
{"version", no_argument, 0, 'v'},
- {"debug", no_argument, 0, 'd'},
{"foreground", no_argument, 0, 'f'},
{"store-dir", required_argument, 0, 's'},
+ {"version", no_argument, 0, 'V'},
+ {"verbose", required_argument, 0, 'v'},
+ {"debug", required_argument, 0, 'd'},
{"help", no_argument, 0, 'h'},
{0, 0, 0, 0}
};
- c = getopt_long(argc, argv, "t:i:s:vdh",
+ c = getopt_long(argc, argv, "fshd:v:V",
long_options, &option_index);
if (c == -1)
break;
switch (c) {
- case 'v':
- printf("tsnif "CONFIG_TSNIF_VER"\n");
- break;
-
- case 'd':
- tsnif_debug = 1;
- break;
-
case 'f':
foreground = 1;
break;
@@ -323,6 +319,18 @@ static int get_args(int argc, char **argv)
store_dir = optarg;
break;
+ case 'V':
+ print_version(argv[0]);
+ break;
+
+ case 'v':
+ ret = debug_parse_flags(0, optarg);
+ break;
+
+ case 'd':
+ ret = debug_parse_flags(1, optarg);
+ break;
+
case 'h':
usage();
break;
@@ -332,7 +340,7 @@ static int get_args(int argc, char **argv)
} /* switch(c) */
} /* while(1) */
- return 0;
+ return ret;
}
int main(int argc, char **argv)