From 4eb8af901c51be02a7269029bd7350d817e52092 Mon Sep 17 00:00:00 2001 From: phk Date: Thu, 6 Jul 2006 09:00:27 +0000 Subject: [PATCH] Add -r file and -w file options. These read/write from/to a binary file. git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@342 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- varnish-cache/bin/varnishlog/varnishlog.c | 77 ++++++++++++++++++++--- 1 file changed, 68 insertions(+), 9 deletions(-) diff --git a/varnish-cache/bin/varnishlog/varnishlog.c b/varnish-cache/bin/varnishlog/varnishlog.c index f584325e..f0d7e0d6 100644 --- a/varnish-cache/bin/varnishlog/varnishlog.c +++ b/varnish-cache/bin/varnishlog/varnishlog.c @@ -102,6 +102,12 @@ order(unsigned char *p) /*--------------------------------------------------------------------*/ +static void +Usage(void) +{ + fprintf(stderr, "Usage: varnishlog [-o] [-w file] [-r file]\n"); + exit(2); +} int main(int argc, char **argv) @@ -110,6 +116,11 @@ main(int argc, char **argv) unsigned u; unsigned char *p, *q; int o_flag = 0; + char *w_opt = NULL; + FILE *wfile = NULL; + char *r_opt = NULL; + FILE *rfile = NULL; + unsigned char rbuf[255+4]; struct shmloghead *loghead; loghead = VSL_OpenLog(); @@ -117,25 +128,73 @@ main(int argc, char **argv) for (i = 0; stagnames[i].tag != SLT_ENDMARKER; i++) tagnames[stagnames[i].tag] = stagnames[i].name; - while ((c = getopt(argc, argv, "o")) != -1) { + while ((c = getopt(argc, argv, "or:w:")) != -1) { switch (c) { case 'o': o_flag = 1; break; + case 'r': + r_opt = optarg; + break; + case 'w': + w_opt = optarg; + break; default: - fprintf(stderr, "Usage: varnishlog [-o]\n"); - exit (2); + Usage(); } } + if (r_opt != NULL && w_opt != NULL) + Usage(); + if (o_flag && w_opt != NULL) + Usage(); + + if (r_opt != NULL) { + rfile = fopen(r_opt, "r"); + if (rfile == NULL) + perror(r_opt); + u = 0; + } + if (w_opt != NULL) { + wfile = fopen(w_opt, "w"); + if (wfile == NULL) + perror(w_opt); + u = 0; + } + q = NULL; - while (VSL_NextLog(loghead, &q) != NULL) - ; + if (r_opt == NULL) { + while (VSL_NextLog(loghead, &q) != NULL) + ; + } while (1) { - p = VSL_NextLog(loghead, &q); - if (p == NULL) { - fflush(stdout); - usleep(50000); + if (r_opt == NULL) { + p = VSL_NextLog(loghead, &q); + if (p == NULL) { + if (w_opt == NULL) + fflush(stdout); + usleep(50000); + continue; + } + } else { + i = fread(rbuf, 4, 1, rfile); + if (i != 1) + break; + if (rbuf[1] > 0) + i = fread(rbuf + 4, rbuf[1], 1, rfile); + if (i != 1) + break; + p = rbuf; + } + if (wfile != NULL) { + i = fwrite(p, 4 + p[1], 1, wfile); + if (i != 1) + perror(w_opt); + u++; + if (!(u % 1000)) { + printf("%u\r", u); + fflush(stdout); + } continue; } if (o_flag) { -- 2.39.5