[master] 0d07107 Add VSL_Write* functions for writing binary log output.
Martin Blix Grydeland
martin at varnish-cache.org
Thu Jun 13 12:41:23 CEST 2013
commit 0d07107e8cb19dfbe4faa8ef1ecb98d665e124f3
Author: Martin Blix Grydeland <martin at varnish-software.com>
Date: Wed May 22 10:52:03 2013 +0200
Add VSL_Write* functions for writing binary log output.
diff --git a/include/vapi/vsl.h b/include/vapi/vsl.h
index 62cb69b..30507c0 100644
--- a/include/vapi/vsl.h
+++ b/include/vapi/vsl.h
@@ -279,6 +279,52 @@ int VSL_PrintTransactions(struct VSL_data *vsl,
* !=0: Return value from either VSL_Next or VSL_Print
*/
+FILE *VSL_WriteOpen(struct VSL_data *vsl, const char *name, int append,
+ int unbuffered);
+ /*
+ * Open file name for writing using the VSL_Write* functions. If
+ * append is true, the file will be opened for appending.
+ *
+ * Arguments:
+ * vsl: The VSL data context
+ * name: The file name
+ * append: If true, the file will be appended instead of truncated
+ * unbuf: If true, use unbuffered mode
+ *
+ * Return values:
+ * NULL: Error - see VSL_Error
+ * non-NULL: Success
+ */
+
+
+int VSL_Write(struct VSL_data *vsl, const struct VSL_cursor *c, void *fo);
+ /*
+ * Write the currect record pointed to be c to the FILE* fo
+ *
+ * Return values:
+ * 0: Success
+ * -5: I/O error - see VSL_Error
+ */
+
+int VSL_WriteAll(struct VSL_data *vsl, struct VSL_cursor *c, void *fo);
+ /*
+ * Calls VSL_Next on c until c is exhausted. In turn calls
+ * VSL_Write on all records where VSL_Match returns true.
+ *
+ * Return values:
+ * 0: OK
+ * !=0: Return value from either VSL_Next or VSL_Write
+ */
+
+int VSL_WriteTransactions(struct VSL_data *vsl,
+ struct VSL_transaction *ptrans[], void *fo);
+ /*
+ * Write all transactions in ptrans using VSL_WriteAll
+ * Return values:
+ * 0: OK
+ * !=0: Return value from either VSL_Next or VSL_Write
+ */
+
struct VSLQ *VSLQ_New(struct VSL_data *vsl, struct VSL_cursor **cp,
enum VSL_grouping_e grouping, const char *query);
/*
diff --git a/lib/libvarnishapi/libvarnishapi.map b/lib/libvarnishapi/libvarnishapi.map
index 1e8c513..becda5d 100644
--- a/lib/libvarnishapi/libvarnishapi.map
+++ b/lib/libvarnishapi/libvarnishapi.map
@@ -106,6 +106,10 @@ LIBVARNISHAPI_1.3 {
VSL_PrintTerse;
VSL_PrintAll;
VSL_PrintTransactions;
+ VSL_WriteOpen;
+ VSL_Write;
+ VSL_WriteAll;
+ VSL_WriteTransactions;
VSLQ_New;
VSLQ_Delete;
VSLQ_Dispatch;
diff --git a/lib/libvarnishapi/vsl.c b/lib/libvarnishapi/vsl.c
index a9b1726..28d0e44 100644
--- a/lib/libvarnishapi/vsl.c
+++ b/lib/libvarnishapi/vsl.c
@@ -320,3 +320,71 @@ VSL_PrintTransactions(struct VSL_data *vsl, struct VSL_transaction *pt[],
return (0);
}
+
+FILE*
+VSL_WriteOpen(struct VSL_data *vsl, const char *name, int append, int unbuf)
+{
+ const char head[] = VSL_FILE_ID;
+ FILE* f;
+
+ f = fopen(name, append ? "a" : "w");
+ if (f == NULL) {
+ vsl_diag(vsl, "%s", strerror(errno));
+ return (NULL);
+ }
+ if (unbuf)
+ setbuf(f, NULL);
+ if (0 == ftell(f))
+ fwrite(head, 1, sizeof head, f);
+ return (f);
+}
+
+int
+VSL_Write(struct VSL_data *vsl, const struct VSL_cursor *c, void *fo)
+{
+ size_t r;
+
+ CHECK_OBJ_NOTNULL(vsl, VSL_MAGIC);
+ if (c == NULL || c->rec.ptr == NULL)
+ return (0);
+ if (fo == NULL)
+ fo = stdout;
+ r = fwrite(c->rec.ptr, sizeof *c->rec.ptr,
+ VSL_NEXT(c->rec.ptr) - c->rec.ptr, fo);
+ if (r == 0)
+ return (-5);
+ return (0);
+}
+
+int
+VSL_WriteAll(struct VSL_data *vsl, struct VSL_cursor *c, void *fo)
+{
+ int i;
+
+ if (c == NULL)
+ return (0);
+ while (1) {
+ i = VSL_Next(c);
+ if (i <= 0)
+ return (i);
+ if (!VSL_Match(vsl, c))
+ continue;
+ i = VSL_Write(vsl, c, fo);
+ if (i != 0)
+ return (i);
+ }
+}
+
+int
+VSL_WriteTransactions(struct VSL_data *vsl, struct VSL_transaction *pt[],
+ void *fo)
+{
+ struct VSL_transaction *t;
+ int i;
+
+ if (pt == NULL)
+ return (0);
+ for (i = 0, t = pt[0]; i == 0 && t != NULL; t = *++pt)
+ i = VSL_WriteAll(vsl, t->c, fo);
+ return (i);
+}
More information about the varnish-commit
mailing list