From 9ad7a183ef3ff2cec22423fa326c139a877c0ef6 Mon Sep 17 00:00:00 2001 From: phk Date: Mon, 10 Jul 2006 20:27:52 +0000 Subject: [PATCH] Add tag names array to libvarnishapi, everybody is going to need it. Implement -i tag[,tag ...] and -x tag[,tag ...] generic arguments. git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@412 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- varnish-cache/bin/varnishlog/varnishlog.c | 18 ++- varnish-cache/include/varnishapi.h | 3 +- varnish-cache/lib/libvarnishapi/shmlog.c | 127 ++++++++++++++++++---- 3 files changed, 113 insertions(+), 35 deletions(-) diff --git a/varnish-cache/bin/varnishlog/varnishlog.c b/varnish-cache/bin/varnishlog/varnishlog.c index 20df2315..11049dec 100644 --- a/varnish-cache/bin/varnishlog/varnishlog.c +++ b/varnish-cache/bin/varnishlog/varnishlog.c @@ -16,11 +16,6 @@ #include "shmlog.h" #include "varnishapi.h" -static const char *tagnames[] = { -#define SLTM(foo) [SLT_##foo] = #foo, -#include "shmlog_tags.h" -#undef SLTM -}; static char * vis_it(unsigned char *p) @@ -69,7 +64,7 @@ order(unsigned char *p, int h_opt) switch (p[0]) { case SLT_VCL_call: sbuf_printf(ob[u], "%02x %3d %4d %-12s", - p[0], p[1], u, tagnames[p[0]]); + p[0], p[1], u, VSL_tags[p[0]]); if (p[1] > 0) { sbuf_cat(ob[u], " <"); sbuf_bcat(ob[u], p + 4, p[1]); @@ -106,7 +101,7 @@ order(unsigned char *p, int h_opt) else if (p[1] > 4 && !memcmp(p + 4, "TTD:", 4)) break; sbuf_printf(ob[u], "%02x %3d %4d %-12s", - p[0], p[1], u, tagnames[p[0]]); + p[0], p[1], u, VSL_tags[p[0]]); if (p[1] > 0) sbuf_cat(ob[u], vis_it(p)); sbuf_cat(ob[u], "\n"); @@ -153,7 +148,7 @@ order(unsigned char *p, int h_opt) } if (v) { sbuf_printf(ob[u], "%02x %3d %4d %-12s", - p[0], p[1], u, tagnames[p[0]]); + p[0], p[1], u, VSL_tags[p[0]]); if (p[1] > 0) { sbuf_cat(ob[u], " <"); sbuf_bcat(ob[u], p + 4, p[1]); @@ -210,7 +205,10 @@ main(int argc, char **argv) vd = VSL_New(); while ((c = getopt(argc, argv, VSL_ARGS "how:")) != -1) { - if (VSL_Arg(vd, c, optarg)) + i = VSL_Arg(vd, c, optarg); + if (i < 0) + exit (1); + if (i > 0) continue; switch (c) { case 'h': @@ -273,7 +271,7 @@ main(int argc, char **argv) } u = (p[2] << 8) | p[3]; printf("%02x %3d %4d %-12s", - p[0], p[1], u, tagnames[p[0]]); + p[0], p[1], u, VSL_tags[p[0]]); if (p[1] > 0) { if (p[0] != SLT_Debug) { diff --git a/varnish-cache/include/varnishapi.h b/varnish-cache/include/varnishapi.h index 914c4416..d81a0938 100644 --- a/varnish-cache/include/varnishapi.h +++ b/varnish-cache/include/varnishapi.h @@ -8,13 +8,14 @@ #define V_DEAD __attribute__ ((noreturn)) /* shmlog.c */ -#define VSL_ARGS "r:" +#define VSL_ARGS "r:i:x:" struct VSL_data; struct VSL_data *VSL_New(void); int VSL_OpenLog(struct VSL_data *vd); unsigned char *VSL_NextLog(struct VSL_data *lh); int VSL_Arg(struct VSL_data *vd, int arg, const char *opt); struct varnish_stats *VSL_OpenStats(void); +const char *VSL_tags[256]; /* varnish_debug.c */ diff --git a/varnish-cache/lib/libvarnishapi/shmlog.c b/varnish-cache/lib/libvarnishapi/shmlog.c index 9f5e8165..4626f8ad 100644 --- a/varnish-cache/lib/libvarnishapi/shmlog.c +++ b/varnish-cache/lib/libvarnishapi/shmlog.c @@ -4,6 +4,7 @@ #include #include +#include #include #include #include @@ -18,9 +19,12 @@ struct VSL_data { unsigned char *logstart; unsigned char *logend; unsigned char *ptr; - char *r_arg; + FILE *fi; unsigned char rbuf[4 + 255 + 1]; + + int ix_opt; + unsigned char supr[256]; }; #ifndef MAP_HASSEMAPHORE @@ -32,6 +36,14 @@ static struct shmloghead *vsl_lh; /*--------------------------------------------------------------------*/ +const char *VSL_tags[256] = { +#define SLTM(foo) [SLT_##foo] = #foo, +#include "shmlog_tags.h" +#undef SLTM +}; + +/*--------------------------------------------------------------------*/ + static int vsl_shmem_map(void) { @@ -84,22 +96,16 @@ int VSL_OpenLog(struct VSL_data *vd) { - if (vd->r_arg != NULL) { - if (!strcmp(vd->r_arg, "-")) - vd->fi = stdin; - else - vd->fi = fopen(vd->r_arg, "r"); - if (vd->fi != NULL) - return (0); - perror(vd->r_arg); - return (1); - } + if (vd->fi != NULL) + return (0); + if (vsl_shmem_map()) return (1); + vd->head = vsl_lh; vd->logstart = (unsigned char *)vsl_lh + vsl_lh->start; vd->logend = vd->logstart + vsl_lh->size; - vd->head = vsl_lh; + vd->ptr = vd->logstart; return (0); } @@ -110,21 +116,22 @@ VSL_NextLog(struct VSL_data *vd) int i; if (vd->fi != NULL) { - i = fread(vd->rbuf, 4, 1, vd->fi); - if (i != 1) - return (NULL); - if (vd->rbuf[1] > 0) { - i = fread(vd->rbuf + 4, vd->rbuf[1], 1, vd->fi); + while (1) { + i = fread(vd->rbuf, 4, 1, vd->fi); if (i != 1) return (NULL); + if (vd->rbuf[1] > 0) { + i = fread(vd->rbuf + 4, vd->rbuf[1], 1, vd->fi); + if (i != 1) + return (NULL); + } + if (!vd->supr[vd->rbuf[0]]) + return (vd->rbuf); } - return (vd->rbuf); } p = vd->ptr; - if (p == NULL) - p = vd->logstart; - while (1) { + for (p = vd->ptr; ; p = vd->ptr) { if (*p == SLT_WRAPMARKER) { p = vd->logstart; continue; @@ -134,17 +141,89 @@ VSL_NextLog(struct VSL_data *vd) return (NULL); } vd->ptr = p + p[1] + 4; - return (p); + if (!vd->supr[p[0]]) + return (p); } } +/*--------------------------------------------------------------------*/ + +static int +vsl_r_arg(struct VSL_data *vd, const char *opt) +{ + + if (!strcmp(opt, "-")) + vd->fi = stdin; + else + vd->fi = fopen(opt, "r"); + if (vd->fi != NULL) + return (1); + perror(opt); + return (-1); +} + +/*--------------------------------------------------------------------*/ + +static int +vsl_ix_arg(struct VSL_data *vd, const char *opt, int arg) +{ + int i, j, l; + const char *b, *e, *p, *q; + + /* If first option is 'i', set all bits for supression */ + if (arg == 'i' && vd->ix_opt == 0) + for (i = 0; i < 256; i++) + vd->supr[i] = 1; + vd->ix_opt = 1; + + for (b = opt; *b; b = e) { + while (isspace(*b)) + b++; + e = strchr(b, ','); + if (e == NULL) + e = strchr(b, '\0'); + l = e - b; + if (*e == ',') + e++; + while (isspace(b[l - 1])) + l--; + for (i = 0; i < 256; i++) { + if (VSL_tags[i] == NULL) + continue; + p = VSL_tags[i]; + q = b; + for (j = 0; j < l; j++) + if (tolower(*q++) != tolower(*p++)) + break; + if (j != l) + continue; + + if (arg == 'x') + vd->supr[i] = 1; + else + vd->supr[i] = 0; + break; + } + if (i == 256) { + fprintf(stderr, + "Could not match \"%*.*s\" to any tag\n", l, l, b); + return (-1); + } + } + return (1); +} + +/*--------------------------------------------------------------------*/ + int VSL_Arg(struct VSL_data *vd, int arg, const char *opt) { switch (arg) { + case 'i': + case 'x': + return (vsl_ix_arg(vd, opt, arg)); case 'r': - vd->r_arg = strdup(opt); - return (1); + return (vsl_r_arg(vd, opt)); default: return (0); } -- 2.39.5