[master] 78cb659 Internalize the VSC levels, and expose a function to increase/decrease.

Poul-Henning Kamp phk at FreeBSD.org
Sat May 27 00:39:05 CEST 2017


commit 78cb659978c27f7dab456a47a7a56b761614d440
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Fri May 26 22:38:17 2017 +0000

    Internalize the VSC levels, and expose a function to increase/decrease.

diff --git a/bin/varnishstat/varnishstat_curses.c b/bin/varnishstat/varnishstat_curses.c
index 36cac9b..6d3d142 100644
--- a/bin/varnishstat/varnishstat_curses.c
+++ b/bin/varnishstat/varnishstat_curses.c
@@ -117,7 +117,7 @@ static WINDOW *w_points = NULL;
 static WINDOW *w_bar_b = NULL;
 static WINDOW *w_info = NULL;
 
-static int verbosity = VSC_level_info;
+static const struct VSC_level_desc *verbosity;
 static int keep_running = 1;
 static int hide_unseen = 1;
 static int page_start = 0;
@@ -216,7 +216,7 @@ build_pt_array(void)
 		CHECK_OBJ_NOTNULL(pt, PT_MAGIC);
 		if (!pt->seen && hide_unseen)
 			continue;
-		if (pt->vpt->desc->level->verbosity > verbosity)
+		if (pt->vpt->desc->level > verbosity)
 			continue;
 		assert(n_ptarray < n_ptlist);
 		ptarray[n_ptarray++] = pt;
@@ -938,7 +938,6 @@ static void
 draw_bar_b(void)
 {
 	int x, X;
-	const struct VSC_level_desc *level;
 	char buf[64];
 
 	AN(w_bar_b);
@@ -959,11 +958,10 @@ draw_bar_b(void)
 	mvwprintw(w_bar_b, 0, X - strlen(buf), "%s", buf);
 	X -= strlen(buf) + 2;
 
-	level = VSC_LevelDesc(verbosity);
-	if (level != NULL) {
-		mvwprintw(w_bar_b, 0, X - strlen(level->label), "%s",
-		    level->label);
-		X -= strlen(level->label) + 2;
+	if (verbosity != NULL) {
+		mvwprintw(w_bar_b, 0, X - strlen(verbosity->label), "%s",
+		    verbosity->label);
+		X -= strlen(verbosity->label) + 2;
 	}
 	if (!hide_unseen)
 		mvwprintw(w_bar_b, 0, X - 6, "%s", "UNSEEN");
@@ -1045,9 +1043,11 @@ handle_keypress(int ch)
 		page_start = (current - l_points) + 1;
 		break;
 	case 'v':
-		verbosity++;
-		if (VSC_LevelDesc(verbosity) == NULL)
-			verbosity = 0;
+		verbosity = VSC_ChangeLevel(verbosity, 1);
+		rebuild = 1;
+		break;
+	case 'V':
+		verbosity = VSC_ChangeLevel(verbosity, -1);
 		rebuild = 1;
 		break;
 	case 'q':
@@ -1081,6 +1081,8 @@ do_curses(struct VSM_data *vd, double delay)
 
 	interval = delay;
 
+	verbosity = VSC_ChangeLevel(NULL, 0);
+
 	initscr();
 	raw();
 	noecho();
diff --git a/include/vapi/vsc.h b/include/vapi/vsc.h
index 8d0e993..3f96f09 100644
--- a/include/vapi/vsc.h
+++ b/include/vapi/vsc.h
@@ -78,7 +78,7 @@ struct VSC_desc;
 struct VSC_point;
 
 struct VSC_level_desc {
-	unsigned verbosity;
+	const char *name;		/* name */
 	const char *label;		/* label */
 	const char *sdesc;		/* short description */
 	const char *ldesc;		/* long description */
@@ -138,6 +138,6 @@ int VSC_Iter(struct VSM_data *vd, struct VSM_fantom *fantom, VSC_iter_f *func,
 	 *	0:	Done
 	 */
 
-const struct VSC_level_desc *VSC_LevelDesc(unsigned level);
+const struct VSC_level_desc *VSC_ChangeLevel(const struct VSC_level_desc*, int);
 
 #endif /* VAPI_VSC_H_INCLUDED */
diff --git a/include/vapi/vsc_int.h b/include/vapi/vsc_int.h
index a767369..6859ac6 100644
--- a/include/vapi/vsc_int.h
+++ b/include/vapi/vsc_int.h
@@ -29,11 +29,3 @@
  */
 
 #define VSC_CLASS		"Stat"
-
-/* vsc levels */
-enum VSC_level_e {
-#define VSC_LEVEL_F(v,l,e,d)		\
-	VSC_level_##v,
-#include "tbl/vsc_levels.h"
-	VSC_level_MAX,
-};
diff --git a/lib/libvarnishapi/libvarnishapi.map b/lib/libvarnishapi/libvarnishapi.map
index a07bd30..14c4418 100644
--- a/lib/libvarnishapi/libvarnishapi.map
+++ b/lib/libvarnishapi/libvarnishapi.map
@@ -175,3 +175,8 @@ LIBVARNISHAPI_1.6 {
 	VTIM_timespec;
 	VTIM_timeval;
 } LIBVARNISHAPI_1.0;
+
+LIBVARNISHAPI_1.7 {
+  global:
+	VSC_ChangeLevel;
+} LIBVARNISHAPI_1.0;
diff --git a/lib/libvarnishapi/vsc.c b/lib/libvarnishapi/vsc.c
index 76f62b5..f600c00 100644
--- a/lib/libvarnishapi/vsc.c
+++ b/lib/libvarnishapi/vsc.c
@@ -104,17 +104,24 @@ struct vsc {
  * Build the static level, type and point descriptions
  */
 
-#define VSC_LEVEL_F(v,l,e,d)			\
-	const struct VSC_level_desc VSC_level_desc_##v = \
-		{VSC_level_##v, l, e, d};
+#define VSC_LEVEL_F(v,l,e,d) \
+    static const struct VSC_level_desc level_##v = {#v, l, e, d};
+#include "tbl/vsc_levels.h"
+
+static const struct VSC_level_desc * const levels[] = {
+#define VSC_LEVEL_F(v,l,e,d) &level_##v,
 #include "tbl/vsc_levels.h"
 #undef VSC_LEVEL_F
+};
+
+static const size_t nlevels =
+    sizeof(levels)/sizeof(*levels);
 
 #define VSC_TYPE_F(n,t,l,e,d)	const char *VSC_type_##n = t;
 #include "tbl/vsc_types.h"
 
 #define VSC_DO(U,l,t,h)		const struct VSC_desc VSC_desc_##l[] = {
-#define VSC_F(n,t,l,s,f,v,d,e)		{#n,#t,s,f,&VSC_level_desc_##v,d,e},
+#define VSC_F(n,t,l,s,f,v,d,e)		{#n,#t,s,f,&level_##v,d,e},
 #define VSC_DONE(U,l,t)		};
 #include "tbl/vsc_all.h"
 
@@ -479,16 +486,23 @@ VSC_Iter(struct VSM_data *vd, struct VSM_fantom *fantom, VSC_iter_f *func,
  */
 
 const struct VSC_level_desc *
-VSC_LevelDesc(unsigned level)
+VSC_ChangeLevel(const struct VSC_level_desc *old, int chg)
 {
-	switch (level) {
-#define VSC_LEVEL_F(v,l,e,d)	\
-	case VSC_level_##v:	\
-		return (&VSC_level_desc_##v);
-#include "tbl/vsc_levels.h"
-#undef VSC_LEVEL_F
-	default:
-		return (NULL);
-	}
+	int i;
+
+	if (old == NULL)
+		old = levels[0];
+	for (i = 0; i < nlevels; i++)
+		if (old == levels[i])
+			break;
+	if (i == nlevels)
+		i = 0;
+	else
+		i += chg;
+	if (i >= nlevels)
+		i = nlevels - 1;
+	if (i < 0)
+		i = 0;
+	return (levels[i]);
 }
 



More information about the varnish-commit mailing list