]> err.no Git - varnish/commitdiff
Add tag names array to libvarnishapi, everybody is going to need it.
authorphk <phk@d4fa192b-c00b-0410-8231-f00ffab90ce4>
Mon, 10 Jul 2006 20:27:52 +0000 (20:27 +0000)
committerphk <phk@d4fa192b-c00b-0410-8231-f00ffab90ce4>
Mon, 10 Jul 2006 20:27:52 +0000 (20:27 +0000)
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
varnish-cache/include/varnishapi.h
varnish-cache/lib/libvarnishapi/shmlog.c

index 20df23157b802decf21bb65116746d2b9080b23d..11049dec218e6343612da061f57d60f1b887faf8 100644 (file)
 #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) {
index 914c44167b3b14430221623178738a82d92f3d7c..d81a0938fb071e03731d07d79acb3dce6ddcbcc6 100644 (file)
@@ -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 */
index 9f5e81659e81b272d98138c4579f690530a3a893..4626f8ad39a506410e8731b791cbf16266d71412 100644 (file)
@@ -4,6 +4,7 @@
 
 #include <stdio.h>
 #include <errno.h>
+#include <ctype.h>
 #include <string.h>
 #include <stdlib.h>
 #include <unistd.h>
@@ -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);
        }