aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSergey Poznyakoff <gray@gnu.org.ua>2013-07-22 14:37:06 +0300
committerSergey Poznyakoff <gray@gnu.org.ua>2013-07-22 14:37:06 +0300
commit7ca01d78447f87d7b61c26b0e3dba6b91296e471 (patch)
tree7d5f6ec12304ec6e6f2dac441466d090a92ebfdd
parent9f40809dc638ea0157fac25e4d8e7a5fa338797c (diff)
downloadvmod-tbf-7ca01d78447f87d7b61c26b0e3dba6b91296e471.tar.gz
vmod-tbf-7ca01d78447f87d7b61c26b0e3dba6b91296e471.tar.bz2
Improve debugging
* src/vmod_tbf.c (debugprt): Declare unconditionally. (debug): Rewrite macro. All uses changed, (debug_level): New static. (tbf_disabled): New static. (MKW_DEBUG): New keyword code. (mode_kw_tab): New keyword "debug=". (tbf_open_internal): Handle "debug=" keyword. Set tbf_disabled if unable to open database. (tbf_open): Return immediately if tbf_disabled is set. (vmod_close): Clear tbf_disabled.
-rw-r--r--src/vmod_tbf.c52
1 files changed, 35 insertions, 17 deletions
diff --git a/src/vmod_tbf.c b/src/vmod_tbf.c
index 4c95272..88c9890 100644
--- a/src/vmod_tbf.c
+++ b/src/vmod_tbf.c
@@ -27,4 +27,4 @@
-#ifdef VMODDEBUG
-# include <stdarg.h>
+static int debug_level;
+
static void
@@ -37,6 +37,3 @@ debugprt(const char *fmt, ...)
}
-#define debug(c) debugprt c
-#else
-# define debug(c)
-#endif
+#define debug(n,c) do { if (debug_level>=(n)) debugprt c; } while (0)
@@ -50,2 +47,3 @@ static uint64_t autosync_max;
static uint64_t autosync_count;
+static int tbf_disabled;
@@ -73,3 +71,4 @@ enum {
MKW_MODE,
- MKW_SYNC
+ MKW_SYNC,
+ MKW_DEBUG,
};
@@ -82,2 +81,3 @@ static struct mode_kw mode_kw_tab[] = {
{ S(sync=), MKW_SYNC },
+ { S(debug=), MKW_DEBUG },
{ NULL }
@@ -97,3 +97,2 @@ tbf_open_internal(const char *mode)
tbf_set_db_name(LOCALSTATEDIR "/tbf.db");
- debug(("opening database %s", dbname));
@@ -148,4 +147,17 @@ tbf_open_internal(const char *mode)
autosync_count = 0;
+ mode = p;
}
break;
+
+ case MKW_DEBUG:
+ errno = 0;
+ n = strtoul(mode, &p, 10);
+ if (errno || !(*p == 0 || *p == ';')) {
+ syslog(LOG_DAEMON|LOG_ERR,
+ "invalid debug level near %s", p);
+ mode += strlen(mode);
+ } else {
+ debug_level = n;
+ mode = p;
+ }
}
@@ -163,2 +175,3 @@ tbf_open_internal(const char *mode)
+ debug(1, ("opening database %s", dbname));
rc = db->open(db, NULL, dbname, NULL, DB_HASH, flags, filemode);
@@ -169,2 +182,3 @@ tbf_open_internal(const char *mode)
db = NULL;
+ tbf_disabled = 1;
}
@@ -175,2 +189,4 @@ tbf_open(const char *mode)
{
+ if (tbf_disabled)
+ return 0;
pthread_mutex_lock(&mutex);
@@ -202,5 +218,6 @@ vmod_close(struct sess *sp)
if (db) {
- debug(("closing database %s", dbname));
+ debug(1, ("closing database %s", dbname));
db->close(db, 0);
db = NULL;
+ tbf_disabled = 0;
}
@@ -212,3 +229,3 @@ vmod_sync(struct sess *sp)
if (db) {
- debug(("synchronizing database"));
+ debug(1, ("synchronizing database"));
db->sync(db, 0);
@@ -254,3 +271,3 @@ vmod_rate(struct sess *sp, const char *key, int cost, double t, int burst_size)
- debug(("entering rate(%s,%d,%g,%d)", key, cost, t, burst_size));
+ debug(2, ("entering rate(%s,%d,%g,%d)", key, cost, t, burst_size));
@@ -301,5 +318,5 @@ vmod_rate(struct sess *sp, const char *key, int cost, double t, int burst_size)
- debug(("found, elapsed time: %"PRIu64" us, "
- "new tokens: %"PRIu64", total: %lu ",
- elapsed, tokens, (unsigned long) bkt->tokens));
+ debug(2, ("found, elapsed time: %"PRIu64" us, "
+ "new tokens: %"PRIu64", total: %lu ",
+ elapsed, tokens, (unsigned long) bkt->tokens));
break;
@@ -322,6 +339,7 @@ vmod_rate(struct sess *sp, const char *key, int cost, double t, int burst_size)
bkt->tokens -= cost;
- debug(("tbf_rate matched %s", key));
+ debug(2, ("tbf_rate matched %s, tokens left %z", key,
+ bkt->tokens));
} else {
res = 0;
- debug(("tbf_rate overlimit on %s", key));
+ debug(1, ("tbf_rate overlimit on %s", key));
}
@@ -342,3 +360,3 @@ vmod_rate(struct sess *sp, const char *key, int cost, double t, int burst_size)
if (autosync_max && ++autosync_count >= autosync_max) {
- debug(("synchronizing database"));
+ debug(1, ("synchronizing database"));
db->sync(db, 0);

Return to:

Send suggestions and report system problems to the System administrator.