# HG changeset patch
# User David Scott <dave.scott@xxxxxxxxxxxxx>
# Date 1300913511 0
# Node ID 67525892e2c6ee27c25438d43699397c5a3e0272
# Parent 3831bd253e02aa0536ed32e936777d026abb955e
tools: ocaml: fix the logging in the ocaml libxl bindings.
We now:
1. ensure the caml_logger fields have sensible initial values
2. adopt the policy that, if the messages won't fit into the buffer then
they are dropped and clipped is set to 1. The default buffer size is 2KiB
which ought to be large enough unless the logging is very spammy (which
is arguably a problem in itself)
Signed-off-by: David Scott <dave.scott@xxxxxxxxxxxxx>
diff -r 3831bd253e02 -r 67525892e2c6 tools/ocaml/libs/xl/xl_stubs.c
--- a/tools/ocaml/libs/xl/xl_stubs.c Mon Mar 21 18:07:06 2011 +0000
+++ b/tools/ocaml/libs/xl/xl_stubs.c Wed Mar 23 20:51:51 2011 +0000
@@ -28,12 +28,21 @@
#include "libxl.h"
+#define MAX_LOG_LEN 2048
+
struct caml_logger {
struct xentoollog_logger logger;
int log_offset;
- char log_buf[2048];
+ char log_buf[MAX_LOG_LEN];
+ int clipped; /* Set to 1 if some log messages won't fit in the buffer */
};
+void log_create(struct caml_logger *logger)
+{
+ logger->log_offset = 0;
+ logger->clipped = 0;
+}
+
typedef struct caml_gc {
int offset;
void *ptrs[64];
@@ -43,16 +52,32 @@
int errnoval, const char *context, const char *format,
va_list al)
{
struct caml_logger *ologger = (struct caml_logger *) logger;
+ int remaining, written;
- ologger->log_offset += vsnprintf(ologger->log_buf + ologger->log_offset,
- 2048 - ologger->log_offset, format,
al);
+ if (ologger->clipped)
+ return; /* if we've already started clipping, drop everything */
+
+ /* If the message doesn't fit in the buffer, we drop it and set clipped
to 1.
+ This shouldn't happen unless the logging is very spammy. */
+
+ remaining = sizeof(ologger->log_buf) - ologger->log_offset;
+ written = vsnprintf(ologger->log_buf + ologger->log_offset, remaining,
format, al);
+ if (written < 0)
+ return; /* nothing really we can do */
+ if (written < remaining){
+ ologger->log_offset += written;
+ return;
+ }
+ /* buffer isn't big enough */
+ ologger->clipped = 1;
}
void log_destroy(struct xentoollog_logger *logger)
{
}
-#define INIT_STRUCT() libxl_ctx ctx; struct caml_logger lg; struct caml_gc gc;
gc.offset = 0;
+
+#define INIT_STRUCT() libxl_ctx ctx; struct caml_logger lg; struct caml_gc gc;
gc.offset = 0; log_create(&lg);
#define INIT_CTX() \
lg.logger.vmessage = log_vmessage; \
_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxxxxxxxx
http://lists.xensource.com/xen-devel
|