From fab876cad55ace66c106aa188d1e3aa2edb0ddc9 Mon Sep 17 00:00:00 2001 From: des Date: Sun, 9 Mar 2008 15:14:04 +0000 Subject: [PATCH] Add -k option which specifies the number of log entries to keep. Along with -s, this allows varnishlog to be used to extract part of a log file, or partition a log file into smaller sections. git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@2575 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- varnish-cache/bin/varnishlog/varnishlog.1 | 7 ++++++- varnish-cache/include/varnishapi.h | 4 ++-- varnish-cache/lib/libvarnishapi/shmlog.c | 24 +++++++++++++++++++++++ 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/varnish-cache/bin/varnishlog/varnishlog.1 b/varnish-cache/bin/varnishlog/varnishlog.1 index 1fd2db2c..77b30aeb 100644 --- a/varnish-cache/bin/varnishlog/varnishlog.1 +++ b/varnish-cache/bin/varnishlog/varnishlog.1 @@ -28,7 +28,7 @@ .\" .\" $Id$ .\" -.Dd March 8, 2008 +.Dd March 9, 2008 .Dt VARNISHLOG 1 .Os .Sh NAME @@ -44,6 +44,7 @@ .Op Fl d .Op Fl I Ar regex .Op Fl i Ar tag +.Op Fl k Ar keep .Op Fl n Ar varnish_name .Op Fl o .Op Fl P Ar file @@ -108,6 +109,10 @@ If neither nor .Fl i is specified, all log entries are included. +.It Fl k Ar num +Only show the first +.Nm num +log records. .It Fl n Specifies the name of the .Nm varnishd diff --git a/varnish-cache/include/varnishapi.h b/varnish-cache/include/varnishapi.h index 8edf72d9..e7c7f647 100644 --- a/varnish-cache/include/varnishapi.h +++ b/varnish-cache/include/varnishapi.h @@ -44,8 +44,8 @@ int base64_decode(char *d, unsigned dlen, const char *s); typedef int vsl_handler(void *priv, enum shmlogtag tag, unsigned fd, unsigned len, unsigned spec, const char *ptr); #define VSL_S_CLIENT (1 << 0) #define VSL_S_BACKEND (1 << 1) -#define VSL_ARGS "bCcdI:i:r:s:X:x:" -#define VSL_USAGE "[-bCcd] [-i tag] [-I regexp] [-r file] [-s skip] [-X regexp] [-x tag]" +#define VSL_ARGS "bCcdI:i:k:r:s:X:x:" +#define VSL_USAGE "[-bCcd] [-i tag] [-I regexp] [-k keep] [-r file] [-s skip] [-X regexp] [-x tag]" vsl_handler VSL_H_Print; struct VSL_data; struct VSL_data *VSL_New(void); diff --git a/varnish-cache/lib/libvarnishapi/shmlog.c b/varnish-cache/lib/libvarnishapi/shmlog.c index d18e79eb..3dc4eb84 100644 --- a/varnish-cache/lib/libvarnishapi/shmlog.c +++ b/varnish-cache/lib/libvarnishapi/shmlog.c @@ -87,6 +87,7 @@ struct VSL_data { regex_t *regexcl; unsigned long skip; + unsigned long keep; }; #ifndef MAP_HASSEMAPHORE @@ -297,6 +298,9 @@ VSL_NextLog(struct VSL_data *vd, unsigned char **pp) if (vd->skip) { --vd->skip; continue; + } else if (vd->keep) { + if (--vd->keep == 0) + return (0); } if (vd->map[p[SHMLOG_TAG]] & M_SELECT) { *pp = p; @@ -501,6 +505,25 @@ vsl_s_arg(struct VSL_data *vd, const char *opt) } return (1); } +/*--------------------------------------------------------------------*/ + +static int +vsl_k_arg(struct VSL_data *vd, const char *opt) +{ + char *end; + + CHECK_OBJ_NOTNULL(vd, VSL_MAGIC); + if (*opt == '\0') { + fprintf(stderr, "number required for -k\n"); + return (-1); + } + vd->keep = strtoul(opt, &end, 10); + if (*end != '\0') { + fprintf(stderr, "invalid number for -k\n"); + return (-1); + } + return (1); +} /*--------------------------------------------------------------------*/ @@ -518,6 +541,7 @@ VSL_Arg(struct VSL_data *vd, int arg, const char *opt) case 'I': case 'X': return (vsl_IX_arg(vd, opt, arg)); case 'C': vd->regflags = REG_ICASE; return (1); case 's': return (vsl_s_arg(vd, opt)); + case 'k': return (vsl_k_arg(vd, opt)); default: return (0); } -- 2.39.5