[master] 3df4eae44 varnishncsa: Refactor %{x}T processing

Nils Goroll nils.goroll at uplex.de
Mon Apr 26 10:56:05 UTC 2021


commit 3df4eae44be86fd16c4a5e9d87015e00be6b68b9
Author: Nils Goroll <nils.goroll at uplex.de>
Date:   Mon Apr 26 12:50:56 2021 +0200

    varnishncsa: Refactor %{x}T processing
    
    As we can safely regard (struct format).time_type is a private contract
    between addf_time() and format_time(), use it to specify the exact
    format type, which moves strcmp() parsing from execution to setup.

diff --git a/bin/varnishncsa/varnishncsa.c b/bin/varnishncsa/varnishncsa.c
index 6c5ea6d23..ba01a98d5 100644
--- a/bin/varnishncsa/varnishncsa.c
+++ b/bin/varnishncsa/varnishncsa.c
@@ -294,23 +294,23 @@ format_time(const struct format *format)
 		(void)localtime_r(&t, &tm);
 		AN(strftime(buf, sizeof buf, format->time_fmt, &tm));
 		AZ(VSB_cat(CTX.vsb, buf));
+		return (1);
+	case 's':
+		l = (long)(t_end - t_start);
 		break;
-	case 'T':
-		AN(format->time_fmt);
-		if (!strcmp(format->time_fmt, "s")) /* same as %T */
-			l = (long)(t_end - t_start);
-		else if (!strcmp(format->time_fmt, "ms"))
-			l = (long)((t_end - t_start) * 1e3);
-		else if (!strcmp(format->time_fmt, "us")) /* same as %D */
-			l = (long)((t_end - t_start) * 1e6);
-		else
-			WRONG("Unreachable branch");
-		AZ(VSB_printf(CTX.vsb, "%ld", l));
+	case 'm':
+		l = (long)((t_end - t_start) * 1e3);
+		break;
+	case 'u':
+		l = (long)((t_end - t_start) * 1e6);
 		break;
 	default:
 		WRONG("Time format specifier");
 	}
 
+	AN(format->time_fmt);
+	AZ(VSB_printf(CTX.vsb, format->time_fmt, l));
+
 	return (1);
 }
 
@@ -449,9 +449,18 @@ addf_time(char type, const char *fmt)
 	f->time_fmt = strdup(fmt);
 	AN(f->time_fmt);
 
-	if (f->time_type == 'T' && strcmp(f->time_fmt, "s") &&
-	    strcmp(f->time_fmt, "ms") && strcmp(f->time_fmt, "us"))
-		VUT_Error(vut, 1, "Unknown specifier: %%{%s}T", f->time_fmt);
+	if (f->time_type == 'T') {
+		if (!strcmp(f->time_fmt, "s"))
+			f->time_type = 's';
+		else if (!strcmp(f->time_fmt, "ms"))
+			f->time_type = 'm';
+		else if (!strcmp(f->time_fmt, "us"))
+			f->time_type = 'u';
+		else
+			VUT_Error(vut, 1, "Unknown specifier: %%{%s}T",
+			    f->time_fmt);
+		REPLACE(f->time_fmt, "%ld");
+	}
 
 	VTAILQ_INSERT_TAIL(&CTX.format, f, list);
 }


More information about the varnish-commit mailing list