[master] b5fa8c5 Move all output handling from VUT to the individual utilities.

Martin Blix Grydeland martin at varnish-cache.org
Tue Nov 19 17:37:57 CET 2013


commit b5fa8c5fb3103514900194adf9314c6413b03c4d
Author: Martin Blix Grydeland <martin at varnish-software.com>
Date:   Wed Nov 6 10:32:46 2013 +0100

    Move all output handling from VUT to the individual utilities.

diff --git a/bin/varnishlog/varnishlog.c b/bin/varnishlog/varnishlog.c
index 571c7d7..2c216df 100644
--- a/bin/varnishlog/varnishlog.c
+++ b/bin/varnishlog/varnishlog.c
@@ -49,9 +49,20 @@
 #include "vsb.h"
 #include "vtim.h"
 #include "vut.h"
+#include "miniobj.h"
 
 static const char progname[] = "varnishlog";
 
+struct log {
+	/* Options */
+	int		a_opt;
+	int		B_opt;
+	char		*w_arg;
+
+	/* State */
+	FILE		*fo;
+} LOG;
+
 static void
 usage(int status)
 {
@@ -63,17 +74,68 @@ usage(int status)
 	exit(status);
 }
 
+static void
+openout(int append)
+{
+
+	AN(LOG.w_arg);
+	if (LOG.B_opt)
+		LOG.fo = VSL_WriteOpen(VUT.vsl, LOG.w_arg, append, VUT.u_opt);
+	else
+		LOG.fo = fopen(LOG.w_arg, append ? "a" : "w");
+	if (LOG.fo == NULL)
+		VUT_Error(1, "Can't open output file (%s)",
+		    LOG.B_opt ? VSL_Error(VUT.vsl) : strerror(errno));
+	VUT.dispatch_priv = LOG.fo;
+}
+
+static int __match_proto__(VUT_cb_f)
+rotateout(void)
+{
+
+	AN(LOG.w_arg);
+	AN(LOG.fo);
+	fclose(LOG.fo);
+	openout(0);
+	AN(LOG.fo);
+	return (0);
+}
+
+static int __match_proto__(VUT_cb_f)
+flushout(void)
+{
+
+	AN(LOG.fo);
+	if (fflush(LOG.fo))
+		return (-5);
+	return (0);
+}
+
 int
 main(int argc, char * const *argv)
 {
 	char opt;
 
+	memset(&LOG, 0, sizeof LOG);
 	VUT_Init(progname);
 
 	while ((opt = getopt(argc, argv, vopt_optstring)) != -1) {
 		switch (opt) {
+		case 'a':
+			/* Append to file */
+			LOG.a_opt = 1;
+			break;
+		case 'B':
+			/* Binary output */
+			LOG.B_opt = 1;
+			break;
 		case 'h':
+			/* Usage help */
 			usage(0);
+		case 'w':
+			/* Write to file */
+			REPLACE(LOG.w_arg, optarg);
+			break;
 		default:
 			if (!VUT_Arg(opt, optarg))
 				usage(1);
@@ -83,9 +145,24 @@ main(int argc, char * const *argv)
 	if (optind != argc)
 		usage(1);
 
+	/* Setup output */
+	if (LOG.B_opt)
+		VUT.dispatch_f = &VSL_WriteTransactions;
+	else
+		VUT.dispatch_f = &VSL_PrintTransactions;
+	if (LOG.w_arg) {
+		openout(LOG.a_opt);
+		AN(LOG.fo);
+		VUT.sighup_f = &rotateout;
+	} else
+		LOG.fo = stdout;
+	VUT.idle_f = &flushout;
+
 	VUT_Setup();
-	VUT_Main(NULL, NULL);
+	VUT_Main();
 	VUT_Fini();
 
+	(void)flushout();
+
 	exit(0);
 }
diff --git a/bin/varnishlog/varnishlog_options.h b/bin/varnishlog/varnishlog_options.h
index b1e4c67..676edde 100644
--- a/bin/varnishlog/varnishlog_options.h
+++ b/bin/varnishlog/varnishlog_options.h
@@ -30,8 +30,28 @@
 #include "vapi/vapi_options.h"
 #include "vut_options.h"
 
-VUT_OPT_a
+#define LOG_OPT_a							\
+	VOPT("a", "[-a]", "Append to file",				\
+	    "When writing output to a file, append to it rather than"	\
+	    " overwrite it."						\
+	)
+
+#define LOG_OPT_B							\
+	VOPT("B", "[-B]", "Binary output",				\
+	    "Output binary data suitable for reading with -r."		\
+	)
+
+#define LOG_OPT_w							\
+	VOPT("w:", "[-w filename]", "Output filename",			\
+	    "Redirect output to file. The file will be overwritten"	\
+	    " unless the -a option was specified. If the application"	\
+	    " receives a SIGHUP the file will be reopened allowing"	\
+	    " the old one to be rotated away."				\
+	)
+
+LOG_OPT_a
 VSL_OPT_b
+LOG_OPT_B
 VSL_OPT_c
 VSL_OPT_C
 VUT_OPT_d
@@ -50,6 +70,6 @@ VSL_OPT_T
 VUT_OPT_u
 VSL_OPT_v
 VUT_OPT_V
-VUT_OPT_w
+LOG_OPT_w
 VSL_OPT_x
 VSL_OPT_X
diff --git a/include/vut.h b/include/vut.h
index 5ba134b..4ddecc1 100644
--- a/include/vut.h
+++ b/include/vut.h
@@ -31,11 +31,12 @@
 
 #include "vdef.h"
 
+typedef int VUT_cb_f(void);
+
 struct VUT {
 	const char	*progname;
 
 	/* Options */
-	int		a_opt;
 	int		d_opt;
 	int		D_opt;
 	int		g_arg;
@@ -43,17 +44,21 @@ struct VUT {
 	char		*q_arg;
 	char		*r_arg;
 	int		u_opt;
-	char		*w_arg;
 
 	/* State */
 	struct VSL_data	*vsl;
 	struct VSM_data	*vsm;
 	struct VSLQ	*vslq;
-	FILE		*fo;
 	struct vpf_fh	*pfh;
 	int		sighup;
 	int		sigint;
 	int		sigusr1;
+
+	/* Callback functions */
+	VUT_cb_f	*idle_f;
+	VUT_cb_f	*sighup_f;
+	VSLQ_dispatch_f	*dispatch_f;
+	void		*dispatch_priv;
 };
 
 extern struct VUT VUT;
@@ -71,4 +76,4 @@ void VUT_Init(const char *progname);
 
 void VUT_Fini(void);
 
-int VUT_Main(VSLQ_dispatch_f *func, void *priv);
+int VUT_Main(void);
diff --git a/include/vut_options.h b/include/vut_options.h
index 3437305..53a1e56 100644
--- a/include/vut_options.h
+++ b/include/vut_options.h
@@ -29,12 +29,6 @@
 
 /* VUT options */
 
-#define VUT_OPT_a							\
-	VOPT("a", "[-a]", "Append binary file output",			\
-	    "When writing binary output to a file, append to it rather"	\
-	    " than overwrite it."					\
-	)
-
 #define VUT_OPT_d \
 	VOPT("d", "[-d]", "Process old log entries on startup",		\
 	    "Start processing log records at the head of the log"	\
@@ -93,12 +87,3 @@
 	VOPT("V", "[-V]", "Version",					\
 	    "Print version information and exit."			\
 	)
-
-#define VUT_OPT_w							\
-	VOPT("w:", "[-w filename]", "Binary output filename",		\
-	    "Write log entries to this file instead of displaying"	\
-	    " them. The file will be overwritten unless the -a option"	\
-	    " was specified. If the application receives a SIGHUP"	\
-	    " while writing to a file, it will reopen the file"		\
-	    " allowing the old one to be rotated away."			\
-	)
diff --git a/lib/libvarnishtools/vut.c b/lib/libvarnishtools/vut.c
index b7e9c90..54c35c6 100644
--- a/lib/libvarnishtools/vut.c
+++ b/lib/libvarnishtools/vut.c
@@ -116,10 +116,6 @@ VUT_Arg(int opt, const char *arg)
 	int i;
 
 	switch (opt) {
-	case 'a':
-		/* Binary file append */
-		VUT.a_opt = 1;
-		return (1);
 	case 'd':
 		/* Head */
 		VUT.d_opt = 1;
@@ -168,10 +164,6 @@ VUT_Arg(int opt, const char *arg)
 		/* Print version number and exit */
 		VCS_Message(VUT.progname);
 		exit(1);
-	case 'w':
-		/* Binary file output */
-		REPLACE(VUT.w_arg, arg);
-		return (1);
 	default:
 		AN(VUT.vsl);
 		i = VSL_Arg(VUT.vsl, opt, arg);
@@ -218,16 +210,6 @@ VUT_Setup(void)
 	if (c == NULL)
 		VUT_Error(1, "Can't open log (%s)", VSL_Error(VUT.vsl));
 
-	/* Output */
-	if (VUT.w_arg) {
-		VUT.fo = VSL_WriteOpen(VUT.vsl, VUT.w_arg, VUT.a_opt,
-		    VUT.u_opt);
-		if (VUT.fo == NULL)
-			VUT_Error(1, "Can't open output file (%s)",
-			    VSL_Error(VUT.vsl));
-	} else
-		VUT.fo = stdout;
-
 	/* Create query */
 	VUT.vslq = VSLQ_New(VUT.vsl, &c, VUT.g_arg, VUT.q_arg);
 	if (VUT.vslq == NULL)
@@ -265,9 +247,6 @@ VUT_Fini(void)
 	free(VUT.r_arg);
 	free(VUT.P_arg);
 
-	if (VUT.fo != NULL)
-		fflush(VUT.fo);
-
 	vut_vpf_remove();
 	AZ(VUT.pfh);
 
@@ -282,40 +261,28 @@ VUT_Fini(void)
 }
 
 int
-VUT_Main(VSLQ_dispatch_f *func, void *priv)
+VUT_Main(void)
 {
 	struct VSL_cursor *c;
 	int i = -1;
 
 	AN(VUT.vslq);
 
-	if (func == NULL) {
-		if (VUT.w_arg)
-			func = VSL_WriteTransactions;
-		else
-			func = VSL_PrintTransactions;
-		AN(VUT.fo);
-		priv = VUT.fo;
-	}
-
 	while (!VUT.sigint) {
-		if (VUT.w_arg && VUT.sighup) {
-			/* Rotate log */
+		if (VUT.sighup && VUT.sighup_f) {
+			/* sighup callback */
 			VUT.sighup = 0;
-			AN(VUT.fo);
-			fclose(VUT.fo);
-			VUT.fo = VSL_WriteOpen(VUT.vsl, VUT.w_arg, 0,
-			    VUT.u_opt);
-			if (VUT.fo == NULL)
-				VUT_Error(1, "Can't open output file (%s)",
-				    VSL_Error(VUT.vsl));
+			i = (VUT.sighup_f)();
+			if (i)
+				break;
 		}
 
 		if (VUT.sigusr1) {
 			/* Flush and report any incomplete records */
 			VUT.sigusr1 = 0;
 			if (VUT.vslq != NULL)
-				VSLQ_Flush(VUT.vslq, func, priv);
+				VSLQ_Flush(VUT.vslq, VUT.dispatch_f,
+				    VUT.dispatch_priv);
 		}
 
 		if (VUT.vslq == NULL) {
@@ -340,14 +307,17 @@ VUT_Main(VSLQ_dispatch_f *func, void *priv)
 			VUT_Error(0, "Log reaquired");
 		}
 
-		i = VSLQ_Dispatch(VUT.vslq, func, priv);
+		i = VSLQ_Dispatch(VUT.vslq, VUT.dispatch_f, VUT.dispatch_priv);
 		if (i == 1)
 			/* Call again */
 			continue;
 		else if (i == 0) {
 			/* Nothing to do but wait */
-			if (VUT.fo)
-				fflush(VUT.fo);
+			if (VUT.idle_f) {
+				i = (VUT.idle_f)();
+				if (i)
+					break;
+			}
 			VTIM_sleep(0.01);
 			continue;
 		} else if (i == -1) {
@@ -360,7 +330,7 @@ VUT_Main(VSLQ_dispatch_f *func, void *priv)
 
 		/* XXX: Make continuation optional */
 
-		VSLQ_Flush(VUT.vslq, func, priv);
+		VSLQ_Flush(VUT.vslq, VUT.dispatch_f, VUT.dispatch_priv);
 		VSLQ_Delete(&VUT.vslq);
 		AZ(VUT.vslq);
 
@@ -374,8 +344,5 @@ VUT_Main(VSLQ_dispatch_f *func, void *priv)
 		}
 	}
 
-	if (VUT.fo)
-		fflush(VUT.fo);
-
 	return (i);
 }



More information about the varnish-commit mailing list