xen-devel.lists.xenproject.org archive mirror
 help / color / mirror / Atom feed
From: Rob Hoes <rob.hoes@citrix.com>
To: xen-devel@lists.xen.org
Cc: ian.jackson@eu.citrix.com, ian.campbell@citrix.com,
	Rob Hoes <rob.hoes@citrix.com>
Subject: [PATCH v4 05/27] libxc: ocaml: add simple binding for xentoollog (output only).
Date: Wed, 6 Nov 2013 17:49:43 +0000	[thread overview]
Message-ID: <1383760205-20397-6-git-send-email-rob.hoes@citrix.com> (raw)
In-Reply-To: <1383760205-20397-1-git-send-email-rob.hoes@citrix.com>

These bindings allow ocaml code to receive log message via xentoollog
but do not support injecting messages into xentoollog from ocaml.
Receiving log messages from libx{c,l} and forwarding them to ocaml is
the use case which is needed by the following patches.

Add a simple noddy test case (tools/ocaml/test).

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
Signed-off-by: Rob Hoes <rob.hoes@citrix.com>
Acked-by: David Scott <dave.scott@eu.citrix.com>
---
 .gitignore                                     |    1 +
 .hgignore                                      |    1 +
 tools/ocaml/Makefile                           |    2 +-
 tools/ocaml/Makefile.rules                     |    2 +-
 tools/ocaml/libs/Makefile                      |    1 +
 tools/ocaml/libs/xentoollog/META.in            |    4 +
 tools/ocaml/libs/xentoollog/Makefile           |   61 ++++++++
 tools/ocaml/libs/xentoollog/caml_xentoollog.h  |   24 +++
 tools/ocaml/libs/xentoollog/genlevels.py       |  127 +++++++++++++++
 tools/ocaml/libs/xentoollog/xentoollog.ml.in   |   48 ++++++
 tools/ocaml/libs/xentoollog/xentoollog.mli.in  |   43 ++++++
 tools/ocaml/libs/xentoollog/xentoollog_stubs.c |  196 ++++++++++++++++++++++++
 tools/ocaml/test/Makefile                      |   28 ++++
 tools/ocaml/test/xtl.ml                        |   40 +++++
 14 files changed, 576 insertions(+), 2 deletions(-)
 create mode 100644 tools/ocaml/libs/xentoollog/META.in
 create mode 100644 tools/ocaml/libs/xentoollog/Makefile
 create mode 100644 tools/ocaml/libs/xentoollog/caml_xentoollog.h
 create mode 100755 tools/ocaml/libs/xentoollog/genlevels.py
 create mode 100644 tools/ocaml/libs/xentoollog/xentoollog.ml.in
 create mode 100644 tools/ocaml/libs/xentoollog/xentoollog.mli.in
 create mode 100644 tools/ocaml/libs/xentoollog/xentoollog_stubs.c
 create mode 100644 tools/ocaml/test/Makefile
 create mode 100644 tools/ocaml/test/xtl.ml

diff --git a/.gitignore b/.gitignore
index 3253675..f51c345 100644
--- a/.gitignore
+++ b/.gitignore
@@ -384,6 +384,7 @@ tools/ocaml/libs/xl/_libxl_types.mli.in
 tools/ocaml/libs/xl/xenlight.ml
 tools/ocaml/libs/xl/xenlight.mli
 tools/ocaml/xenstored/oxenstored
+tools/ocaml/test/xtl
 
 tools/debugger/kdd/kdd
 tools/firmware/etherboot/ipxe.tar.gz
diff --git a/.hgignore b/.hgignore
index 05cb0de..bb1b67d 100644
--- a/.hgignore
+++ b/.hgignore
@@ -308,6 +308,7 @@
 ^tools/ocaml/libs/xl/xenlight\.ml$
 ^tools/ocaml/libs/xl/xenlight\.mli$
 ^tools/ocaml/xenstored/oxenstored$
+^tools/ocaml/test/xtl$
 ^tools/autom4te\.cache$
 ^tools/config\.h$
 ^tools/config\.log$
diff --git a/tools/ocaml/Makefile b/tools/ocaml/Makefile
index 6b22bbe..8e4ca36 100644
--- a/tools/ocaml/Makefile
+++ b/tools/ocaml/Makefile
@@ -1,7 +1,7 @@
 XEN_ROOT = $(CURDIR)/../..
 include $(XEN_ROOT)/tools/Rules.mk
 
-SUBDIRS_PROGRAMS = xenstored
+SUBDIRS_PROGRAMS = xenstored test
 
 SUBDIRS = libs $(SUBDIRS_PROGRAMS)
 
diff --git a/tools/ocaml/Makefile.rules b/tools/ocaml/Makefile.rules
index 5e6d81e..0745e83 100644
--- a/tools/ocaml/Makefile.rules
+++ b/tools/ocaml/Makefile.rules
@@ -24,7 +24,7 @@ ALL_OCAML_OBJS ?= $(OBJS)
 %.cmi: %.mli
 	$(call quiet-command, $(OCAMLC) $(OCAMLCFLAGS) -c -o $@ $<,MLI,$@)
 
-%.cmx: %.ml
+%.cmx %.o: %.ml
 	$(call quiet-command, $(OCAMLOPT) $(OCAMLOPTFLAGS) -c -o $@ $<,MLOPT,$@)
 
 %.ml: %.mll
diff --git a/tools/ocaml/libs/Makefile b/tools/ocaml/libs/Makefile
index bca0fa2..3afdc89 100644
--- a/tools/ocaml/libs/Makefile
+++ b/tools/ocaml/libs/Makefile
@@ -3,6 +3,7 @@ include $(XEN_ROOT)/tools/Rules.mk
 
 SUBDIRS= \
 	mmap \
+	xentoollog \
 	xc eventchn \
 	xb xs xl
 
diff --git a/tools/ocaml/libs/xentoollog/META.in b/tools/ocaml/libs/xentoollog/META.in
new file mode 100644
index 0000000..7b06683
--- /dev/null
+++ b/tools/ocaml/libs/xentoollog/META.in
@@ -0,0 +1,4 @@
+version = "@VERSION@"
+description = "Xen Tools Logger Interface"
+archive(byte) = "xentoollog.cma"
+archive(native) = "xentoollog.cmxa"
diff --git a/tools/ocaml/libs/xentoollog/Makefile b/tools/ocaml/libs/xentoollog/Makefile
new file mode 100644
index 0000000..e535ba5
--- /dev/null
+++ b/tools/ocaml/libs/xentoollog/Makefile
@@ -0,0 +1,61 @@
+TOPLEVEL=$(CURDIR)/../..
+XEN_ROOT=$(TOPLEVEL)/../..
+include $(TOPLEVEL)/common.make
+
+CFLAGS += $(CFLAGS_libxenctrl) $(CFLAGS_libxenguest)
+OCAMLINCLUDE +=
+
+OBJS = xentoollog
+INTF = xentoollog.cmi
+LIBS = xentoollog.cma xentoollog.cmxa
+
+LIBS_xentoollog = $(LDLIBS_libxenctrl)
+
+xentoollog_OBJS = $(OBJS)
+xentoollog_C_OBJS = xentoollog_stubs
+
+OCAML_LIBRARY = xentoollog
+
+GENERATED_FILES += xentoollog.ml xentoollog.ml.tmp xentoollog.mli xentoollog.mli.tmp
+GENERATED_FILES += _xtl_levels.mli.in _xtl_levels.ml.in _xtl_levels.inc META
+
+all: $(INTF) $(LIBS)
+
+xentoollog.ml: xentoollog.ml.in _xtl_levels.ml.in
+	$(Q)sed -e '1i\
+(*\
+ * AUTO-GENERATED FILE DO NOT EDIT\
+ * Generated from xentoollog.ml.in and _xtl_levels.ml.in\
+ *)\
+' \
+	    -e '/^(\* @@XTL_LEVELS@@ \*)$$/r_xtl_levels.ml.in' \
+	  < xentoollog.ml.in > xentoollog.ml.tmp
+	$(Q)mv xentoollog.ml.tmp xentoollog.ml
+
+xentoollog.mli: xentoollog.mli.in _xtl_levels.mli.in
+	$(Q)sed -e '1i\
+(*\
+ * AUTO-GENERATED FILE DO NOT EDIT\
+ * Generated from xentoollog.mli.in and _xtl_levels.mli.in\
+ *)\
+' \
+	    -e '/^(\* @@XTL_LEVELS@@ \*)$$/r_xtl_levels.mli.in' \
+	  < xentoollog.mli.in > xentoollog.mli.tmp
+	$(Q)mv xentoollog.mli.tmp xentoollog.mli
+
+libs: $(LIBS)
+
+_xtl_levels.ml.in _xtl_levels.mli.in _xtl_levels.inc: genlevels.py $(XEN_ROOT)/tools/libxc/xentoollog.h
+	$(PYTHON) genlevels.py _xtl_levels.mli.in _xtl_levels.ml.in _xtl_levels.inc
+
+.PHONY: install
+install: $(LIBS) META
+	mkdir -p $(OCAMLDESTDIR)
+	ocamlfind remove -destdir $(OCAMLDESTDIR) xentoollog
+	ocamlfind install -destdir $(OCAMLDESTDIR) -ldconf ignore xentoollog META $(INTF) $(LIBS) *.a *.so *.cmx
+
+.PHONY: uninstall
+uninstall:
+	ocamlfind remove -destdir $(OCAMLDESTDIR) xentoollog
+
+include $(TOPLEVEL)/Makefile.rules
diff --git a/tools/ocaml/libs/xentoollog/caml_xentoollog.h b/tools/ocaml/libs/xentoollog/caml_xentoollog.h
new file mode 100644
index 0000000..0eb7618
--- /dev/null
+++ b/tools/ocaml/libs/xentoollog/caml_xentoollog.h
@@ -0,0 +1,24 @@
+/*
+ * Copyright (C) 2013      Citrix Ltd.
+ * Author Ian Campbell <ian.campbell@citrix.com>
+ * Author Rob Hoes <rob.hoes@citrix.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; version 2.1 only. with the special
+ * exception on linking described in file LICENSE.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ */
+
+struct caml_xtl {
+	xentoollog_logger vtable;
+	char *vmessage_cb;
+	char *progress_cb;
+};
+
+#define Xtl_val(x)(*((struct caml_xtl **) Data_custom_val(x)))
+
diff --git a/tools/ocaml/libs/xentoollog/genlevels.py b/tools/ocaml/libs/xentoollog/genlevels.py
new file mode 100755
index 0000000..6b42f21
--- /dev/null
+++ b/tools/ocaml/libs/xentoollog/genlevels.py
@@ -0,0 +1,127 @@
+#!/usr/bin/python
+
+import sys
+
+def read_levels():
+	f = open('../../../libxc/xentoollog.h', 'r')
+
+	levels = []
+	record = False
+	for l in f.readlines():
+		if 'XTL_NUM_LEVELS' in l:
+			break
+		if record == True:
+			levels.append(l.split(',')[0].strip())
+		if 'XTL_NONE' in l:
+			record = True
+
+	f.close()
+
+	olevels = [level[4:].capitalize() for level in levels]
+
+	return levels, olevels
+
+# .ml
+
+def gen_ml(olevels):
+	s = ""
+
+	s += "type level = \n"
+	for level in olevels:
+		s += '\t| %s\n' % level
+
+	s += "\nlet level_to_string level =\n"
+	s +=  "\tmatch level with\n"
+	for level in olevels:
+		s += '\t| %s -> "%s"\n' % (level, level)
+
+	s += "\nlet level_to_prio level =\n"
+	s += "\tmatch level with\n"
+	for index,level in enumerate(olevels):
+		s += '\t| %s -> %d\n' % (level, index)
+
+	return s
+
+# .mli
+
+def gen_mli(olevels):
+	s = ""
+
+	s += "type level = \n"
+	for level in olevels:
+		s += '\t| %s\n' % level
+
+	return s
+
+# .c
+
+def gen_c(level):
+	s = ""
+
+	s += "static value Val_level(xentoollog_level c_level)\n"
+	s += "{\n"
+	s += "\tswitch (c_level) {\n"
+	s += "\tcase XTL_NONE: /* Not a real value */\n"
+	s += '\t\tcaml_raise_sys_error(caml_copy_string("Val_level XTL_NONE"));\n'
+	s += "\t\tbreak;\n"
+
+	for index,level in enumerate(levels):
+		s += "\tcase %s:\n\t\treturn Val_int(%d);\n" % (level, index)
+
+	s += """\tcase XTL_NUM_LEVELS: /* Not a real value! */
+	\t\tcaml_raise_sys_error(
+	\t\t\tcaml_copy_string("Val_level XTL_NUM_LEVELS"));
+	#if 0 /* Let the compiler catch this */
+	\tdefault:
+	\t\tcaml_raise_sys_error(caml_copy_string("Val_level Unknown"));
+	\t\tbreak;
+	#endif
+	\t}
+	\tabort();
+	}
+	"""
+
+	return s
+
+def autogen_header(open_comment, close_comment):
+    s = open_comment + " AUTO-GENERATED FILE DO NOT EDIT " + close_comment + "\n"
+    s += open_comment + " autogenerated by \n"
+    s += reduce(lambda x,y: x + " ", range(len(open_comment + " ")), "")
+    s += "%s" % " ".join(sys.argv)
+    s += "\n " + close_comment + "\n\n"
+    return s
+
+if __name__ == '__main__':
+	if len(sys.argv) < 3:
+		print >>sys.stderr, "Usage: genlevels.py <mli> <ml> <c-inc>"
+		sys.exit(1)
+
+	levels, olevels = read_levels()
+
+	_mli = sys.argv[1]
+	mli = open(_mli, 'w')
+	mli.write(autogen_header("(*", "*)"))
+
+	_ml = sys.argv[2]
+	ml = open(_ml, 'w')
+	ml.write(autogen_header("(*", "*)"))
+
+	_cinc = sys.argv[3]
+	cinc = open(_cinc, 'w')
+	cinc.write(autogen_header("/*", "*/"))
+
+	mli.write(gen_mli(olevels))
+	mli.write("\n")
+
+	ml.write(gen_ml(olevels))
+	ml.write("\n")
+
+	cinc.write(gen_c(levels))
+	cinc.write("\n")
+
+	ml.write("(* END OF AUTO-GENERATED CODE *)\n")
+	ml.close()
+	mli.write("(* END OF AUTO-GENERATED CODE *)\n")
+	mli.close()
+	cinc.close()
+
diff --git a/tools/ocaml/libs/xentoollog/xentoollog.ml.in b/tools/ocaml/libs/xentoollog/xentoollog.ml.in
new file mode 100644
index 0000000..ce9ea1d
--- /dev/null
+++ b/tools/ocaml/libs/xentoollog/xentoollog.ml.in
@@ -0,0 +1,48 @@
+(*
+ * Copyright (C) 2012      Citrix Ltd.
+ * Author Ian Campbell <ian.campbell@citrix.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; version 2.1 only. with the special
+ * exception on linking described in file LICENSE.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *)
+
+open Printf
+open Random
+open Callback
+
+(* @@XTL_LEVELS@@ *)
+
+let compare_level x y =
+	compare (level_to_prio x) (level_to_prio y)
+
+type handle
+
+type logger_cbs = {
+	vmessage : level -> int option -> string option -> string -> unit;
+	progress : string option -> string -> int -> int64 -> int64 -> unit;
+	(*destroy : unit -> unit*)
+}
+
+external _create_logger: (string * string) -> handle = "stub_xtl_create_logger"
+external test: handle -> unit = "stub_xtl_test"
+
+let counter = ref 0L
+
+let create name cbs : handle =
+	(* Callback names are supposed to be unique *)
+	let suffix = Int64.to_string !counter in
+	counter := Int64.succ !counter;
+	let vmessage_name = sprintf "%s_vmessage_%s" name suffix in
+	let progress_name = sprintf "%s_progress_%s" name suffix in
+	(*let destroy_name = sprintf "%s_destroy" name in*)
+	Callback.register vmessage_name cbs.vmessage;
+	Callback.register progress_name cbs.progress;
+	_create_logger (vmessage_name, progress_name)
+
diff --git a/tools/ocaml/libs/xentoollog/xentoollog.mli.in b/tools/ocaml/libs/xentoollog/xentoollog.mli.in
new file mode 100644
index 0000000..05c098a
--- /dev/null
+++ b/tools/ocaml/libs/xentoollog/xentoollog.mli.in
@@ -0,0 +1,43 @@
+(*
+ * Copyright (C) 2012      Citrix Ltd.
+ * Author Ian Campbell <ian.campbell@citrix.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; version 2.1 only. with the special
+ * exception on linking described in file LICENSE.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *)
+
+(* @@XTL_LEVELS@@ *)
+
+val level_to_string : level -> string
+val compare_level : level -> level -> int
+
+type handle
+
+(** call back arguments. See xentoollog.h for more info.
+    vmessage:
+      level: level as above
+      errno: Some <errno> or None
+      context: Some <string> or None
+      message: The log message (already formatted)
+    progress:
+      context: Some <string> or None
+      doing_what: string
+      percent, done, total.
+*)
+type logger_cbs = {
+	vmessage : level -> int option -> string option -> string -> unit;
+	progress : string option -> string -> int -> int64 -> int64 -> unit;
+	(*destroy : handle -> unit*)
+}
+
+external test: handle -> unit = "stub_xtl_test"
+
+val create : string -> logger_cbs -> handle
+
diff --git a/tools/ocaml/libs/xentoollog/xentoollog_stubs.c b/tools/ocaml/libs/xentoollog/xentoollog_stubs.c
new file mode 100644
index 0000000..3b2f91b
--- /dev/null
+++ b/tools/ocaml/libs/xentoollog/xentoollog_stubs.c
@@ -0,0 +1,196 @@
+/*
+ * Copyright (C) 2012      Citrix Ltd.
+ * Author Ian Campbell <ian.campbell@citrix.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published
+ * by the Free Software Foundation; version 2.1 only. with the special
+ * exception on linking described in file LICENSE.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ */
+
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <errno.h>
+
+#define CAML_NAME_SPACE
+#include <caml/alloc.h>
+#include <caml/memory.h>
+#include <caml/signals.h>
+#include <caml/fail.h>
+#include <caml/callback.h>
+#include <caml/custom.h>
+
+#include <xentoollog.h>
+
+#include "caml_xentoollog.h"
+
+#define XTL ((xentoollog_logger *) Xtl_val(handle))
+
+static char * dup_String_val(value s)
+{
+	int len;
+	char *c;
+	len = caml_string_length(s);
+	c = calloc(len + 1, sizeof(char));
+	if (!c)
+		caml_raise_out_of_memory();
+	memcpy(c, String_val(s), len);
+	return c;
+}
+
+#include "_xtl_levels.inc"
+
+/* Option type support as per http://www.linux-nantes.org/~fmonnier/ocaml/ocaml-wrapping-c.php */
+#define Val_none Val_int(0)
+#define Some_val(v) Field(v,0)
+
+static value Val_some(value v)
+{
+	CAMLparam1(v);
+	CAMLlocal1(some);
+	some = caml_alloc(1, 0);
+	Store_field(some, 0, v);
+	CAMLreturn(some);
+}
+
+static value Val_errno(int errnoval)
+{
+	if (errnoval == -1)
+		return Val_none;
+	return Val_some(Val_int(errnoval));
+}
+
+static value Val_context(const char *context)
+{
+	if (context == NULL)
+		return Val_none;
+	return Val_some(caml_copy_string(context));
+}
+
+static void stub_xtl_ocaml_vmessage(struct xentoollog_logger *logger,
+	xentoollog_level level,
+	int errnoval,
+	const char *context,
+	const char *format,
+	va_list al)
+{
+	CAMLparam0();
+	CAMLlocalN(args, 4);
+	struct caml_xtl *xtl = (struct caml_xtl*)logger;
+	value *func = caml_named_value(xtl->vmessage_cb) ;
+	char *msg;
+
+	if (args == NULL)
+		caml_raise_out_of_memory();
+	if (func == NULL)
+		caml_raise_sys_error(caml_copy_string("Unable to find callback"));
+	if (vasprintf(&msg, format, al) < 0)
+		caml_raise_out_of_memory();
+
+	/* vmessage : level -> int option -> string option -> string -> unit; */
+	args[0] = Val_level(level);
+	args[1] = Val_errno(errnoval);
+	args[2] = Val_context(context);
+	args[3] = caml_copy_string(msg);
+
+	free(msg);
+
+	caml_callbackN(*func, 4, args);
+	CAMLreturn0;
+}
+
+static void stub_xtl_ocaml_progress(struct xentoollog_logger *logger,
+	const char *context,
+	const char *doing_what /* no \r,\n */,
+	int percent, unsigned long done, unsigned long total)
+{
+	CAMLparam0();
+	CAMLlocalN(args, 5);
+	struct caml_xtl *xtl = (struct caml_xtl*)logger;
+	value *func = caml_named_value(xtl->progress_cb) ;
+
+	if (args == NULL)
+		caml_raise_out_of_memory();
+	if (func == NULL)
+		caml_raise_sys_error(caml_copy_string("Unable to find callback"));
+
+	/* progress : string option -> string -> int -> int64 -> int64 -> unit; */
+	args[0] = Val_context(context);
+	args[1] = caml_copy_string(doing_what);
+	args[2] = Val_int(percent);
+	args[3] = caml_copy_int64(done);
+	args[4] = caml_copy_int64(total);
+
+	caml_callbackN(*func, 5, args);
+	CAMLreturn0;
+}
+
+static void xtl_destroy(struct xentoollog_logger *logger)
+{
+	struct caml_xtl *xtl = (struct caml_xtl*)logger;
+	free(xtl->vmessage_cb);
+	free(xtl->progress_cb);
+	free(xtl);
+}
+
+void xtl_finalize(value handle)
+{
+	xtl_destroy(XTL);
+}
+
+static struct custom_operations xentoollogger_custom_operations = {
+	"xentoollogger_custom_operations",
+	xtl_finalize /* custom_finalize_default */,
+	custom_compare_default,
+	custom_hash_default,
+	custom_serialize_default,
+	custom_deserialize_default
+};
+
+/* external _create_logger: (string * string) -> handle = "stub_xtl_create_logger" */
+CAMLprim value stub_xtl_create_logger(value cbs)
+{
+	CAMLparam1(cbs);
+	CAMLlocal1(handle);
+	struct caml_xtl *xtl = malloc(sizeof(*xtl));
+	if (xtl == NULL)
+		caml_raise_out_of_memory();
+
+	memset(xtl, 0, sizeof(*xtl));
+
+	xtl->vtable.vmessage = &stub_xtl_ocaml_vmessage;
+	xtl->vtable.progress = &stub_xtl_ocaml_progress;
+	xtl->vtable.destroy = &xtl_destroy;
+
+	xtl->vmessage_cb = dup_String_val(Field(cbs, 0));
+	xtl->progress_cb = dup_String_val(Field(cbs, 1));
+
+	handle = caml_alloc_custom(&xentoollogger_custom_operations, sizeof(xtl), 0, 1);
+	Xtl_val(handle) = xtl;
+
+	CAMLreturn(handle);
+}
+
+/* external test: handle -> unit = "stub_xtl_test" */
+CAMLprim value stub_xtl_test(value handle)
+{
+	unsigned long l;
+	CAMLparam1(handle);
+	xtl_log(XTL, XTL_DEBUG, -1, "debug", "%s -- debug", __func__);
+	xtl_log(XTL, XTL_INFO, -1, "test", "%s -- test 1", __func__);
+	xtl_log(XTL, XTL_INFO, ENOSYS, "test errno", "%s -- test 2", __func__);
+	xtl_log(XTL, XTL_CRITICAL, -1, "critical", "%s -- critical", __func__);
+	for (l = 0UL; l<=100UL; l += 10UL) {
+		xtl_progress(XTL, "progress", "testing", l, 100UL);
+		usleep(10000);
+	}
+	CAMLreturn(Val_unit);
+}
+
diff --git a/tools/ocaml/test/Makefile b/tools/ocaml/test/Makefile
new file mode 100644
index 0000000..980054c
--- /dev/null
+++ b/tools/ocaml/test/Makefile
@@ -0,0 +1,28 @@
+XEN_ROOT = $(CURDIR)/../../..
+OCAML_TOPLEVEL = $(CURDIR)/..
+include $(OCAML_TOPLEVEL)/common.make
+
+OCAMLINCLUDE += \
+	-I $(OCAML_TOPLEVEL)/libs/xentoollog
+
+OBJS = xtl
+
+PROGRAMS = xtl
+
+xtl_LIBS =  \
+	-ccopt -L -ccopt $(OCAML_TOPLEVEL)/libs/xentoollog $(OCAML_TOPLEVEL)/libs/xentoollog/xentoollog.cmxa \
+	-cclib -lxenctrl
+
+xtl_OBJS = xtl
+
+OCAML_PROGRAM = xtl
+
+all: $(PROGRAMS)
+
+bins: $(PROGRAMS)
+
+install: all
+	$(INSTALL_DIR) $(DESTDIR)$(BINDIR)
+	$(INSTALL_PROG) $(PROGRAMS) $(DESTDIR)$(BINDIR)
+
+include $(OCAML_TOPLEVEL)/Makefile.rules
diff --git a/tools/ocaml/test/xtl.ml b/tools/ocaml/test/xtl.ml
new file mode 100644
index 0000000..db30aae
--- /dev/null
+++ b/tools/ocaml/test/xtl.ml
@@ -0,0 +1,40 @@
+open Arg
+open Printf
+open Xentoollog
+
+let stdio_vmessage min_level level errno ctx msg =
+	let level_str = level_to_string level
+	and errno_str = match errno with None -> "" | Some s -> sprintf ": errno=%d" s
+	and ctx_str = match ctx with None -> "" | Some s -> sprintf ": %s" s in
+	if compare min_level level <= 0 then begin
+		printf "%s%s%s: %s\n" level_str ctx_str errno_str msg;
+		flush stdout;
+	end
+
+let stdio_progress ctx what percent dne total =
+	let nl = if dne = total then "\n" else "" in
+	printf "\rProgress %s %d%% (%Ld/%Ld)%s" what percent dne total nl;
+	flush stdout
+
+let create_stdio_logger ?(level=Info) () =
+	let cbs = {
+		vmessage = stdio_vmessage level;
+		progress = stdio_progress; } in
+	create "Xentoollog.stdio_logger" cbs
+
+let do_test level = 
+  let lgr = create_stdio_logger ~level:level () in
+  begin
+    test lgr;
+  end
+
+let () =
+  let debug_level = ref Info in
+  let speclist = [
+    ("-v", Arg.Unit (fun () -> debug_level := Debug), "Verbose");
+    ("-q", Arg.Unit (fun () -> debug_level := Critical), "Quiet");
+  ] in
+  let usage_msg = "usage: xtl [OPTIONS]" in
+  Arg.parse speclist (fun s -> ()) usage_msg;
+
+  do_test !debug_level
-- 
1.7.10.4

  parent reply	other threads:[~2013-11-06 17:49 UTC|newest]

Thread overview: 146+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-11-06 17:49 [PATCH v4 00/27] libxl: ocaml: improve the bindings Rob Hoes
2013-11-06 17:49 ` [PATCH v4 01/27] libxl: ocaml: support for Arrays in bindings generator Rob Hoes
2013-11-06 17:49 ` [PATCH v4 02/27] libxl: ocaml: avoid reserved words in type and field names Rob Hoes
2013-11-11 14:17   ` Ian Campbell
2013-11-06 17:49 ` [PATCH v4 03/27] libxl: ocaml: support for KeyedUnion in the bindings generator Rob Hoes
2013-11-06 17:49 ` [PATCH v4 04/27] libxl: ocaml: add some more builtin types Rob Hoes
2013-11-06 17:49 ` Rob Hoes [this message]
2013-11-11 15:30   ` [PATCH v4 05/27] libxc: ocaml: add simple binding for xentoollog (output only) Ian Campbell
2013-11-12 13:46     ` Rob Hoes
2013-11-12 13:52       ` Ian Campbell
2013-11-06 17:49 ` [PATCH v4 06/27] libxl: ocaml: allocate a long lived libxl context Rob Hoes
2013-11-06 17:49 ` [PATCH v4 07/27] libxl: ocaml: switch all functions over to take a context Rob Hoes
2013-11-06 17:49 ` [PATCH v4 08/27] libxl: idl: add Enumeration.value_namespace property Rob Hoes
2013-11-11 14:31   ` Ian Campbell
2013-11-06 17:49 ` [PATCH v4 09/27] libxl: make the libxl error type an IDL enum Rob Hoes
2013-11-11 14:31   ` Ian Campbell
2013-11-06 17:49 ` [PATCH v4 10/27] libxl: ocaml: generate string_of_* functions for enums Rob Hoes
2013-11-11 14:33   ` Ian Campbell
2013-11-06 17:49 ` [PATCH v4 11/27] libxl: ocaml: propagate the libxl return error code in exceptions Rob Hoes
2013-11-06 17:49 ` [PATCH v4 12/27] libxl: ocaml: make Val_defbool GC-proof Rob Hoes
2013-11-06 17:49 ` [PATCH v4 13/27] libxl: ocaml: add domain_build/create_info/config and events to the bindings Rob Hoes
2013-11-06 17:49 ` [PATCH v4 14/27] libxl: ocaml: fix the handling of enums in the bindings generator Rob Hoes
2013-11-06 17:49 ` [PATCH v4 15/27] libxl: ocaml: use the "string option" type for IDL strings Rob Hoes
2013-11-06 17:49 ` [PATCH v4 16/27] libxl: ocaml: add dominfo_list and dominfo_get Rob Hoes
2013-11-06 17:49 ` [PATCH v4 17/27] libxl: ocaml: implement some simple tests Rob Hoes
2013-11-06 17:49 ` [PATCH v4 18/27] libxl: ocaml: event management Rob Hoes
2013-08-22 10:50   ` [PATCH v2-resend 00/30] libxl: ocaml: improve the bindings Rob Hoes
2013-08-22 10:50     ` [PATCH v2-resend 01/30] libxl: Add LIBXL_SHUTDOWN_REASON_UNKNOWN Rob Hoes
2013-08-27 14:54       ` Ian Jackson
     [not found]       ` <12f36dbf-3fdc-45e8-b3c1-5194ea356197@FTLPEX01CL02.citrite.net>
2013-09-10 10:55         ` Ian Campbell
2013-08-22 10:50     ` [PATCH v2-resend 02/30] libxl: idl: allow KeyedUnion members to be empty Rob Hoes
2013-08-27 14:53       ` Ian Jackson
2013-08-27 14:56         ` Ian Campbell
2013-08-27 14:59           ` Ian Jackson
2013-08-27 15:04             ` Ian Campbell
2013-08-27 16:27               ` Ian Jackson
2013-08-22 10:50     ` [PATCH v2-resend 03/30] libxl: idl: add domain_type field to libxl_dominfo struct Rob Hoes
2013-08-27 14:55       ` Ian Jackson
2013-09-10 10:56         ` Ian Campbell
2013-09-10 11:00           ` Ian Campbell
2013-09-10 11:03             ` Rob Hoes
2013-08-22 10:50     ` [PATCH v2-resend 04/30] libxl: idl: complete some enums in the IDL with their defaults Rob Hoes
2013-08-27 14:56       ` Ian Jackson
2013-09-10 10:57         ` Ian Campbell
2013-09-10 11:02           ` Ian Campbell
2013-08-22 10:50     ` [PATCH v2-resend 05/30] libxl: ocaml: fix code intended to output comments before definitions Rob Hoes
2013-08-27 14:57       ` Ian Jackson
2013-09-10 10:57         ` Ian Campbell
2013-08-22 10:50     ` [PATCH v2-resend 06/30] libxl: ocaml: support for Arrays in bindings generator Rob Hoes
2013-08-27 14:59       ` Ian Jackson
2013-08-27 15:06         ` Ian Campbell
2013-08-27 15:12           ` Ian Jackson
2013-08-28 14:37             ` Rob Hoes
2013-08-22 10:50     ` [PATCH v2-resend 07/30] libxl: ocaml: avoid reserved words in type and field names Rob Hoes
2013-08-27 15:01       ` Ian Jackson
2013-08-29 10:29         ` Rob Hoes
2013-08-22 10:50     ` [PATCH v2-resend 08/30] libxl: ocaml: support for KeyedUnion in the bindings generator Rob Hoes
2013-08-27 15:09       ` Ian Jackson
2013-08-27 15:13         ` Ian Campbell
2013-08-27 15:20           ` Ian Jackson
2013-08-27 15:28             ` Ian Campbell
2013-08-28 14:47               ` Rob Hoes
2013-08-22 10:50     ` [PATCH v2-resend 09/30] libxl: ocaml: add some more builtin types Rob Hoes
2013-08-27 15:21       ` Ian Jackson
2013-08-28 14:52         ` Rob Hoes
2013-08-22 10:50     ` [PATCH v2-resend 10/30] libxc: ocaml: add simple binding for xentoollog (output only) Rob Hoes
2013-08-27 15:33       ` Ian Jackson
2013-08-29 12:54         ` Rob Hoes
2013-08-29 13:12           ` Ian Campbell
2013-08-29 15:07             ` Ian Jackson
2013-08-29 15:05           ` Ian Jackson
2013-08-22 10:50     ` [PATCH v2-resend 11/30] libxl: ocaml: allocate a long lived libxl context Rob Hoes
2013-08-27 15:38       ` Ian Jackson
2013-08-28 15:55         ` Rob Hoes
2013-08-22 10:51     ` [PATCH v2-resend 12/30] libxl: ocaml: switch all functions over to take a context Rob Hoes
2013-08-27 15:41       ` Ian Jackson
2013-08-22 10:51     ` [PATCH v2-resend 13/30] libxl: ocaml: propagate the libxl return error code in exceptions Rob Hoes
2013-08-27 15:43       ` Ian Jackson
2013-08-22 10:51     ` [PATCH v2-resend 14/30] libxl: ocaml: make Val_defbool GC-proof Rob Hoes
2013-08-27 15:48       ` Ian Jackson
2013-08-22 10:51     ` [PATCH v2-resend 15/30] libxl: ocaml: add domain_build/create_info/config and events to the bindings Rob Hoes
2013-08-27 15:50       ` Ian Jackson
2013-08-22 10:51     ` [PATCH v2-resend 16/30] libxl: ocaml: add META to list of generated files in Makefile Rob Hoes
2013-08-27 17:49       ` Ian Jackson
2013-08-22 10:51     ` [PATCH v2-resend 17/30] libxl: ocaml: fix the handling of enums in the bindings generator Rob Hoes
2013-08-27 17:41       ` Ian Jackson
2013-08-22 10:51     ` [PATCH v2-resend 18/30] libxl: ocaml: use the "string option" type for IDL strings Rob Hoes
2013-08-27 17:44       ` Ian Jackson
2013-08-28  8:30         ` Ian Campbell
2013-08-28 10:33           ` Ian Jackson
2013-08-28 10:41             ` Ian Campbell
2013-08-22 10:51     ` [PATCH v2-resend 19/30] libxl: ocaml: add xen_console_read Rob Hoes
2013-08-27 17:46       ` Ian Jackson
2013-08-22 10:51     ` [PATCH v2-resend 20/30] libxl: ocaml: add dominfo_list and dominfo_get Rob Hoes
2013-08-27 17:51       ` Ian Jackson
2013-08-22 10:51     ` [PATCH v2-resend 21/30] libxl: ocaml: implement some simple tests Rob Hoes
2013-08-27 17:52       ` Ian Jackson
2013-08-22 10:51     ` [PATCH v2-resend 22/30] libxl: ocaml: event management Rob Hoes
2013-08-27 17:56       ` Ian Jackson
2013-11-11 14:42         ` [PATCH v2-resend 22/30] libxl: ocaml: event management [and 1 more messages] Ian Jackson
2013-11-11 15:39           ` Rob Hoes
2013-11-12 14:56             ` Ian Jackson
2013-11-12 15:49               ` David Scott
2013-11-12 16:41                 ` Ian Jackson
2013-11-12 17:14               ` Rob Hoes
2013-11-12 17:18                 ` Ian Jackson
2013-11-14 17:39                   ` Rob Hoes
2013-11-14 18:08                     ` Ian Jackson
2013-11-26 18:03                       ` Rob Hoes
2013-11-28 16:46               ` Ian Jackson
2013-11-28 17:53                 ` Rob Hoes
2013-08-22 10:51     ` [PATCH v2-resend 23/30] libxl: ocaml: allow device operations to be called asynchronously Rob Hoes
2013-08-22 10:51     ` [PATCH v2-resend 24/30] libxl: ocaml: add NIC helper functions Rob Hoes
2013-08-22 10:51     ` [PATCH v2-resend 25/30] libxl: ocaml: add PCI device " Rob Hoes
2013-08-22 10:51     ` [PATCH v2-resend 26/30] libxl: ocaml: add disk and cdrom " Rob Hoes
2013-08-22 10:51     ` [PATCH v2-resend 27/30] libxl: ocaml: add VM lifecycle operations Rob Hoes
2013-08-22 10:51     ` [PATCH v2-resend 28/30] libxl: ocaml: in send_debug_keys, clean up before raising exception Rob Hoes
2013-08-22 10:51     ` [PATCH v2-resend 29/30] libxl: ocaml: provide defaults for libxl types Rob Hoes
2013-08-22 10:51     ` [PATCH v2-resend 30/30] libxl: ocaml: use CAMLlocal1 macro rather than value-type in auto-generated C-code Rob Hoes
2013-09-10 10:58     ` [PATCH v2-resend 00/30] libxl: ocaml: improve the bindings Ian Campbell
2013-09-10 11:02       ` Rob Hoes
2013-09-10 12:57         ` Ian Campbell
2013-09-10 13:06           ` Rob Hoes
2013-11-06 17:49 ` [PATCH v4 19/27] libxl: ocaml: allow device operations to be called asynchronously Rob Hoes
2013-11-06 17:49 ` [PATCH v4 20/27] libxl: ocaml: add NIC helper functions Rob Hoes
2013-11-11 14:43   ` Ian Campbell
2013-11-06 17:49 ` [PATCH v4 21/27] libxl: ocaml: add PCI device " Rob Hoes
2013-11-06 17:50 ` [PATCH v4 22/27] libxl: ocaml: add disk and cdrom " Rob Hoes
2013-11-11 14:44   ` Ian Campbell
2013-11-06 17:50 ` [PATCH v4 23/27] libxl: ocaml: add VM lifecycle operations Rob Hoes
2013-11-11 14:50   ` Ian Campbell
2013-11-11 15:54     ` Rob Hoes
2013-11-11 15:58       ` Ian Campbell
2013-11-12 12:27         ` Rob Hoes
2013-11-12 13:54           ` Ian Campbell
2013-11-12 14:09             ` Rob Hoes
2013-11-12 14:16               ` Ian Campbell
2013-11-12 14:18   ` Ian Campbell
2013-11-06 17:50 ` [PATCH v4 24/27] libxl: ocaml: in send_debug_keys, clean up before raising exception Rob Hoes
2013-11-06 17:50 ` [PATCH v4 25/27] libxl: ocaml: provide defaults for libxl types Rob Hoes
2013-11-06 17:50 ` [PATCH v4 26/27] libxl: ocaml: use CAMLlocal1 macro rather than value-type in auto-generated C-code Rob Hoes
2013-11-06 17:50 ` [PATCH v4 27/27] libxl: ocaml: add console reader functions Rob Hoes
2013-11-11 15:47 ` [PATCH v4 00/27] libxl: ocaml: improve the bindings Ian Campbell
2013-11-12 11:28   ` Rob Hoes
2013-11-20 17:15     ` Ian Campbell
2013-11-20 17:43       ` Rob Hoes

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1383760205-20397-6-git-send-email-rob.hoes@citrix.com \
    --to=rob.hoes@citrix.com \
    --cc=ian.campbell@citrix.com \
    --cc=ian.jackson@eu.citrix.com \
    --cc=xen-devel@lists.xen.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).