summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBent Bisballe Nyeng <deva@aasimon.org>2012-12-07 14:50:07 +0100
committerBent Bisballe Nyeng <deva@aasimon.org>2012-12-07 14:50:07 +0100
commit26b8b48b4952249a13855a21946955a10c77837f (patch)
treed5b07924fa90e75784ebb29afa1eeca6473ce697
parente8f12bfd6d9fa3e813c3f8d4f6102a6b1c7037ab (diff)
Add filter functionality.
-rw-r--r--Makefile2
-rw-r--r--Makefile.files6
-rw-r--r--debug.c176
-rw-r--r--debug.h9
-rw-r--r--debug_filter.c123
-rw-r--r--debug_filter.h36
-rw-r--r--main.c11
7 files changed, 201 insertions, 162 deletions
diff --git a/Makefile b/Makefile
index fc5b439..ef8849c 100644
--- a/Makefile
+++ b/Makefile
@@ -1,5 +1,5 @@
include Makefile.files
all:
- gcc ${DEBUG_SOURCES} main.c -o debug
+ gcc ${DEBUG_SOURCES} main.c -o debug -DWITH_DBG_FILTER
# -DWITH_DBG_MUTEX \ No newline at end of file
diff --git a/Makefile.files b/Makefile.files
index e707d13..4781f2c 100644
--- a/Makefile.files
+++ b/Makefile.files
@@ -4,7 +4,9 @@
#
DEBUG_SOURCES = \
- debug.c
+ debug.c \
+ debug_filter.c
DEBUG_EXTRA_DIST = \
- debug.h \ No newline at end of file
+ debug.h \
+ debug_filter.h \ No newline at end of file
diff --git a/debug.c b/debug.c
index 22c0000..a69e673 100644
--- a/debug.c
+++ b/debug.c
@@ -41,6 +41,10 @@
#include <pthread.h>
#endif
+#ifdef WITH_DBG_FILTER
+#include "debug_filter.h"
+#endif
+
struct {
unsigned int flags;
#ifdef WITH_DBG_MUTEX
@@ -114,6 +118,11 @@ dbg_status_t dbg_init(unsigned int flags, ...)
dbg_config.file_fd = open(filename, O_CREAT | O_RDWR, 0777);
}
break;
+#ifdef WITH_DBG_FILTER
+ case DBG_OPTION_FILTER:
+ dbg_filter_parse((const char*)va_arg(vl, char*));
+ break;
+#endif
default:
status = DBG_STATUS_UNKNOWN_OPTION;
printf("option: %x\n", option);
@@ -138,9 +147,16 @@ void dbg_close()
dbg_mutex_close();
}
-static const char * const debug_class_str[] =
+const char * const debug_class_str[] =
{ "fixme", "err", "warn", "info", "debug" };
+/*
+static unsigned int gettid()
+{
+ return (unsigned int)pthread_self();
+}
+*/
+
static int dbg_create_header(char *hdr, size_t size)
{
time_t rawtime = time(NULL);
@@ -178,6 +194,10 @@ int __debug(const char *func, const int line,
dbg_mutex_lock();
+#ifdef WITH_DBG_FILTER
+ if(!dbg_filter_enabled(cl, ch)) goto done;
+#endif
+
//
// Generate message
//
@@ -218,157 +238,3 @@ int __debug(const char *func, const int line,
return result;
}
-#if 0
-
-#include <stdlib.h>
-#include <stdarg.h>
-#include <string.h>
-//#include <pthread.h>
-#include <string>
-#include <time.h>
-
-//#include "mutex.h"
-
-//static Mutex mutex;
-
-static unsigned int gettid()
-{
- return 0;//(unsigned int)pthread_self();
-}
-
-static FILE *logfp = stderr;
-
-#define NELEM(x) (sizeof(x)/sizeof((x)[0]))
-struct __debug_channel
-{
- char name[32];
- unsigned flags;
-};
-
-static const char * const debug_class_str[] =
- { "fixme", "err", "warn", "info", "debug" };
-
-#define __DEBUG_CHANNEL_MAX 256
-
-static struct __debug_channel debug_channel[__DEBUG_CHANNEL_MAX];
-static unsigned n_debug_channel = 0;
-static unsigned debug_flags = (1 << __class_err) | (1 << __class_fixme);
-
-static int __debug_enabled(const enum __debug_class cl, const char *ch)
-{
- unsigned i;
- for(i = 0; i < n_debug_channel; i++) {
- if(!strcmp(ch, debug_channel[i].name)) {
- return (debug_channel[i].flags & (1 << cl)) != 0;
- }
- }
- return debug_flags & (1 << cl);
-}
-
-int __debug(const char *func, const int line,
- const enum __debug_class cl,
- const char *ch, const char *fmt, ...)
-{
- time_t rawtime = time(NULL);
- struct tm *t = localtime(&rawtime);
-
- // MutexAutolock m(mutex);
- int ret = 0;
- if(__debug_enabled(cl, ch)) {
- if((unsigned)cl < NELEM(debug_class_str))
- ret += fprintf(logfp, "%d-%02d-%02d %02d:%02d:%02d %u %s:%s:%s:%d ",
- t->tm_year + 1900,
- t->tm_mon + 1,
- t->tm_mday,
- t->tm_hour,
- t->tm_min,
- t->tm_sec,
- gettid(),
- debug_class_str[(unsigned)cl], ch, func, line);
- if(fmt) {
- va_list va;
- va_start(va, fmt);
- ret += vfprintf(logfp, fmt, va);
- va_end(va);
- }
- }
- if(ret){
- fflush(logfp);
- }
- return ret;
-}
-
-void debug_init(FILE *fp)
-{
- // mutex.name = "debug";
- // MutexAutolock m(mutex);
- logfp = fp;
-}
-
-/*
- * fmt := [set[,set]*]*
- * set := [+-]channel
- * | class[+-]channel
- * | [+-]all
- */
-void debug_parse(const char *fmt)
-{
- // MutexAutolock m(mutex);
- char *s;
- char *next;
- char *opt;
-
- if(!(s = strdup(fmt))) return;
-
- for(opt = s; opt; opt = next) {
- int set = 0;
- int clr = 0;
- unsigned i;
- if((next = strchr(opt, ','))) *next++ = '\0';
- char *p = opt + strcspn(opt, "+-");
- if(!*p) p = opt; // All chars -> a channel name
- if(p > opt) {
- // we have a class
- for(i = 0; i < NELEM(debug_class_str); i++) {
- int n = strlen(debug_class_str[i]);
- if(n != (p - opt)) continue;
- if(!memcmp(opt, debug_class_str[i], n)) {
- // Found the class
- if(*p == '+')
- set = 1 << i;
- else
- clr = 1 << i;
- break;
- }
- }
- if(i == NELEM(debug_class_str)) continue;
- } else {
- if(*p == '-')
- clr = ~0;
- else
- set = ~0;
- }
- if(*p == '+' || *p == '-') p++;
- if(!*p) continue;
- if(!strcmp("all", p)) {
- debug_flags = (debug_flags & ~clr) | set;
- } else {
- if(strlen(p) >= sizeof(debug_channel[0].name)) continue;
- for(i = 0; i < n_debug_channel; i++) {
- if(!strcmp(p, debug_channel[i].name)) {
- debug_channel[i].flags = (debug_channel[i].flags & ~clr) | set;
- break;
- }
- }
- if(i == n_debug_channel && n_debug_channel < __DEBUG_CHANNEL_MAX) {
- strcpy(debug_channel[i].name, p);
- debug_channel[i].flags = (debug_flags & ~clr) | set;
- n_debug_channel++;
- }
- }
- }
- free(s);
-}
-
-
-#endif
diff --git a/debug.h b/debug.h
index 1241f67..5044156 100644
--- a/debug.h
+++ b/debug.h
@@ -89,8 +89,15 @@ enum DBG_OPTION {
#endif
/**
- *
+ * Filter option. Argument is a const char *.
+ * fmt := [set[,set]*]*
+ * set := [+-]channel
+ * | class[+-]channel
+ * | [+-]all
*/
+#ifdef WITH_DBG_FILTER
+ DBG_OPTION_FILTER,
+#endif
};
typedef enum {
diff --git a/debug_filter.c b/debug_filter.c
new file mode 100644
index 0000000..f93b4c9
--- /dev/null
+++ b/debug_filter.c
@@ -0,0 +1,123 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set et sw=2 ts=2: */
+/***************************************************************************
+ * debug_filter.c
+ *
+ * Fri Dec 7 14:24:27 CET 2012
+ * Copyright 2012 Bent Bisballe Nyeng
+ * deva@aasimon.org
+ ****************************************************************************/
+
+/*
+ * This file is part of Debug Module.
+ *
+ * Debug Module is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Debug Module is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Debug Module; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+#include "debug_filter.h"
+
+#include <string.h>
+#include <stdlib.h>
+
+#define NELEM(x) (sizeof(x)/sizeof((x)[0]))
+struct __debug_channel
+{
+ char name[32];
+ unsigned flags;
+};
+
+#define __DEBUG_CHANNEL_MAX 256
+
+static struct __debug_channel debug_channel[__DEBUG_CHANNEL_MAX];
+static unsigned n_debug_channel = 0;
+static unsigned debug_flags = (1 << __class_err) | (1 << __class_fixme);
+
+int dbg_filter_enabled(const enum __debug_class cl, const char *ch)
+{
+ unsigned i;
+ for(i = 0; i < n_debug_channel; i++) {
+ if(!strcmp(ch, debug_channel[i].name)) {
+ return (debug_channel[i].flags & (1 << cl)) != 0;
+ }
+ }
+ return debug_flags & (1 << cl);
+}
+
+extern const char * const *debug_class_str;
+
+/*
+ * fmt := [set[,set]*]*
+ * set := [+-]channel
+ * | class[+-]channel
+ * | [+-]all
+ */
+int dbg_filter_parse(const char *filter)
+{
+ char *s;
+ char *next;
+ char *opt;
+
+ if(!(s = strdup(filter))) return 1;
+
+ for(opt = s; opt; opt = next) {
+ int set = 0;
+ int clr = 0;
+ unsigned i;
+ if((next = strchr(opt, ','))) *next++ = '\0';
+ char *p = opt + strcspn(opt, "+-");
+ if(!*p) p = opt; // All chars -> a channel name
+ if(p > opt) {
+ // we have a class
+ for(i = 0; i < NELEM(debug_class_str); i++) {
+ int n = strlen(debug_class_str[i]);
+ if(n != (p - opt)) continue;
+ if(!memcmp(opt, debug_class_str[i], n)) {
+ // Found the class
+ if(*p == '+')
+ set = 1 << i;
+ else
+ clr = 1 << i;
+ break;
+ }
+ }
+ if(i == NELEM(debug_class_str)) continue;
+ } else {
+ if(*p == '-')
+ clr = ~0;
+ else
+ set = ~0;
+ }
+ if(*p == '+' || *p == '-') p++;
+ if(!*p) continue;
+ if(!strcmp("all", p)) {
+ debug_flags = (debug_flags & ~clr) | set;
+ } else {
+ if(strlen(p) >= sizeof(debug_channel[0].name)) continue;
+ for(i = 0; i < n_debug_channel; i++) {
+ if(!strcmp(p, debug_channel[i].name)) {
+ debug_channel[i].flags = (debug_channel[i].flags & ~clr) | set;
+ break;
+ }
+ }
+ if(i == n_debug_channel && n_debug_channel < __DEBUG_CHANNEL_MAX) {
+ strcpy(debug_channel[i].name, p);
+ debug_channel[i].flags = (debug_flags & ~clr) | set;
+ n_debug_channel++;
+ }
+ }
+ }
+ free(s);
+
+ return 0;
+}
diff --git a/debug_filter.h b/debug_filter.h
new file mode 100644
index 0000000..d312d56
--- /dev/null
+++ b/debug_filter.h
@@ -0,0 +1,36 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set et sw=2 ts=2: */
+/***************************************************************************
+ * debug_filter.h
+ *
+ * Fri Dec 7 14:24:27 CET 2012
+ * Copyright 2012 Bent Bisballe Nyeng
+ * deva@aasimon.org
+ ****************************************************************************/
+
+/*
+ * This file is part of Debug Module.
+ *
+ * Debug Module is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Debug Module is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Debug Module; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+#ifndef __DEBUG_MODULE_DEBUG_FILTER_H__
+#define __DEBUG_MODULE_DEBUG_FILTER_H__
+
+#include "debug.h"
+
+int dbg_filter_enabled(const enum __debug_class cl, const char *ch);
+int dbg_filter_parse(const char *filter);
+
+#endif/*__DEBUG_MODULE_DEBUG_FILTER_H__*/
diff --git a/main.c b/main.c
index 8e0efca..97f6d15 100644
--- a/main.c
+++ b/main.c
@@ -42,13 +42,16 @@ int main(int argc, char *argv[])
}
*/
dbg_status_t status = dbg_init(//DBG_FLAG_USE_MUTEX |
- DBG_FLAG_OUTPUT_TO_FILE |
+ //DBG_FLAG_OUTPUT_TO_FILE |
//DBG_FLAG_OUTPUT_TO_FD |
- //DBG_FLAG_OUTPUT_TO_STDOUT |
+ DBG_FLAG_OUTPUT_TO_STDOUT |
//DBG_FLAG_OUTPUT_TO_STDERR |
+ DBG_FLAG_USE_FILTER |
0,
//DBG_OPTION_FD, fd,
- DBG_OPTION_FILENAME, "/tmp/my2.log",
+ //DBG_OPTION_FILENAME, "/tmp/my2.log",
+ DBG_OPTION_FILTER, "-all,+foo",
+
DBG_OPTION_END);
if(status != DBG_STATUS_OK) {
@@ -59,6 +62,8 @@ int main(int argc, char *argv[])
INFO(example, "We are up and running");
DEBUG(example, "Or are we %d?", 42);
+
+ DEBUG(foo, "Or are we %d?", 42);
dbg_close();